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>
41 #include <sys/sysctl.h>
43 #include <ufs/ufs/dinode.h>
44 #include <ufs/ufs/dir.h>
45 #include <ufs/ffs/fs.h>
53 static ino_t startinum;
55 static int iblock(struct inodesc *, long ilevel, off_t isize);
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);
98 DIP(dp, di_size) = i * sblock.fs_bsize;
100 "YOU MUST RERUN FSCK AFTERWARDS\n");
108 idesc->id_blkno = DIP(&dino, di_db[i]);
109 if (idesc->id_type != DATA)
110 ret = (*idesc->id_func)(idesc);
112 ret = dirscan(idesc);
116 idesc->id_numfrags = sblock.fs_frag;
117 remsize = DIP(&dino, di_size) - sblock.fs_bsize * NDADDR;
118 sizepb = sblock.fs_bsize;
119 for (i = 0; i < NIADDR; i++) {
120 sizepb *= NINDIR(&sblock);
121 if (DIP(&dino, di_ib[i])) {
122 idesc->id_blkno = DIP(&dino, di_ib[i]);
123 ret = iblock(idesc, i + 1, remsize);
127 idesc->id_lbn += sizepb / sblock.fs_bsize;
128 if (idesc->id_type == DATA && remsize > 0) {
129 /* An empty block in a directory XXX */
130 getpathname(pathbuf, idesc->id_number,
132 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
134 if (reply("ADJUST LENGTH") == 1) {
135 dp = ginode(idesc->id_number);
136 DIP(dp, di_size) -= remsize;
139 "YOU MUST RERUN FSCK AFTERWARDS\n");
152 iblock(struct inodesc *idesc, long ilevel, off_t isize)
155 int i, n, (*func)(struct inodesc *), nif;
158 char pathbuf[MAXPATHLEN + 1];
161 if (idesc->id_type != DATA) {
162 func = idesc->id_func;
163 if (((n = (*func)(idesc)) & KEEPON) == 0)
167 if (chkrange(idesc->id_blkno, idesc->id_numfrags))
169 bp = getdatablk(idesc->id_blkno, sblock.fs_bsize);
171 for (sizepb = sblock.fs_bsize, i = 0; i < ilevel; i++)
172 sizepb *= NINDIR(&sblock);
173 if (howmany(isize, sizepb) > NINDIR(&sblock))
174 nif = NINDIR(&sblock);
176 nif = howmany(isize, sizepb);
177 if (idesc->id_func == pass1check && nif < NINDIR(&sblock)) {
178 for (i = nif; i < NINDIR(&sblock); i++) {
179 if (IBLK(bp, i) == 0)
181 (void)sprintf(buf, "PARTIALLY TRUNCATED INODE I=%lu",
182 (u_long)idesc->id_number);
185 } else if (dofix(idesc, buf)) {
190 flush(fswritefd, bp);
192 for (i = 0; i < nif; i++) {
196 idesc->id_blkno = IBLK(bp, i);
200 n = iblock(idesc, ilevel, isize);
202 bp->b_flags &= ~B_INUSE;
206 if (idesc->id_type == DATA && isize > 0) {
207 /* An empty block in a directory XXX */
208 getpathname(pathbuf, idesc->id_number,
210 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
212 if (reply("ADJUST LENGTH") == 1) {
213 dp = ginode(idesc->id_number);
214 DIP(dp, di_size) -= isize;
217 "YOU MUST RERUN FSCK AFTERWARDS\n");
220 bp->b_flags &= ~B_INUSE;
227 bp->b_flags &= ~B_INUSE;
232 * Check that a block in a legal block number.
233 * Return 0 if in range, 1 if out of range.
236 chkrange(ufs2_daddr_t blk, int cnt)
240 if (cnt <= 0 || blk <= 0 || blk > maxfsblock ||
241 cnt - 1 > maxfsblock - blk)
243 if (cnt > sblock.fs_frag ||
244 fragnum(&sblock, blk) + cnt > sblock.fs_frag) {
246 printf("bad size: blk %ld, offset %i, size %d\n",
247 (long)blk, (int)fragnum(&sblock, blk), cnt);
250 c = dtog(&sblock, blk);
251 if (blk < cgdmin(&sblock, c)) {
252 if ((blk + cnt) > cgsblock(&sblock, c)) {
254 printf("blk %ld < cgdmin %ld;",
255 (long)blk, (long)cgdmin(&sblock, c));
256 printf(" blk + cnt %ld > cgsbase %ld\n",
258 (long)cgsblock(&sblock, c));
263 if ((blk + cnt) > cgbase(&sblock, c+1)) {
265 printf("blk %ld >= cgdmin %ld;",
266 (long)blk, (long)cgdmin(&sblock, c));
267 printf(" blk + cnt %ld > sblock.fs_fpg %ld\n",
268 (long)(blk + cnt), (long)sblock.fs_fpg);
277 * General purpose interface for reading inodes.
280 ginode(ino_t inumber)
284 if (inumber < ROOTINO || inumber > maxino)
285 errx(EEXIT, "bad inode number %d to ginode", inumber);
286 if (startinum == 0 ||
287 inumber < startinum || inumber >= startinum + INOPB(&sblock)) {
288 iblk = ino_to_fsba(&sblock, inumber);
290 pbp->b_flags &= ~B_INUSE;
291 pbp = getdatablk(iblk, sblock.fs_bsize);
292 startinum = (inumber / INOPB(&sblock)) * INOPB(&sblock);
294 if (sblock.fs_magic == FS_UFS1_MAGIC)
295 return ((union dinode *)
296 &pbp->b_un.b_dinode1[inumber % INOPB(&sblock)]);
297 return ((union dinode *)&pbp->b_un.b_dinode2[inumber % INOPB(&sblock)]);
301 * Special purpose version of ginode used to optimize first pass
302 * over all the inodes in numerical order.
304 static ino_t nextino, lastinum, lastvalidinum;
305 static long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
306 static caddr_t inodebuf;
309 getnextinode(ino_t inumber)
314 static caddr_t nextinop;
316 if (inumber != nextino++ || inumber > lastvalidinum)
317 errx(EEXIT, "bad inode number %d to nextinode", inumber);
318 if (inumber >= lastinum) {
320 dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
321 if (readcnt % readpercg == 0) {
323 lastinum += partialcnt;
329 * If bread returns an error, it will already have zeroed
330 * out the buffer, so we do not need to do so here.
332 (void)bread(fsreadfd, inodebuf, dblk, size);
335 dp = (union dinode *)nextinop;
336 if (sblock.fs_magic == FS_UFS1_MAGIC)
337 nextinop += sizeof(struct ufs1_dinode);
339 nextinop += sizeof(struct ufs2_dinode);
344 setinodebuf(ino_t inum)
347 if (inum % sblock.fs_ipg != 0)
348 errx(EEXIT, "bad inode number %d to setinodebuf", inum);
349 lastvalidinum = inum + sblock.fs_ipg - 1;
354 if (inodebuf != NULL)
356 inobufsize = blkroundup(&sblock, INOBUFSIZE);
357 fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ?
358 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
359 readpercg = sblock.fs_ipg / fullcnt;
360 partialcnt = sblock.fs_ipg % fullcnt;
361 partialsize = partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ?
362 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
363 if (partialcnt != 0) {
366 partialcnt = fullcnt;
367 partialsize = inobufsize;
369 if ((inodebuf = malloc((unsigned)inobufsize)) == NULL)
370 errx(EEXIT, "cannot allocate space for inode buffer");
377 if (inodebuf != NULL)
378 free((char *)inodebuf);
383 * Routines to maintain information about directory inodes.
384 * This is built during the first pass and used during the
385 * second and third passes.
387 * Enter inodes into the cache.
390 cacheino(union dinode *dp, ino_t inumber)
392 struct inoinfo *inp, **inpp;
395 if (howmany(DIP(dp, di_size), sblock.fs_bsize) > NDADDR)
396 blks = NDADDR + NIADDR;
398 blks = howmany(DIP(dp, di_size), sblock.fs_bsize);
399 inp = (struct inoinfo *)
400 malloc(sizeof(*inp) + (blks - 1) * sizeof(ufs2_daddr_t));
402 errx(EEXIT, "cannot increase directory list");
403 inpp = &inphead[inumber % dirhash];
404 inp->i_nexthash = *inpp;
406 inp->i_parent = inumber == ROOTINO ? ROOTINO : (ino_t)0;
407 inp->i_dotdot = (ino_t)0;
408 inp->i_number = inumber;
409 inp->i_isize = DIP(dp, di_size);
410 inp->i_numblks = blks;
411 for (i = 0; i < (blks < NDADDR ? blks : NDADDR); i++)
412 inp->i_blks[i] = DIP(dp, di_db[i]);
414 for (i = 0; i < NIADDR; i++)
415 inp->i_blks[NDADDR + i] = DIP(dp, di_ib[i]);
416 if (inplast == listmax) {
418 inpsort = (struct inoinfo **)realloc((char *)inpsort,
419 (unsigned)listmax * sizeof(struct inoinfo *));
421 errx(EEXIT, "cannot increase directory list");
423 inpsort[inplast++] = inp;
427 * Look up an inode cache structure.
430 getinoinfo(ino_t inumber)
434 for (inp = inphead[inumber % dirhash]; inp; inp = inp->i_nexthash) {
435 if (inp->i_number != inumber)
439 errx(EEXIT, "cannot find inode %d", inumber);
440 return ((struct inoinfo *)0);
444 * Clean up all the inode cache structure.
449 struct inoinfo **inpp;
453 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
454 free((char *)(*inpp));
455 free((char *)inphead);
456 free((char *)inpsort);
457 inphead = inpsort = NULL;
468 clri(struct inodesc *idesc, const char *type, int flag)
472 dp = ginode(idesc->id_number);
475 (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE");
476 pinode(idesc->id_number);
478 if (preen || reply("CLEAR") == 1) {
480 printf(" (CLEARED)\n");
482 if (bkgrdflag == 0) {
483 (void)ckinode(dp, idesc);
484 inoinfo(idesc->id_number)->ino_state = USTATE;
488 cmd.value = idesc->id_number;
489 cmd.size = -DIP(dp, di_nlink);
491 printf("adjrefcnt ino %ld amt %lld\n",
492 (long)cmd.value, (long long)cmd.size);
493 if (sysctl(adjrefcnt, MIBSIZE, 0, 0,
494 &cmd, sizeof cmd) == -1)
495 rwerror("ADJUST INODE", cmd.value);
501 findname(struct inodesc *idesc)
503 struct direct *dirp = idesc->id_dirp;
505 if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
509 memmove(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1);
514 findino(struct inodesc *idesc)
516 struct direct *dirp = idesc->id_dirp;
518 if (dirp->d_ino == 0)
520 if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
521 dirp->d_ino >= ROOTINO && dirp->d_ino <= maxino) {
522 idesc->id_parent = dirp->d_ino;
529 clearentry(struct inodesc *idesc)
531 struct direct *dirp = idesc->id_dirp;
533 if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
538 return (STOP|FOUND|ALTERED);
549 printf(" I=%lu ", (u_long)ino);
550 if (ino < ROOTINO || ino > maxino)
554 if ((pw = getpwuid((int)DIP(dp, di_uid))) != 0)
555 printf("%s ", pw->pw_name);
557 printf("%u ", (unsigned)DIP(dp, di_uid));
558 printf("MODE=%o\n", DIP(dp, di_mode));
560 printf("%s: ", cdevname);
561 printf("SIZE=%ju ", (uintmax_t)DIP(dp, di_size));
562 t = DIP(dp, di_mtime);
564 printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
568 blkerror(ino_t ino, const char *type, ufs2_daddr_t blk)
571 pfatal("%jd %s I=%ju", (intmax_t)blk, type, (uintmax_t)ino);
573 switch (inoinfo(ino)->ino_state) {
576 inoinfo(ino)->ino_state = FCLEAR;
580 inoinfo(ino)->ino_state = DCLEAR;
588 errx(EEXIT, "BAD STATE %d TO BLKERR", inoinfo(ino)->ino_state);
594 * allocate an unused inode
597 allocino(ino_t request, int type)
601 struct cg *cgp = &cgrp;
606 else if (inoinfo(request)->ino_state != USTATE)
608 for (ino = request; ino < maxino; ino++)
609 if (inoinfo(ino)->ino_state == USTATE)
613 cg = ino_to_cg(&sblock, ino);
614 getblk(&cgblk, cgtod(&sblock, cg), sblock.fs_cgsize);
615 if (!cg_chkmagic(cgp))
616 pfatal("CG %d: BAD MAGIC NUMBER\n", cg);
617 setbit(cg_inosused(cgp), ino % sblock.fs_ipg);
618 cgp->cg_cs.cs_nifree--;
619 switch (type & IFMT) {
621 inoinfo(ino)->ino_state = DSTATE;
622 cgp->cg_cs.cs_ndir++;
626 inoinfo(ino)->ino_state = FSTATE;
633 DIP(dp, di_db[0]) = allocblk((long)1);
634 if (DIP(dp, di_db[0]) == 0) {
635 inoinfo(ino)->ino_state = USTATE;
638 DIP(dp, di_mode) = type;
639 DIP(dp, di_flags) = 0;
640 DIP(dp, di_atime) = time(NULL);
641 DIP(dp, di_mtime) = DIP(dp, di_ctime) = DIP(dp, di_atime);
642 DIP(dp, di_mtimensec) = 0;
643 DIP(dp, di_ctimensec) = 0;
644 DIP(dp, di_atimensec) = 0;
645 DIP(dp, di_size) = sblock.fs_fsize;
646 DIP(dp, di_blocks) = btodb(sblock.fs_fsize);
649 inoinfo(ino)->ino_type = IFTODT(type);
654 * deallocate an inode
659 struct inodesc idesc;
662 memset(&idesc, 0, sizeof(struct inodesc));
663 idesc.id_type = ADDR;
664 idesc.id_func = pass4check;
665 idesc.id_number = ino;
667 (void)ckinode(dp, &idesc);
670 inoinfo(ino)->ino_state = USTATE;