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