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 Public
24 #include <sys/types.h>
31 struct ext2_struct_inode_scan {
34 ext2_ino_t current_inode;
37 ext2_ino_t inodes_left;
40 blk_t inode_buffer_blocks;
46 errcode_t (*done_group)(ext2_filsys fs,
50 void * done_group_data;
57 * This routine flushes the icache, if it exists.
59 errcode_t ext2fs_flush_icache(ext2_filsys fs)
66 for (i=0; i < fs->icache->cache_size; i++)
67 fs->icache->cache[i].ino = 0;
69 fs->icache->buffer_blk = 0;
73 static errcode_t create_icache(ext2_filsys fs)
79 retval = ext2fs_get_mem(sizeof(struct ext2_inode_cache), &fs->icache);
83 memset(fs->icache, 0, sizeof(struct ext2_inode_cache));
84 retval = ext2fs_get_mem(fs->blocksize, &fs->icache->buffer);
86 ext2fs_free_mem(&fs->icache);
89 fs->icache->buffer_blk = 0;
90 fs->icache->cache_last = -1;
91 fs->icache->cache_size = 4;
92 fs->icache->refcount = 1;
93 retval = ext2fs_get_mem(sizeof(struct ext2_inode_cache_ent)
94 * fs->icache->cache_size,
97 ext2fs_free_mem(&fs->icache->buffer);
98 ext2fs_free_mem(&fs->icache);
101 ext2fs_flush_icache(fs);
105 errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks,
106 ext2_inode_scan *ret_scan)
108 ext2_inode_scan scan;
110 errcode_t (*save_get_blocks)(ext2_filsys f, ext2_ino_t ino, blk_t *blocks);
112 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
115 * If fs->badblocks isn't set, then set it --- since the inode
116 * scanning functions require it.
118 if (fs->badblocks == 0) {
120 * Temporarly save fs->get_blocks and set it to zero,
121 * for compatibility with old e2fsck's.
123 save_get_blocks = fs->get_blocks;
125 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
126 if (retval && fs->badblocks) {
127 ext2fs_badblocks_list_free(fs->badblocks);
130 fs->get_blocks = save_get_blocks;
133 retval = ext2fs_get_mem(sizeof(struct ext2_struct_inode_scan), &scan);
136 memset(scan, 0, sizeof(struct ext2_struct_inode_scan));
138 scan->magic = EXT2_ET_MAGIC_INODE_SCAN;
140 scan->inode_size = EXT2_INODE_SIZE(fs->super);
141 scan->bytes_left = 0;
142 scan->current_group = 0;
143 scan->groups_left = fs->group_desc_count - 1;
144 scan->inode_buffer_blocks = buffer_blocks ? buffer_blocks : 8;
145 scan->current_block = scan->fs->
146 group_desc[scan->current_group].bg_inode_table;
147 scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super);
148 scan->blocks_left = scan->fs->inode_blocks_per_group;
149 retval = ext2fs_get_mem((size_t) (scan->inode_buffer_blocks *
151 &scan->inode_buffer);
152 scan->done_group = 0;
153 scan->done_group_data = 0;
154 scan->bad_block_ptr = 0;
156 ext2fs_free_mem(&scan);
159 retval = ext2fs_get_mem(scan->inode_size, &scan->temp_buffer);
161 ext2fs_free_mem(&scan->inode_buffer);
162 ext2fs_free_mem(&scan);
165 if (scan->fs->badblocks && scan->fs->badblocks->num)
166 scan->scan_flags |= EXT2_SF_CHK_BADBLOCKS;
167 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
168 EXT2_FEATURE_COMPAT_LAZY_BG))
169 scan->scan_flags |= EXT2_SF_DO_LAZY;
174 void ext2fs_close_inode_scan(ext2_inode_scan scan)
176 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
179 ext2fs_free_mem(&scan->inode_buffer);
180 scan->inode_buffer = NULL;
181 ext2fs_free_mem(&scan->temp_buffer);
182 scan->temp_buffer = NULL;
183 ext2fs_free_mem(&scan);
187 void ext2fs_set_inode_callback(ext2_inode_scan scan,
188 errcode_t (*done_group)(ext2_filsys fs,
189 ext2_inode_scan scan,
192 void *done_group_data)
194 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
197 scan->done_group = done_group;
198 scan->done_group_data = done_group_data;
201 int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags,
206 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
209 old_flags = scan->scan_flags;
210 scan->scan_flags &= ~clear_flags;
211 scan->scan_flags |= set_flags;
216 * This function is called by ext2fs_get_next_inode when it needs to
217 * get ready to read in a new blockgroup.
219 static errcode_t get_next_blockgroup(ext2_inode_scan scan)
221 scan->current_group++;
224 scan->current_block = scan->fs->
225 group_desc[scan->current_group].bg_inode_table;
227 scan->current_inode = scan->current_group *
228 EXT2_INODES_PER_GROUP(scan->fs->super);
230 scan->bytes_left = 0;
231 scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super);
232 scan->blocks_left = scan->fs->inode_blocks_per_group;
236 errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,
239 scan->current_group = group - 1;
240 scan->groups_left = scan->fs->group_desc_count - group;
241 return get_next_blockgroup(scan);
245 * This function is called by get_next_blocks() to check for bad
246 * blocks in the inode table.
248 * This function assumes that badblocks_list->list is sorted in
251 static errcode_t check_for_inode_bad_blocks(ext2_inode_scan scan,
254 blk_t blk = scan->current_block;
255 badblocks_list bb = scan->fs->badblocks;
258 * If the inode table is missing, then obviously there are no
265 * If the current block is greater than the bad block listed
266 * in the bad block list, then advance the pointer until this
267 * is no longer the case. If we run out of bad blocks, then
268 * we don't need to do any more checking!
270 while (blk > bb->list[scan->bad_block_ptr]) {
271 if (++scan->bad_block_ptr >= bb->num) {
272 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
278 * If the current block is equal to the bad block listed in
279 * the bad block list, then handle that one block specially.
280 * (We could try to handle runs of bad blocks, but that
281 * only increases CPU efficiency by a small amount, at the
282 * expense of a huge expense of code complexity, and for an
283 * uncommon case at that.)
285 if (blk == bb->list[scan->bad_block_ptr]) {
286 scan->scan_flags |= EXT2_SF_BAD_INODE_BLK;
288 if (++scan->bad_block_ptr >= bb->num)
289 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
294 * If there is a bad block in the range that we're about to
295 * read in, adjust the number of blocks to read so that we we
296 * don't read in the bad block. (Then the next block to read
297 * will be the bad block, which is handled in the above case.)
299 if ((blk + *num_blocks) > bb->list[scan->bad_block_ptr])
300 *num_blocks = (int) (bb->list[scan->bad_block_ptr] - blk);
306 * This function is called by ext2fs_get_next_inode when it needs to
307 * read in more blocks from the current blockgroup's inode table.
309 static errcode_t get_next_blocks(ext2_inode_scan scan)
315 * Figure out how many blocks to read; we read at most
316 * inode_buffer_blocks, and perhaps less if there aren't that
317 * many blocks left to read.
319 num_blocks = scan->inode_buffer_blocks;
320 if (num_blocks > scan->blocks_left)
321 num_blocks = scan->blocks_left;
324 * If the past block "read" was a bad block, then mark the
325 * left-over extra bytes as also being bad.
327 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) {
328 if (scan->bytes_left)
329 scan->scan_flags |= EXT2_SF_BAD_EXTRA_BYTES;
330 scan->scan_flags &= ~EXT2_SF_BAD_INODE_BLK;
334 * Do inode bad block processing, if necessary.
336 if (scan->scan_flags & EXT2_SF_CHK_BADBLOCKS) {
337 retval = check_for_inode_bad_blocks(scan, &num_blocks);
342 if ((scan->scan_flags & EXT2_SF_BAD_INODE_BLK) ||
343 (scan->current_block == 0)) {
344 memset(scan->inode_buffer, 0,
345 (size_t) num_blocks * scan->fs->blocksize);
347 retval = io_channel_read_blk(scan->fs->io,
352 return EXT2_ET_NEXT_INODE_READ;
354 scan->ptr = scan->inode_buffer;
355 scan->bytes_left = num_blocks * scan->fs->blocksize;
357 scan->blocks_left -= num_blocks;
358 if (scan->current_block)
359 scan->current_block += num_blocks;
365 * Returns 1 if the entire inode_buffer has a non-zero size and
366 * contains all zeros. (Not just deleted inodes, since that means
367 * that part of the inode table was used at one point; we want all
368 * zeros, which means that the inode table is pristine.)
370 static inline int is_empty_scan(ext2_inode_scan scan)
374 if (scan->bytes_left == 0)
377 for (i=0; i < scan->bytes_left; i++)
384 errcode_t ext2fs_get_next_inode_full(ext2_inode_scan scan, ext2_ino_t *ino,
385 struct ext2_inode *inode, int bufsize)
390 EXT2_CHECK_MAGIC(scan, EXT2_ET_MAGIC_INODE_SCAN);
393 * Do we need to start reading a new block group?
395 if (scan->inodes_left <= 0) {
397 if (scan->done_group) {
398 retval = (scan->done_group)
399 (scan->fs, scan, scan->current_group,
400 scan->done_group_data);
404 if (scan->groups_left <= 0) {
408 retval = get_next_blockgroup(scan);
413 * These checks are done outside the above if statement so
414 * they can be done for block group #0.
416 if ((scan->scan_flags & EXT2_SF_DO_LAZY) &&
417 (scan->fs->group_desc[scan->current_group].bg_flags &
418 EXT2_BG_INODE_UNINIT))
419 goto force_new_group;
420 if (scan->current_block == 0) {
421 if (scan->scan_flags & EXT2_SF_SKIP_MISSING_ITABLE) {
422 goto force_new_group;
424 return EXT2_ET_MISSING_INODE_TABLE;
429 * Have we run out of space in the inode buffer? If so, we
430 * need to read in more blocks.
432 if (scan->bytes_left < scan->inode_size) {
433 memcpy(scan->temp_buffer, scan->ptr, scan->bytes_left);
434 extra_bytes = scan->bytes_left;
436 retval = get_next_blocks(scan);
441 * XXX test Need check for used inode somehow.
442 * (Note: this is hard.)
444 if (is_empty_scan(scan))
445 goto force_new_group;
451 memcpy(scan->temp_buffer+extra_bytes, scan->ptr,
452 scan->inode_size - extra_bytes);
453 scan->ptr += scan->inode_size - extra_bytes;
454 scan->bytes_left -= scan->inode_size - extra_bytes;
456 #ifdef WORDS_BIGENDIAN
457 memset(inode, 0, bufsize);
458 ext2fs_swap_inode_full(scan->fs,
459 (struct ext2_inode_large *) inode,
460 (struct ext2_inode_large *) scan->temp_buffer,
463 *inode = *((struct ext2_inode *) scan->temp_buffer);
465 if (scan->scan_flags & EXT2_SF_BAD_EXTRA_BYTES)
466 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
467 scan->scan_flags &= ~EXT2_SF_BAD_EXTRA_BYTES;
469 #ifdef WORDS_BIGENDIAN
470 memset(inode, 0, bufsize);
471 ext2fs_swap_inode_full(scan->fs,
472 (struct ext2_inode_large *) inode,
473 (struct ext2_inode_large *) scan->ptr,
476 memcpy(inode, scan->ptr, bufsize);
478 scan->ptr += scan->inode_size;
479 scan->bytes_left -= scan->inode_size;
480 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK)
481 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
485 scan->current_inode++;
486 *ino = scan->current_inode;
490 errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ext2_ino_t *ino,
491 struct ext2_inode *inode)
493 return ext2fs_get_next_inode_full(scan, ino, inode,
494 sizeof(struct ext2_inode));
498 * Functions to read and write a single inode.
500 errcode_t ext2fs_read_inode_full(ext2_filsys fs, ext2_ino_t ino,
501 struct ext2_inode * inode, int bufsize)
503 unsigned long group, block, block_nr, offset;
506 int clen, i, inodes_per_block, length;
509 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
511 /* Check to see if user has an override function */
512 if (fs->read_inode) {
513 retval = (fs->read_inode)(fs, ino, inode);
514 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
517 /* Create inode cache if not present */
519 retval = create_icache(fs);
523 /* Check to see if it's in the inode cache */
524 if (bufsize == sizeof(struct ext2_inode)) {
525 /* only old good inode can be retrieve from the cache */
526 for (i=0; i < fs->icache->cache_size; i++) {
527 if (fs->icache->cache[i].ino == ino) {
528 *inode = fs->icache->cache[i].inode;
533 if ((ino == 0) || (ino > fs->super->s_inodes_count))
534 return EXT2_ET_BAD_INODE_NUM;
535 if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
536 inodes_per_block = fs->blocksize / EXT2_INODE_SIZE(fs->super);
537 block_nr = fs->image_header->offset_inode / fs->blocksize;
538 block_nr += (ino - 1) / inodes_per_block;
539 offset = ((ino - 1) % inodes_per_block) *
540 EXT2_INODE_SIZE(fs->super);
543 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
544 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
545 EXT2_INODE_SIZE(fs->super);
546 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
547 if (!fs->group_desc[(unsigned)group].bg_inode_table)
548 return EXT2_ET_MISSING_INODE_TABLE;
549 block_nr = fs->group_desc[(unsigned)group].bg_inode_table +
553 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
555 length = EXT2_INODE_SIZE(fs->super);
556 if (bufsize < length)
559 ptr = (char *) inode;
562 if ((offset + length) > fs->blocksize)
563 clen = fs->blocksize - offset;
565 if (block_nr != fs->icache->buffer_blk) {
566 retval = io_channel_read_blk(io, block_nr, 1,
570 fs->icache->buffer_blk = block_nr;
573 memcpy(ptr, ((char *) fs->icache->buffer) + (unsigned) offset,
582 #ifdef WORDS_BIGENDIAN
583 ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) inode,
584 (struct ext2_inode_large *) inode,
588 /* Update the inode cache */
589 fs->icache->cache_last = (fs->icache->cache_last + 1) %
590 fs->icache->cache_size;
591 fs->icache->cache[fs->icache->cache_last].ino = ino;
592 fs->icache->cache[fs->icache->cache_last].inode = *inode;
597 errcode_t ext2fs_read_inode(ext2_filsys fs, ext2_ino_t ino,
598 struct ext2_inode * inode)
600 return ext2fs_read_inode_full(fs, ino, inode,
601 sizeof(struct ext2_inode));
604 errcode_t ext2fs_write_inode_full(ext2_filsys fs, ext2_ino_t ino,
605 struct ext2_inode * inode, int bufsize)
607 unsigned long group, block, block_nr, offset;
608 errcode_t retval = 0;
609 struct ext2_inode_large temp_inode, *w_inode;
613 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
615 /* Check to see if user provided an override function */
616 if (fs->write_inode) {
617 retval = (fs->write_inode)(fs, ino, inode);
618 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
622 /* Check to see if the inode cache needs to be updated */
624 for (i=0; i < fs->icache->cache_size; i++) {
625 if (fs->icache->cache[i].ino == ino) {
626 fs->icache->cache[i].inode = *inode;
631 retval = create_icache(fs);
636 if (!(fs->flags & EXT2_FLAG_RW))
637 return EXT2_ET_RO_FILSYS;
639 if ((ino == 0) || (ino > fs->super->s_inodes_count))
640 return EXT2_ET_BAD_INODE_NUM;
643 if (length < EXT2_INODE_SIZE(fs->super))
644 length = EXT2_INODE_SIZE(fs->super);
646 if (length > (int) sizeof(struct ext2_inode_large)) {
647 w_inode = malloc(length);
651 w_inode = &temp_inode;
652 memset(w_inode, 0, length);
654 #ifdef WORDS_BIGENDIAN
655 ext2fs_swap_inode_full(fs, w_inode,
656 (struct ext2_inode_large *) inode,
659 memcpy(w_inode, inode, bufsize);
662 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
663 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
664 EXT2_INODE_SIZE(fs->super);
665 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
666 if (!fs->group_desc[(unsigned) group].bg_inode_table) {
667 retval = EXT2_ET_MISSING_INODE_TABLE;
670 block_nr = fs->group_desc[(unsigned) group].bg_inode_table + block;
672 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
674 length = EXT2_INODE_SIZE(fs->super);
675 if (length > bufsize)
678 ptr = (char *) w_inode;
682 if ((offset + length) > fs->blocksize)
683 clen = fs->blocksize - offset;
685 if (fs->icache->buffer_blk != block_nr) {
686 retval = io_channel_read_blk(fs->io, block_nr, 1,
690 fs->icache->buffer_blk = block_nr;
694 memcpy((char *) fs->icache->buffer + (unsigned) offset,
697 retval = io_channel_write_blk(fs->io, block_nr, 1,
708 fs->flags |= EXT2_FLAG_CHANGED;
710 if (w_inode && w_inode != &temp_inode)
715 errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino,
716 struct ext2_inode *inode)
718 return ext2fs_write_inode_full(fs, ino, inode,
719 sizeof(struct ext2_inode));
723 * This function should be called when writing a new inode. It makes
724 * sure that extra part of large inodes is initialized properly.
726 errcode_t ext2fs_write_new_inode(ext2_filsys fs, ext2_ino_t ino,
727 struct ext2_inode *inode)
729 struct ext2_inode *buf;
730 int size = EXT2_INODE_SIZE(fs->super);
731 struct ext2_inode_large *large_inode;
734 if (size == sizeof(struct ext2_inode))
735 return ext2fs_write_inode_full(fs, ino, inode,
736 sizeof(struct ext2_inode));
742 memset(buf, 0, size);
745 large_inode = (struct ext2_inode_large *) buf;
746 large_inode->i_extra_isize = sizeof(struct ext2_inode_large) -
747 EXT2_GOOD_OLD_INODE_SIZE;
749 retval = ext2fs_write_inode_full(fs, ino, buf, size);
755 errcode_t ext2fs_get_blocks(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks)
757 struct ext2_inode inode;
761 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
763 if (ino > fs->super->s_inodes_count)
764 return EXT2_ET_BAD_INODE_NUM;
766 if (fs->get_blocks) {
767 if (!(*fs->get_blocks)(fs, ino, blocks))
770 retval = ext2fs_read_inode(fs, ino, &inode);
773 for (i=0; i < EXT2_N_BLOCKS; i++)
774 blocks[i] = inode.i_block[i];
778 errcode_t ext2fs_check_directory(ext2_filsys fs, ext2_ino_t ino)
780 struct ext2_inode inode;
783 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
785 if (ino > fs->super->s_inodes_count)
786 return EXT2_ET_BAD_INODE_NUM;
788 if (fs->check_directory) {
789 retval = (fs->check_directory)(fs, ino);
790 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
793 retval = ext2fs_read_inode(fs, ino, &inode);
796 if (!LINUX_S_ISDIR(inode.i_mode))
797 return EXT2_ET_NO_DIRECTORY;