inode.c revision 1.29 1 /* $NetBSD: inode.c,v 1.29 2009/04/06 12:50:36 lukem 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 /*
33 * Copyright (c) 1997 Manuel Bouyer.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by Manuel Bouyer.
46 * 4. The name of the author may not be used to endorse or promote products
47 * derived from this software without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 */
60
61 #include <sys/cdefs.h>
62 #ifndef lint
63 #if 0
64 static char sccsid[] = "@(#)inode.c 8.5 (Berkeley) 2/8/95";
65 #else
66 __RCSID("$NetBSD: inode.c,v 1.29 2009/04/06 12:50:36 lukem Exp $");
67 #endif
68 #endif /* not lint */
69
70 #include <sys/param.h>
71 #include <sys/time.h>
72 #include <ufs/ext2fs/ext2fs_dinode.h>
73 #include <ufs/ext2fs/ext2fs_dir.h>
74 #include <ufs/ext2fs/ext2fs.h>
75
76 #include <ufs/ufs/dinode.h> /* for IFMT & friends */
77 #ifndef SMALL
78 #include <pwd.h>
79 #endif
80 #include <stdio.h>
81 #include <stdlib.h>
82 #include <string.h>
83 #include <time.h>
84
85 #include "fsck.h"
86 #include "fsutil.h"
87 #include "extern.h"
88
89 /*
90 * CG is stored in fs byte order in memory, so we can't use ino_to_fsba
91 * here.
92 */
93
94 #define fsck_ino_to_fsba(fs, x) \
95 (fs2h32((fs)->e2fs_gd[ino_to_cg(fs, x)].ext2bgd_i_tables) + \
96 (((x)-1) % (fs)->e2fs.e2fs_ipg)/(fs)->e2fs_ipb)
97
98
99 static ino_t startinum;
100
101 static int iblock(struct inodesc *, long, u_int64_t);
102
103 static int setlarge(void);
104
105 static int
106 setlarge(void)
107 {
108 if (sblock.e2fs.e2fs_rev < E2FS_REV1) {
109 pfatal("LARGE FILES UNSUPPORTED ON REVISION 0 FILESYSTEMS");
110 return 0;
111 }
112 if (!(sblock.e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_LARGEFILE)) {
113 if (preen)
114 pwarn("SETTING LARGE FILE INDICATOR\n");
115 else if (!reply("SET LARGE FILE INDICATOR"))
116 return 0;
117 sblock.e2fs.e2fs_features_rocompat |= EXT2F_ROCOMPAT_LARGEFILE;
118 sbdirty();
119 }
120 return 1;
121 }
122
123 u_int64_t
124 inosize(struct ext2fs_dinode *dp)
125 {
126 u_int64_t size = fs2h32(dp->e2di_size);
127
128 if ((fs2h16(dp->e2di_mode) & IFMT) == IFREG)
129 size |= (u_int64_t)fs2h32(dp->e2di_dacl) << 32;
130 if (size > INT32_MAX)
131 (void)setlarge();
132 return size;
133 }
134
135 void
136 inossize(struct ext2fs_dinode *dp, u_int64_t size)
137 {
138 if ((fs2h16(dp->e2di_mode) & IFMT) == IFREG) {
139 dp->e2di_dacl = h2fs32(size >> 32);
140 if (size > INT32_MAX)
141 if (!setlarge())
142 return;
143 } else if (size > INT32_MAX) {
144 pfatal("TRYING TO SET FILESIZE TO %llu ON MODE %x FILE\n",
145 (unsigned long long)size, fs2h16(dp->e2di_mode) & IFMT);
146 return;
147 }
148 dp->e2di_size = h2fs32(size);
149 }
150
151 int
152 ckinode(struct ext2fs_dinode *dp, struct inodesc *idesc)
153 {
154 u_int32_t *ap;
155 long ret, n, ndb;
156 struct ext2fs_dinode dino;
157 u_int64_t remsize, sizepb;
158 mode_t mode;
159 char pathbuf[MAXPATHLEN + 1];
160
161 if (idesc->id_fix != IGNORE)
162 idesc->id_fix = DONTKNOW;
163 idesc->id_entryno = 0;
164 idesc->id_filesize = inosize(dp);
165 mode = fs2h16(dp->e2di_mode) & IFMT;
166 if (mode == IFBLK || mode == IFCHR || mode == IFIFO ||
167 (mode == IFLNK && (inosize(dp) < EXT2_MAXSYMLINKLEN)))
168 return (KEEPON);
169 dino = *dp;
170 ndb = howmany(inosize(&dino), sblock.e2fs_bsize);
171 for (ap = &dino.e2di_blocks[0]; ap < &dino.e2di_blocks[NDADDR];
172 ap++,ndb--) {
173 idesc->id_numfrags = 1;
174 if (*ap == 0) {
175 if (idesc->id_type == DATA && ndb > 0) {
176 /* An empty block in a directory XXX */
177 getpathname(pathbuf, sizeof(pathbuf),
178 idesc->id_number, idesc->id_number);
179 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
180 pathbuf);
181 if (reply("ADJUST LENGTH") == 1) {
182 dp = ginode(idesc->id_number);
183 inossize(dp,
184 (ap - &dino.e2di_blocks[0]) *
185 sblock.e2fs_bsize);
186 printf(
187 "YOU MUST RERUN FSCK AFTERWARDS\n");
188 rerun = 1;
189 inodirty();
190 }
191 }
192 continue;
193 }
194 idesc->id_blkno = fs2h32(*ap);
195 if (idesc->id_type == ADDR)
196 ret = (*idesc->id_func)(idesc);
197 else
198 ret = dirscan(idesc);
199 if (ret & STOP)
200 return (ret);
201 }
202 idesc->id_numfrags = 1;
203 remsize = inosize(&dino) - sblock.e2fs_bsize * NDADDR;
204 sizepb = sblock.e2fs_bsize;
205 for (ap = &dino.e2di_blocks[NDADDR], n = 1; n <= NIADDR; ap++, n++) {
206 if (*ap) {
207 idesc->id_blkno = fs2h32(*ap);
208 ret = iblock(idesc, n, remsize);
209 if (ret & STOP)
210 return (ret);
211 } else {
212 if (idesc->id_type == DATA && remsize > 0) {
213 /* An empty block in a directory XXX */
214 getpathname(pathbuf, sizeof(pathbuf),
215 idesc->id_number, idesc->id_number);
216 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
217 pathbuf);
218 if (reply("ADJUST LENGTH") == 1) {
219 dp = ginode(idesc->id_number);
220 inossize(dp, inosize(dp) - remsize);
221 remsize = 0;
222 printf(
223 "YOU MUST RERUN FSCK AFTERWARDS\n");
224 rerun = 1;
225 inodirty();
226 break;
227 }
228 }
229 }
230 sizepb *= NINDIR(&sblock);
231 remsize -= sizepb;
232 }
233 return (KEEPON);
234 }
235
236 static int
237 iblock(struct inodesc *idesc, long ilevel, u_int64_t isize)
238 {
239 /* XXX ondisk32 */
240 int32_t *ap;
241 int32_t *aplim;
242 struct bufarea *bp;
243 int i, n, (*func)(struct inodesc *);
244 size_t nif;
245 u_int64_t sizepb;
246 char buf[BUFSIZ];
247 char pathbuf[MAXPATHLEN + 1];
248 struct ext2fs_dinode *dp;
249
250 if (idesc->id_type == ADDR) {
251 func = idesc->id_func;
252 if (((n = (*func)(idesc)) & KEEPON) == 0)
253 return (n);
254 } else
255 func = dirscan;
256 if (chkrange(idesc->id_blkno, idesc->id_numfrags))
257 return (SKIP);
258 bp = getdatablk(idesc->id_blkno, sblock.e2fs_bsize);
259 ilevel--;
260 for (sizepb = sblock.e2fs_bsize, i = 0; i < ilevel; i++)
261 sizepb *= NINDIR(&sblock);
262 if (isize > sizepb * NINDIR(&sblock))
263 nif = NINDIR(&sblock);
264 else
265 nif = howmany(isize, sizepb);
266 if (idesc->id_func == pass1check &&
267 nif < NINDIR(&sblock)) {
268 aplim = &bp->b_un.b_indir[NINDIR(&sblock)];
269 for (ap = &bp->b_un.b_indir[nif]; ap < aplim; ap++) {
270 if (*ap == 0)
271 continue;
272 (void)snprintf(buf, sizeof(buf),
273 "PARTIALLY TRUNCATED INODE I=%llu",
274 (unsigned long long)idesc->id_number);
275 if (dofix(idesc, buf)) {
276 *ap = 0;
277 dirty(bp);
278 }
279 }
280 flush(fswritefd, bp);
281 }
282 aplim = &bp->b_un.b_indir[nif];
283 for (ap = bp->b_un.b_indir; ap < aplim; ap++) {
284 if (*ap) {
285 idesc->id_blkno = fs2h32(*ap);
286 if (ilevel == 0)
287 n = (*func)(idesc);
288 else
289 n = iblock(idesc, ilevel, isize);
290 if (n & STOP) {
291 bp->b_flags &= ~B_INUSE;
292 return (n);
293 }
294 } else {
295 if (idesc->id_type == DATA && isize > 0) {
296 /* An empty block in a directory XXX */
297 getpathname(pathbuf, sizeof(pathbuf),
298 idesc->id_number, idesc->id_number);
299 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
300 pathbuf);
301 if (reply("ADJUST LENGTH") == 1) {
302 dp = ginode(idesc->id_number);
303 inossize(dp, inosize(dp) - isize);
304 isize = 0;
305 printf(
306 "YOU MUST RERUN FSCK AFTERWARDS\n");
307 rerun = 1;
308 inodirty();
309 bp->b_flags &= ~B_INUSE;
310 return(STOP);
311 }
312 }
313 }
314 isize -= sizepb;
315 }
316 bp->b_flags &= ~B_INUSE;
317 return (KEEPON);
318 }
319
320 /*
321 * Check that a block in a legal block number.
322 * Return 0 if in range, 1 if out of range.
323 */
324 int
325 chkrange(daddr_t blk, int cnt)
326 {
327 int c, overh;
328
329 if ((unsigned int)(blk + cnt) > maxfsblock)
330 return (1);
331 c = dtog(&sblock, blk);
332 overh = cgoverhead(c);
333 if (blk < sblock.e2fs.e2fs_bpg * c + overh +
334 sblock.e2fs.e2fs_first_dblock) {
335 if ((blk + cnt) > sblock.e2fs.e2fs_bpg * c + overh +
336 sblock.e2fs.e2fs_first_dblock) {
337 if (debug) {
338 printf("blk %lld < cgdmin %d;",
339 (long long)blk,
340 sblock.e2fs.e2fs_bpg * c + overh +
341 sblock.e2fs.e2fs_first_dblock);
342 printf(" blk + cnt %lld > cgsbase %d\n",
343 (long long)(blk + cnt),
344 sblock.e2fs.e2fs_bpg * c +
345 overh + sblock.e2fs.e2fs_first_dblock);
346 }
347 return (1);
348 }
349 } else {
350 if ((blk + cnt) > sblock.e2fs.e2fs_bpg * (c + 1) + overh +
351 sblock.e2fs.e2fs_first_dblock) {
352 if (debug) {
353 printf("blk %lld >= cgdmin %d;",
354 (long long)blk,
355 sblock.e2fs.e2fs_bpg * c + overh +
356 sblock.e2fs.e2fs_first_dblock);
357 printf(" blk + cnt %lld > cgdmax %d\n",
358 (long long)(blk+cnt),
359 sblock.e2fs.e2fs_bpg * (c + 1) +
360 overh + sblock.e2fs.e2fs_first_dblock);
361 }
362 return (1);
363 }
364 }
365 return (0);
366 }
367
368 /*
369 * General purpose interface for reading inodes.
370 */
371 struct ext2fs_dinode *
372 ginode(ino_t inumber)
373 {
374 daddr_t iblk;
375 struct ext2fs_dinode *dp;
376
377 if ((inumber < EXT2_FIRSTINO &&
378 inumber != EXT2_ROOTINO &&
379 !(inumber == EXT2_RESIZEINO &&
380 (sblock.e2fs.e2fs_features_compat & EXT2F_COMPAT_RESIZE) != 0))
381 || inumber > maxino)
382 errexit("bad inode number %llu to ginode",
383 (unsigned long long)inumber);
384 if (startinum == 0 ||
385 inumber < startinum || inumber >= startinum + sblock.e2fs_ipb) {
386 iblk = fsck_ino_to_fsba(&sblock, inumber);
387 if (pbp != 0)
388 pbp->b_flags &= ~B_INUSE;
389 pbp = getdatablk(iblk, sblock.e2fs_bsize);
390 startinum =
391 ((inumber - 1) / sblock.e2fs_ipb) * sblock.e2fs_ipb + 1;
392 }
393 dp = (struct ext2fs_dinode *)(pbp->b_un.b_buf +
394 EXT2_DINODE_SIZE(&sblock) * ino_to_fsbo(&sblock, inumber));
395
396 return dp;
397 }
398
399 /*
400 * Special purpose version of ginode used to optimize first pass
401 * over all the inodes in numerical order.
402 */
403 ino_t nextino, lastinum;
404 long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
405 char *inodebuf;
406
407 struct ext2fs_dinode *
408 getnextinode(ino_t inumber)
409 {
410 long size;
411 daddr_t dblk;
412 struct ext2fs_dinode *dp;
413 static char *bp;
414
415 if (inumber != nextino++ || inumber > maxino)
416 errexit("bad inode number %llu to nextinode",
417 (unsigned long long)inumber);
418 if (inumber >= lastinum) {
419 readcnt++;
420 dblk = fsbtodb(&sblock, fsck_ino_to_fsba(&sblock, lastinum));
421 if (readcnt % readpercg == 0) {
422 size = partialsize;
423 lastinum += partialcnt;
424 } else {
425 size = inobufsize;
426 lastinum += fullcnt;
427 }
428 (void)bread(fsreadfd, inodebuf, dblk, size);
429 bp = inodebuf;
430 }
431 dp = (struct ext2fs_dinode *)bp;
432 bp += EXT2_DINODE_SIZE(&sblock);
433
434 return dp;
435 }
436
437 void
438 resetinodebuf(void)
439 {
440
441 startinum = 0;
442 nextino = 1;
443 lastinum = 1;
444 readcnt = 0;
445 inobufsize = blkroundup(&sblock, INOBUFSIZE);
446 fullcnt = inobufsize / EXT2_DINODE_SIZE(&sblock);
447 readpercg = sblock.e2fs.e2fs_ipg / fullcnt;
448 partialcnt = sblock.e2fs.e2fs_ipg % fullcnt;
449 partialsize = partialcnt * EXT2_DINODE_SIZE(&sblock);
450 if (partialcnt != 0) {
451 readpercg++;
452 } else {
453 partialcnt = fullcnt;
454 partialsize = inobufsize;
455 }
456 if (inodebuf == NULL &&
457 (inodebuf = malloc((unsigned int)inobufsize)) == NULL)
458 errexit("Cannot allocate space for inode buffer");
459 while (nextino < EXT2_ROOTINO)
460 (void)getnextinode(nextino);
461 }
462
463 void
464 freeinodebuf(void)
465 {
466
467 if (inodebuf != NULL)
468 free(inodebuf);
469 inodebuf = NULL;
470 }
471
472 /*
473 * Routines to maintain information about directory inodes.
474 * This is built during the first pass and used during the
475 * second and third passes.
476 *
477 * Enter inodes into the cache.
478 */
479 void
480 cacheino(struct ext2fs_dinode *dp, ino_t inumber)
481 {
482 struct inoinfo *inp;
483 struct inoinfo **inpp;
484 unsigned int blks;
485
486 blks = howmany(inosize(dp), sblock.e2fs_bsize);
487 if (blks > NDADDR)
488 blks = NDADDR + NIADDR;
489 /* XXX ondisk32 */
490 inp = malloc(sizeof(*inp) + (blks - 1) * sizeof(int32_t));
491 if (inp == NULL)
492 return;
493 inpp = &inphead[inumber % numdirs];
494 inp->i_nexthash = *inpp;
495 *inpp = inp;
496 inp->i_child = inp->i_sibling = inp->i_parentp = 0;
497 if (inumber == EXT2_ROOTINO)
498 inp->i_parent = EXT2_ROOTINO;
499 else
500 inp->i_parent = (ino_t)0;
501 inp->i_dotdot = (ino_t)0;
502 inp->i_number = inumber;
503 inp->i_isize = inosize(dp);
504 /* XXX ondisk32 */
505 inp->i_numblks = blks * sizeof(int32_t);
506 memcpy(&inp->i_blks[0], &dp->e2di_blocks[0], (size_t)inp->i_numblks);
507 if (inplast == listmax) {
508 listmax += 100;
509 inpsort = (struct inoinfo **)realloc((char *)inpsort,
510 (unsigned int)listmax * sizeof(struct inoinfo *));
511 if (inpsort == NULL)
512 errexit("cannot increase directory list");
513 }
514 inpsort[inplast++] = inp;
515 }
516
517 /*
518 * Look up an inode cache structure.
519 */
520 struct inoinfo *
521 getinoinfo(ino_t inumber)
522 {
523 struct inoinfo *inp;
524
525 for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
526 if (inp->i_number != inumber)
527 continue;
528 return (inp);
529 }
530 errexit("cannot find inode %llu", (unsigned long long)inumber);
531 return ((struct inoinfo *)0);
532 }
533
534 /*
535 * Clean up all the inode cache structure.
536 */
537 void
538 inocleanup(void)
539 {
540 struct inoinfo **inpp;
541
542 if (inphead == NULL)
543 return;
544 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
545 free(*inpp);
546 free(inphead);
547 free(inpsort);
548 inphead = inpsort = NULL;
549 }
550
551 void
552 inodirty(void)
553 {
554
555 dirty(pbp);
556 }
557
558 void
559 clri(struct inodesc *idesc, const char *type, int flag)
560 {
561 struct ext2fs_dinode *dp;
562
563 dp = ginode(idesc->id_number);
564 if (flag == 1) {
565 pwarn("%s %s", type,
566 (fs2h16(dp->e2di_mode) & IFMT) == IFDIR ? "DIR" : "FILE");
567 pinode(idesc->id_number);
568 }
569 if (preen || reply("CLEAR") == 1) {
570 if (preen)
571 printf(" (CLEARED)\n");
572 n_files--;
573 (void)ckinode(dp, idesc);
574 clearinode(dp);
575 statemap[idesc->id_number] = USTATE;
576 inodirty();
577 }
578 }
579
580 int
581 findname(struct inodesc *idesc)
582 {
583 struct ext2fs_direct *dirp = idesc->id_dirp;
584 u_int16_t namlen = dirp->e2d_namlen;
585 /* from utilities.c namebuf[] variable */
586 char *buf = __UNCONST(idesc->id_name);
587 if (namlen > MAXPATHLEN) {
588 /* XXX: Prevent overflow but don't fix */
589 namlen = MAXPATHLEN;
590 }
591
592 if (fs2h32(dirp->e2d_ino) != idesc->id_parent)
593 return (KEEPON);
594 (void)memcpy(buf, dirp->e2d_name, (size_t)namlen);
595 buf[namlen] = '\0';
596 return (STOP|FOUND);
597 }
598
599 int
600 findino(struct inodesc *idesc)
601 {
602 struct ext2fs_direct *dirp = idesc->id_dirp;
603 u_int32_t ino = fs2h32(dirp->e2d_ino);
604
605 if (ino == 0)
606 return (KEEPON);
607 if (strcmp(dirp->e2d_name, idesc->id_name) == 0 &&
608 (ino == EXT2_ROOTINO || ino >= EXT2_FIRSTINO)
609 && ino <= maxino) {
610 idesc->id_parent = ino;
611 return (STOP|FOUND);
612 }
613 return (KEEPON);
614 }
615
616 void
617 pinode(ino_t ino)
618 {
619 struct ext2fs_dinode *dp;
620 char *p;
621 struct passwd *pw;
622 time_t t;
623 uid_t uid;
624
625 printf(" I=%llu ", (unsigned long long)ino);
626 if ((ino < EXT2_FIRSTINO && ino != EXT2_ROOTINO) || ino > maxino)
627 return;
628 dp = ginode(ino);
629 uid = fs2h16(dp->e2di_uid);
630 if (sblock.e2fs.e2fs_rev > E2FS_REV0)
631 uid |= fs2h16(dp->e2di_uid_high) << 16;
632 printf(" OWNER=");
633 #ifndef SMALL
634 if (Uflag && (pw = getpwuid(uid)) != 0)
635 printf("%s ", pw->pw_name);
636 else
637 #endif
638 printf("%u ", (unsigned int)uid);
639 printf("MODE=%o\n", fs2h16(dp->e2di_mode));
640 if (preen)
641 printf("%s: ", cdevname());
642 printf("SIZE=%llu ", (long long)inosize(dp));
643 t = fs2h32(dp->e2di_mtime);
644 p = ctime(&t);
645 printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
646 }
647
648 void
649 blkerror(ino_t ino, const char *type, daddr_t blk)
650 {
651
652 pfatal("%lld %s I=%llu", (long long)blk, type, (unsigned long long)ino);
653 printf("\n");
654 switch (statemap[ino]) {
655
656 case FSTATE:
657 statemap[ino] = FCLEAR;
658 return;
659
660 case DSTATE:
661 statemap[ino] = DCLEAR;
662 return;
663
664 case FCLEAR:
665 case DCLEAR:
666 return;
667
668 default:
669 errexit("BAD STATE %d TO BLKERR", statemap[ino]);
670 /* NOTREACHED */
671 }
672 }
673
674 /*
675 * allocate an unused inode
676 */
677 ino_t
678 allocino(ino_t request, int type)
679 {
680 ino_t ino;
681 struct ext2fs_dinode *dp;
682 time_t t;
683
684 if (request == 0)
685 request = EXT2_ROOTINO;
686 else if (statemap[request] != USTATE)
687 return (0);
688 for (ino = request; ino < maxino; ino++) {
689 if ((ino > EXT2_ROOTINO) && (ino < EXT2_FIRSTINO))
690 continue;
691 if (statemap[ino] == USTATE)
692 break;
693 }
694 if (ino == maxino)
695 return (0);
696 switch (type & IFMT) {
697 case IFDIR:
698 statemap[ino] = DSTATE;
699 break;
700 case IFREG:
701 case IFLNK:
702 statemap[ino] = FSTATE;
703 break;
704 default:
705 return (0);
706 }
707 dp = ginode(ino);
708 dp->e2di_blocks[0] = h2fs32(allocblk());
709 if (dp->e2di_blocks[0] == 0) {
710 statemap[ino] = USTATE;
711 return (0);
712 }
713 dp->e2di_mode = h2fs16(type);
714 (void)time(&t);
715 dp->e2di_atime = h2fs32(t);
716 dp->e2di_mtime = dp->e2di_ctime = dp->e2di_atime;
717 dp->e2di_dtime = 0;
718 inossize(dp, sblock.e2fs_bsize);
719 dp->e2di_nblock = h2fs32(btodb(sblock.e2fs_bsize));
720 n_files++;
721 inodirty();
722 typemap[ino] = E2IFTODT(type);
723 return (ino);
724 }
725
726 /*
727 * deallocate an inode
728 */
729 void
730 freeino(ino_t ino)
731 {
732 struct inodesc idesc;
733 struct ext2fs_dinode *dp;
734
735 memset(&idesc, 0, sizeof(struct inodesc));
736 idesc.id_type = ADDR;
737 idesc.id_func = pass4check;
738 idesc.id_number = ino;
739 dp = ginode(ino);
740 (void)ckinode(dp, &idesc);
741 clearinode(dp);
742 inodirty();
743 statemap[ino] = USTATE;
744 n_files--;
745 }
746