1 /* $NetBSD: inode.c,v 1.31 2010/02/04 23:55:42 christos Exp $ */
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
33 * Copyright (c) 1997 Manuel Bouyer.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
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.
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
45 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
48 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
49 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
53 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 #include <sys/cdefs.h>
59 static char sccsid
[] = "@(#)inode.c 8.5 (Berkeley) 2/8/95";
61 __RCSID("$NetBSD: inode.c,v 1.31 2010/02/04 23:55:42 christos Exp $");
65 #include <sys/param.h>
67 #include <ufs/ext2fs/ext2fs_dinode.h>
68 #include <ufs/ext2fs/ext2fs_dir.h>
69 #include <ufs/ext2fs/ext2fs.h>
71 #include <ufs/ufs/dinode.h> /* for IFMT & friends */
85 * CG is stored in fs byte order in memory, so we can't use ino_to_fsba
89 #define fsck_ino_to_fsba(fs, x) \
90 (fs2h32((fs)->e2fs_gd[ino_to_cg(fs, x)].ext2bgd_i_tables) + \
91 (((x)-1) % (fs)->e2fs.e2fs_ipg)/(fs)->e2fs_ipb)
94 static ino_t startinum
;
96 static int iblock(struct inodesc
*, long, u_int64_t
);
98 static int setlarge(void);
103 if (sblock
.e2fs
.e2fs_rev
< E2FS_REV1
) {
104 pfatal("LARGE FILES UNSUPPORTED ON REVISION 0 FILESYSTEMS");
107 if (!(sblock
.e2fs
.e2fs_features_rocompat
& EXT2F_ROCOMPAT_LARGEFILE
)) {
109 pwarn("SETTING LARGE FILE INDICATOR\n");
110 else if (!reply("SET LARGE FILE INDICATOR"))
112 sblock
.e2fs
.e2fs_features_rocompat
|= EXT2F_ROCOMPAT_LARGEFILE
;
119 inosize(struct ext2fs_dinode
*dp
)
121 u_int64_t size
= fs2h32(dp
->e2di_size
);
123 if ((fs2h16(dp
->e2di_mode
) & IFMT
) == IFREG
)
124 size
|= (u_int64_t
)fs2h32(dp
->e2di_dacl
) << 32;
125 if (size
> INT32_MAX
)
131 inossize(struct ext2fs_dinode
*dp
, u_int64_t size
)
133 if ((fs2h16(dp
->e2di_mode
) & IFMT
) == IFREG
) {
134 dp
->e2di_dacl
= h2fs32(size
>> 32);
135 if (size
> INT32_MAX
)
138 } else if (size
> INT32_MAX
) {
139 pfatal("TRYING TO SET FILESIZE TO %llu ON MODE %x FILE\n",
140 (unsigned long long)size
, fs2h16(dp
->e2di_mode
) & IFMT
);
143 dp
->e2di_size
= h2fs32(size
);
147 ckinode(struct ext2fs_dinode
*dp
, struct inodesc
*idesc
)
151 struct ext2fs_dinode dino
;
152 u_int64_t remsize
, sizepb
;
154 char pathbuf
[MAXPATHLEN
+ 1];
156 if (idesc
->id_fix
!= IGNORE
)
157 idesc
->id_fix
= DONTKNOW
;
158 idesc
->id_entryno
= 0;
159 idesc
->id_filesize
= inosize(dp
);
160 mode
= fs2h16(dp
->e2di_mode
) & IFMT
;
161 if (mode
== IFBLK
|| mode
== IFCHR
|| mode
== IFIFO
||
162 (mode
== IFLNK
&& (inosize(dp
) < EXT2_MAXSYMLINKLEN
)))
165 ndb
= howmany(inosize(&dino
), sblock
.e2fs_bsize
);
166 for (ap
= &dino
.e2di_blocks
[0]; ap
< &dino
.e2di_blocks
[NDADDR
];
168 idesc
->id_numfrags
= 1;
170 if (idesc
->id_type
== DATA
&& ndb
> 0) {
171 /* An empty block in a directory XXX */
172 getpathname(pathbuf
, sizeof(pathbuf
),
173 idesc
->id_number
, idesc
->id_number
);
174 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
176 if (reply("ADJUST LENGTH") == 1) {
177 dp
= ginode(idesc
->id_number
);
179 (ap
- &dino
.e2di_blocks
[0]) *
182 "YOU MUST RERUN FSCK AFTERWARDS\n");
189 idesc
->id_blkno
= fs2h32(*ap
);
190 if (idesc
->id_type
== ADDR
)
191 ret
= (*idesc
->id_func
)(idesc
);
193 ret
= dirscan(idesc
);
197 idesc
->id_numfrags
= 1;
198 remsize
= inosize(&dino
) - sblock
.e2fs_bsize
* NDADDR
;
199 sizepb
= sblock
.e2fs_bsize
;
200 for (ap
= &dino
.e2di_blocks
[NDADDR
], n
= 1; n
<= NIADDR
; ap
++, n
++) {
202 idesc
->id_blkno
= fs2h32(*ap
);
203 ret
= iblock(idesc
, n
, remsize
);
207 if (idesc
->id_type
== DATA
&& remsize
> 0) {
208 /* An empty block in a directory XXX */
209 getpathname(pathbuf
, sizeof(pathbuf
),
210 idesc
->id_number
, idesc
->id_number
);
211 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
213 if (reply("ADJUST LENGTH") == 1) {
214 dp
= ginode(idesc
->id_number
);
215 inossize(dp
, inosize(dp
) - remsize
);
218 "YOU MUST RERUN FSCK AFTERWARDS\n");
225 sizepb
*= NINDIR(&sblock
);
232 iblock(struct inodesc
*idesc
, long ilevel
, u_int64_t isize
)
238 int i
, n
, (*func
)(struct inodesc
*);
242 char pathbuf
[MAXPATHLEN
+ 1];
243 struct ext2fs_dinode
*dp
;
245 if (idesc
->id_type
== ADDR
) {
246 func
= idesc
->id_func
;
247 if (((n
= (*func
)(idesc
)) & KEEPON
) == 0)
251 if (chkrange(idesc
->id_blkno
, idesc
->id_numfrags
))
253 bp
= getdatablk(idesc
->id_blkno
, sblock
.e2fs_bsize
);
255 for (sizepb
= sblock
.e2fs_bsize
, i
= 0; i
< ilevel
; i
++)
256 sizepb
*= NINDIR(&sblock
);
257 if (isize
> sizepb
* NINDIR(&sblock
))
258 nif
= NINDIR(&sblock
);
260 nif
= howmany(isize
, sizepb
);
261 if (idesc
->id_func
== pass1check
&&
262 nif
< NINDIR(&sblock
)) {
263 aplim
= &bp
->b_un
.b_indir
[NINDIR(&sblock
)];
264 for (ap
= &bp
->b_un
.b_indir
[nif
]; ap
< aplim
; ap
++) {
267 (void)snprintf(buf
, sizeof(buf
),
268 "PARTIALLY TRUNCATED INODE I=%llu",
269 (unsigned long long)idesc
->id_number
);
270 if (dofix(idesc
, buf
)) {
275 flush(fswritefd
, bp
);
277 aplim
= &bp
->b_un
.b_indir
[nif
];
278 for (ap
= bp
->b_un
.b_indir
; ap
< aplim
; ap
++) {
280 idesc
->id_blkno
= fs2h32(*ap
);
284 n
= iblock(idesc
, ilevel
, isize
);
286 bp
->b_flags
&= ~B_INUSE
;
290 if (idesc
->id_type
== DATA
&& isize
> 0) {
291 /* An empty block in a directory XXX */
292 getpathname(pathbuf
, sizeof(pathbuf
),
293 idesc
->id_number
, idesc
->id_number
);
294 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
296 if (reply("ADJUST LENGTH") == 1) {
297 dp
= ginode(idesc
->id_number
);
298 inossize(dp
, inosize(dp
) - isize
);
301 "YOU MUST RERUN FSCK AFTERWARDS\n");
304 bp
->b_flags
&= ~B_INUSE
;
311 bp
->b_flags
&= ~B_INUSE
;
316 * Check that a block in a legal block number.
317 * Return 0 if in range, 1 if out of range.
320 chkrange(daddr_t blk
, int cnt
)
324 if ((unsigned int)(blk
+ cnt
) > maxfsblock
)
326 c
= dtog(&sblock
, blk
);
327 overh
= cgoverhead(c
);
328 if (blk
< sblock
.e2fs
.e2fs_bpg
* c
+ overh
+
329 sblock
.e2fs
.e2fs_first_dblock
) {
330 if ((blk
+ cnt
) > sblock
.e2fs
.e2fs_bpg
* c
+ overh
+
331 sblock
.e2fs
.e2fs_first_dblock
) {
333 printf("blk %lld < cgdmin %d;",
335 sblock
.e2fs
.e2fs_bpg
* c
+ overh
+
336 sblock
.e2fs
.e2fs_first_dblock
);
337 printf(" blk + cnt %lld > cgsbase %d\n",
338 (long long)(blk
+ cnt
),
339 sblock
.e2fs
.e2fs_bpg
* c
+
340 overh
+ sblock
.e2fs
.e2fs_first_dblock
);
345 if ((blk
+ cnt
) > sblock
.e2fs
.e2fs_bpg
* (c
+ 1) + overh
+
346 sblock
.e2fs
.e2fs_first_dblock
) {
348 printf("blk %lld >= cgdmin %d;",
350 sblock
.e2fs
.e2fs_bpg
* c
+ overh
+
351 sblock
.e2fs
.e2fs_first_dblock
);
352 printf(" blk + cnt %lld > cgdmax %d\n",
353 (long long)(blk
+cnt
),
354 sblock
.e2fs
.e2fs_bpg
* (c
+ 1) +
355 overh
+ sblock
.e2fs
.e2fs_first_dblock
);
364 * General purpose interface for reading inodes.
366 struct ext2fs_dinode
*
367 ginode(ino_t inumber
)
370 struct ext2fs_dinode
*dp
;
372 if ((inumber
< EXT2_FIRSTINO
&&
373 inumber
!= EXT2_ROOTINO
&&
374 !(inumber
== EXT2_RESIZEINO
&&
375 (sblock
.e2fs
.e2fs_features_compat
& EXT2F_COMPAT_RESIZE
) != 0))
377 errexit("bad inode number %llu to ginode",
378 (unsigned long long)inumber
);
379 if (startinum
== 0 ||
380 inumber
< startinum
|| inumber
>= startinum
+ sblock
.e2fs_ipb
) {
381 iblk
= fsck_ino_to_fsba(&sblock
, inumber
);
383 pbp
->b_flags
&= ~B_INUSE
;
384 pbp
= getdatablk(iblk
, sblock
.e2fs_bsize
);
386 ((inumber
- 1) / sblock
.e2fs_ipb
) * sblock
.e2fs_ipb
+ 1;
388 dp
= (struct ext2fs_dinode
*)(pbp
->b_un
.b_buf
+
389 EXT2_DINODE_SIZE(&sblock
) * ino_to_fsbo(&sblock
, inumber
));
395 * Special purpose version of ginode used to optimize first pass
396 * over all the inodes in numerical order.
398 ino_t nextino
, lastinum
;
399 long readcnt
, readpercg
, fullcnt
, inobufsize
, partialcnt
, partialsize
;
402 struct ext2fs_dinode
*
403 getnextinode(ino_t inumber
)
407 struct ext2fs_dinode
*dp
;
410 if (inumber
!= nextino
++ || inumber
> maxino
)
411 errexit("bad inode number %llu to nextinode",
412 (unsigned long long)inumber
);
413 if (inumber
>= lastinum
) {
415 dblk
= fsbtodb(&sblock
, fsck_ino_to_fsba(&sblock
, lastinum
));
416 if (readcnt
% readpercg
== 0) {
418 lastinum
+= partialcnt
;
423 (void)bread(fsreadfd
, inodebuf
, dblk
, size
);
426 dp
= (struct ext2fs_dinode
*)bp
;
427 bp
+= EXT2_DINODE_SIZE(&sblock
);
440 inobufsize
= blkroundup(&sblock
, INOBUFSIZE
);
441 fullcnt
= inobufsize
/ EXT2_DINODE_SIZE(&sblock
);
442 readpercg
= sblock
.e2fs
.e2fs_ipg
/ fullcnt
;
443 partialcnt
= sblock
.e2fs
.e2fs_ipg
% fullcnt
;
444 partialsize
= partialcnt
* EXT2_DINODE_SIZE(&sblock
);
445 if (partialcnt
!= 0) {
448 partialcnt
= fullcnt
;
449 partialsize
= inobufsize
;
451 if (inodebuf
== NULL
&&
452 (inodebuf
= malloc((unsigned int)inobufsize
)) == NULL
)
453 errexit("Cannot allocate space for inode buffer");
454 while (nextino
< EXT2_ROOTINO
)
455 (void)getnextinode(nextino
);
462 if (inodebuf
!= NULL
)
468 * Routines to maintain information about directory inodes.
469 * This is built during the first pass and used during the
470 * second and third passes.
472 * Enter inodes into the cache.
475 cacheino(struct ext2fs_dinode
*dp
, ino_t inumber
)
478 struct inoinfo
**inpp
;
481 blks
= howmany(inosize(dp
), sblock
.e2fs_bsize
);
483 blks
= NDADDR
+ NIADDR
;
485 inp
= malloc(sizeof(*inp
) + (blks
- 1) * sizeof(int32_t));
488 inpp
= &inphead
[inumber
% numdirs
];
489 inp
->i_nexthash
= *inpp
;
491 inp
->i_child
= inp
->i_sibling
= inp
->i_parentp
= 0;
492 if (inumber
== EXT2_ROOTINO
)
493 inp
->i_parent
= EXT2_ROOTINO
;
495 inp
->i_parent
= (ino_t
)0;
496 inp
->i_dotdot
= (ino_t
)0;
497 inp
->i_number
= inumber
;
498 inp
->i_isize
= inosize(dp
);
500 inp
->i_numblks
= blks
* sizeof(int32_t);
501 memcpy(&inp
->i_blks
[0], &dp
->e2di_blocks
[0], (size_t)inp
->i_numblks
);
502 if (inplast
== listmax
) {
504 inpsort
= (struct inoinfo
**)realloc((char *)inpsort
,
505 (unsigned int)listmax
* sizeof(struct inoinfo
*));
507 errexit("cannot increase directory list");
509 inpsort
[inplast
++] = inp
;
513 * Look up an inode cache structure.
516 getinoinfo(ino_t inumber
)
520 for (inp
= inphead
[inumber
% numdirs
]; inp
; inp
= inp
->i_nexthash
) {
521 if (inp
->i_number
!= inumber
)
525 errexit("cannot find inode %llu", (unsigned long long)inumber
);
526 return ((struct inoinfo
*)0);
530 * Clean up all the inode cache structure.
535 struct inoinfo
**inpp
;
539 for (inpp
= &inpsort
[inplast
- 1]; inpp
>= inpsort
; inpp
--)
543 inphead
= inpsort
= NULL
;
554 clri(struct inodesc
*idesc
, const char *type
, int flag
)
556 struct ext2fs_dinode
*dp
;
558 dp
= ginode(idesc
->id_number
);
561 (fs2h16(dp
->e2di_mode
) & IFMT
) == IFDIR
? "DIR" : "FILE");
562 pinode(idesc
->id_number
);
564 if (preen
|| reply("CLEAR") == 1) {
566 printf(" (CLEARED)\n");
568 (void)ckinode(dp
, idesc
);
570 statemap
[idesc
->id_number
] = USTATE
;
576 findname(struct inodesc
*idesc
)
578 struct ext2fs_direct
*dirp
= idesc
->id_dirp
;
579 u_int16_t namlen
= dirp
->e2d_namlen
;
580 /* from utilities.c namebuf[] variable */
581 char *buf
= __UNCONST(idesc
->id_name
);
582 if (namlen
> MAXPATHLEN
) {
583 /* XXX: Prevent overflow but don't fix */
587 if (fs2h32(dirp
->e2d_ino
) != idesc
->id_parent
)
589 (void)memcpy(buf
, dirp
->e2d_name
, (size_t)namlen
);
595 findino(struct inodesc
*idesc
)
597 struct ext2fs_direct
*dirp
= idesc
->id_dirp
;
598 u_int32_t ino
= fs2h32(dirp
->e2d_ino
);
602 if (strcmp(dirp
->e2d_name
, idesc
->id_name
) == 0 &&
603 (ino
== EXT2_ROOTINO
|| ino
>= EXT2_FIRSTINO
)
605 idesc
->id_parent
= ino
;
614 struct ext2fs_dinode
*dp
;
618 printf(" I=%llu ", (unsigned long long)ino
);
619 if ((ino
< EXT2_FIRSTINO
&& ino
!= EXT2_ROOTINO
) || ino
> maxino
)
622 uid
= fs2h16(dp
->e2di_uid
);
623 if (sblock
.e2fs
.e2fs_rev
> E2FS_REV0
)
624 uid
|= fs2h16(dp
->e2di_uid_high
) << 16;
627 if (Uflag
&& (pw
= getpwuid(uid
)) != 0)
628 printf("%s ", pw
->pw_name
);
631 printf("%u ", (unsigned int)uid
);
632 printf("MODE=%o\n", fs2h16(dp
->e2di_mode
));
634 printf("%s: ", cdevname());
635 printf("SIZE=%llu ", (long long)inosize(dp
));
636 printf("MTIME=%s ", print_mtime(fs2h32(dp
->e2di_mtime
)));
640 blkerror(ino_t ino
, const char *type
, daddr_t blk
)
643 pfatal("%lld %s I=%llu", (long long)blk
, type
, (unsigned long long)ino
);
645 switch (statemap
[ino
]) {
648 statemap
[ino
] = FCLEAR
;
652 statemap
[ino
] = DCLEAR
;
660 errexit("BAD STATE %d TO BLKERR", statemap
[ino
]);
666 * allocate an unused inode
669 allocino(ino_t request
, int type
)
672 struct ext2fs_dinode
*dp
;
676 request
= EXT2_ROOTINO
;
677 else if (statemap
[request
] != USTATE
)
679 for (ino
= request
; ino
< maxino
; ino
++) {
680 if ((ino
> EXT2_ROOTINO
) && (ino
< EXT2_FIRSTINO
))
682 if (statemap
[ino
] == USTATE
)
687 switch (type
& IFMT
) {
689 statemap
[ino
] = DSTATE
;
693 statemap
[ino
] = FSTATE
;
699 dp
->e2di_blocks
[0] = h2fs32(allocblk());
700 if (dp
->e2di_blocks
[0] == 0) {
701 statemap
[ino
] = USTATE
;
704 dp
->e2di_mode
= h2fs16(type
);
706 dp
->e2di_atime
= h2fs32(t
);
707 dp
->e2di_mtime
= dp
->e2di_ctime
= dp
->e2di_atime
;
709 inossize(dp
, sblock
.e2fs_bsize
);
710 dp
->e2di_nblock
= h2fs32(btodb(sblock
.e2fs_bsize
));
713 typemap
[ino
] = E2IFTODT(type
);
718 * deallocate an inode
723 struct inodesc idesc
;
724 struct ext2fs_dinode
*dp
;
726 memset(&idesc
, 0, sizeof(struct inodesc
));
727 idesc
.id_type
= ADDR
;
728 idesc
.id_func
= pass4check
;
729 idesc
.id_number
= ino
;
731 (void)ckinode(dp
, &idesc
);
734 statemap
[ino
] = USTATE
;