inode.c revision 1.41 1 /* $NetBSD: inode.c,v 1.41 2003/09/19 08:35:15 itojun 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.41 2003/09/19 08:35:15 itojun 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
356 dp1 = (struct ufs1_dinode *)&dp->dp1;
357 for (i = 0; i < n; i++, dp1++) {
358 ffs_dinode1_swap(dp1, dp1);
359 if ((iswap16(dp1->di_mode) & IFMT) != IFLNK ||
360 (isappleufs && (iswap64(dp1->di_size) >
361 APPLEUFS_MAXSYMLINKLEN)) ||
362 iswap64(dp1->di_size) > sblock->fs_maxsymlinklen) {
363 for (j = 0; j < (NDADDR + NIADDR); j++)
364 dp1->di_db[j] = bswap32(dp1->di_db[j]);
365 }
366 }
367 }
368
369 static void
370 swap_dinode2(union dinode *dp, int n)
371 {
372 int i, j;
373 struct ufs2_dinode *dp2;
374
375 dp2 = (struct ufs2_dinode *)&dp->dp2;
376 for (i = 0; i < n; i++, dp2++) {
377 ffs_dinode2_swap(dp2, dp2);
378 if ((iswap16(dp2->di_mode) & IFMT) != IFLNK) {
379 for (j = 0; j < (NDADDR + NIADDR + NXADDR); j++)
380 dp2->di_extb[j] = bswap64(dp2->di_extb[j]);
381 }
382 }
383 }
384
385 /*
386 * Special purpose version of ginode used to optimize first pass
387 * over all the inodes in numerical order.
388 */
389 ino_t nextino, lastinum, lastvalidinum;
390 long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
391 union dinode *inodebuf;
392
393 union dinode *
394 getnextinode(inumber)
395 ino_t inumber;
396 {
397 long size;
398 daddr_t dblk;
399 static union dinode *dp;
400 union dinode *ret;
401
402 if (inumber != nextino++ || inumber > lastvalidinum) {
403 abort();
404 errx(EEXIT, "bad inode number %d to nextinode", inumber);
405 }
406
407 if (inumber >= lastinum) {
408 readcnt++;
409 dblk = fsbtodb(sblock, ino_to_fsba(sblock, lastinum));
410 if (readcnt % readpercg == 0) {
411 size = partialsize;
412 lastinum += partialcnt;
413 } else {
414 size = inobufsize;
415 lastinum += fullcnt;
416 }
417 (void)bread(fsreadfd, (caddr_t)inodebuf, dblk, size);
418 if (doswap) {
419 if (is_ufs2)
420 swap_dinode2(inodebuf, lastinum - inumber);
421 else
422 swap_dinode1(inodebuf, lastinum - inumber);
423 bwrite(fswritefd, (char *)inodebuf, dblk, size);
424 }
425 dp = (union dinode *)inodebuf;
426 }
427 ret = dp;
428 dp = (union dinode *)
429 ((char *)dp + (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE));
430 return ret;
431 }
432
433 void
434 setinodebuf(inum)
435 ino_t inum;
436 {
437
438 if (inum % sblock->fs_ipg != 0)
439 errx(EEXIT, "bad inode number %d to setinodebuf", inum);
440
441 lastvalidinum = inum + sblock->fs_ipg - 1;
442 startinum = 0;
443 nextino = inum;
444 lastinum = inum;
445 readcnt = 0;
446 if (inodebuf != NULL)
447 return;
448 inobufsize = blkroundup(sblock, INOBUFSIZE);
449 fullcnt = inobufsize / (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
450 readpercg = sblock->fs_ipg / fullcnt;
451 partialcnt = sblock->fs_ipg % fullcnt;
452 partialsize = partialcnt * (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
453 if (partialcnt != 0) {
454 readpercg++;
455 } else {
456 partialcnt = fullcnt;
457 partialsize = inobufsize;
458 }
459 if (inodebuf == NULL &&
460 (inodebuf = malloc((unsigned)inobufsize)) == NULL)
461 errx(EEXIT, "Cannot allocate space for inode buffer");
462 }
463
464 void
465 freeinodebuf()
466 {
467
468 if (inodebuf != NULL)
469 free((char *)inodebuf);
470 inodebuf = NULL;
471 }
472
473 /*
474 * Routines to maintain information about directory inodes.
475 * This is built during the first pass and used during the
476 * second and third passes.
477 *
478 * Enter inodes into the cache.
479 */
480 void
481 cacheino(dp, inumber)
482 union dinode *dp;
483 ino_t inumber;
484 {
485 struct inoinfo *inp;
486 struct inoinfo **inpp, **ninpsort;
487 unsigned int blks;
488 int i;
489 int64_t size;
490
491 size = iswap64(DIP(dp, size));
492 blks = howmany(size, sblock->fs_bsize);
493 if (blks > NDADDR)
494 blks = NDADDR + NIADDR;
495
496 inp = (struct inoinfo *) malloc(sizeof(*inp) + (blks - 1)
497 * sizeof (int64_t));
498 if (inp == NULL)
499 return;
500 inpp = &inphead[inumber % dirhash];
501 inp->i_nexthash = *inpp;
502 *inpp = inp;
503 inp->i_child = inp->i_sibling = inp->i_parentp = 0;
504 if (inumber == ROOTINO)
505 inp->i_parent = ROOTINO;
506 else
507 inp->i_parent = (ino_t)0;
508 inp->i_dotdot = (ino_t)0;
509 inp->i_number = inumber;
510 inp->i_isize = size;
511 inp->i_numblks = blks;
512 for (i = 0; i < (blks < NDADDR ? blks : NDADDR); i++)
513 inp->i_blks[i] = DIP(dp, db[i]);
514 if (blks > NDADDR)
515 for (i = 0; i < NIADDR; i++)
516 inp->i_blks[NDADDR + i] = DIP(dp, ib[i]);
517 if (inplast == listmax) {
518 ninpsort = (struct inoinfo **)realloc((char *)inpsort,
519 (unsigned)(listmax + 100) * sizeof(struct inoinfo *));
520 if (inpsort == NULL)
521 errx(EEXIT, "cannot increase directory list");
522 inpsort = ninpsort;
523 listmax += 100;
524 }
525 inpsort[inplast++] = inp;
526 }
527
528 /*
529 * Look up an inode cache structure.
530 */
531 struct inoinfo *
532 getinoinfo(inumber)
533 ino_t inumber;
534 {
535 struct inoinfo *inp;
536
537 for (inp = inphead[inumber % dirhash]; inp; inp = inp->i_nexthash) {
538 if (inp->i_number != inumber)
539 continue;
540 return (inp);
541 }
542 errx(EEXIT, "cannot find inode %d", inumber);
543 return ((struct inoinfo *)0);
544 }
545
546 /*
547 * Clean up all the inode cache structure.
548 */
549 void
550 inocleanup()
551 {
552 struct inoinfo **inpp;
553
554 if (inphead == NULL)
555 return;
556 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
557 free((char *)(*inpp));
558 free((char *)inphead);
559 free((char *)inpsort);
560 inphead = inpsort = NULL;
561 }
562
563 void
564 inodirty()
565 {
566
567 dirty(pbp);
568 }
569
570 void
571 clri(idesc, type, flag)
572 struct inodesc *idesc;
573 char *type;
574 int flag;
575 {
576 union dinode *dp;
577
578 dp = ginode(idesc->id_number);
579 if (flag == 1) {
580 pwarn("%s %s", type,
581 (iswap16(DIP(dp, mode)) & IFMT) == IFDIR ? "DIR" : "FILE");
582 pinode(idesc->id_number);
583 }
584 if (preen || reply("CLEAR") == 1) {
585 if (preen)
586 printf(" (CLEARED)\n");
587 n_files--;
588 (void)ckinode(dp, idesc);
589 clearinode(dp);
590 inoinfo(idesc->id_number)->ino_state = USTATE;
591 inodirty();
592 } else
593 markclean= 0;
594 }
595
596 int
597 findname(idesc)
598 struct inodesc *idesc;
599 {
600 struct direct *dirp = idesc->id_dirp;
601
602 if (iswap32(dirp->d_ino) != idesc->id_parent || idesc->id_entryno < 2) {
603 idesc->id_entryno++;
604 return (KEEPON);
605 }
606 memmove(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1);
607 return (STOP|FOUND);
608 }
609
610 int
611 findino(idesc)
612 struct inodesc *idesc;
613 {
614 struct direct *dirp = idesc->id_dirp;
615
616 if (dirp->d_ino == 0)
617 return (KEEPON);
618 if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
619 iswap32(dirp->d_ino) >= ROOTINO && iswap32(dirp->d_ino) <= maxino) {
620 idesc->id_parent = iswap32(dirp->d_ino);
621 return (STOP|FOUND);
622 }
623 return (KEEPON);
624 }
625
626 int
627 clearentry(idesc)
628 struct inodesc *idesc;
629 {
630 struct direct *dirp = idesc->id_dirp;
631
632 if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
633 idesc->id_entryno++;
634 return (KEEPON);
635 }
636 dirp->d_ino = 0;
637 return (STOP|FOUND|ALTERED);
638 }
639
640 void
641 pinode(ino)
642 ino_t ino;
643 {
644 union dinode *dp;
645 char *p;
646 struct passwd *pw;
647 time_t t;
648
649 printf(" I=%u ", ino);
650 if (ino < ROOTINO || ino > maxino)
651 return;
652 dp = ginode(ino);
653 printf(" OWNER=");
654 #ifndef SMALL
655 if ((pw = getpwuid((int)iswap32(DIP(dp, uid)))) != 0)
656 printf("%s ", pw->pw_name);
657 else
658 #endif
659 printf("%u ", (unsigned)iswap32(DIP(dp, uid)));
660 printf("MODE=%o\n", iswap16(DIP(dp, mode)));
661 if (preen)
662 printf("%s: ", cdevname());
663 printf("SIZE=%llu ", (unsigned long long)iswap64(DIP(dp, size)));
664 t = iswap32(DIP(dp, mtime));
665 p = ctime(&t);
666 printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
667 }
668
669 void
670 blkerror(ino, type, blk)
671 ino_t ino;
672 char *type;
673 daddr_t blk;
674 {
675 struct inostat *info;
676
677 pfatal("%lld %s I=%u", (long long)blk, type, ino);
678 printf("\n");
679 info = inoinfo(ino);
680 switch (info->ino_state) {
681
682 case FSTATE:
683 info->ino_state = FCLEAR;
684 return;
685
686 case DSTATE:
687 info->ino_state = DCLEAR;
688 return;
689
690 case FCLEAR:
691 case DCLEAR:
692 return;
693
694 default:
695 errx(EEXIT, "BAD STATE %d TO BLKERR", info->ino_state);
696 /* NOTREACHED */
697 }
698 }
699
700 /*
701 * allocate an unused inode
702 */
703 ino_t
704 allocino(request, type)
705 ino_t request;
706 int type;
707 {
708 ino_t ino;
709 union dinode *dp;
710 struct ufs1_dinode *dp1;
711 struct ufs2_dinode *dp2;
712 time_t t;
713 struct cg *cgp = cgrp;
714 int cg;
715 struct inostat *info;
716
717 if (request == 0)
718 request = ROOTINO;
719 else if (inoinfo(request)->ino_state != USTATE)
720 return (0);
721 for (ino = request; ino < maxino; ino++) {
722 info = inoinfo(ino);
723 if (info->ino_state == USTATE)
724 break;
725 }
726 if (ino == maxino)
727 return (0);
728 cg = ino_to_cg(sblock, ino);
729 getblk(&cgblk, cgtod(sblock, cg), sblock->fs_cgsize);
730 memcpy(cgp, cgblk.b_un.b_cg, sblock->fs_cgsize);
731 if ((doswap && !needswap) || (!doswap && needswap))
732 ffs_cg_swap(cgblk.b_un.b_cg, cgp, sblock);
733 if (!cg_chkmagic(cgp, 0))
734 pfatal("CG %d: ALLOCINO: BAD MAGIC NUMBER\n", cg);
735 if (doswap)
736 cgdirty();
737 setbit(cg_inosused(cgp, 0), ino % sblock->fs_ipg);
738 cgp->cg_cs.cs_nifree--;
739 switch (type & IFMT) {
740 case IFDIR:
741 info->ino_state = DSTATE;
742 cgp->cg_cs.cs_ndir++;
743 break;
744 case IFREG:
745 case IFLNK:
746 info->ino_state = FSTATE;
747 break;
748 default:
749 return (0);
750 }
751 cgdirty();
752 dp = ginode(ino);
753 if (is_ufs2) {
754 dp2 = &dp->dp2;
755 dp2->di_db[0] = iswap64(allocblk(1));
756 if (dp2->di_db[0] == 0) {
757 info->ino_state = USTATE;
758 return (0);
759 }
760 dp2->di_mode = iswap16(type);
761 dp2->di_flags = 0;
762 (void)time(&t);
763 dp2->di_atime = iswap64(t);
764 dp2->di_mtime = dp2->di_ctime = dp2->di_atime;
765 dp2->di_size = iswap64(sblock->fs_fsize);
766 dp2->di_blocks = iswap64(btodb(sblock->fs_fsize));
767 } else {
768 dp1 = &dp->dp1;
769 dp1->di_db[0] = iswap32(allocblk(1));
770 if (dp1->di_db[0] == 0) {
771 info->ino_state = USTATE;
772 return (0);
773 }
774 dp1->di_mode = iswap16(type);
775 dp1->di_flags = 0;
776 (void)time(&t);
777 dp1->di_atime = iswap32(t);
778 dp1->di_mtime = dp1->di_ctime = dp1->di_atime;
779 dp1->di_size = iswap64(sblock->fs_fsize);
780 dp1->di_blocks = iswap32(btodb(sblock->fs_fsize));
781 }
782 n_files++;
783 inodirty();
784 if (newinofmt)
785 info->ino_type = IFTODT(type);
786 return (ino);
787 }
788
789 /*
790 * deallocate an inode
791 */
792 void
793 freeino(ino)
794 ino_t ino;
795 {
796 struct inodesc idesc;
797 union dinode *dp;
798
799 memset(&idesc, 0, sizeof(struct inodesc));
800 idesc.id_type = ADDR;
801 idesc.id_func = pass4check;
802 idesc.id_number = ino;
803 dp = ginode(ino);
804 (void)ckinode(dp, &idesc);
805 clearinode(dp);
806 inodirty();
807 inoinfo(ino)->ino_state = USTATE;
808 n_files--;
809 }
810