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