inode.c revision 1.30 1 /* $NetBSD: inode.c,v 1.30 1998/10/23 01:13:33 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)inode.c 8.8 (Berkeley) 4/28/95";
40 #else
41 __RCSID("$NetBSD: inode.c,v 1.30 1998/10/23 01:13:33 thorpej Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/time.h>
47
48 #include <ufs/ufs/dinode.h>
49 #include <ufs/ufs/dir.h>
50 #include <ufs/ffs/fs.h>
51 #include <ufs/ffs/ffs_extern.h>
52
53 #ifndef SMALL
54 #include <err.h>
55 #include <pwd.h>
56 #endif
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <time.h>
61
62 #include "fsck.h"
63 #include "fsutil.h"
64 #include "extern.h"
65
66 static ino_t startinum;
67
68 static int iblock __P((struct inodesc *, long, u_int64_t));
69
70 int
71 ckinode(dp, idesc)
72 struct dinode *dp;
73 struct inodesc *idesc;
74 {
75 ufs_daddr_t *ap;
76 long ret, n, ndb, offset;
77 struct dinode dino;
78 u_int64_t sizepb;
79 int64_t remsize;
80 mode_t mode;
81 char pathbuf[MAXPATHLEN + 1];
82
83 if (idesc->id_fix != IGNORE)
84 idesc->id_fix = DONTKNOW;
85 idesc->id_entryno = 0;
86 idesc->id_filesize = iswap64(dp->di_size);
87 mode = iswap16(dp->di_mode) & IFMT;
88 if (mode == IFBLK || mode == IFCHR || (mode == IFLNK &&
89 (idesc->id_filesize < sblock->fs_maxsymlinklen ||
90 (sblock->fs_maxsymlinklen == 0 && dp->di_blocks == 0))))
91 return (KEEPON);
92 dino = *dp;
93 ndb = howmany(iswap64(dino.di_size), sblock->fs_bsize);
94 for (ap = &dino.di_db[0]; ap < &dino.di_db[NDADDR]; ap++) {
95 if (--ndb == 0 &&
96 (offset = blkoff(sblock, iswap64(dino.di_size))) != 0)
97 idesc->id_numfrags =
98 numfrags(sblock, fragroundup(sblock, offset));
99 else
100 idesc->id_numfrags = sblock->fs_frag;
101 if (*ap == 0) {
102 if (idesc->id_type == DATA && ndb >= 0) {
103 /* An empty block in a directory XXX */
104 markclean = 0;
105 getpathname(pathbuf, idesc->id_number,
106 idesc->id_number);
107 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
108 pathbuf);
109 if (reply("ADJUST LENGTH") == 1) {
110 dp = ginode(idesc->id_number);
111 dp->di_size = iswap64((ap - &dino.di_db[0]) *
112 sblock->fs_bsize);
113 printf(
114 "YOU MUST RERUN FSCK AFTERWARDS\n");
115 rerun = 1;
116 inodirty();
117 }
118 }
119 continue;
120 }
121 idesc->id_blkno = iswap32(*ap);
122 if (idesc->id_type == ADDR)
123 ret = (*idesc->id_func)(idesc);
124 else
125 ret = dirscan(idesc);
126 if (ret & STOP)
127 return (ret);
128 }
129 idesc->id_numfrags = sblock->fs_frag;
130 remsize = iswap64(dino.di_size) - sblock->fs_bsize * NDADDR;
131 sizepb = sblock->fs_bsize;
132 for (ap = &dino.di_ib[0], n = 1; n <= NIADDR; ap++, n++) {
133 if (*ap) {
134 idesc->id_blkno = iswap32(*ap);
135 ret = iblock(idesc, n, remsize);
136 if (ret & STOP)
137 return (ret);
138 } else {
139 if (idesc->id_type == DATA && remsize > 0) {
140 /* An empty block in a directory XXX */
141 markclean = 0;
142 getpathname(pathbuf, idesc->id_number,
143 idesc->id_number);
144 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
145 pathbuf);
146 if (reply("ADJUST LENGTH") == 1) {
147 dp = ginode(idesc->id_number);
148 dp->di_size = iswap64(iswap64(dp->di_size) - remsize);
149 remsize = 0;
150 printf(
151 "YOU MUST RERUN FSCK AFTERWARDS\n");
152 rerun = 1;
153 inodirty();
154 break;
155 }
156 }
157 }
158 sizepb *= NINDIR(sblock);
159 remsize -= sizepb;
160 }
161 return (KEEPON);
162 }
163
164 static int
165 iblock(idesc, ilevel, isize)
166 struct inodesc *idesc;
167 long ilevel;
168 u_int64_t isize;
169 {
170 ufs_daddr_t *ap;
171 ufs_daddr_t *aplim;
172 struct bufarea *bp;
173 int i, n, (*func) __P((struct inodesc *)), nif;
174 u_int64_t sizepb;
175 char buf[BUFSIZ];
176 char pathbuf[MAXPATHLEN + 1];
177 struct dinode *dp;
178
179 if (idesc->id_type == ADDR) {
180 func = idesc->id_func;
181 if (((n = (*func)(idesc)) & KEEPON) == 0)
182 return (n);
183 } else
184 func = dirscan;
185 if (chkrange(idesc->id_blkno, idesc->id_numfrags))
186 return (SKIP);
187 bp = getdatablk(idesc->id_blkno, sblock->fs_bsize);
188 ilevel--;
189 for (sizepb = sblock->fs_bsize, i = 0; i < ilevel; i++)
190 sizepb *= NINDIR(sblock);
191 if (isize > sizepb * NINDIR(sblock))
192 nif = NINDIR(sblock);
193 else
194 nif = howmany(isize, sizepb);
195 if (do_blkswap) { /* swap byte order of the whole blk */
196 aplim = &bp->b_un.b_indir[nif];
197 for (ap = bp->b_un.b_indir; ap < aplim; ap++)
198 *ap = bswap32(*ap);
199 dirty(bp);
200 flush(fswritefd, bp);
201 }
202 if (idesc->id_func == pass1check && nif < NINDIR(sblock)) {
203 aplim = &bp->b_un.b_indir[NINDIR(sblock)];
204 for (ap = &bp->b_un.b_indir[nif]; ap < aplim; ap++) {
205 if (*ap == 0)
206 continue;
207 (void)snprintf(buf, sizeof(buf),
208 "PARTIALLY TRUNCATED INODE I=%u", idesc->id_number);
209 if (dofix(idesc, buf)) {
210 *ap = 0;
211 dirty(bp);
212 } else
213 markclean= 0;
214 }
215 flush(fswritefd, bp);
216 }
217 aplim = &bp->b_un.b_indir[nif];
218 for (ap = bp->b_un.b_indir; ap < aplim; ap++) {
219 if (*ap) {
220 idesc->id_blkno = iswap32(*ap);
221 if (ilevel == 0)
222 n = (*func)(idesc);
223 else
224 n = iblock(idesc, ilevel, isize);
225 if (n & STOP) {
226 bp->b_flags &= ~B_INUSE;
227 return (n);
228 }
229 } else {
230 if (idesc->id_type == DATA && isize > 0) {
231 /* An empty block in a directory XXX */
232 markclean= 0;
233 getpathname(pathbuf, idesc->id_number,
234 idesc->id_number);
235 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
236 pathbuf);
237 if (reply("ADJUST LENGTH") == 1) {
238 dp = ginode(idesc->id_number);
239 dp->di_size = iswap64(iswap64(dp->di_size) - isize);
240 isize = 0;
241 printf(
242 "YOU MUST RERUN FSCK AFTERWARDS\n");
243 rerun = 1;
244 inodirty();
245 bp->b_flags &= ~B_INUSE;
246 return(STOP);
247 }
248 }
249 }
250 isize -= sizepb;
251 }
252 bp->b_flags &= ~B_INUSE;
253 return (KEEPON);
254 }
255
256 /*
257 * Check that a block in a legal block number.
258 * Return 0 if in range, 1 if out of range.
259 */
260 int
261 chkrange(blk, cnt)
262 ufs_daddr_t blk;
263 int cnt;
264 {
265 int c;
266
267 if ((unsigned)(blk + cnt) > maxfsblock)
268 return (1);
269 c = dtog(sblock, blk);
270 if (blk < cgdmin(sblock, c)) {
271 if ((blk + cnt) > cgsblock(sblock, c)) {
272 if (debug) {
273 printf("blk %d < cgdmin %d;",
274 blk, cgdmin(sblock, c));
275 printf(" blk + cnt %d > cgsbase %d\n",
276 blk + cnt, cgsblock(sblock, c));
277 }
278 return (1);
279 }
280 } else {
281 if ((blk + cnt) > cgbase(sblock, c+1)) {
282 if (debug) {
283 printf("blk %d >= cgdmin %d;",
284 blk, cgdmin(sblock, c));
285 printf(" blk + cnt %d > sblock->fs_fpg %d\n",
286 blk+cnt, sblock->fs_fpg);
287 }
288 return (1);
289 }
290 }
291 return (0);
292 }
293
294 /*
295 * General purpose interface for reading inodes.
296 */
297 struct dinode *
298 ginode(inumber)
299 ino_t inumber;
300 {
301 ufs_daddr_t iblk;
302 int blkoff;
303
304 if (inumber < ROOTINO || inumber > maxino)
305 errx(EEXIT, "bad inode number %d to ginode", inumber);
306 if (startinum == 0 ||
307 inumber < startinum || inumber >= startinum + INOPB(sblock)) {
308 iblk = ino_to_fsba(sblock, inumber);
309 if (pbp != 0)
310 pbp->b_flags &= ~B_INUSE;
311 pbp = getdatablk(iblk, sblock->fs_bsize);
312 startinum = (inumber / INOPB(sblock)) * INOPB(sblock);
313 }
314 blkoff = (inumber % INOPB(sblock)) * DINODE_SIZE;
315 return ((struct dinode *)((caddr_t)pbp->b_un.b_buf + blkoff));
316 }
317
318 /*
319 * Special purpose version of ginode used to optimize first pass
320 * over all the inodes in numerical order.
321 */
322 ino_t nextino, lastinum;
323 long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
324 struct dinode *inodebuf;
325
326 struct dinode *
327 getnextinode(inumber)
328 ino_t inumber;
329 {
330 long size;
331 ufs_daddr_t dblk;
332 static struct dinode *dp;
333
334 if (inumber != nextino++ || inumber > maxino)
335 errx(EEXIT, "bad inode number %d to nextinode", inumber);
336 if (inumber >= lastinum) {
337 readcnt++;
338 dblk = fsbtodb(sblock, ino_to_fsba(sblock, lastinum));
339 if (readcnt % readpercg == 0) {
340 size = partialsize;
341 lastinum += partialcnt;
342 } else {
343 size = inobufsize;
344 lastinum += fullcnt;
345 }
346 (void)bread(fsreadfd, (char *)inodebuf, dblk, size); /* ??? */
347 if (doswap) {
348 int i, j;
349 for (i = inumber, dp = inodebuf; i < lastinum; i++, dp++) {
350 ffs_dinode_swap(dp, dp);
351 /* ffs_dinode_swap() doesn't swap blocks addrs */
352 if ((iswap16(dp->di_mode) & IFMT) != IFLNK ||
353 iswap64(dp->di_size) > sblock->fs_maxsymlinklen) {
354 for (j=0; j<NDADDR + NIADDR; j++)
355 dp->di_db[j] = bswap32(dp->di_db[j]);
356 }
357 }
358 bwrite(fswritefd, (char *)inodebuf, dblk, size);
359 }
360 dp = inodebuf;
361 }
362 return (dp++);
363 }
364
365 void
366 resetinodebuf()
367 {
368
369 startinum = 0;
370 nextino = 0;
371 lastinum = 0;
372 readcnt = 0;
373 inobufsize = blkroundup(sblock, INOBUFSIZE);
374 fullcnt = inobufsize / DINODE_SIZE;
375 readpercg = sblock->fs_ipg / fullcnt;
376 partialcnt = sblock->fs_ipg % fullcnt;
377 partialsize = partialcnt * DINODE_SIZE;
378 if (partialcnt != 0) {
379 readpercg++;
380 } else {
381 partialcnt = fullcnt;
382 partialsize = inobufsize;
383 }
384 if (inodebuf == NULL &&
385 (inodebuf = (struct dinode *)malloc((unsigned)inobufsize)) == NULL)
386 errx(EEXIT, "Cannot allocate space for inode buffer");
387 while (nextino < ROOTINO)
388 (void)getnextinode(nextino);
389 }
390
391 void
392 freeinodebuf()
393 {
394
395 if (inodebuf != NULL)
396 free((char *)inodebuf);
397 inodebuf = NULL;
398 }
399
400 /*
401 * Routines to maintain information about directory inodes.
402 * This is built during the first pass and used during the
403 * second and third passes.
404 *
405 * Enter inodes into the cache.
406 */
407 void
408 cacheino(dp, inumber)
409 struct dinode *dp;
410 ino_t inumber;
411 {
412 struct inoinfo *inp;
413 struct inoinfo **inpp;
414 unsigned int blks;
415
416 blks = howmany(iswap64(dp->di_size), sblock->fs_bsize);
417 if (blks > NDADDR)
418 blks = NDADDR + NIADDR;
419 inp = (struct inoinfo *)
420 malloc(sizeof(*inp) + (blks - 1) * sizeof(ufs_daddr_t));
421 if (inp == NULL)
422 return;
423 inpp = &inphead[inumber % numdirs];
424 inp->i_nexthash = *inpp;
425 *inpp = inp;
426 inp->i_child = inp->i_sibling = inp->i_parentp = 0;
427 if (inumber == ROOTINO)
428 inp->i_parent = ROOTINO;
429 else
430 inp->i_parent = (ino_t)0;
431 inp->i_dotdot = (ino_t)0;
432 inp->i_number = inumber;
433 inp->i_isize = iswap64(dp->di_size);
434 inp->i_numblks = blks * sizeof(ufs_daddr_t);
435 memmove(&inp->i_blks[0], &dp->di_db[0], (size_t)inp->i_numblks);
436 if (inplast == listmax) {
437 listmax += 100;
438 inpsort = (struct inoinfo **)realloc((char *)inpsort,
439 (unsigned)listmax * sizeof(struct inoinfo *));
440 if (inpsort == NULL)
441 errx(EEXIT, "cannot increase directory list");
442 }
443 inpsort[inplast++] = inp;
444 }
445
446 /*
447 * Look up an inode cache structure.
448 */
449 struct inoinfo *
450 getinoinfo(inumber)
451 ino_t inumber;
452 {
453 struct inoinfo *inp;
454
455 for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
456 if (inp->i_number != inumber)
457 continue;
458 return (inp);
459 }
460 errx(EEXIT, "cannot find inode %d", inumber);
461 return ((struct inoinfo *)0);
462 }
463
464 /*
465 * Clean up all the inode cache structure.
466 */
467 void
468 inocleanup()
469 {
470 struct inoinfo **inpp;
471
472 if (inphead == NULL)
473 return;
474 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
475 free((char *)(*inpp));
476 free((char *)inphead);
477 free((char *)inpsort);
478 inphead = inpsort = NULL;
479 }
480
481 void
482 inodirty()
483 {
484
485 dirty(pbp);
486 }
487
488 void
489 clri(idesc, type, flag)
490 struct inodesc *idesc;
491 char *type;
492 int flag;
493 {
494 struct dinode *dp;
495
496 dp = ginode(idesc->id_number);
497 if (flag == 1) {
498 pwarn("%s %s", type,
499 (iswap16(dp->di_mode) & IFMT) == IFDIR ? "DIR" : "FILE");
500 pinode(idesc->id_number);
501 }
502 if (preen || reply("CLEAR") == 1) {
503 if (preen)
504 printf(" (CLEARED)\n");
505 n_files--;
506 (void)ckinode(dp, idesc);
507 clearinode(dp);
508 statemap[idesc->id_number] = USTATE;
509 inodirty();
510 } else
511 markclean= 0;
512 }
513
514 int
515 findname(idesc)
516 struct inodesc *idesc;
517 {
518 struct direct *dirp = idesc->id_dirp;
519
520 if (iswap32(dirp->d_ino) != idesc->id_parent)
521 return (KEEPON);
522 memmove(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1);
523 return (STOP|FOUND);
524 }
525
526 int
527 findino(idesc)
528 struct inodesc *idesc;
529 {
530 struct direct *dirp = idesc->id_dirp;
531
532 if (dirp->d_ino == 0)
533 return (KEEPON);
534 if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
535 iswap32(dirp->d_ino) >= ROOTINO && iswap32(dirp->d_ino) <= maxino) {
536 idesc->id_parent = iswap32(dirp->d_ino);
537 return (STOP|FOUND);
538 }
539 return (KEEPON);
540 }
541
542 void
543 pinode(ino)
544 ino_t ino;
545 {
546 struct dinode *dp;
547 char *p;
548 struct passwd *pw;
549 time_t t;
550
551 printf(" I=%u ", ino);
552 if (ino < ROOTINO || ino > maxino)
553 return;
554 dp = ginode(ino);
555 printf(" OWNER=");
556 #ifndef SMALL
557 if ((pw = getpwuid((int)iswap32(dp->di_uid))) != 0)
558 printf("%s ", pw->pw_name);
559 else
560 #endif
561 printf("%u ", (unsigned)iswap32(dp->di_uid));
562 printf("MODE=%o\n", iswap16(dp->di_mode));
563 if (preen)
564 printf("%s: ", cdevname());
565 printf("SIZE=%qu ", (unsigned long long)iswap64(dp->di_size));
566 t = iswap32(dp->di_mtime);
567 p = ctime(&t);
568 printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
569 }
570
571 void
572 blkerror(ino, type, blk)
573 ino_t ino;
574 char *type;
575 ufs_daddr_t blk;
576 {
577
578 pfatal("%d %s I=%u", blk, type, ino);
579 printf("\n");
580 switch (statemap[ino]) {
581
582 case FSTATE:
583 statemap[ino] = FCLEAR;
584 return;
585
586 case DSTATE:
587 statemap[ino] = DCLEAR;
588 return;
589
590 case FCLEAR:
591 case DCLEAR:
592 return;
593
594 default:
595 errx(EEXIT, "BAD STATE %d TO BLKERR", statemap[ino]);
596 /* NOTREACHED */
597 }
598 }
599
600 /*
601 * allocate an unused inode
602 */
603 ino_t
604 allocino(request, type)
605 ino_t request;
606 int type;
607 {
608 ino_t ino;
609 struct dinode *dp;
610 time_t t;
611
612 if (request == 0)
613 request = ROOTINO;
614 else if (statemap[request] != USTATE)
615 return (0);
616 for (ino = request; ino < maxino; ino++)
617 if (statemap[ino] == USTATE)
618 break;
619 if (ino == maxino)
620 return (0);
621 switch (type & IFMT) {
622 case IFDIR:
623 statemap[ino] = DSTATE;
624 break;
625 case IFREG:
626 case IFLNK:
627 statemap[ino] = FSTATE;
628 break;
629 default:
630 return (0);
631 }
632 dp = ginode(ino);
633 dp->di_db[0] = iswap32(allocblk((long)1));
634 if (dp->di_db[0] == 0) {
635 statemap[ino] = USTATE;
636 return (0);
637 }
638 dp->di_mode = iswap16(type);
639 (void)time(&t);
640 dp->di_atime = iswap32(t);
641 dp->di_mtime = dp->di_ctime = dp->di_atime;
642 dp->di_size = iswap64(sblock->fs_fsize);
643 dp->di_blocks = iswap32(btodb(sblock->fs_fsize));
644 n_files++;
645 inodirty();
646 if (newinofmt)
647 typemap[ino] = IFTODT(type);
648 return (ino);
649 }
650
651 /*
652 * deallocate an inode
653 */
654 void
655 freeino(ino)
656 ino_t ino;
657 {
658 struct inodesc idesc;
659 struct dinode *dp;
660
661 memset(&idesc, 0, sizeof(struct inodesc));
662 idesc.id_type = ADDR;
663 idesc.id_func = pass4check;
664 idesc.id_number = ino;
665 dp = ginode(ino);
666 (void)ckinode(dp, &idesc);
667 clearinode(dp);
668 inodirty();
669 statemap[ino] = USTATE;
670 n_files--;
671 }
672