inode.c revision 1.63.2.1 1 /* $NetBSD: inode.c,v 1.63.2.1 2011/01/20 14:24:53 bouyer 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.63.2.1 2011/01/20 14:24:53 bouyer 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(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(union dinode *dp, struct inodesc *idesc)
71 {
72 int ret, offset, i;
73 union dinode dino;
74 u_int64_t sizepb;
75 int64_t remsize;
76 daddr_t ndb;
77 mode_t mode;
78 char pathbuf[MAXPATHLEN + 1];
79
80 if (idesc->id_fix != IGNORE)
81 idesc->id_fix = DONTKNOW;
82 idesc->id_entryno = 0;
83 idesc->id_filesize = iswap64(DIP(dp, size));
84 mode = iswap16(DIP(dp, mode)) & IFMT;
85 if (mode == IFBLK || mode == IFCHR || (mode == IFLNK &&
86 (idesc->id_filesize < sblock->fs_maxsymlinklen ||
87 (isappleufs && (idesc->id_filesize < APPLEUFS_MAXSYMLINKLEN)) ||
88 (sblock->fs_maxsymlinklen == 0 && DIP(dp, blocks) == 0))))
89 return (KEEPON);
90 if (is_ufs2)
91 dino.dp2 = dp->dp2;
92 else
93 dino.dp1 = dp->dp1;
94 ndb = howmany(iswap64(DIP(&dino, size)), sblock->fs_bsize);
95 for (i = 0; i < NDADDR; i++) {
96 if (--ndb == 0 &&
97 (offset = blkoff(sblock, iswap64(DIP(&dino, size)))) != 0)
98 idesc->id_numfrags =
99 numfrags(sblock, fragroundup(sblock, offset));
100 else
101 idesc->id_numfrags = sblock->fs_frag;
102 if (DIP(&dino, db[i]) == 0) {
103 if (idesc->id_type == DATA && ndb >= 0) {
104 /* An empty block in a directory XXX */
105 markclean = 0;
106 getpathname(pathbuf, sizeof(pathbuf),
107 idesc->id_number, idesc->id_number);
108 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
109 pathbuf);
110 if (reply("ADJUST LENGTH") == 1) {
111 dp = ginode(idesc->id_number);
112 DIP_SET(dp, size, iswap64(i *
113 sblock->fs_bsize));
114 printf(
115 "YOU MUST RERUN FSCK AFTERWARDS\n");
116 rerun = 1;
117 inodirty();
118 }
119 }
120 continue;
121 }
122 if (is_ufs2)
123 idesc->id_blkno = iswap64(dino.dp2.di_db[i]);
124 else
125 idesc->id_blkno = iswap32(dino.dp1.di_db[i]);
126 if (idesc->id_type != DATA)
127 ret = (*idesc->id_func)(idesc);
128 else
129 ret = dirscan(idesc);
130 if (ret & STOP)
131 return (ret);
132 }
133 idesc->id_numfrags = sblock->fs_frag;
134 remsize = iswap64(DIP(&dino, size)) - sblock->fs_bsize * NDADDR;
135 sizepb = sblock->fs_bsize;
136 for (i = 0; i < NIADDR; i++) {
137 if (DIP(&dino, ib[i])) {
138 if (is_ufs2)
139 idesc->id_blkno = iswap64(dino.dp2.di_ib[i]);
140 else
141 idesc->id_blkno = iswap32(dino.dp1.di_ib[i]);
142 ret = iblock(idesc, i + 1, remsize);
143 if (ret & STOP)
144 return (ret);
145 } else {
146 if (idesc->id_type == DATA && remsize > 0) {
147 /* An empty block in a directory XXX */
148 markclean = 0;
149 getpathname(pathbuf, sizeof(pathbuf),
150 idesc->id_number, idesc->id_number);
151 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
152 pathbuf);
153 if (reply("ADJUST LENGTH") == 1) {
154 dp = ginode(idesc->id_number);
155 DIP_SET(dp, size,
156 iswap64(iswap64(DIP(dp, size))
157 - remsize));
158 remsize = 0;
159 printf(
160 "YOU MUST RERUN FSCK AFTERWARDS\n");
161 rerun = 1;
162 inodirty();
163 break;
164 }
165 }
166 }
167 sizepb *= NINDIR(sblock);
168 remsize -= sizepb;
169 }
170 return (KEEPON);
171 }
172
173 static int
174 iblock(struct inodesc *idesc, long ilevel, u_int64_t isize)
175 {
176 struct bufarea *bp;
177 int i, n, (*func) (struct inodesc *), nif;
178 u_int64_t sizepb;
179 char buf[BUFSIZ];
180 char pathbuf[MAXPATHLEN + 1];
181 union dinode *dp;
182
183 if (idesc->id_type != DATA) {
184 func = idesc->id_func;
185 if (((n = (*func)(idesc)) & KEEPON) == 0)
186 return (n);
187 } else
188 func = dirscan;
189 if (chkrange(idesc->id_blkno, idesc->id_numfrags))
190 return (SKIP);
191 bp = getdatablk(idesc->id_blkno, sblock->fs_bsize);
192 ilevel--;
193 for (sizepb = sblock->fs_bsize, i = 0; i < ilevel; i++)
194 sizepb *= NINDIR(sblock);
195 if (howmany(isize, sizepb) > (size_t)NINDIR(sblock))
196 nif = NINDIR(sblock);
197 else
198 nif = howmany(isize, sizepb);
199 if (do_blkswap) { /* swap byte order of the whole blk */
200 if (is_ufs2) {
201 for (i = 0; i < nif; i++)
202 bp->b_un.b_indir2[i] =
203 bswap64(bp->b_un.b_indir2[i]);
204 } else {
205 for (i = 0; i < nif; i++)
206 bp->b_un.b_indir1[i] =
207 bswap32(bp->b_un.b_indir1[i]);
208 }
209 dirty(bp);
210 flush(fswritefd, bp);
211 }
212 if (idesc->id_func == pass1check && nif < NINDIR(sblock)) {
213 for (i = nif; i < NINDIR(sblock); i++) {
214 if (IBLK(bp, i) == 0)
215 continue;
216 (void)snprintf(buf, sizeof(buf),
217 "PARTIALLY TRUNCATED INODE I=%llu",
218 (unsigned long long)idesc->id_number);
219 if (dofix(idesc, buf)) {
220 IBLK_SET(bp, i, 0);
221 dirty(bp);
222 } else
223 markclean = 0;
224 }
225 flush(fswritefd, bp);
226 }
227 for (i = 0; i < nif; i++) {
228 if (IBLK(bp, i)) {
229 if (is_ufs2)
230 idesc->id_blkno = iswap64(bp->b_un.b_indir2[i]);
231 else
232 idesc->id_blkno = iswap32(bp->b_un.b_indir1[i]);
233 if (ilevel == 0)
234 n = (*func)(idesc);
235 else
236 n = iblock(idesc, ilevel, isize);
237 if (n & STOP) {
238 bp->b_flags &= ~B_INUSE;
239 return (n);
240 }
241 } else {
242 if (idesc->id_type == DATA && isize > 0) {
243 /* An empty block in a directory XXX */
244 markclean = 0;
245 getpathname(pathbuf, sizeof(pathbuf),
246 idesc->id_number, idesc->id_number);
247 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
248 pathbuf);
249 if (reply("ADJUST LENGTH") == 1) {
250 dp = ginode(idesc->id_number);
251 DIP_SET(dp, size,
252 iswap64(iswap64(DIP(dp, size))
253 - isize));
254 isize = 0;
255 printf(
256 "YOU MUST RERUN FSCK AFTERWARDS\n");
257 rerun = 1;
258 inodirty();
259 bp->b_flags &= ~B_INUSE;
260 return(STOP);
261 }
262 }
263 }
264 isize -= sizepb;
265 }
266 bp->b_flags &= ~B_INUSE;
267 return (KEEPON);
268 }
269
270 /*
271 * Check that a block in a legal block number.
272 * Return 0 if in range, 1 if out of range.
273 */
274 int
275 chkrange(daddr_t blk, int cnt)
276 {
277 int c;
278
279 if (cnt <= 0 || blk <= 0 || blk > maxfsblock ||
280 cnt - 1 > maxfsblock - blk)
281 return (1);
282 if (cnt > sblock->fs_frag ||
283 fragnum(sblock, blk) + cnt > sblock->fs_frag) {
284 if (debug)
285 printf("bad size: blk %lld, offset %d, size %d\n",
286 (long long)blk, (int)fragnum(sblock, blk), cnt);
287 }
288 c = dtog(sblock, blk);
289 if (blk < cgdmin(sblock, c)) {
290 if ((blk + cnt) > cgsblock(sblock, c)) {
291 if (debug) {
292 printf("blk %lld < cgdmin %lld;",
293 (long long)blk,
294 (long long)cgdmin(sblock, c));
295 printf(" blk + cnt %lld > cgsbase %lld\n",
296 (long long)(blk + cnt),
297 (long long)cgsblock(sblock, c));
298 }
299 return (1);
300 }
301 } else {
302 if ((blk + cnt) > cgbase(sblock, c+1)) {
303 if (debug) {
304 printf("blk %lld >= cgdmin %lld;",
305 (long long)blk,
306 (long long)cgdmin(sblock, c));
307 printf(" blk + cnt %lld > sblock->fs_fpg %d\n",
308 (long long)(blk+cnt), sblock->fs_fpg);
309 }
310 return (1);
311 }
312 }
313 return (0);
314 }
315
316 /*
317 * General purpose interface for reading inodes.
318 */
319 union dinode *
320 ginode(ino_t inumber)
321 {
322 daddr_t iblk;
323 int blkoff;
324
325 if (inumber < ROOTINO || inumber > maxino)
326 errexit("bad inode number %llu to ginode",
327 (unsigned long long)inumber);
328 if (startinum == 0 ||
329 inumber < startinum || inumber >= startinum + INOPB(sblock)) {
330 iblk = ino_to_fsba(sblock, inumber);
331 if (pbp != 0)
332 pbp->b_flags &= ~B_INUSE;
333 pbp = getdatablk(iblk, sblock->fs_bsize);
334 startinum = (inumber / INOPB(sblock)) * INOPB(sblock);
335 }
336 if (is_ufs2) {
337 blkoff = (inumber % INOPB(sblock)) * DINODE2_SIZE;
338 return ((union dinode *)((caddr_t)pbp->b_un.b_buf + blkoff));
339 }
340 blkoff = (inumber % INOPB(sblock)) * DINODE1_SIZE;
341 return ((union dinode *)((caddr_t)pbp->b_un.b_buf + blkoff));
342 }
343
344 static void
345 swap_dinode1(union dinode *dp, int n)
346 {
347 int i, j;
348 struct ufs1_dinode *dp1;
349 int32_t maxsymlinklen = sblock->fs_maxsymlinklen;
350 if (isappleufs)
351 maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
352
353 dp1 = (struct ufs1_dinode *)&dp->dp1;
354 for (i = 0; i < n; i++, dp1++) {
355 ffs_dinode1_swap(dp1, dp1);
356 if (((iswap16(dp1->di_mode) & IFMT) != IFLNK) ||
357 doinglevel2 ||
358 (maxsymlinklen < 0) ||
359 (iswap64(dp1->di_size) > (uint64_t)maxsymlinklen)) {
360 for (j = 0; j < (NDADDR + NIADDR); j++)
361 dp1->di_db[j] = bswap32(dp1->di_db[j]);
362 }
363 }
364 }
365
366 static void
367 swap_dinode2(union dinode *dp, int n)
368 {
369 int i, j;
370 struct ufs2_dinode *dp2;
371
372 dp2 = (struct ufs2_dinode *)&dp->dp2;
373 for (i = 0; i < n; i++, dp2++) {
374 ffs_dinode2_swap(dp2, dp2);
375 if ((iswap16(dp2->di_mode) & IFMT) != IFLNK) {
376 for (j = 0; j < (NDADDR + NIADDR + NXADDR); j++)
377 dp2->di_extb[j] = bswap64(dp2->di_extb[j]);
378 }
379 }
380 }
381
382 /*
383 * Special purpose version of ginode used to optimize first pass
384 * over all the inodes in numerical order.
385 */
386 ino_t nextino, lastinum, lastvalidinum;
387 long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
388 union dinode *inodebuf;
389
390 union dinode *
391 getnextinode(ino_t inumber)
392 {
393 long size;
394 daddr_t dblk;
395 static union dinode *dp;
396 union dinode *ret;
397
398 if (inumber != nextino++ || inumber > lastvalidinum)
399 errexit("bad inode number %llu to nextinode",
400 (unsigned long long)inumber);
401
402 if (inumber >= lastinum) {
403 readcnt++;
404 dblk = fsbtodb(sblock, ino_to_fsba(sblock, lastinum));
405 if (readcnt % readpercg == 0) {
406 size = partialsize;
407 lastinum += partialcnt;
408 } else {
409 size = inobufsize;
410 lastinum += fullcnt;
411 }
412 (void)bread(fsreadfd, (caddr_t)inodebuf, dblk, size);
413 if (doswap) {
414 if (is_ufs2)
415 swap_dinode2(inodebuf, lastinum - inumber);
416 else
417 swap_dinode1(inodebuf, lastinum - inumber);
418 bwrite(fswritefd, (char *)inodebuf, dblk, size);
419 }
420 dp = (union dinode *)inodebuf;
421 }
422 ret = dp;
423 dp = (union dinode *)
424 ((char *)dp + (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE));
425 return ret;
426 }
427
428 void
429 setinodebuf(ino_t inum)
430 {
431
432 if (inum % sblock->fs_ipg != 0)
433 errexit("bad inode number %llu to setinodebuf",
434 (unsigned long long)inum);
435
436 lastvalidinum = inum + sblock->fs_ipg - 1;
437 startinum = 0;
438 nextino = inum;
439 lastinum = inum;
440 readcnt = 0;
441 if (inodebuf != NULL)
442 return;
443 inobufsize = blkroundup(sblock, INOBUFSIZE);
444 fullcnt = inobufsize / (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
445 readpercg = sblock->fs_ipg / fullcnt;
446 partialcnt = sblock->fs_ipg % fullcnt;
447 partialsize = partialcnt * (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
448 if (partialcnt != 0) {
449 readpercg++;
450 } else {
451 partialcnt = fullcnt;
452 partialsize = inobufsize;
453 }
454 if (inodebuf == NULL &&
455 (inodebuf = malloc((unsigned)inobufsize)) == NULL)
456 errexit("Cannot allocate space for inode buffer");
457 }
458
459 void
460 freeinodebuf(void)
461 {
462
463 if (inodebuf != NULL)
464 free((char *)inodebuf);
465 inodebuf = NULL;
466 }
467
468 /*
469 * Routines to maintain information about directory inodes.
470 * This is built during the first pass and used during the
471 * second and third passes.
472 *
473 * Enter inodes into the cache.
474 */
475 void
476 cacheino(union dinode *dp, ino_t inumber)
477 {
478 struct inoinfo *inp;
479 struct inoinfo **inpp, **ninpsort;
480 unsigned int i, blks, extra;
481 int64_t size;
482
483 size = iswap64(DIP(dp, size));
484 blks = howmany(size, sblock->fs_bsize);
485 if (blks > NDADDR)
486 blks = NDADDR + NIADDR;
487 if (blks > 0)
488 extra = (blks - 1) * sizeof (int64_t);
489 else
490 extra = 0;
491 inp = malloc(sizeof(*inp) + extra);
492 if (inp == NULL)
493 return;
494 inpp = &inphead[inumber % dirhash];
495 inp->i_nexthash = *inpp;
496 *inpp = inp;
497 inp->i_child = inp->i_sibling = 0;
498 if (inumber == ROOTINO)
499 inp->i_parent = ROOTINO;
500 else
501 inp->i_parent = (ino_t)0;
502 inp->i_dotdot = (ino_t)0;
503 inp->i_number = inumber;
504 inp->i_isize = size;
505 inp->i_numblks = blks;
506 for (i = 0; i < (blks < NDADDR ? blks : NDADDR); i++)
507 inp->i_blks[i] = DIP(dp, db[i]);
508 if (blks > NDADDR)
509 for (i = 0; i < NIADDR; i++)
510 inp->i_blks[NDADDR + i] = DIP(dp, ib[i]);
511 if (inplast == listmax) {
512 ninpsort = (struct inoinfo **)realloc((char *)inpsort,
513 (unsigned)(listmax + 100) * sizeof(struct inoinfo *));
514 if (inpsort == NULL)
515 errexit("cannot increase directory list");
516 inpsort = ninpsort;
517 listmax += 100;
518 }
519 inpsort[inplast++] = inp;
520 }
521
522 /*
523 * Look up an inode cache structure.
524 */
525 struct inoinfo *
526 getinoinfo(ino_t inumber)
527 {
528 struct inoinfo *inp;
529
530 for (inp = inphead[inumber % dirhash]; inp; inp = inp->i_nexthash) {
531 if (inp->i_number != inumber)
532 continue;
533 return (inp);
534 }
535 errexit("cannot find inode %llu", (unsigned long long)inumber);
536 return ((struct inoinfo *)0);
537 }
538
539 /*
540 * Clean up all the inode cache structure.
541 */
542 void
543 inocleanup(void)
544 {
545 struct inoinfo **inpp;
546
547 if (inphead == NULL)
548 return;
549 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
550 free((char *)(*inpp));
551 free((char *)inphead);
552 free((char *)inpsort);
553 inphead = inpsort = NULL;
554 }
555
556 void
557 inodirty(void)
558 {
559
560 dirty(pbp);
561 }
562
563 void
564 clri(struct inodesc *idesc, const char *type, int flag)
565 {
566 union dinode *dp;
567
568 dp = ginode(idesc->id_number);
569 if (flag == 1) {
570 pwarn("%s %s", type,
571 (iswap16(DIP(dp, mode)) & IFMT) == IFDIR ? "DIR" : "FILE");
572 pinode(idesc->id_number);
573 }
574 if (preen || reply("CLEAR") == 1) {
575 if (preen)
576 printf(" (CLEARED)\n");
577 n_files--;
578 /*
579 * ckinode will call id_func (actually always pass4check)
580 * which will update the block count
581 */
582 update_uquot(idesc->id_number, idesc->id_uid, idesc->id_gid,
583 0, -1);
584 (void)ckinode(dp, idesc);
585 clearinode(dp);
586 inoinfo(idesc->id_number)->ino_state = USTATE;
587 inodirty();
588 } else
589 markclean = 0;
590 }
591
592 int
593 findname(struct inodesc *idesc)
594 {
595 struct direct *dirp = idesc->id_dirp;
596 size_t len;
597 char *buf;
598
599 if (iswap32(dirp->d_ino) != idesc->id_parent || idesc->id_entryno < 2) {
600 idesc->id_entryno++;
601 return (KEEPON);
602 }
603 if ((len = dirp->d_namlen + 1) > MAXPATHLEN) {
604 /* XXX: We don't fix but we ignore */
605 len = MAXPATHLEN;
606 }
607 /* this is namebuf from utilities.c */
608 buf = __UNCONST(idesc->id_name);
609 (void)memcpy(buf, dirp->d_name, (size_t)dirp->d_namlen + 1);
610 return (STOP|FOUND);
611 }
612
613 int
614 findino(struct inodesc *idesc)
615 {
616 struct direct *dirp = idesc->id_dirp;
617
618 if (dirp->d_ino == 0)
619 return (KEEPON);
620 if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
621 iswap32(dirp->d_ino) >= ROOTINO && iswap32(dirp->d_ino) <= maxino) {
622 idesc->id_parent = iswap32(dirp->d_ino);
623 return (STOP|FOUND);
624 }
625 return (KEEPON);
626 }
627
628 int
629 clearentry(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_t ino)
643 {
644 union dinode *dp;
645 struct passwd *pw;
646
647 printf(" I=%llu ", (unsigned long long)ino);
648 if (ino < ROOTINO || ino > maxino)
649 return;
650 dp = ginode(ino);
651 printf(" OWNER=");
652 #ifndef SMALL
653 if (Uflag && (pw = getpwuid((int)iswap32(DIP(dp, uid)))) != 0)
654 printf("%s ", pw->pw_name);
655 else
656 #endif
657 printf("%u ", (unsigned)iswap32(DIP(dp, uid)));
658 printf("MODE=%o\n", iswap16(DIP(dp, mode)));
659 if (preen)
660 printf("%s: ", cdevname());
661 printf("SIZE=%llu ", (unsigned long long)iswap64(DIP(dp, size)));
662 printf("MTIME=%s ", print_mtime(iswap32(DIP(dp, mtime))));
663 }
664
665 void
666 blkerror(ino_t ino, const char *type, daddr_t blk)
667 {
668 struct inostat *info;
669
670 pfatal("%lld %s I=%llu", (long long)blk, type, (unsigned long long)ino);
671 printf("\n");
672 info = inoinfo(ino);
673 switch (info->ino_state) {
674
675 case FSTATE:
676 info->ino_state = FCLEAR;
677 return;
678
679 case DSTATE:
680 info->ino_state = DCLEAR;
681 return;
682
683 case FCLEAR:
684 case DCLEAR:
685 return;
686
687 default:
688 errexit("BAD STATE %d TO BLKERR", info->ino_state);
689 /* NOTREACHED */
690 }
691 }
692
693 /*
694 * allocate an unused inode
695 */
696 ino_t
697 allocino(ino_t request, int type)
698 {
699 ino_t ino;
700 union dinode *dp;
701 struct ufs1_dinode *dp1;
702 struct ufs2_dinode *dp2;
703 time_t t;
704 struct cg *cgp = cgrp;
705 int cg;
706 struct inostat *info = NULL;
707 int nfrags;
708
709 if (request == 0)
710 request = ROOTINO;
711 else if (inoinfo(request)->ino_state != USTATE)
712 return (0);
713 for (ino = request; ino < maxino; ino++) {
714 info = inoinfo(ino);
715 if (info->ino_state == USTATE)
716 break;
717 }
718 if (ino == maxino)
719 return (0);
720 cg = ino_to_cg(sblock, ino);
721 /* If necessary, extend the inoinfo array. grow exponentially */
722 if ((ino % sblock->fs_ipg) >= (uint64_t)inostathead[cg].il_numalloced) {
723 unsigned long newalloced, i;
724 newalloced = MIN(sblock->fs_ipg,
725 MAX(2 * inostathead[cg].il_numalloced, 10));
726 info = calloc(newalloced, sizeof(struct inostat));
727 if (info == NULL) {
728 pwarn("cannot alloc %lu bytes to extend inoinfo\n",
729 sizeof(struct inostat) * newalloced);
730 return 0;
731 }
732 memmove(info, inostathead[cg].il_stat,
733 inostathead[cg].il_numalloced * sizeof(*info));
734 for (i = inostathead[cg].il_numalloced; i < newalloced; i++) {
735 info[i].ino_state = USTATE;
736 }
737 if (inostathead[cg].il_numalloced)
738 free(inostathead[cg].il_stat);
739 inostathead[cg].il_stat = info;
740 inostathead[cg].il_numalloced = newalloced;
741 info = inoinfo(ino);
742 }
743 getblk(&cgblk, cgtod(sblock, cg), sblock->fs_cgsize);
744 memcpy(cgp, cgblk.b_un.b_cg, sblock->fs_cgsize);
745 if ((doswap && !needswap) || (!doswap && needswap))
746 ffs_cg_swap(cgblk.b_un.b_cg, cgp, sblock);
747 if (!cg_chkmagic(cgp, 0))
748 pfatal("CG %d: ALLOCINO: BAD MAGIC NUMBER\n", cg);
749 if (doswap)
750 cgdirty();
751 setbit(cg_inosused(cgp, 0), ino % sblock->fs_ipg);
752 cgp->cg_cs.cs_nifree--;
753 sblock->fs_cstotal.cs_nifree--;
754 sblock->fs_cs(fs, cg).cs_nifree--;
755 sbdirty();
756 switch (type & IFMT) {
757 case IFDIR:
758 info->ino_state = DSTATE;
759 cgp->cg_cs.cs_ndir++;
760 nfrags = 1;
761 break;
762 case IFREG:
763 info->ino_state = FSTATE;
764 nfrags = sblock->fs_frag;
765 break;
766 case IFLNK:
767 info->ino_state = FSTATE;
768 nfrags = 1;
769 break;
770 default:
771 return (0);
772 }
773 cgdirty();
774 dp = ginode(ino);
775 if (is_ufs2) {
776 dp2 = &dp->dp2;
777 dp2->di_db[0] = iswap64(allocblk(nfrags));
778 if (dp2->di_db[0] == 0) {
779 info->ino_state = USTATE;
780 return (0);
781 }
782 dp2->di_mode = iswap16(type);
783 dp2->di_flags = 0;
784 (void)time(&t);
785 dp2->di_atime = iswap64(t);
786 dp2->di_mtime = dp2->di_ctime = dp2->di_atime;
787 dp2->di_size = iswap64(lfragtosize(sblock, nfrags));
788 dp2->di_blocks = iswap64(btodb(lfragtosize(sblock, nfrags)));
789 } else {
790 dp1 = &dp->dp1;
791 dp1->di_db[0] = iswap32(allocblk(nfrags));
792 if (dp1->di_db[0] == 0) {
793 info->ino_state = USTATE;
794 return (0);
795 }
796 dp1->di_mode = iswap16(type);
797 dp1->di_flags = 0;
798 (void)time(&t);
799 dp1->di_atime = iswap32(t);
800 dp1->di_mtime = dp1->di_ctime = dp1->di_atime;
801 dp1->di_size = iswap64(lfragtosize(sblock, nfrags));
802 dp1->di_blocks = iswap32(btodb(lfragtosize(sblock, nfrags)));
803 }
804 n_files++;
805 inodirty();
806 if (newinofmt)
807 info->ino_type = IFTODT(type);
808 return (ino);
809 }
810
811 /*
812 * deallocate an inode
813 */
814 void
815 freeino(ino_t ino)
816 {
817 struct inodesc idesc;
818 union dinode *dp;
819 struct cg *cgp = cgrp;
820 int cg;
821
822 cg = ino_to_cg(sblock, ino);
823 getblk(&cgblk, cgtod(sblock, cg), sblock->fs_cgsize);
824 memcpy(cgp, cgblk.b_un.b_cg, sblock->fs_cgsize);
825 if ((doswap && !needswap) || (!doswap && needswap))
826 ffs_cg_swap(cgblk.b_un.b_cg, cgp, sblock);
827 if (!cg_chkmagic(cgp, 0)) {
828 pwarn("CG %d: FREEINO: BAD MAGIC NUMBER\n", cg);
829 cgp = NULL;
830 }
831
832 memset(&idesc, 0, sizeof(struct inodesc));
833 idesc.id_type = ADDR;
834 idesc.id_func = pass4check;
835 idesc.id_number = ino;
836 dp = ginode(ino);
837 idesc.id_uid = iswap32(DIP(dp, uid));
838 idesc.id_gid = iswap32(DIP(dp, gid));
839 (void)ckinode(dp, &idesc);
840 clearinode(dp);
841 inodirty();
842 inoinfo(ino)->ino_state = USTATE;
843 update_uquot(idesc.id_number, idesc.id_uid, idesc.id_gid, 0, -1);
844 n_files--;
845 if (cgp) {
846 clrbit(cg_inosused(cgp, 0), ino % sblock->fs_ipg);
847 cgp->cg_cs.cs_nifree++;
848 sblock->fs_cstotal.cs_nifree++;
849 sblock->fs_cs(fs, cg).cs_nifree++;
850 sbdirty();
851 cgdirty();
852 }
853 }
854
855 /* read a data block from inode */
856 ssize_t
857 readblk(union dinode *dp, off_t offset, struct bufarea **bp)
858 {
859 daddr_t blkno = lblkno(sblock, offset);
860 daddr_t iblkno;
861 int type = IFMT & iswap16(DIP(dp, mode));
862 ssize_t filesize = iswap64(DIP(dp, size));
863 int ilevel;
864 daddr_t nblks;
865 const daddr_t naddrperblk = sblock->fs_bsize /
866 (is_ufs2 ? sizeof(uint64_t) : sizeof(uint32_t));
867 struct bufarea *ibp;
868
869 *bp = NULL;
870 offset &= ~(sblock->fs_bsize - 1);
871
872 if (type != IFREG)
873 return 0;
874 if (offset >= filesize)
875 return 0; /* short read */
876 if (blkno < NDADDR) {
877 blkno = is_ufs2 ? iswap64(dp->dp2.di_db[blkno]) :
878 iswap32(dp->dp1.di_db[blkno]);
879 if (blkno == 0)
880 return 0;
881 *bp = getdatablk(blkno, sblock->fs_bsize);
882 return (bp != NULL) ? sblock->fs_bsize : 0;
883 }
884 blkno -= NDADDR;
885 /* find indir level */
886 for (ilevel = 1, nblks = naddrperblk;
887 ilevel <= NIADDR;
888 ilevel++, nblks *= naddrperblk) {
889 if (blkno < nblks)
890 break;
891 else
892 blkno -= nblks;
893 }
894 if (ilevel > NIADDR)
895 errexit("bad ofsset %" PRIu64 " to readblk", offset);
896
897 /* get the first indirect block */
898 iblkno = is_ufs2 ? iswap64(dp->dp2.di_ib[ilevel - 1]) :
899 iswap32(dp->dp1.di_ib[ilevel - 1]);
900 if (iblkno == 0)
901 return 0;
902 ibp = getdatablk(iblkno, sblock->fs_bsize);
903 /* walk indirect blocks up to the data block */
904 for (; ilevel >0 ; ilevel--) {
905 nblks = nblks / naddrperblk;
906 if (is_ufs2)
907 iblkno = iswap64(ibp->b_un.b_indir2[blkno / nblks]);
908 else
909 iblkno = iswap32(ibp->b_un.b_indir1[blkno / nblks]);
910 if (iblkno == 0)
911 return 0;
912 blkno = blkno % nblks;
913 ibp->b_flags &= ~B_INUSE;
914 ibp = getdatablk(iblkno, sblock->fs_bsize);
915 }
916 *bp = ibp;
917 return sblock->fs_bsize;
918 }
919
920 static struct bufarea * getnewblk(daddr_t *);
921 static struct bufarea *
922 getnewblk(daddr_t *blkno)
923 {
924 struct bufarea *bp;
925 *blkno = allocblk(sblock->fs_frag);
926 if (*blkno == 0)
927 return NULL;
928 bp = getdatablk(*blkno, sblock->fs_bsize);
929 memset(bp->b_un.b_buf, 0, sblock->fs_bsize);
930 return bp;
931 }
932
933 /* expand given inode by one full fs block */
934 struct bufarea *
935 expandfile(union dinode *dp)
936 {
937 uint64_t filesize = iswap64(DIP(dp, size));
938 daddr_t newblk, blkno, iblkno, nblks;
939 daddr_t di_blocks;
940 int ilevel;
941 const daddr_t naddrperblk = sblock->fs_bsize /
942 (is_ufs2 ? sizeof(uint64_t) : sizeof(uint32_t));
943 struct bufarea *ibp, *bp = NULL;
944
945 di_blocks = is_ufs2 ? iswap64(dp->dp2.di_blocks) :
946 iswap32(dp->dp1.di_blocks);
947 /* compute location of new block */
948 blkno = lblkno(sblock, filesize);
949
950 if (blkno < NDADDR) {
951 /* easy way: allocate a direct block */
952 if ((bp = getnewblk(&newblk)) == NULL) {
953 return NULL;
954 }
955 di_blocks += btodb(sblock->fs_bsize);
956
957 if (is_ufs2) {
958 dp->dp2.di_db[blkno] = iswap64(newblk);
959 } else {
960 dp->dp1.di_db[blkno] = iswap32(newblk);
961 }
962 goto out;
963 }
964 blkno -= NDADDR;
965 /* find indir level */
966 for (ilevel = 1, nblks = naddrperblk;
967 ilevel <= NIADDR;
968 ilevel++, nblks *= naddrperblk) {
969 if (blkno < nblks)
970 break;
971 else
972 blkno -= nblks;
973 }
974 if (ilevel > NIADDR)
975 errexit("bad filesize %" PRIu64 " to expandfile", filesize);
976
977 /* get the first indirect block, allocating if needed */
978 if ((is_ufs2 ? iswap64(dp->dp2.di_ib[ilevel - 1]) :
979 iswap32(dp->dp1.di_ib[ilevel - 1])) == 0) {
980 if ((ibp = getnewblk(&newblk)) == NULL)
981 return 0;
982 di_blocks += btodb(sblock->fs_bsize);
983 if (is_ufs2)
984 dp->dp2.di_ib[ilevel - 1] = iswap64(newblk);
985 else
986 dp->dp1.di_ib[ilevel - 1] = iswap32(newblk);
987 } else {
988 ibp = getdatablk(is_ufs2 ? iswap64(dp->dp2.di_ib[ilevel - 1]) :
989 iswap32(dp->dp2.di_ib[ilevel - 1]), sblock->fs_bsize);
990 }
991 /* walk indirect blocks up to the data block */
992 for (; ilevel >0 ; ilevel--) {
993 nblks = nblks / naddrperblk;
994 if (is_ufs2)
995 iblkno = iswap64(ibp->b_un.b_indir2[blkno / nblks]);
996 else
997 iblkno = iswap32(ibp->b_un.b_indir1[blkno / nblks]);
998 if (iblkno == 0) {
999 if ((bp = getnewblk(&newblk)) == NULL)
1000 return NULL;
1001 di_blocks += btodb(sblock->fs_bsize);
1002 if (is_ufs2)
1003 ibp->b_un.b_indir2[blkno / nblks] =
1004 iswap64(newblk);
1005 else
1006 ibp->b_un.b_indir1[blkno / nblks] =
1007 iswap32(newblk);
1008 dirty(ibp);
1009 ibp->b_flags &= ~B_INUSE;
1010 ibp = bp;
1011 } else {
1012 ibp->b_flags &= ~B_INUSE;
1013 ibp = getdatablk(iblkno, sblock->fs_bsize);
1014 bp = NULL;
1015 }
1016 blkno = blkno % nblks;
1017 }
1018 if (bp == NULL) {
1019 errexit("INTERNAL ERROR: "
1020 "expandfile() failed to allocate a new block\n");
1021 }
1022
1023 out:
1024 filesize += sblock->fs_bsize;
1025 if (is_ufs2) {
1026 dp->dp2.di_size = iswap64(filesize);
1027 dp->dp2.di_blocks = iswap64(di_blocks);
1028 } else {
1029 dp->dp1.di_size = iswap64(filesize);
1030 dp->dp1.di_blocks = iswap32(di_blocks);
1031 }
1032 inodirty();
1033 return bp;
1034 }
1035