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)) {
154 ext2fs_bg_itable_unused(fs, scan->current_group);
157 (fs->blocksize / scan->inode_size - 1)) *
158 scan->inode_size / fs->blocksize;
160 retval = io_channel_alloc_buf(fs->io, scan->inode_buffer_blocks,
161 &scan->inode_buffer);
162 scan->done_group = 0;
163 scan->done_group_data = 0;
164 scan->bad_block_ptr = 0;
166 ext2fs_free_mem(&scan);
169 retval = ext2fs_get_mem(scan->inode_size, &scan->temp_buffer);
171 ext2fs_free_mem(&scan->inode_buffer);
172 ext2fs_free_mem(&scan);
175 if (scan->fs->badblocks && scan->fs->badblocks->num)
176 scan->scan_flags |= EXT2_SF_CHK_BADBLOCKS;
177 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
178 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
179 scan->scan_flags |= EXT2_SF_DO_LAZY;
184 void ext2fs_close_inode_scan(ext2_inode_scan scan)
186 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
189 ext2fs_free_mem(&scan->inode_buffer);
190 scan->inode_buffer = NULL;
191 ext2fs_free_mem(&scan->temp_buffer);
192 scan->temp_buffer = NULL;
193 ext2fs_free_mem(&scan);
197 void ext2fs_set_inode_callback(ext2_inode_scan scan,
198 errcode_t (*done_group)(ext2_filsys fs,
199 ext2_inode_scan scan,
202 void *done_group_data)
204 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
207 scan->done_group = done_group;
208 scan->done_group_data = done_group_data;
211 int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags,
216 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
219 old_flags = scan->scan_flags;
220 scan->scan_flags &= ~clear_flags;
221 scan->scan_flags |= set_flags;
226 * This function is called by ext2fs_get_next_inode when it needs to
227 * get ready to read in a new blockgroup.
229 static errcode_t get_next_blockgroup(ext2_inode_scan scan)
231 ext2_filsys fs = scan->fs;
233 scan->current_group++;
236 scan->current_block = ext2fs_inode_table_loc(scan->fs,
237 scan->current_group);
238 scan->current_inode = scan->current_group *
239 EXT2_INODES_PER_GROUP(fs->super);
241 scan->bytes_left = 0;
242 scan->inodes_left = EXT2_INODES_PER_GROUP(fs->super);
243 scan->blocks_left = fs->inode_blocks_per_group;
244 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
245 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
247 ext2fs_bg_itable_unused(fs, scan->current_group);
250 (fs->blocksize / scan->inode_size - 1)) *
251 scan->inode_size / fs->blocksize;
257 errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,
260 scan->current_group = group - 1;
261 scan->groups_left = scan->fs->group_desc_count - group;
262 return get_next_blockgroup(scan);
266 * This function is called by get_next_blocks() to check for bad
267 * blocks in the inode table.
269 * This function assumes that badblocks_list->list is sorted in
272 static errcode_t check_for_inode_bad_blocks(ext2_inode_scan scan,
275 blk64_t blk = scan->current_block;
276 badblocks_list bb = scan->fs->badblocks;
279 * If the inode table is missing, then obviously there are no
286 * If the current block is greater than the bad block listed
287 * in the bad block list, then advance the pointer until this
288 * is no longer the case. If we run out of bad blocks, then
289 * we don't need to do any more checking!
291 while (blk > bb->list[scan->bad_block_ptr]) {
292 if (++scan->bad_block_ptr >= bb->num) {
293 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
299 * If the current block is equal to the bad block listed in
300 * the bad block list, then handle that one block specially.
301 * (We could try to handle runs of bad blocks, but that
302 * only increases CPU efficiency by a small amount, at the
303 * expense of a huge expense of code complexity, and for an
304 * uncommon case at that.)
306 if (blk == bb->list[scan->bad_block_ptr]) {
307 scan->scan_flags |= EXT2_SF_BAD_INODE_BLK;
309 if (++scan->bad_block_ptr >= bb->num)
310 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
315 * If there is a bad block in the range that we're about to
316 * read in, adjust the number of blocks to read so that we we
317 * don't read in the bad block. (Then the next block to read
318 * will be the bad block, which is handled in the above case.)
320 if ((blk + *num_blocks) > bb->list[scan->bad_block_ptr])
321 *num_blocks = (int) (bb->list[scan->bad_block_ptr] - blk);
327 * This function is called by ext2fs_get_next_inode when it needs to
328 * read in more blocks from the current blockgroup's inode table.
330 static errcode_t get_next_blocks(ext2_inode_scan scan)
336 * Figure out how many blocks to read; we read at most
337 * inode_buffer_blocks, and perhaps less if there aren't that
338 * many blocks left to read.
340 num_blocks = scan->inode_buffer_blocks;
341 if (num_blocks > scan->blocks_left)
342 num_blocks = scan->blocks_left;
345 * If the past block "read" was a bad block, then mark the
346 * left-over extra bytes as also being bad.
348 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) {
349 if (scan->bytes_left)
350 scan->scan_flags |= EXT2_SF_BAD_EXTRA_BYTES;
351 scan->scan_flags &= ~EXT2_SF_BAD_INODE_BLK;
355 * Do inode bad block processing, if necessary.
357 if (scan->scan_flags & EXT2_SF_CHK_BADBLOCKS) {
358 retval = check_for_inode_bad_blocks(scan, &num_blocks);
363 if ((scan->scan_flags & EXT2_SF_BAD_INODE_BLK) ||
364 (scan->current_block == 0)) {
365 memset(scan->inode_buffer, 0,
366 (size_t) num_blocks * scan->fs->blocksize);
368 retval = io_channel_read_blk64(scan->fs->io,
373 return EXT2_ET_NEXT_INODE_READ;
375 scan->ptr = scan->inode_buffer;
376 scan->bytes_left = num_blocks * scan->fs->blocksize;
378 scan->blocks_left -= num_blocks;
379 if (scan->current_block)
380 scan->current_block += num_blocks;
386 * Returns 1 if the entire inode_buffer has a non-zero size and
387 * contains all zeros. (Not just deleted inodes, since that means
388 * that part of the inode table was used at one point; we want all
389 * zeros, which means that the inode table is pristine.)
391 static inline int is_empty_scan(ext2_inode_scan scan)
395 if (scan->bytes_left == 0)
398 for (i=0; i < scan->bytes_left; i++)
405 errcode_t ext2fs_get_next_inode_full(ext2_inode_scan scan, ext2_ino_t *ino,
406 struct ext2_inode *inode, int bufsize)
411 EXT2_CHECK_MAGIC(scan, EXT2_ET_MAGIC_INODE_SCAN);
414 * Do we need to start reading a new block group?
416 if (scan->inodes_left <= 0) {
418 if (scan->done_group) {
419 retval = (scan->done_group)
420 (scan->fs, scan, scan->current_group,
421 scan->done_group_data);
425 if (scan->groups_left <= 0) {
429 retval = get_next_blockgroup(scan);
434 * These checks are done outside the above if statement so
435 * they can be done for block group #0.
437 if ((scan->scan_flags & EXT2_SF_DO_LAZY) &&
438 (ext2fs_bg_flags_test(scan->fs, scan->current_group, EXT2_BG_INODE_UNINIT)
440 goto force_new_group;
441 if (scan->inodes_left == 0)
442 goto force_new_group;
443 if (scan->current_block == 0) {
444 if (scan->scan_flags & EXT2_SF_SKIP_MISSING_ITABLE) {
445 goto force_new_group;
447 return EXT2_ET_MISSING_INODE_TABLE;
452 * Have we run out of space in the inode buffer? If so, we
453 * need to read in more blocks.
455 if (scan->bytes_left < scan->inode_size) {
456 memcpy(scan->temp_buffer, scan->ptr, scan->bytes_left);
457 extra_bytes = scan->bytes_left;
459 retval = get_next_blocks(scan);
464 * XXX test Need check for used inode somehow.
465 * (Note: this is hard.)
467 if (is_empty_scan(scan))
468 goto force_new_group;
474 memcpy(scan->temp_buffer+extra_bytes, scan->ptr,
475 scan->inode_size - extra_bytes);
476 scan->ptr += scan->inode_size - extra_bytes;
477 scan->bytes_left -= scan->inode_size - extra_bytes;
479 #ifdef WORDS_BIGENDIAN
480 memset(inode, 0, bufsize);
481 ext2fs_swap_inode_full(scan->fs,
482 (struct ext2_inode_large *) inode,
483 (struct ext2_inode_large *) scan->temp_buffer,
486 *inode = *((struct ext2_inode *) scan->temp_buffer);
488 if (scan->scan_flags & EXT2_SF_BAD_EXTRA_BYTES)
489 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
490 scan->scan_flags &= ~EXT2_SF_BAD_EXTRA_BYTES;
492 #ifdef WORDS_BIGENDIAN
493 memset(inode, 0, bufsize);
494 ext2fs_swap_inode_full(scan->fs,
495 (struct ext2_inode_large *) inode,
496 (struct ext2_inode_large *) scan->ptr,
499 memcpy(inode, scan->ptr, bufsize);
501 scan->ptr += scan->inode_size;
502 scan->bytes_left -= scan->inode_size;
503 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK)
504 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
508 scan->current_inode++;
509 *ino = scan->current_inode;
513 errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ext2_ino_t *ino,
514 struct ext2_inode *inode)
516 return ext2fs_get_next_inode_full(scan, ino, inode,
517 sizeof(struct ext2_inode));
521 * Functions to read and write a single inode.
523 errcode_t ext2fs_read_inode_full(ext2_filsys fs, ext2_ino_t ino,
524 struct ext2_inode * inode, int bufsize)
527 unsigned long group, block, offset;
530 int clen, i, inodes_per_block, length;
533 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
535 /* Check to see if user has an override function */
536 if (fs->read_inode &&
537 ((bufsize == sizeof(struct ext2_inode)) ||
538 (EXT2_INODE_SIZE(fs->super) == sizeof(struct ext2_inode)))) {
539 retval = (fs->read_inode)(fs, ino, inode);
540 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
543 if ((ino == 0) || (ino > fs->super->s_inodes_count))
544 return EXT2_ET_BAD_INODE_NUM;
545 /* Create inode cache if not present */
547 retval = create_icache(fs);
551 /* Check to see if it's in the inode cache */
552 if (bufsize == sizeof(struct ext2_inode)) {
553 /* only old good inode can be retrieved from the cache */
554 for (i=0; i < fs->icache->cache_size; i++) {
555 if (fs->icache->cache[i].ino == ino) {
556 *inode = fs->icache->cache[i].inode;
561 if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
562 inodes_per_block = fs->blocksize / EXT2_INODE_SIZE(fs->super);
563 block_nr = fs->image_header->offset_inode / fs->blocksize;
564 block_nr += (ino - 1) / inodes_per_block;
565 offset = ((ino - 1) % inodes_per_block) *
566 EXT2_INODE_SIZE(fs->super);
569 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
570 if (group > fs->group_desc_count)
571 return EXT2_ET_BAD_INODE_NUM;
572 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
573 EXT2_INODE_SIZE(fs->super);
574 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
575 if (!ext2fs_inode_table_loc(fs, (unsigned) group))
576 return EXT2_ET_MISSING_INODE_TABLE;
577 block_nr = ext2fs_inode_table_loc(fs, group) +
581 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
583 length = EXT2_INODE_SIZE(fs->super);
584 if (bufsize < length)
587 ptr = (char *) inode;
590 if ((offset + length) > fs->blocksize)
591 clen = fs->blocksize - offset;
593 if (block_nr != fs->icache->buffer_blk) {
594 retval = io_channel_read_blk64(io, block_nr, 1,
598 fs->icache->buffer_blk = block_nr;
601 memcpy(ptr, ((char *) fs->icache->buffer) + (unsigned) offset,
610 #ifdef WORDS_BIGENDIAN
611 ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) inode,
612 (struct ext2_inode_large *) inode,
616 /* Update the inode cache */
617 fs->icache->cache_last = (fs->icache->cache_last + 1) %
618 fs->icache->cache_size;
619 fs->icache->cache[fs->icache->cache_last].ino = ino;
620 fs->icache->cache[fs->icache->cache_last].inode = *inode;
625 errcode_t ext2fs_read_inode(ext2_filsys fs, ext2_ino_t ino,
626 struct ext2_inode * inode)
628 return ext2fs_read_inode_full(fs, ino, inode,
629 sizeof(struct ext2_inode));
632 errcode_t ext2fs_write_inode_full(ext2_filsys fs, ext2_ino_t ino,
633 struct ext2_inode * inode, int bufsize)
636 unsigned long group, block, offset;
637 errcode_t retval = 0;
638 struct ext2_inode_large temp_inode, *w_inode;
642 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
644 /* Check to see if user provided an override function */
645 if (fs->write_inode) {
646 retval = (fs->write_inode)(fs, ino, inode);
647 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
651 /* Check to see if the inode cache needs to be updated */
653 for (i=0; i < fs->icache->cache_size; i++) {
654 if (fs->icache->cache[i].ino == ino) {
655 fs->icache->cache[i].inode = *inode;
660 retval = create_icache(fs);
665 if (!(fs->flags & EXT2_FLAG_RW))
666 return EXT2_ET_RO_FILSYS;
668 if ((ino == 0) || (ino > fs->super->s_inodes_count))
669 return EXT2_ET_BAD_INODE_NUM;
672 if (length < EXT2_INODE_SIZE(fs->super))
673 length = EXT2_INODE_SIZE(fs->super);
675 if (length > (int) sizeof(struct ext2_inode_large)) {
676 w_inode = malloc(length);
682 w_inode = &temp_inode;
683 memset(w_inode, 0, length);
685 #ifdef WORDS_BIGENDIAN
686 ext2fs_swap_inode_full(fs, w_inode,
687 (struct ext2_inode_large *) inode,
690 memcpy(w_inode, inode, bufsize);
693 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
694 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
695 EXT2_INODE_SIZE(fs->super);
696 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
697 if (!ext2fs_inode_table_loc(fs, (unsigned) group)) {
698 retval = EXT2_ET_MISSING_INODE_TABLE;
701 block_nr = ext2fs_inode_table_loc(fs, (unsigned) group) + block;
703 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
705 length = EXT2_INODE_SIZE(fs->super);
706 if (length > bufsize)
709 ptr = (char *) w_inode;
713 if ((offset + length) > fs->blocksize)
714 clen = fs->blocksize - offset;
716 if (fs->icache->buffer_blk != block_nr) {
717 retval = io_channel_read_blk64(fs->io, block_nr, 1,
721 fs->icache->buffer_blk = block_nr;
725 memcpy((char *) fs->icache->buffer + (unsigned) offset,
728 retval = io_channel_write_blk64(fs->io, block_nr, 1,
739 fs->flags |= EXT2_FLAG_CHANGED;
741 if (w_inode && w_inode != &temp_inode)
746 errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino,
747 struct ext2_inode *inode)
749 return ext2fs_write_inode_full(fs, ino, inode,
750 sizeof(struct ext2_inode));
754 * This function should be called when writing a new inode. It makes
755 * sure that extra part of large inodes is initialized properly.
757 errcode_t ext2fs_write_new_inode(ext2_filsys fs, ext2_ino_t ino,
758 struct ext2_inode *inode)
760 struct ext2_inode *buf;
761 int size = EXT2_INODE_SIZE(fs->super);
762 struct ext2_inode_large *large_inode;
764 __u32 t = fs->now ? fs->now : time(NULL);
773 if (size == sizeof(struct ext2_inode))
774 return ext2fs_write_inode_full(fs, ino, inode,
775 sizeof(struct ext2_inode));
781 memset(buf, 0, size);
784 large_inode = (struct ext2_inode_large *) buf;
785 large_inode->i_extra_isize = sizeof(struct ext2_inode_large) -
786 EXT2_GOOD_OLD_INODE_SIZE;
787 if (!large_inode->i_crtime)
788 large_inode->i_crtime = t;
790 retval = ext2fs_write_inode_full(fs, ino, buf, size);
796 errcode_t ext2fs_get_blocks(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks)
798 struct ext2_inode inode;
802 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
804 if (ino > fs->super->s_inodes_count)
805 return EXT2_ET_BAD_INODE_NUM;
807 if (fs->get_blocks) {
808 if (!(*fs->get_blocks)(fs, ino, blocks))
811 retval = ext2fs_read_inode(fs, ino, &inode);
814 for (i=0; i < EXT2_N_BLOCKS; i++)
815 blocks[i] = inode.i_block[i];
819 errcode_t ext2fs_check_directory(ext2_filsys fs, ext2_ino_t ino)
821 struct ext2_inode inode;
824 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
826 if (ino > fs->super->s_inodes_count)
827 return EXT2_ET_BAD_INODE_NUM;
829 if (fs->check_directory) {
830 retval = (fs->check_directory)(fs, ino);
831 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
834 retval = ext2fs_read_inode(fs, ino, &inode);
837 if (!LINUX_S_ISDIR(inode.i_mode))
838 return EXT2_ET_NO_DIRECTORY;