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