inode.c revision 1.10.2.2 1 /* $NetBSD: inode.c,v 1.10.2.2 2001/06/30 01:28:30 perseant Exp $ */
2
3 /*
4 * Copyright (c) 1997, 1998
5 * Konrad Schroder. All rights reserved.
6 * Copyright (c) 1980, 1986, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38 #include <sys/param.h>
39 #include <sys/time.h>
40 #include <ufs/ufs/dinode.h>
41 #include <ufs/ufs/dir.h>
42 #include <sys/mount.h> /* XXX */
43 #include <ufs/lfs/lfs.h>
44 #ifndef SMALL
45 #include <pwd.h>
46 #endif
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50
51 #include "fsck.h"
52 #include "fsutil.h"
53 #include "extern.h"
54
55 extern SEGUSE *seg_table;
56 extern daddr_t *din_table;
57
58 static int iblock(struct inodesc *, long, u_int64_t);
59 int blksreqd(struct lfs *, int);
60 int lfs_maxino(void);
61 /* static void dump_inoblk (struct lfs *, struct dinode *); */
62
63 /* stolen from lfs_inode.c */
64 /* Search a block for a specific dinode. */
65 struct dinode *
66 lfs_difind(struct lfs * fs, ino_t ino, struct dinode * dip)
67 {
68 struct dinode *ldip, *fin;
69
70 if (fs->lfs_version == 1)
71 fin = dip + INOPB(fs);
72 else
73 fin = dip + INOPS(fs);
74
75 for (ldip = dip; ldip < fin; ++ldip) {
76 if (ldip->di_inumber == ino)
77 return ldip;
78 }
79 /* printf("lfs_difind: dinode %u not found\n", ino); */
80 return NULL;
81 }
82
83 /*
84 * Calculate the number of blocks required to be able to address data block
85 * blkno (counting, of course, indirect blocks). blkno must >=0.
86 */
87 int
88 blksreqd(struct lfs * fs, int blkno)
89 {
90 long n = blkno;
91
92 if (blkno < NDADDR)
93 return blkno;
94 n -= NDADDR;
95 if (n < NINDIR(fs))
96 return blkno + 1;
97 n -= NINDIR(fs);
98 if (n < NINDIR(fs) * NINDIR(fs))
99 return blkno + 2 + n / NINDIR(fs) + 1;
100 n -= NINDIR(fs) * NINDIR(fs);
101 return blkno + 2 + NINDIR(fs) + n / (NINDIR(fs) * NINDIR(fs)) + 1;
102 }
103
104 #define BASE_SINDIR (NDADDR)
105 #define BASE_DINDIR (NDADDR+NINDIR(fs))
106 #define BASE_TINDIR (NDADDR+NINDIR(fs)+NINDIR(fs)*NINDIR(fs))
107
108 #define D_UNITS (NINDIR(fs))
109 #define T_UNITS (NINDIR(fs)*NINDIR(fs))
110
111 ufs_daddr_t lfs_bmap(struct lfs *, struct dinode *, ufs_daddr_t);
112
113 ufs_daddr_t
114 lfs_bmap(struct lfs * fs, struct dinode * idinode, ufs_daddr_t lbn)
115 {
116 ufs_daddr_t residue, up, off = 0;
117 struct bufarea *bp;
118
119 if (lbn > 0 && lbn > (idinode->di_size - 1) / dev_bsize) {
120 return UNASSIGNED;
121 }
122 /*
123 * Indirect blocks: if it is a first-level indirect, pull its
124 * address from the inode; otherwise, call ourselves to find the
125 * address of the parent indirect block, and load that to find
126 * the desired address.
127 */
128 if (lbn < 0) {
129 lbn *= -1;
130 if (lbn == NDADDR) {
131 /* printf("lbn %d: single indir base\n", -lbn); */
132 return idinode->di_ib[0]; /* single indirect */
133 } else if (lbn == BASE_DINDIR + 1) {
134 /* printf("lbn %d: double indir base\n", -lbn); */
135 return idinode->di_ib[1]; /* double indirect */
136 } else if (lbn == BASE_TINDIR + 2) {
137 /* printf("lbn %d: triple indir base\n", -lbn); */
138 return idinode->di_ib[2]; /* triple indirect */
139 }
140 /*
141 * Find the immediate parent. This is essentially finding the
142 * residue of modulus, and then rounding accordingly.
143 */
144 residue = (lbn - NDADDR) % NINDIR(fs);
145 if (residue == 1) {
146 /* Double indirect. Parent is the triple. */
147 up = idinode->di_ib[2];
148 off = (lbn - 2 - BASE_TINDIR) / (NINDIR(fs) * NINDIR(fs));
149 if (up == UNASSIGNED || up == LFS_UNUSED_DADDR)
150 return UNASSIGNED;
151 /* printf("lbn %d: parent is the triple\n", -lbn); */
152 bp = getddblk(up, sblock.lfs_bsize);
153 bp->b_flags &= ~B_INUSE;
154 return ((daddr_t *)(bp->b_un.b_buf))[off];
155 } else { /* residue == 0 */
156 /* Single indirect. Two cases. */
157 if (lbn < BASE_TINDIR) {
158 /* Parent is the double, simple */
159 up = -(BASE_DINDIR) - 1;
160 off = (lbn - BASE_DINDIR) / D_UNITS;
161 /*
162 * printf("lbn %d: parent is %d/%d\n", -lbn,
163 * up,off);
164 */
165 } else {
166 /* Ancestor is the triple, more complex */
167 up = ((lbn - BASE_TINDIR) / T_UNITS)
168 * T_UNITS + BASE_TINDIR + 1;
169 off = (lbn / D_UNITS) - (up / D_UNITS);
170 up = -up;
171 /*
172 * printf("lbn %d: parent is %d/%d\n", -lbn,
173 * up,off);
174 */
175 }
176 }
177 } else {
178 /* Direct block. Its parent must be a single indirect. */
179 if (lbn < NDADDR)
180 return idinode->di_db[lbn];
181 else {
182 /* Parent is an indirect block. */
183 up = -(((lbn - NDADDR) / D_UNITS) * D_UNITS + NDADDR);
184 off = (lbn - NDADDR) % D_UNITS;
185 /* printf("lbn %d: parent is %d/%d\n", lbn,up,off); */
186 }
187 }
188 up = lfs_bmap(fs, idinode, up);
189 if (up == UNASSIGNED || up == LFS_UNUSED_DADDR)
190 return UNASSIGNED;
191 bp = getddblk(up, sblock.lfs_bsize);
192 bp->b_flags &= ~B_INUSE;
193 return ((daddr_t *)(bp->b_un.b_buf))[off];
194 }
195
196 /*
197 * This is kind of gross. We use this to find the nth block
198 * from a file whose inode has disk address idaddr. In practice
199 * we will only use this to find blocks of the ifile.
200 */
201 static struct bufarea empty;
202
203 struct bufarea *
204 getfileblk(struct lfs * fs, struct dinode * idinode, ino_t lbn)
205 {
206 struct bufarea *bp;
207 ufs_daddr_t blkno;
208 static char empty_buf[65536];
209
210 empty.b_un.b_buf = &(empty_buf[0]);
211
212 blkno = lfs_bmap(fs, idinode, lbn);
213 if (blkno == UNASSIGNED || blkno == LFS_UNUSED_DADDR) {
214 printf("Warning: ifile lbn %d unassigned!\n", lbn);
215 return ∅
216 }
217 bp = getddblk(blkno, sblock.lfs_bsize);
218 return bp;
219 }
220
221 #if 0
222 static struct dinode *
223 gidinode(void)
224 {
225 static struct dinode *idinode;
226
227 if (!idinode) { /* only need to do this once */
228 idinode = lfs_difind(&sblock, sblock.lfs_ifile, &ifblock);
229 }
230 return idinode;
231 }
232 #endif
233
234 struct ifile *
235 lfs_ientry(ino_t ino, struct bufarea ** bpp)
236 {
237 IFILE *ifp;
238
239 *bpp = getfileblk(&sblock, lfs_ginode(LFS_IFILE_INUM),
240 ino / sblock.lfs_ifpb + sblock.lfs_cleansz +
241 sblock.lfs_segtabsz);
242 if (*bpp == &empty) {
243 printf("Warning: ino %d ientry in unassigned block\n", ino);
244 }
245 if (*bpp) {
246 if (sblock.lfs_version > 1) {
247 ifp = (((IFILE *)((*bpp)->b_un.b_buf)) +
248 (ino % sblock.lfs_ifpb));
249 } else {
250 ifp = (IFILE *)(((IFILE_V1 *)
251 ((*bpp)->b_un.b_buf)) +
252 (ino % sblock.lfs_ifpb));
253 }
254 return ifp;
255 } else
256 return NULL;
257 }
258
259 SEGUSE *
260 lfs_gseguse(int segnum, struct bufarea ** bpp)
261 {
262 int blkno;
263 struct bufarea *bp;
264
265 blkno = segnum / sblock.lfs_sepb + sblock.lfs_cleansz;
266 (*bpp) = bp = getfileblk(&sblock, lfs_ginode(LFS_IFILE_INUM), blkno);
267 if (sblock.lfs_version == 1)
268 return (SEGUSE *)((SEGUSE_V1 *)(bp->b_un.b_buf) +
269 segnum % sblock.lfs_sepb);
270 else
271 return (SEGUSE *)(bp->b_un.b_buf) + segnum % sblock.lfs_sepb;
272 }
273
274 daddr_t
275 lfs_ino_daddr(ino_t inumber)
276 {
277 daddr_t daddr;
278 IFILE *ifp;
279 struct bufarea *bp;
280
281 if (din_table[inumber]) {
282 daddr = din_table[inumber];
283 } else {
284 if (inumber == LFS_IFILE_INUM)
285 daddr = idaddr;
286 else {
287 ifp = lfs_ientry(inumber, &bp);
288 if (ifp == NULL) {
289 return NULL;
290 }
291 if (ifp->if_daddr == LFS_UNUSED_DADDR) {
292 bp->b_flags &= ~B_INUSE;
293 return NULL;
294 }
295 bp->b_flags &= ~B_INUSE;
296 daddr = ifp->if_daddr;
297 }
298
299 din_table[inumber] = daddr;
300 seg_table[datosn(&sblock, daddr)].su_nbytes += DINODE_SIZE;
301 }
302 return daddr;
303 }
304
305 struct dinode *
306 lfs_ginode(ino_t inumber)
307 {
308 struct ifile *ifp;
309 struct dinode *din;
310 struct bufarea *bp;
311 daddr_t daddr;
312
313 if (inumber >= maxino)
314 errexit("bad inode number %d to lfs_ginode\n", inumber);
315
316 #if 0
317 if (inumber == LFS_IFILE_INUM) {
318 daddr = idaddr;
319 if (din_table[LFS_IFILE_INUM] == 0) {
320 din_table[LFS_IFILE_INUM] = daddr;
321 seg_table[datosn(&sblock, daddr)].su_nbytes += DINODE_SIZE;
322 }
323 return gidinode();
324 }
325 #endif
326
327 daddr = lfs_ino_daddr(inumber);
328 if (daddr == 0)
329 return NULL;
330
331 if (pbp)
332 pbp->b_flags &= ~B_INUSE;
333
334 pbp = getddblk(daddr, sblock.lfs_bsize);
335 din = lfs_difind(&sblock, inumber, pbp->b_un.b_dinode);
336
337 if (din == NULL) {
338 pfatal("INODE %d NOT FOUND\n", inumber);
339 if (reply("free")) {
340 ifp = lfs_ientry(inumber, &bp);
341 ifp->if_daddr = LFS_UNUSED_DADDR;
342 ifp->if_nextfree = sblock.lfs_free;
343 sblock.lfs_free = inumber;
344 sbdirty();
345 dirty(bp);
346 bp->b_flags &= ~B_INUSE;
347 }
348 }
349 return din;
350 }
351
352 /* imported from lfs_vfsops.c */
353 int
354 ino_to_fsba(struct lfs * fs, ino_t ino)
355 {
356 daddr_t daddr = LFS_UNUSED_DADDR;
357 struct ifile *ifp;
358 struct bufarea *bp;
359
360 /* Translate the inode number to a disk address. */
361 if (ino == LFS_IFILE_INUM)
362 daddr = fs->lfs_idaddr;
363 else {
364 ifp = lfs_ientry(ino, &bp);
365 if (ifp) {
366 daddr = ifp->if_daddr;
367 } else {
368 pwarn("Can't locate inode #%ud\n", ino);
369 }
370 bp->b_flags &= ~B_INUSE;
371 }
372 return daddr;
373 }
374
375 /*
376 * Check validity of held (direct) blocks in an inode.
377 */
378 int
379 ckinode(struct dinode *dp, struct inodesc *idesc)
380 {
381 register ufs_daddr_t *ap;
382 long ret, n, ndb, offset;
383 struct dinode dino;
384 u_int64_t remsize, sizepb;
385 mode_t mode;
386 char pathbuf[MAXPATHLEN + 1];
387
388 if (idesc->id_fix != IGNORE)
389 idesc->id_fix = DONTKNOW;
390 idesc->id_entryno = 0;
391 idesc->id_filesize = dp->di_size;
392 mode = dp->di_mode & IFMT;
393 if (mode == IFBLK || mode == IFCHR ||
394 (mode == IFLNK && (dp->di_size < sblock.lfs_maxsymlinklen ||
395 (sblock.lfs_maxsymlinklen == 0 &&
396 dp->di_blocks == 0))))
397 return (KEEPON);
398 dino = *dp;
399 ndb = howmany(dino.di_size, sblock.lfs_bsize);
400
401 for (ap = &dino.di_db[0]; ap < &dino.di_db[NDADDR]; ap++) {
402 if (--ndb == 0 && (offset = blkoff(&sblock, dino.di_size)) != 0) {
403 idesc->id_numfrags =
404 numfrags(&sblock, fragroundup(&sblock, offset));
405 } else
406 idesc->id_numfrags = sblock.lfs_frag;
407 if (*ap == 0) {
408 if (idesc->id_type == DATA && ndb >= 0) {
409 /* An empty block in a directory XXX */
410 getpathname(pathbuf, idesc->id_number,
411 idesc->id_number);
412 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
413 pathbuf);
414 if (reply("ADJUST LENGTH") == 1) {
415 dp = ginode(idesc->id_number);
416 dp->di_size = (ap - &dino.di_db[0]) *
417 sblock.lfs_bsize;
418 printf(
419 "YOU MUST RERUN FSCK AFTERWARDS\n");
420 rerun = 1;
421 inodirty();
422 }
423 }
424 continue;
425 }
426 idesc->id_blkno = *ap;
427 idesc->id_lblkno = ap - &dino.di_db[0];
428 if (idesc->id_type == ADDR) {
429 ret = (*idesc->id_func)(idesc);
430 } else
431 ret = dirscan(idesc);
432 idesc->id_lblkno = 0;
433 if (ret & STOP)
434 return (ret);
435 }
436 idesc->id_numfrags = sblock.lfs_frag;
437 remsize = dino.di_size - sblock.lfs_bsize * NDADDR;
438 sizepb = sblock.lfs_bsize;
439 for (ap = &dino.di_ib[0], n = 1; n <= NIADDR; ap++, n++) {
440 if (*ap) {
441 idesc->id_blkno = *ap;
442 ret = iblock(idesc, n, remsize);
443 if (ret & STOP)
444 return (ret);
445 } else {
446 if (idesc->id_type == DATA && remsize > 0) {
447 /* An empty block in a directory XXX */
448 getpathname(pathbuf, idesc->id_number,
449 idesc->id_number);
450 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
451 pathbuf);
452 if (reply("ADJUST LENGTH") == 1) {
453 dp = ginode(idesc->id_number);
454 dp->di_size -= remsize;
455 remsize = 0;
456 printf(
457 "YOU MUST RERUN FSCK AFTERWARDS\n");
458 rerun = 1;
459 inodirty();
460 break;
461 }
462 }
463 }
464 sizepb *= NINDIR(&sblock);
465 remsize -= sizepb;
466 }
467 return (KEEPON);
468 }
469
470 static int
471 iblock(struct inodesc * idesc, long ilevel, u_int64_t isize)
472 {
473 daddr_t *ap, *aplim;
474 struct bufarea *bp;
475 int i, n, (*func)(struct inodesc *), nif;
476 u_int64_t sizepb;
477 char pathbuf[MAXPATHLEN + 1], buf[BUFSIZ];
478 struct dinode *dp;
479
480 if (idesc->id_type == ADDR) {
481 func = idesc->id_func;
482 n = (*func)(idesc);
483 if ((n & KEEPON) == 0)
484 return (n);
485 } else
486 func = dirscan;
487 if (chkrange(idesc->id_blkno, fragstodb(&sblock, idesc->id_numfrags)))
488 return (SKIP);
489 bp = getddblk(idesc->id_blkno, sblock.lfs_bsize);
490 ilevel--;
491 for (sizepb = sblock.lfs_bsize, i = 0; i < ilevel; i++)
492 sizepb *= NINDIR(&sblock);
493 if (isize > sizepb * NINDIR(&sblock))
494 nif = NINDIR(&sblock);
495 else
496 nif = howmany(isize, sizepb);
497 if (idesc->id_func == pass1check && nif < NINDIR(&sblock)) {
498 aplim = &bp->b_un.b_indir[NINDIR(&sblock)];
499 for (ap = &bp->b_un.b_indir[nif]; ap < aplim; ap++) {
500 if (*ap == 0)
501 continue;
502 (void)sprintf(buf, "PARTIALLY TRUNCATED INODE I=%u",
503 idesc->id_number);
504 if (dofix(idesc, buf)) {
505 *ap = 0;
506 dirty(bp);
507 }
508 }
509 flush(fswritefd, bp);
510 }
511 aplim = &bp->b_un.b_indir[nif];
512 for (ap = bp->b_un.b_indir; ap < aplim; ap++) {
513 if (*ap) {
514 idesc->id_blkno = *ap;
515 if (ilevel == 0)
516 n = (*func)(idesc);
517 else
518 n = iblock(idesc, ilevel, isize);
519 if (n & STOP) {
520 bp->b_flags &= ~B_INUSE;
521 return (n);
522 }
523 } else {
524 if (idesc->id_type == DATA && isize > 0) {
525 /* An empty block in a directory XXX */
526 getpathname(pathbuf, idesc->id_number,
527 idesc->id_number);
528 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
529 pathbuf);
530 if (reply("ADJUST LENGTH") == 1) {
531 dp = ginode(idesc->id_number);
532 dp->di_size -= isize;
533 isize = 0;
534 printf(
535 "YOU MUST RERUN FSCK AFTERWARDS\n");
536 rerun = 1;
537 inodirty();
538 bp->b_flags &= ~B_INUSE;
539 return (STOP);
540 }
541 }
542 }
543 isize -= sizepb;
544 }
545 bp->b_flags &= ~B_INUSE;
546 return (KEEPON);
547 }
548
549 /*
550 * Check that a block in a legal block number.
551 * Return 0 if in range, 1 if out of range.
552 */
553 int
554 chkrange(daddr_t blk, int cnt)
555 {
556 if (blk < sntoda(&sblock, 0)) {
557 return (1);
558 }
559 if (blk > maxfsblock) {
560 return (1);
561 }
562 if (blk + cnt < sntoda(&sblock, 0)) {
563 return (1);
564 }
565 if (blk + cnt > maxfsblock) {
566 return (1);
567 }
568 return (0);
569 }
570
571 /*
572 * General purpose interface for reading inodes.
573 */
574 struct dinode *
575 ginode(ino_t inumber)
576 {
577 return lfs_ginode(inumber);
578 }
579
580 /*
581 * Routines to maintain information about directory inodes.
582 * This is built during the first pass and used during the
583 * second and third passes.
584 *
585 * Enter inodes into the cache.
586 */
587 void
588 cacheino(struct dinode *dp, ino_t inumber)
589 {
590 register struct inoinfo *inp;
591 struct inoinfo **inpp;
592 unsigned int blks;
593
594 blks = howmany(dp->di_size, sblock.lfs_bsize);
595 if (blks > NDADDR)
596 blks = NDADDR + NIADDR;
597 inp = (struct inoinfo *)
598 malloc(sizeof(*inp) + (blks - 1) * sizeof(daddr_t));
599 if (inp == NULL)
600 return;
601 inpp = &inphead[inumber % numdirs];
602 inp->i_nexthash = *inpp;
603 *inpp = inp;
604 inp->i_child = inp->i_sibling = inp->i_parentp = 0;
605 if (inumber == ROOTINO)
606 inp->i_parent = ROOTINO;
607 else
608 inp->i_parent = (ino_t)0;
609 inp->i_dotdot = (ino_t)0;
610 inp->i_number = inumber;
611 inp->i_isize = dp->di_size;
612 inp->i_numblks = blks * sizeof(daddr_t);
613 memcpy(&inp->i_blks[0], &dp->di_db[0], (size_t)inp->i_numblks);
614 if (inplast == listmax) {
615 listmax += 100;
616 inpsort = (struct inoinfo **)realloc((char *) inpsort,
617 (unsigned)listmax * sizeof(struct inoinfo *));
618 if (inpsort == NULL)
619 errexit("cannot increase directory list");
620 }
621 inpsort[inplast++] = inp;
622 }
623
624 /*
625 * Look up an inode cache structure.
626 */
627 struct inoinfo *
628 getinoinfo(ino_t inumber)
629 {
630 register struct inoinfo *inp;
631
632 for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
633 if (inp->i_number != inumber)
634 continue;
635 return (inp);
636 }
637 errexit("cannot find inode %d\n", inumber);
638 return ((struct inoinfo *)0);
639 }
640
641 /*
642 * Clean up all the inode cache structure.
643 */
644 void
645 inocleanup()
646 {
647 register struct inoinfo **inpp;
648
649 if (inphead == NULL)
650 return;
651 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
652 free((char *)(*inpp));
653 free((char *)inphead);
654 free((char *)inpsort);
655 inphead = inpsort = NULL;
656 }
657
658 void
659 inodirty()
660 {
661 dirty(pbp);
662 }
663
664 void
665 clri(struct inodesc *idesc, char *type, int flag)
666 {
667 register struct dinode *dp;
668 struct bufarea *bp;
669 IFILE *ifp;
670
671 dp = ginode(idesc->id_number);
672 if (flag == 1) {
673 pwarn("%s %s", type,
674 (dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE");
675 pinode(idesc->id_number);
676 }
677 if (preen || reply("CLEAR") == 1) {
678 if (preen)
679 printf(" (CLEARED)\n");
680 n_files--;
681 (void)ckinode(dp, idesc);
682 clearinode(dp);
683 statemap[idesc->id_number] = USTATE;
684 inodirty();
685
686 /* Send cleared inode to the free list */
687
688 ifp = lfs_ientry(idesc->id_number, &bp);
689 ifp->if_daddr = LFS_UNUSED_DADDR;
690 ifp->if_nextfree = sblock.lfs_free;
691 sblock.lfs_free = idesc->id_number;
692 sbdirty();
693 dirty(bp);
694 bp->b_flags &= ~B_INUSE;
695 }
696 }
697
698 int
699 findname(struct inodesc *idesc)
700 {
701 register struct direct *dirp = idesc->id_dirp;
702
703 if (dirp->d_ino != idesc->id_parent)
704 return (KEEPON);
705 memcpy(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1);
706 return (STOP | FOUND);
707 }
708
709 int
710 findino(struct inodesc *idesc)
711 {
712 register struct direct *dirp = idesc->id_dirp;
713
714 if (dirp->d_ino == 0)
715 return (KEEPON);
716 if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
717 dirp->d_ino >= ROOTINO && dirp->d_ino < maxino) {
718 idesc->id_parent = dirp->d_ino;
719 return (STOP | FOUND);
720 }
721 return (KEEPON);
722 }
723
724 void
725 pinode(ino_t ino)
726 {
727 register struct dinode *dp;
728 register char *p;
729 struct passwd *pw;
730 time_t t;
731
732 printf(" I=%u ", ino);
733 if (ino < ROOTINO || ino >= maxino)
734 return;
735 dp = ginode(ino);
736 if (dp) {
737 printf(" OWNER=");
738 #ifndef SMALL
739 if ((pw = getpwuid((int)dp->di_uid)) != 0)
740 printf("%s ", pw->pw_name);
741 else
742 #endif
743 printf("%u ", (unsigned)dp->di_uid);
744 printf("MODE=%o\n", dp->di_mode);
745 if (preen)
746 printf("%s: ", cdevname());
747 printf("SIZE=%llu ", (unsigned long long)dp->di_size);
748 t = dp->di_mtime;
749 p = ctime(&t);
750 printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
751 }
752 }
753
754 void
755 blkerror(ino_t ino, char *type, daddr_t blk)
756 {
757
758 pfatal("%d %s I=%u", blk, type, ino);
759 printf("\n");
760 if (exitonfail)
761 exit(1);
762 switch (statemap[ino]) {
763
764 case FSTATE:
765 statemap[ino] = FCLEAR;
766 return;
767
768 case DSTATE:
769 statemap[ino] = DCLEAR;
770 return;
771
772 case FCLEAR:
773 case DCLEAR:
774 return;
775
776 default:
777 errexit("BAD STATE %d TO BLKERR", statemap[ino]);
778 /* NOTREACHED */
779 }
780 }
781
782 /*
783 * allocate an unused inode
784 */
785 ino_t
786 allocino(ino_t request, int type)
787 {
788 register ino_t ino;
789 register struct dinode *dp;
790 time_t t;
791
792 if (request == 0)
793 request = ROOTINO;
794 else if (statemap[request] != USTATE)
795 return (0);
796 for (ino = request; ino < maxino; ino++)
797 if (statemap[ino] == USTATE)
798 break;
799 if (ino == maxino)
800 return (0);
801 switch (type & IFMT) {
802 case IFDIR:
803 statemap[ino] = DSTATE;
804 break;
805 case IFREG:
806 case IFLNK:
807 statemap[ino] = FSTATE;
808 break;
809 default:
810 return (0);
811 }
812 dp = ginode(ino);
813 dp->di_db[0] = allocblk((long)1);
814 if (dp->di_db[0] == 0) {
815 statemap[ino] = USTATE;
816 return (0);
817 }
818 dp->di_mode = type;
819 (void)time(&t);
820 dp->di_atime = t;
821 dp->di_mtime = dp->di_ctime = dp->di_atime;
822 dp->di_size = sblock.lfs_fsize;
823 dp->di_blocks = btodb(sblock.lfs_fsize);
824 n_files++;
825 inodirty();
826 if (newinofmt)
827 typemap[ino] = IFTODT(type);
828 return (ino);
829 }
830
831 /*
832 * deallocate an inode
833 */
834 void
835 freeino(ino_t ino)
836 {
837 struct inodesc idesc;
838 struct dinode *dp;
839
840 memset(&idesc, 0, sizeof(struct inodesc));
841 idesc.id_type = ADDR;
842 idesc.id_func = pass4check;
843 idesc.id_number = ino;
844 dp = ginode(ino);
845 (void)ckinode(dp, &idesc);
846 clearinode(dp);
847 inodirty();
848 statemap[ino] = USTATE;
849
850 n_files--;
851 }
852