2 * inode.c --- utility routines to read and write inodes
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
26 #include <sys/types.h>
33 struct ext2_struct_inode_scan {
36 ext2_ino_t current_inode;
37 blk64_t current_block;
39 ext2_ino_t inodes_left;
42 blk_t inode_buffer_blocks;
48 errcode_t (*done_group)(ext2_filsys fs,
52 void * done_group_data;
59 * This routine flushes the icache, if it exists.
61 errcode_t ext2fs_flush_icache(ext2_filsys fs)
68 for (i=0; i < fs->icache->cache_size; i++)
69 fs->icache->cache[i].ino = 0;
71 fs->icache->buffer_blk = 0;
75 static errcode_t create_icache(ext2_filsys fs)
81 retval = ext2fs_get_mem(sizeof(struct ext2_inode_cache), &fs->icache);
85 memset(fs->icache, 0, sizeof(struct ext2_inode_cache));
86 retval = ext2fs_get_mem(fs->blocksize, &fs->icache->buffer);
88 ext2fs_free_mem(&fs->icache);
91 fs->icache->buffer_blk = 0;
92 fs->icache->cache_last = -1;
93 fs->icache->cache_size = 4;
94 fs->icache->refcount = 1;
95 retval = ext2fs_get_array(fs->icache->cache_size,
96 sizeof(struct ext2_inode_cache_ent),
99 ext2fs_free_mem(&fs->icache->buffer);
100 ext2fs_free_mem(&fs->icache);
103 ext2fs_flush_icache(fs);
107 errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks,
108 ext2_inode_scan *ret_scan)
110 ext2_inode_scan scan;
112 errcode_t (*save_get_blocks)(ext2_filsys f, ext2_ino_t ino, blk_t *blocks);
114 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
117 * If fs->badblocks isn't set, then set it --- since the inode
118 * scanning functions require it.
120 if (fs->badblocks == 0) {
122 * Temporarly save fs->get_blocks and set it to zero,
123 * for compatibility with old e2fsck's.
125 save_get_blocks = fs->get_blocks;
127 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
128 if (retval && fs->badblocks) {
129 ext2fs_badblocks_list_free(fs->badblocks);
132 fs->get_blocks = save_get_blocks;
135 retval = ext2fs_get_mem(sizeof(struct ext2_struct_inode_scan), &scan);
138 memset(scan, 0, sizeof(struct ext2_struct_inode_scan));
140 scan->magic = EXT2_ET_MAGIC_INODE_SCAN;
142 scan->inode_size = EXT2_INODE_SIZE(fs->super);
143 scan->bytes_left = 0;
144 scan->current_group = 0;
145 scan->groups_left = fs->group_desc_count - 1;
146 scan->inode_buffer_blocks = buffer_blocks ? buffer_blocks : 8;
147 scan->current_block = ext2fs_inode_table_loc(scan->fs,
148 scan->current_group);
149 scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super);
150 scan->blocks_left = scan->fs->inode_blocks_per_group;
151 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
152 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
153 __u32 unused = ext2fs_bg_itable_unused(fs, scan->current_group);
154 if (scan->inodes_left > unused)
155 scan->inodes_left -= unused;
157 scan->inodes_left = 0;
160 (fs->blocksize / scan->inode_size - 1)) *
161 scan->inode_size / fs->blocksize;
163 retval = io_channel_alloc_buf(fs->io, scan->inode_buffer_blocks,
164 &scan->inode_buffer);
165 scan->done_group = 0;
166 scan->done_group_data = 0;
167 scan->bad_block_ptr = 0;
169 ext2fs_free_mem(&scan);
172 retval = ext2fs_get_mem(scan->inode_size, &scan->temp_buffer);
174 ext2fs_free_mem(&scan->inode_buffer);
175 ext2fs_free_mem(&scan);
178 if (scan->fs->badblocks && scan->fs->badblocks->num)
179 scan->scan_flags |= EXT2_SF_CHK_BADBLOCKS;
180 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
181 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
182 scan->scan_flags |= EXT2_SF_DO_LAZY;
187 void ext2fs_close_inode_scan(ext2_inode_scan scan)
189 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
192 ext2fs_free_mem(&scan->inode_buffer);
193 scan->inode_buffer = NULL;
194 ext2fs_free_mem(&scan->temp_buffer);
195 scan->temp_buffer = NULL;
196 ext2fs_free_mem(&scan);
200 void ext2fs_set_inode_callback(ext2_inode_scan scan,
201 errcode_t (*done_group)(ext2_filsys fs,
202 ext2_inode_scan scan,
205 void *done_group_data)
207 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
210 scan->done_group = done_group;
211 scan->done_group_data = done_group_data;
214 int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags,
219 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
222 old_flags = scan->scan_flags;
223 scan->scan_flags &= ~clear_flags;
224 scan->scan_flags |= set_flags;
229 * This function is called by ext2fs_get_next_inode when it needs to
230 * get ready to read in a new blockgroup.
232 static errcode_t get_next_blockgroup(ext2_inode_scan scan)
234 ext2_filsys fs = scan->fs;
236 scan->current_group++;
239 scan->current_block = ext2fs_inode_table_loc(scan->fs,
240 scan->current_group);
241 scan->current_inode = scan->current_group *
242 EXT2_INODES_PER_GROUP(fs->super);
244 scan->bytes_left = 0;
245 scan->inodes_left = EXT2_INODES_PER_GROUP(fs->super);
246 scan->blocks_left = fs->inode_blocks_per_group;
247 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
248 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
249 __u32 unused = ext2fs_bg_itable_unused(fs, scan->current_group);
250 if (scan->inodes_left > unused)
251 scan->inodes_left -= unused;
253 scan->inodes_left = 0;
256 (fs->blocksize / scan->inode_size - 1)) *
257 scan->inode_size / fs->blocksize;
263 errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,
266 scan->current_group = group - 1;
267 scan->groups_left = scan->fs->group_desc_count - group;
268 return get_next_blockgroup(scan);
272 * This function is called by get_next_blocks() to check for bad
273 * blocks in the inode table.
275 * This function assumes that badblocks_list->list is sorted in
278 static errcode_t check_for_inode_bad_blocks(ext2_inode_scan scan,
281 blk64_t blk = scan->current_block;
282 badblocks_list bb = scan->fs->badblocks;
285 * If the inode table is missing, then obviously there are no
292 * If the current block is greater than the bad block listed
293 * in the bad block list, then advance the pointer until this
294 * is no longer the case. If we run out of bad blocks, then
295 * we don't need to do any more checking!
297 while (blk > bb->list[scan->bad_block_ptr]) {
298 if (++scan->bad_block_ptr >= bb->num) {
299 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
305 * If the current block is equal to the bad block listed in
306 * the bad block list, then handle that one block specially.
307 * (We could try to handle runs of bad blocks, but that
308 * only increases CPU efficiency by a small amount, at the
309 * expense of a huge expense of code complexity, and for an
310 * uncommon case at that.)
312 if (blk == bb->list[scan->bad_block_ptr]) {
313 scan->scan_flags |= EXT2_SF_BAD_INODE_BLK;
315 if (++scan->bad_block_ptr >= bb->num)
316 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
321 * If there is a bad block in the range that we're about to
322 * read in, adjust the number of blocks to read so that we we
323 * don't read in the bad block. (Then the next block to read
324 * will be the bad block, which is handled in the above case.)
326 if ((blk + *num_blocks) > bb->list[scan->bad_block_ptr])
327 *num_blocks = (int) (bb->list[scan->bad_block_ptr] - blk);
333 * This function is called by ext2fs_get_next_inode when it needs to
334 * read in more blocks from the current blockgroup's inode table.
336 static errcode_t get_next_blocks(ext2_inode_scan scan)
342 * Figure out how many blocks to read; we read at most
343 * inode_buffer_blocks, and perhaps less if there aren't that
344 * many blocks left to read.
346 num_blocks = scan->inode_buffer_blocks;
347 if (num_blocks > scan->blocks_left)
348 num_blocks = scan->blocks_left;
351 * If the past block "read" was a bad block, then mark the
352 * left-over extra bytes as also being bad.
354 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) {
355 if (scan->bytes_left)
356 scan->scan_flags |= EXT2_SF_BAD_EXTRA_BYTES;
357 scan->scan_flags &= ~EXT2_SF_BAD_INODE_BLK;
361 * Do inode bad block processing, if necessary.
363 if (scan->scan_flags & EXT2_SF_CHK_BADBLOCKS) {
364 retval = check_for_inode_bad_blocks(scan, &num_blocks);
369 if ((scan->scan_flags & EXT2_SF_BAD_INODE_BLK) ||
370 (scan->current_block == 0)) {
371 memset(scan->inode_buffer, 0,
372 (size_t) num_blocks * scan->fs->blocksize);
374 retval = io_channel_read_blk64(scan->fs->io,
379 return EXT2_ET_NEXT_INODE_READ;
381 scan->ptr = scan->inode_buffer;
382 scan->bytes_left = num_blocks * scan->fs->blocksize;
384 scan->blocks_left -= num_blocks;
385 if (scan->current_block)
386 scan->current_block += num_blocks;
392 * Returns 1 if the entire inode_buffer has a non-zero size and
393 * contains all zeros. (Not just deleted inodes, since that means
394 * that part of the inode table was used at one point; we want all
395 * zeros, which means that the inode table is pristine.)
397 static inline int is_empty_scan(ext2_inode_scan scan)
401 if (scan->bytes_left == 0)
404 for (i=0; i < scan->bytes_left; i++)
411 errcode_t ext2fs_get_next_inode_full(ext2_inode_scan scan, ext2_ino_t *ino,
412 struct ext2_inode *inode, int bufsize)
417 EXT2_CHECK_MAGIC(scan, EXT2_ET_MAGIC_INODE_SCAN);
420 * Do we need to start reading a new block group?
422 if (scan->inodes_left <= 0) {
424 if (scan->done_group) {
425 retval = (scan->done_group)
426 (scan->fs, scan, scan->current_group,
427 scan->done_group_data);
431 if (scan->groups_left <= 0) {
435 retval = get_next_blockgroup(scan);
440 * These checks are done outside the above if statement so
441 * they can be done for block group #0.
443 if ((scan->scan_flags & EXT2_SF_DO_LAZY) &&
444 (ext2fs_bg_flags_test(scan->fs, scan->current_group, EXT2_BG_INODE_UNINIT)
446 goto force_new_group;
447 if (scan->inodes_left == 0)
448 goto force_new_group;
449 if (scan->current_block == 0) {
450 if (scan->scan_flags & EXT2_SF_SKIP_MISSING_ITABLE) {
451 goto force_new_group;
453 return EXT2_ET_MISSING_INODE_TABLE;
458 * Have we run out of space in the inode buffer? If so, we
459 * need to read in more blocks.
461 if (scan->bytes_left < scan->inode_size) {
462 memcpy(scan->temp_buffer, scan->ptr, scan->bytes_left);
463 extra_bytes = scan->bytes_left;
465 retval = get_next_blocks(scan);
470 * XXX test Need check for used inode somehow.
471 * (Note: this is hard.)
473 if (is_empty_scan(scan))
474 goto force_new_group;
480 memcpy(scan->temp_buffer+extra_bytes, scan->ptr,
481 scan->inode_size - extra_bytes);
482 scan->ptr += scan->inode_size - extra_bytes;
483 scan->bytes_left -= scan->inode_size - extra_bytes;
485 #ifdef WORDS_BIGENDIAN
486 memset(inode, 0, bufsize);
487 ext2fs_swap_inode_full(scan->fs,
488 (struct ext2_inode_large *) inode,
489 (struct ext2_inode_large *) scan->temp_buffer,
492 *inode = *((struct ext2_inode *) scan->temp_buffer);
494 if (scan->scan_flags & EXT2_SF_BAD_EXTRA_BYTES)
495 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
496 scan->scan_flags &= ~EXT2_SF_BAD_EXTRA_BYTES;
498 #ifdef WORDS_BIGENDIAN
499 memset(inode, 0, bufsize);
500 ext2fs_swap_inode_full(scan->fs,
501 (struct ext2_inode_large *) inode,
502 (struct ext2_inode_large *) scan->ptr,
505 memcpy(inode, scan->ptr, bufsize);
507 scan->ptr += scan->inode_size;
508 scan->bytes_left -= scan->inode_size;
509 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK)
510 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
514 scan->current_inode++;
515 *ino = scan->current_inode;
519 errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ext2_ino_t *ino,
520 struct ext2_inode *inode)
522 return ext2fs_get_next_inode_full(scan, ino, inode,
523 sizeof(struct ext2_inode));
527 * Functions to read and write a single inode.
529 errcode_t ext2fs_read_inode_full(ext2_filsys fs, ext2_ino_t ino,
530 struct ext2_inode * inode, int bufsize)
533 unsigned long group, block, offset;
536 int clen, i, inodes_per_block, length;
539 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
541 /* Check to see if user has an override function */
542 if (fs->read_inode &&
543 ((bufsize == sizeof(struct ext2_inode)) ||
544 (EXT2_INODE_SIZE(fs->super) == sizeof(struct ext2_inode)))) {
545 retval = (fs->read_inode)(fs, ino, inode);
546 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
549 if ((ino == 0) || (ino > fs->super->s_inodes_count))
550 return EXT2_ET_BAD_INODE_NUM;
551 /* Create inode cache if not present */
553 retval = create_icache(fs);
557 /* Check to see if it's in the inode cache */
558 if (bufsize == sizeof(struct ext2_inode)) {
559 /* only old good inode can be retrieved from the cache */
560 for (i=0; i < fs->icache->cache_size; i++) {
561 if (fs->icache->cache[i].ino == ino) {
562 *inode = fs->icache->cache[i].inode;
567 if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
568 inodes_per_block = fs->blocksize / EXT2_INODE_SIZE(fs->super);
569 block_nr = fs->image_header->offset_inode / fs->blocksize;
570 block_nr += (ino - 1) / inodes_per_block;
571 offset = ((ino - 1) % inodes_per_block) *
572 EXT2_INODE_SIZE(fs->super);
575 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
576 if (group > fs->group_desc_count)
577 return EXT2_ET_BAD_INODE_NUM;
578 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
579 EXT2_INODE_SIZE(fs->super);
580 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
581 if (!ext2fs_inode_table_loc(fs, (unsigned) group))
582 return EXT2_ET_MISSING_INODE_TABLE;
583 block_nr = ext2fs_inode_table_loc(fs, group) +
587 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
589 length = EXT2_INODE_SIZE(fs->super);
590 if (bufsize < length)
593 ptr = (char *) inode;
596 if ((offset + length) > fs->blocksize)
597 clen = fs->blocksize - offset;
599 if (block_nr != fs->icache->buffer_blk) {
600 retval = io_channel_read_blk64(io, block_nr, 1,
604 fs->icache->buffer_blk = block_nr;
607 memcpy(ptr, ((char *) fs->icache->buffer) + (unsigned) offset,
616 #ifdef WORDS_BIGENDIAN
617 ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) inode,
618 (struct ext2_inode_large *) inode,
622 /* Update the inode cache */
623 fs->icache->cache_last = (fs->icache->cache_last + 1) %
624 fs->icache->cache_size;
625 fs->icache->cache[fs->icache->cache_last].ino = ino;
626 fs->icache->cache[fs->icache->cache_last].inode = *inode;
631 errcode_t ext2fs_read_inode(ext2_filsys fs, ext2_ino_t ino,
632 struct ext2_inode * inode)
634 return ext2fs_read_inode_full(fs, ino, inode,
635 sizeof(struct ext2_inode));
638 errcode_t ext2fs_write_inode_full(ext2_filsys fs, ext2_ino_t ino,
639 struct ext2_inode * inode, int bufsize)
642 unsigned long group, block, offset;
643 errcode_t retval = 0;
644 struct ext2_inode_large temp_inode, *w_inode;
648 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
650 /* Check to see if user provided an override function */
651 if (fs->write_inode) {
652 retval = (fs->write_inode)(fs, ino, inode);
653 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
657 /* Check to see if the inode cache needs to be updated */
659 for (i=0; i < fs->icache->cache_size; i++) {
660 if (fs->icache->cache[i].ino == ino) {
661 fs->icache->cache[i].inode = *inode;
666 retval = create_icache(fs);
671 if (!(fs->flags & EXT2_FLAG_RW))
672 return EXT2_ET_RO_FILSYS;
674 if ((ino == 0) || (ino > fs->super->s_inodes_count))
675 return EXT2_ET_BAD_INODE_NUM;
678 if (length < EXT2_INODE_SIZE(fs->super))
679 length = EXT2_INODE_SIZE(fs->super);
681 if (length > (int) sizeof(struct ext2_inode_large)) {
682 w_inode = malloc(length);
688 w_inode = &temp_inode;
689 memset(w_inode, 0, length);
691 #ifdef WORDS_BIGENDIAN
692 ext2fs_swap_inode_full(fs, w_inode,
693 (struct ext2_inode_large *) inode,
696 memcpy(w_inode, inode, bufsize);
699 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
700 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
701 EXT2_INODE_SIZE(fs->super);
702 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
703 if (!ext2fs_inode_table_loc(fs, (unsigned) group)) {
704 retval = EXT2_ET_MISSING_INODE_TABLE;
707 block_nr = ext2fs_inode_table_loc(fs, (unsigned) group) + block;
709 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
711 length = EXT2_INODE_SIZE(fs->super);
712 if (length > bufsize)
715 ptr = (char *) w_inode;
719 if ((offset + length) > fs->blocksize)
720 clen = fs->blocksize - offset;
722 if (fs->icache->buffer_blk != block_nr) {
723 retval = io_channel_read_blk64(fs->io, block_nr, 1,
727 fs->icache->buffer_blk = block_nr;
731 memcpy((char *) fs->icache->buffer + (unsigned) offset,
734 retval = io_channel_write_blk64(fs->io, block_nr, 1,
745 fs->flags |= EXT2_FLAG_CHANGED;
747 if (w_inode && w_inode != &temp_inode)
752 errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino,
753 struct ext2_inode *inode)
755 return ext2fs_write_inode_full(fs, ino, inode,
756 sizeof(struct ext2_inode));
760 * This function should be called when writing a new inode. It makes
761 * sure that extra part of large inodes is initialized properly.
763 errcode_t ext2fs_write_new_inode(ext2_filsys fs, ext2_ino_t ino,
764 struct ext2_inode *inode)
766 struct ext2_inode *buf;
767 int size = EXT2_INODE_SIZE(fs->super);
768 struct ext2_inode_large *large_inode;
770 __u32 t = fs->now ? fs->now : time(NULL);
779 if (size == sizeof(struct ext2_inode))
780 return ext2fs_write_inode_full(fs, ino, inode,
781 sizeof(struct ext2_inode));
787 memset(buf, 0, size);
790 large_inode = (struct ext2_inode_large *) buf;
791 large_inode->i_extra_isize = sizeof(struct ext2_inode_large) -
792 EXT2_GOOD_OLD_INODE_SIZE;
793 if (!large_inode->i_crtime)
794 large_inode->i_crtime = t;
796 retval = ext2fs_write_inode_full(fs, ino, buf, size);
802 errcode_t ext2fs_get_blocks(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks)
804 struct ext2_inode inode;
808 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
810 if (ino > fs->super->s_inodes_count)
811 return EXT2_ET_BAD_INODE_NUM;
813 if (fs->get_blocks) {
814 if (!(*fs->get_blocks)(fs, ino, blocks))
817 retval = ext2fs_read_inode(fs, ino, &inode);
820 for (i=0; i < EXT2_N_BLOCKS; i++)
821 blocks[i] = inode.i_block[i];
825 errcode_t ext2fs_check_directory(ext2_filsys fs, ext2_ino_t ino)
827 struct ext2_inode inode;
830 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
832 if (ino > fs->super->s_inodes_count)
833 return EXT2_ET_BAD_INODE_NUM;
835 if (fs->check_directory) {
836 retval = (fs->check_directory)(fs, ino);
837 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
840 retval = ext2fs_read_inode(fs, ino, &inode);
843 if (!LINUX_S_ISDIR(inode.i_mode))
844 return EXT2_ET_NO_DIRECTORY;