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
25 #include <sys/types.h>
32 struct ext2_struct_inode_scan {
35 ext2_ino_t current_inode;
38 ext2_ino_t inodes_left;
41 blk_t inode_buffer_blocks;
47 errcode_t (*done_group)(ext2_filsys fs,
51 void * done_group_data;
58 * This routine flushes the icache, if it exists.
60 errcode_t ext2fs_flush_icache(ext2_filsys fs)
67 for (i=0; i < fs->icache->cache_size; i++)
68 fs->icache->cache[i].ino = 0;
70 fs->icache->buffer_blk = 0;
74 static errcode_t create_icache(ext2_filsys fs)
80 retval = ext2fs_get_mem(sizeof(struct ext2_inode_cache), &fs->icache);
84 memset(fs->icache, 0, sizeof(struct ext2_inode_cache));
85 retval = ext2fs_get_mem(fs->blocksize, &fs->icache->buffer);
87 ext2fs_free_mem(&fs->icache);
90 fs->icache->buffer_blk = 0;
91 fs->icache->cache_last = -1;
92 fs->icache->cache_size = 4;
93 fs->icache->refcount = 1;
94 retval = ext2fs_get_array(fs->icache->cache_size,
95 sizeof(struct ext2_inode_cache_ent),
98 ext2fs_free_mem(&fs->icache->buffer);
99 ext2fs_free_mem(&fs->icache);
102 ext2fs_flush_icache(fs);
106 errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks,
107 ext2_inode_scan *ret_scan)
109 ext2_inode_scan scan;
111 errcode_t (*save_get_blocks)(ext2_filsys f, ext2_ino_t ino, blk_t *blocks);
113 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
116 * If fs->badblocks isn't set, then set it --- since the inode
117 * scanning functions require it.
119 if (fs->badblocks == 0) {
121 * Temporarly save fs->get_blocks and set it to zero,
122 * for compatibility with old e2fsck's.
124 save_get_blocks = fs->get_blocks;
126 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
127 if (retval && fs->badblocks) {
128 ext2fs_badblocks_list_free(fs->badblocks);
131 fs->get_blocks = save_get_blocks;
134 retval = ext2fs_get_mem(sizeof(struct ext2_struct_inode_scan), &scan);
137 memset(scan, 0, sizeof(struct ext2_struct_inode_scan));
139 scan->magic = EXT2_ET_MAGIC_INODE_SCAN;
141 scan->inode_size = EXT2_INODE_SIZE(fs->super);
142 scan->bytes_left = 0;
143 scan->current_group = 0;
144 scan->groups_left = fs->group_desc_count - 1;
145 scan->inode_buffer_blocks = buffer_blocks ? buffer_blocks : 8;
146 scan->current_block = scan->fs->
147 group_desc[scan->current_group].bg_inode_table;
148 scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super);
149 scan->blocks_left = scan->fs->inode_blocks_per_group;
150 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
151 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
153 fs->group_desc[scan->current_group].bg_itable_unused;
156 (fs->blocksize / scan->inode_size - 1)) *
157 scan->inode_size / fs->blocksize;
159 retval = ext2fs_get_array(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 =fs->group_desc[scan->current_group].bg_inode_table;
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 fs->group_desc[scan->current_group].bg_itable_unused;
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 blk_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_blk(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 (scan->fs->group_desc[scan->current_group].bg_flags &
439 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)
526 unsigned long group, block, block_nr, offset;
529 int clen, i, inodes_per_block, length;
532 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
534 /* Check to see if user has an override function */
535 if (fs->read_inode) {
536 retval = (fs->read_inode)(fs, ino, inode);
537 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
540 if ((ino == 0) || (ino > fs->super->s_inodes_count))
541 return EXT2_ET_BAD_INODE_NUM;
542 /* Create inode cache if not present */
544 retval = create_icache(fs);
548 /* Check to see if it's in the inode cache */
549 if (bufsize == sizeof(struct ext2_inode)) {
550 /* only old good inode can be retrieved from the cache */
551 for (i=0; i < fs->icache->cache_size; i++) {
552 if (fs->icache->cache[i].ino == ino) {
553 *inode = fs->icache->cache[i].inode;
558 if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
559 inodes_per_block = fs->blocksize / EXT2_INODE_SIZE(fs->super);
560 block_nr = fs->image_header->offset_inode / fs->blocksize;
561 block_nr += (ino - 1) / inodes_per_block;
562 offset = ((ino - 1) % inodes_per_block) *
563 EXT2_INODE_SIZE(fs->super);
566 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
567 if (group > fs->group_desc_count)
568 return EXT2_ET_BAD_INODE_NUM;
569 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
570 EXT2_INODE_SIZE(fs->super);
571 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
572 if (!fs->group_desc[(unsigned)group].bg_inode_table)
573 return EXT2_ET_MISSING_INODE_TABLE;
574 block_nr = fs->group_desc[(unsigned)group].bg_inode_table +
578 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
580 length = EXT2_INODE_SIZE(fs->super);
581 if (bufsize < length)
584 ptr = (char *) inode;
587 if ((offset + length) > fs->blocksize)
588 clen = fs->blocksize - offset;
590 if (block_nr != fs->icache->buffer_blk) {
591 retval = io_channel_read_blk(io, block_nr, 1,
595 fs->icache->buffer_blk = block_nr;
598 memcpy(ptr, ((char *) fs->icache->buffer) + (unsigned) offset,
607 #ifdef WORDS_BIGENDIAN
608 ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) inode,
609 (struct ext2_inode_large *) inode,
613 /* Update the inode cache */
614 fs->icache->cache_last = (fs->icache->cache_last + 1) %
615 fs->icache->cache_size;
616 fs->icache->cache[fs->icache->cache_last].ino = ino;
617 fs->icache->cache[fs->icache->cache_last].inode = *inode;
622 errcode_t ext2fs_read_inode(ext2_filsys fs, ext2_ino_t ino,
623 struct ext2_inode * inode)
625 return ext2fs_read_inode_full(fs, ino, inode,
626 sizeof(struct ext2_inode));
629 errcode_t ext2fs_write_inode_full(ext2_filsys fs, ext2_ino_t ino,
630 struct ext2_inode * inode, int bufsize)
632 unsigned long group, block, block_nr, offset;
633 errcode_t retval = 0;
634 struct ext2_inode_large temp_inode, *w_inode;
638 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
640 /* Check to see if user provided an override function */
641 if (fs->write_inode) {
642 retval = (fs->write_inode)(fs, ino, inode);
643 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
647 /* Check to see if the inode cache needs to be updated */
649 for (i=0; i < fs->icache->cache_size; i++) {
650 if (fs->icache->cache[i].ino == ino) {
651 fs->icache->cache[i].inode = *inode;
656 retval = create_icache(fs);
661 if (!(fs->flags & EXT2_FLAG_RW))
662 return EXT2_ET_RO_FILSYS;
664 if ((ino == 0) || (ino > fs->super->s_inodes_count))
665 return EXT2_ET_BAD_INODE_NUM;
668 if (length < EXT2_INODE_SIZE(fs->super))
669 length = EXT2_INODE_SIZE(fs->super);
671 if (length > (int) sizeof(struct ext2_inode_large)) {
672 w_inode = malloc(length);
676 w_inode = &temp_inode;
677 memset(w_inode, 0, length);
679 #ifdef WORDS_BIGENDIAN
680 ext2fs_swap_inode_full(fs, w_inode,
681 (struct ext2_inode_large *) inode,
684 memcpy(w_inode, inode, bufsize);
687 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
688 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
689 EXT2_INODE_SIZE(fs->super);
690 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
691 if (!fs->group_desc[(unsigned) group].bg_inode_table) {
692 retval = EXT2_ET_MISSING_INODE_TABLE;
695 block_nr = fs->group_desc[(unsigned) group].bg_inode_table + block;
697 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
699 length = EXT2_INODE_SIZE(fs->super);
700 if (length > bufsize)
703 ptr = (char *) w_inode;
707 if ((offset + length) > fs->blocksize)
708 clen = fs->blocksize - offset;
710 if (fs->icache->buffer_blk != block_nr) {
711 retval = io_channel_read_blk(fs->io, block_nr, 1,
715 fs->icache->buffer_blk = block_nr;
719 memcpy((char *) fs->icache->buffer + (unsigned) offset,
722 retval = io_channel_write_blk(fs->io, block_nr, 1,
733 fs->flags |= EXT2_FLAG_CHANGED;
735 if (w_inode && w_inode != &temp_inode)
740 errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino,
741 struct ext2_inode *inode)
743 return ext2fs_write_inode_full(fs, ino, inode,
744 sizeof(struct ext2_inode));
748 * This function should be called when writing a new inode. It makes
749 * sure that extra part of large inodes is initialized properly.
751 errcode_t ext2fs_write_new_inode(ext2_filsys fs, ext2_ino_t ino,
752 struct ext2_inode *inode)
754 struct ext2_inode *buf;
755 int size = EXT2_INODE_SIZE(fs->super);
756 struct ext2_inode_large *large_inode;
758 __u32 t = fs->now ? fs->now : time(NULL);
767 if (size == sizeof(struct ext2_inode))
768 return ext2fs_write_inode_full(fs, ino, inode,
769 sizeof(struct ext2_inode));
775 memset(buf, 0, size);
778 large_inode = (struct ext2_inode_large *) buf;
779 large_inode->i_extra_isize = sizeof(struct ext2_inode_large) -
780 EXT2_GOOD_OLD_INODE_SIZE;
781 if (!large_inode->i_crtime)
782 large_inode->i_crtime = t;
784 retval = ext2fs_write_inode_full(fs, ino, buf, size);
790 errcode_t ext2fs_get_blocks(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks)
792 struct ext2_inode inode;
796 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
798 if (ino > fs->super->s_inodes_count)
799 return EXT2_ET_BAD_INODE_NUM;
801 if (fs->get_blocks) {
802 if (!(*fs->get_blocks)(fs, ino, blocks))
805 retval = ext2fs_read_inode(fs, ino, &inode);
808 for (i=0; i < EXT2_N_BLOCKS; i++)
809 blocks[i] = inode.i_block[i];
813 errcode_t ext2fs_check_directory(ext2_filsys fs, ext2_ino_t ino)
815 struct ext2_inode inode;
818 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
820 if (ino > fs->super->s_inodes_count)
821 return EXT2_ET_BAD_INODE_NUM;
823 if (fs->check_directory) {
824 retval = (fs->check_directory)(fs, ino);
825 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
828 retval = ext2fs_read_inode(fs, ino, &inode);
831 if (!LINUX_S_ISDIR(inode.i_mode))
832 return EXT2_ET_NO_DIRECTORY;