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 #define IBLOCK_STATUS_CSUMS_OK 1
34 #define IBLOCK_STATUS_INSANE 2
35 #define SCAN_BLOCK_STATUS(scan) ((scan)->temp_buffer + (scan)->inode_size)
37 struct ext2_struct_inode_scan {
40 ext2_ino_t current_inode;
41 blk64_t current_block;
43 ext2_ino_t inodes_left;
46 blk_t inode_buffer_blocks;
52 errcode_t (*done_group)(ext2_filsys fs,
56 void * done_group_data;
63 * This routine flushes the icache, if it exists.
65 errcode_t ext2fs_flush_icache(ext2_filsys fs)
72 for (i=0; i < fs->icache->cache_size; i++)
73 fs->icache->cache[i].ino = 0;
75 fs->icache->buffer_blk = 0;
80 * Free the inode cache structure
82 void ext2fs_free_inode_cache(struct ext2_inode_cache *icache)
86 if (--icache->refcount)
89 ext2fs_free_mem(&icache->buffer);
90 for (i = 0; i < icache->cache_size; i++)
91 ext2fs_free_mem(&icache->cache[i].inode);
93 ext2fs_free_mem(&icache->cache);
94 icache->buffer_blk = 0;
95 ext2fs_free_mem(&icache);
98 errcode_t ext2fs_create_inode_cache(ext2_filsys fs, unsigned int cache_size)
105 retval = ext2fs_get_mem(sizeof(struct ext2_inode_cache), &fs->icache);
109 memset(fs->icache, 0, sizeof(struct ext2_inode_cache));
110 retval = ext2fs_get_mem(fs->blocksize, &fs->icache->buffer);
114 fs->icache->buffer_blk = 0;
115 fs->icache->cache_last = -1;
116 fs->icache->cache_size = cache_size;
117 fs->icache->refcount = 1;
118 retval = ext2fs_get_array(fs->icache->cache_size,
119 sizeof(struct ext2_inode_cache_ent),
124 for (i = 0; i < fs->icache->cache_size; i++) {
125 retval = ext2fs_get_mem(EXT2_INODE_SIZE(fs->super),
126 &fs->icache->cache[i].inode);
131 ext2fs_flush_icache(fs);
134 ext2fs_free_inode_cache(fs->icache);
139 errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks,
140 ext2_inode_scan *ret_scan)
142 ext2_inode_scan scan;
144 errcode_t (*save_get_blocks)(ext2_filsys f, ext2_ino_t ino, blk_t *blocks);
146 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
148 if (ext2fs_has_feature_journal_dev(fs->super))
149 return EXT2_ET_EXTERNAL_JOURNAL_NOSUPP;
151 if (fs->blocksize < 1024)
152 return EXT2_FILSYS_CORRUPTED; /* Should never happen */
155 * If fs->badblocks isn't set, then set it --- since the inode
156 * scanning functions require it.
158 if (fs->badblocks == 0) {
160 * Temporarily save fs->get_blocks and set it to zero,
161 * for compatibility with old e2fsck's.
163 save_get_blocks = fs->get_blocks;
165 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
166 if (retval && fs->badblocks) {
167 ext2fs_badblocks_list_free(fs->badblocks);
170 fs->get_blocks = save_get_blocks;
173 retval = ext2fs_get_mem(sizeof(struct ext2_struct_inode_scan), &scan);
176 memset(scan, 0, sizeof(struct ext2_struct_inode_scan));
178 scan->magic = EXT2_ET_MAGIC_INODE_SCAN;
180 scan->inode_size = EXT2_INODE_SIZE(fs->super);
181 scan->bytes_left = 0;
182 scan->current_group = 0;
183 scan->groups_left = fs->group_desc_count - 1;
184 scan->inode_buffer_blocks = buffer_blocks ? buffer_blocks :
185 EXT2_INODE_SCAN_DEFAULT_BUFFER_BLOCKS;
186 scan->current_block = ext2fs_inode_table_loc(scan->fs,
187 scan->current_group);
188 if (scan->current_block &&
189 ((scan->current_block < fs->super->s_first_data_block) ||
190 (scan->current_block + fs->inode_blocks_per_group - 1 >=
191 ext2fs_blocks_count(fs->super)))) {
192 ext2fs_free_mem(&scan);
193 return EXT2_ET_GDESC_BAD_INODE_TABLE;
196 scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super);
197 scan->blocks_left = scan->fs->inode_blocks_per_group;
198 if (ext2fs_has_group_desc_csum(fs)) {
199 __u32 unused = ext2fs_bg_itable_unused(fs, scan->current_group);
200 if (scan->inodes_left > unused)
201 scan->inodes_left -= unused;
203 scan->inodes_left = 0;
206 (fs->blocksize / scan->inode_size - 1)) *
207 scan->inode_size / fs->blocksize;
209 retval = io_channel_alloc_buf(fs->io, scan->inode_buffer_blocks,
210 &scan->inode_buffer);
211 scan->done_group = 0;
212 scan->done_group_data = 0;
213 scan->bad_block_ptr = 0;
215 ext2fs_free_mem(&scan);
218 retval = ext2fs_get_mem(scan->inode_size + scan->inode_buffer_blocks,
221 ext2fs_free_mem(&scan->inode_buffer);
222 ext2fs_free_mem(&scan);
225 memset(SCAN_BLOCK_STATUS(scan), 0, scan->inode_buffer_blocks);
226 if (scan->fs->badblocks && scan->fs->badblocks->num)
227 scan->scan_flags |= EXT2_SF_CHK_BADBLOCKS;
228 if (ext2fs_has_group_desc_csum(fs))
229 scan->scan_flags |= EXT2_SF_DO_LAZY;
234 void ext2fs_close_inode_scan(ext2_inode_scan scan)
236 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
239 ext2fs_free_mem(&scan->inode_buffer);
240 scan->inode_buffer = NULL;
241 ext2fs_free_mem(&scan->temp_buffer);
242 scan->temp_buffer = NULL;
243 ext2fs_free_mem(&scan);
247 void ext2fs_set_inode_callback(ext2_inode_scan scan,
248 errcode_t (*done_group)(ext2_filsys fs,
249 ext2_inode_scan scan,
252 void *done_group_data)
254 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
257 scan->done_group = done_group;
258 scan->done_group_data = done_group_data;
261 int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags,
266 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
269 old_flags = scan->scan_flags;
270 scan->scan_flags &= ~clear_flags;
271 scan->scan_flags |= set_flags;
276 * This function is called by ext2fs_get_next_inode when it needs to
277 * get ready to read in a new blockgroup.
279 static errcode_t get_next_blockgroup(ext2_inode_scan scan)
281 ext2_filsys fs = scan->fs;
283 scan->current_group++;
286 scan->current_block = ext2fs_inode_table_loc(scan->fs,
287 scan->current_group);
288 scan->current_inode = scan->current_group *
289 EXT2_INODES_PER_GROUP(fs->super);
291 scan->bytes_left = 0;
292 scan->inodes_left = EXT2_INODES_PER_GROUP(fs->super);
293 scan->blocks_left = fs->inode_blocks_per_group;
294 if (ext2fs_has_group_desc_csum(fs)) {
295 __u32 unused = ext2fs_bg_itable_unused(fs, scan->current_group);
296 if (scan->inodes_left > unused)
297 scan->inodes_left -= unused;
299 scan->inodes_left = 0;
302 (fs->blocksize / scan->inode_size - 1)) *
303 scan->inode_size / fs->blocksize;
305 if (scan->current_block &&
306 ((scan->current_block < fs->super->s_first_data_block) ||
307 (scan->current_block + fs->inode_blocks_per_group - 1 >=
308 ext2fs_blocks_count(fs->super))))
309 return EXT2_ET_GDESC_BAD_INODE_TABLE;
313 errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,
316 scan->current_group = group - 1;
317 scan->groups_left = scan->fs->group_desc_count - group;
318 scan->bad_block_ptr = 0;
319 return get_next_blockgroup(scan);
323 * This function is called by get_next_blocks() to check for bad
324 * blocks in the inode table.
326 * This function assumes that badblocks_list->list is sorted in
329 static errcode_t check_for_inode_bad_blocks(ext2_inode_scan scan,
332 blk64_t blk = scan->current_block;
333 badblocks_list bb = scan->fs->badblocks;
336 * If the inode table is missing, then obviously there are no
342 /* Make sure bad_block_ptr is still valid */
343 if (scan->bad_block_ptr >= bb->num) {
344 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
349 * If the current block is greater than the bad block listed
350 * in the bad block list, then advance the pointer until this
351 * is no longer the case. If we run out of bad blocks, then
352 * we don't need to do any more checking!
354 while (blk > bb->list[scan->bad_block_ptr]) {
355 if (++scan->bad_block_ptr >= bb->num) {
356 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
362 * If the current block is equal to the bad block listed in
363 * the bad block list, then handle that one block specially.
364 * (We could try to handle runs of bad blocks, but that
365 * only increases CPU efficiency by a small amount, at the
366 * expense of a huge expense of code complexity, and for an
367 * uncommon case at that.)
369 if (blk == bb->list[scan->bad_block_ptr]) {
370 scan->scan_flags |= EXT2_SF_BAD_INODE_BLK;
372 if (++scan->bad_block_ptr >= bb->num)
373 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
378 * If there is a bad block in the range that we're about to
379 * read in, adjust the number of blocks to read so that we we
380 * don't read in the bad block. (Then the next block to read
381 * will be the bad block, which is handled in the above case.)
383 if ((blk + *num_blocks) > bb->list[scan->bad_block_ptr])
384 *num_blocks = (int) (bb->list[scan->bad_block_ptr] - blk);
389 static int block_map_looks_insane(ext2_filsys fs,
390 struct ext2_inode_large *inode)
394 /* We're only interested in block mapped files, dirs, and symlinks */
395 if ((inode->i_flags & EXT4_INLINE_DATA_FL) ||
396 (inode->i_flags & EXT4_EXTENTS_FL))
398 if (!LINUX_S_ISREG(inode->i_mode) &&
399 !LINUX_S_ISLNK(inode->i_mode) &&
400 !LINUX_S_ISDIR(inode->i_mode))
402 if (LINUX_S_ISLNK(inode->i_mode) &&
403 EXT2_I_SIZE(inode) <= sizeof(inode->i_block))
406 /* Unused inodes probably aren't insane */
407 if (inode->i_links_count == 0)
410 /* See if more than half the block maps are insane */
411 for (i = 0, bad = 0; i < EXT2_N_BLOCKS; i++)
412 if (inode->i_block[i] != 0 &&
413 (inode->i_block[i] < fs->super->s_first_data_block ||
414 inode->i_block[i] >= ext2fs_blocks_count(fs->super)))
416 return bad > EXT2_N_BLOCKS / 2;
419 static int extent_head_looks_insane(struct ext2_inode_large *inode)
421 if (!(inode->i_flags & EXT4_EXTENTS_FL) ||
422 ext2fs_extent_header_verify(inode->i_block,
423 sizeof(inode->i_block)) == 0)
429 * Check all the inodes that we just read into the buffer. Record what we
430 * find here -- currently, we can observe that all checksums are ok; more
431 * than half the inodes are insane; or no conclusions at all.
433 static void check_inode_block_sanity(ext2_inode_scan scan, blk64_t num_blocks)
435 ext2_ino_t ino, inodes_to_scan;
436 unsigned int badness, checksum_failures;
437 unsigned int inodes_in_buf, inodes_per_block;
439 struct ext2_inode_large *inode;
441 unsigned int blk, bad_csum;
443 if (!(scan->scan_flags & EXT2_SF_WARN_GARBAGE_INODES))
446 inodes_to_scan = scan->inodes_left;
447 inodes_in_buf = num_blocks * scan->fs->blocksize / scan->inode_size;
448 if (inodes_to_scan > inodes_in_buf)
449 inodes_to_scan = inodes_in_buf;
451 p = (char *) scan->inode_buffer;
452 ino = scan->current_inode + 1;
453 checksum_failures = badness = 0;
454 block_status = SCAN_BLOCK_STATUS(scan);
455 memset(block_status, 0, scan->inode_buffer_blocks);
456 inodes_per_block = EXT2_INODES_PER_BLOCK(scan->fs->super);
458 if (inodes_per_block < 2)
461 #ifdef WORDS_BIGENDIAN
462 if (ext2fs_get_mem(EXT2_INODE_SIZE(scan->fs->super), &inode))
466 while (inodes_to_scan > 0) {
467 blk = (p - (char *)scan->inode_buffer) / scan->fs->blocksize;
468 bad_csum = ext2fs_inode_csum_verify(scan->fs, ino,
469 (struct ext2_inode_large *) p) == 0;
471 #ifdef WORDS_BIGENDIAN
472 ext2fs_swap_inode_full(scan->fs,
473 (struct ext2_inode_large *) inode,
474 (struct ext2_inode_large *) p,
475 0, EXT2_INODE_SIZE(scan->fs->super));
477 inode = (struct ext2_inode_large *) p;
480 /* Is this inode insane? */
484 } else if (extent_head_looks_insane(inode) ||
485 block_map_looks_insane(scan->fs, inode))
488 /* If more than half are insane, declare the whole block bad */
489 if (badness > inodes_per_block / 2) {
490 unsigned int ino_adj;
492 block_status[blk] |= IBLOCK_STATUS_INSANE;
493 ino_adj = inodes_per_block -
494 ((ino - 1) % inodes_per_block);
495 if (ino_adj > inodes_to_scan)
496 ino_adj = inodes_to_scan;
497 inodes_to_scan -= ino_adj;
498 p += scan->inode_size * ino_adj;
500 checksum_failures = badness = 0;
504 if ((ino % inodes_per_block) == 0) {
505 if (checksum_failures == 0)
506 block_status[blk] |= IBLOCK_STATUS_CSUMS_OK;
507 checksum_failures = badness = 0;
510 p += scan->inode_size;
514 #ifdef WORDS_BIGENDIAN
515 ext2fs_free_mem(&inode);
520 * This function is called by ext2fs_get_next_inode when it needs to
521 * read in more blocks from the current blockgroup's inode table.
523 static errcode_t get_next_blocks(ext2_inode_scan scan)
529 * Figure out how many blocks to read; we read at most
530 * inode_buffer_blocks, and perhaps less if there aren't that
531 * many blocks left to read.
533 num_blocks = scan->inode_buffer_blocks;
534 if (num_blocks > scan->blocks_left)
535 num_blocks = scan->blocks_left;
538 * If the past block "read" was a bad block, then mark the
539 * left-over extra bytes as also being bad.
541 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) {
542 if (scan->bytes_left)
543 scan->scan_flags |= EXT2_SF_BAD_EXTRA_BYTES;
544 scan->scan_flags &= ~EXT2_SF_BAD_INODE_BLK;
548 * Do inode bad block processing, if necessary.
550 if (scan->scan_flags & EXT2_SF_CHK_BADBLOCKS) {
551 retval = check_for_inode_bad_blocks(scan, &num_blocks);
556 if ((scan->scan_flags & EXT2_SF_BAD_INODE_BLK) ||
557 (scan->current_block == 0)) {
558 memset(scan->inode_buffer, 0,
559 (size_t) num_blocks * scan->fs->blocksize);
561 retval = io_channel_read_blk64(scan->fs->io,
566 return EXT2_ET_NEXT_INODE_READ;
568 check_inode_block_sanity(scan, num_blocks);
570 scan->ptr = scan->inode_buffer;
571 scan->bytes_left = num_blocks * scan->fs->blocksize;
573 scan->blocks_left -= num_blocks;
574 if (scan->current_block)
575 scan->current_block += num_blocks;
582 * Returns 1 if the entire inode_buffer has a non-zero size and
583 * contains all zeros. (Not just deleted inodes, since that means
584 * that part of the inode table was used at one point; we want all
585 * zeros, which means that the inode table is pristine.)
587 static inline int is_empty_scan(ext2_inode_scan scan)
591 if (scan->bytes_left == 0)
594 for (i=0; i < scan->bytes_left; i++)
601 errcode_t ext2fs_get_next_inode_full(ext2_inode_scan scan, ext2_ino_t *ino,
602 struct ext2_inode *inode, int bufsize)
607 struct ext2_inode_large *iptr = (struct ext2_inode_large *)inode;
611 EXT2_CHECK_MAGIC(scan, EXT2_ET_MAGIC_INODE_SCAN);
612 length = EXT2_INODE_SIZE(scan->fs->super);
613 iblock_status = SCAN_BLOCK_STATUS(scan);
616 * Do we need to start reading a new block group?
618 if (scan->inodes_left <= 0) {
620 if (scan->done_group) {
621 retval = (scan->done_group)
622 (scan->fs, scan, scan->current_group,
623 scan->done_group_data);
627 if (scan->groups_left <= 0) {
631 retval = get_next_blockgroup(scan);
636 * These checks are done outside the above if statement so
637 * they can be done for block group #0.
639 if ((scan->scan_flags & EXT2_SF_DO_LAZY) &&
640 (ext2fs_bg_flags_test(scan->fs, scan->current_group, EXT2_BG_INODE_UNINIT)
642 goto force_new_group;
643 if (scan->inodes_left == 0)
644 goto force_new_group;
645 if (scan->current_block == 0) {
646 if (scan->scan_flags & EXT2_SF_SKIP_MISSING_ITABLE) {
647 goto force_new_group;
649 return EXT2_ET_MISSING_INODE_TABLE;
654 * Have we run out of space in the inode buffer? If so, we
655 * need to read in more blocks.
657 if (scan->bytes_left < scan->inode_size) {
658 if (scan->bytes_left)
659 memcpy(scan->temp_buffer, scan->ptr, scan->bytes_left);
660 extra_bytes = scan->bytes_left;
662 retval = get_next_blocks(scan);
667 * XXX test Need check for used inode somehow.
668 * (Note: this is hard.)
670 if (is_empty_scan(scan))
671 goto force_new_group;
675 if (bufsize < length) {
676 retval = ext2fs_get_mem(length, &iptr);
682 iblk = scan->current_inode % EXT2_INODES_PER_GROUP(scan->fs->super) /
683 EXT2_INODES_PER_BLOCK(scan->fs->super) %
684 scan->inode_buffer_blocks;
686 memcpy(scan->temp_buffer+extra_bytes, scan->ptr,
687 scan->inode_size - extra_bytes);
688 scan->ptr += scan->inode_size - extra_bytes;
689 scan->bytes_left -= scan->inode_size - extra_bytes;
691 /* Verify the inode checksum. */
692 if (!(iblock_status[iblk] & IBLOCK_STATUS_CSUMS_OK) &&
693 !(scan->fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
694 !ext2fs_inode_csum_verify(scan->fs, scan->current_inode + 1,
695 (struct ext2_inode_large *)scan->temp_buffer))
696 retval = EXT2_ET_INODE_CSUM_INVALID;
698 #ifdef WORDS_BIGENDIAN
699 memset(iptr, 0, length);
700 ext2fs_swap_inode_full(scan->fs,
701 (struct ext2_inode_large *) iptr,
702 (struct ext2_inode_large *) scan->temp_buffer,
705 memcpy(iptr, scan->temp_buffer, length);
707 if (scan->scan_flags & EXT2_SF_BAD_EXTRA_BYTES)
708 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
709 scan->scan_flags &= ~EXT2_SF_BAD_EXTRA_BYTES;
711 /* Verify the inode checksum. */
712 if (!(iblock_status[iblk] & IBLOCK_STATUS_CSUMS_OK) &&
713 !(scan->fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
714 !ext2fs_inode_csum_verify(scan->fs, scan->current_inode + 1,
715 (struct ext2_inode_large *)scan->ptr))
716 retval = EXT2_ET_INODE_CSUM_INVALID;
718 #ifdef WORDS_BIGENDIAN
719 memset(iptr, 0, length);
720 ext2fs_swap_inode_full(scan->fs,
721 (struct ext2_inode_large *) iptr,
722 (struct ext2_inode_large *) scan->ptr,
725 memcpy(iptr, scan->ptr, length);
727 scan->ptr += scan->inode_size;
728 scan->bytes_left -= scan->inode_size;
729 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK)
730 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
732 if ((iblock_status[iblk] & IBLOCK_STATUS_INSANE) &&
733 (retval == 0 || retval == EXT2_ET_INODE_CSUM_INVALID))
734 retval = EXT2_ET_INODE_IS_GARBAGE;
737 scan->current_inode++;
738 *ino = scan->current_inode;
739 if (iptr != (struct ext2_inode_large *)inode) {
740 memcpy(inode, iptr, bufsize);
741 ext2fs_free_mem(&iptr);
746 errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ext2_ino_t *ino,
747 struct ext2_inode *inode)
749 return ext2fs_get_next_inode_full(scan, ino, inode,
750 sizeof(struct ext2_inode));
754 * Functions to read and write a single inode.
756 errcode_t ext2fs_read_inode2(ext2_filsys fs, ext2_ino_t ino,
757 struct ext2_inode * inode, int bufsize,
762 unsigned long block, offset;
766 int clen, inodes_per_block;
768 int length = EXT2_INODE_SIZE(fs->super);
769 struct ext2_inode_large *iptr;
770 int cache_slot, fail_csum;
772 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
774 if (ext2fs_has_feature_journal_dev(fs->super))
775 return EXT2_ET_EXTERNAL_JOURNAL_NOSUPP;
777 if (fs->blocksize < 1024)
778 return EXT2_FILSYS_CORRUPTED; /* Should never happen */
780 /* Check to see if user has an override function */
781 if (fs->read_inode &&
782 ((bufsize == sizeof(struct ext2_inode)) ||
783 (EXT2_INODE_SIZE(fs->super) == sizeof(struct ext2_inode)))) {
784 retval = (fs->read_inode)(fs, ino, inode);
785 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
788 if ((ino == 0) || (ino > fs->super->s_inodes_count))
789 return EXT2_ET_BAD_INODE_NUM;
790 /* Create inode cache if not present */
792 retval = ext2fs_create_inode_cache(fs, 4);
796 /* Check to see if it's in the inode cache */
797 for (i = 0; i < fs->icache->cache_size; i++) {
798 if (fs->icache->cache[i].ino == ino) {
799 memcpy(inode, fs->icache->cache[i].inode,
800 (bufsize > length) ? length : bufsize);
804 if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
805 inodes_per_block = fs->blocksize / EXT2_INODE_SIZE(fs->super);
806 block_nr = ext2fs_le32_to_cpu(fs->image_header->offset_inode) / fs->blocksize;
807 block_nr += (ino - 1) / inodes_per_block;
808 offset = ((ino - 1) % inodes_per_block) *
809 EXT2_INODE_SIZE(fs->super);
812 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
813 if (group > fs->group_desc_count)
814 return EXT2_ET_BAD_INODE_NUM;
815 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
816 EXT2_INODE_SIZE(fs->super);
817 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
818 block_nr = ext2fs_inode_table_loc(fs, group);
820 return EXT2_ET_MISSING_INODE_TABLE;
821 if ((block_nr < fs->super->s_first_data_block) ||
822 (block_nr + fs->inode_blocks_per_group - 1 >=
823 ext2fs_blocks_count(fs->super)))
824 return EXT2_ET_GDESC_BAD_INODE_TABLE;
828 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
830 cache_slot = (fs->icache->cache_last + 1) % fs->icache->cache_size;
831 iptr = (struct ext2_inode_large *)fs->icache->cache[cache_slot].inode;
836 if ((offset + length) > fs->blocksize)
837 clen = fs->blocksize - offset;
839 if (block_nr != fs->icache->buffer_blk) {
840 retval = io_channel_read_blk64(io, block_nr, 1,
844 fs->icache->buffer_blk = block_nr;
847 memcpy(ptr, ((char *) fs->icache->buffer) + (unsigned) offset,
855 length = EXT2_INODE_SIZE(fs->super);
857 /* Verify the inode checksum. */
858 fail_csum = !ext2fs_inode_csum_verify(fs, ino, iptr);
860 #ifdef WORDS_BIGENDIAN
861 ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) iptr,
862 (struct ext2_inode_large *) iptr,
866 /* Update the inode cache bookkeeping */
868 fs->icache->cache_last = cache_slot;
869 fs->icache->cache[cache_slot].ino = ino;
871 memcpy(inode, iptr, (bufsize > length) ? length : bufsize);
873 if (!(fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
874 !(flags & READ_INODE_NOCSUM) && fail_csum)
875 return EXT2_ET_INODE_CSUM_INVALID;
880 errcode_t ext2fs_read_inode_full(ext2_filsys fs, ext2_ino_t ino,
881 struct ext2_inode * inode, int bufsize)
883 return ext2fs_read_inode2(fs, ino, inode, bufsize, 0);
886 errcode_t ext2fs_read_inode(ext2_filsys fs, ext2_ino_t ino,
887 struct ext2_inode * inode)
889 return ext2fs_read_inode2(fs, ino, inode,
890 sizeof(struct ext2_inode), 0);
893 errcode_t ext2fs_write_inode2(ext2_filsys fs, ext2_ino_t ino,
894 struct ext2_inode * inode, int bufsize,
899 unsigned long block, offset;
900 errcode_t retval = 0;
901 struct ext2_inode_large *w_inode;
905 int length = EXT2_INODE_SIZE(fs->super);
907 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
909 if (ext2fs_has_feature_journal_dev(fs->super))
910 return EXT2_ET_EXTERNAL_JOURNAL_NOSUPP;
912 /* Check to see if user provided an override function */
913 if (fs->write_inode) {
914 retval = (fs->write_inode)(fs, ino, inode);
915 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
919 if ((ino == 0) || (ino > fs->super->s_inodes_count))
920 return EXT2_ET_BAD_INODE_NUM;
922 /* Prepare our shadow buffer for read/modify/byteswap/write */
923 retval = ext2fs_get_mem(length, &w_inode);
927 if (bufsize < length) {
928 retval = ext2fs_read_inode2(fs, ino,
929 (struct ext2_inode *)w_inode,
930 length, READ_INODE_NOCSUM);
935 /* Check to see if the inode cache needs to be updated */
937 for (i=0; i < fs->icache->cache_size; i++) {
938 if (fs->icache->cache[i].ino == ino) {
939 memcpy(fs->icache->cache[i].inode, inode,
940 (bufsize > length) ? length : bufsize);
945 retval = ext2fs_create_inode_cache(fs, 4);
949 memcpy(w_inode, inode, (bufsize > length) ? length : bufsize);
951 if (!(fs->flags & EXT2_FLAG_RW)) {
952 retval = EXT2_ET_RO_FILSYS;
956 #ifdef WORDS_BIGENDIAN
957 ext2fs_swap_inode_full(fs, w_inode, w_inode, 1, length);
960 if ((flags & WRITE_INODE_NOCSUM) == 0) {
961 retval = ext2fs_inode_csum_set(fs, ino, w_inode);
966 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
967 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
968 EXT2_INODE_SIZE(fs->super);
969 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
970 block_nr = ext2fs_inode_table_loc(fs, (unsigned) group);
972 retval = EXT2_ET_MISSING_INODE_TABLE;
975 if ((block_nr < fs->super->s_first_data_block) ||
976 (block_nr + fs->inode_blocks_per_group - 1 >=
977 ext2fs_blocks_count(fs->super))) {
978 retval = EXT2_ET_GDESC_BAD_INODE_TABLE;
983 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
985 ptr = (char *) w_inode;
989 if ((offset + length) > fs->blocksize)
990 clen = fs->blocksize - offset;
992 if (fs->icache->buffer_blk != block_nr) {
993 retval = io_channel_read_blk64(fs->io, block_nr, 1,
997 fs->icache->buffer_blk = block_nr;
1001 memcpy((char *) fs->icache->buffer + (unsigned) offset,
1004 retval = io_channel_write_blk64(fs->io, block_nr, 1,
1005 fs->icache->buffer);
1015 fs->flags |= EXT2_FLAG_CHANGED;
1017 ext2fs_free_mem(&w_inode);
1021 errcode_t ext2fs_write_inode_full(ext2_filsys fs, ext2_ino_t ino,
1022 struct ext2_inode * inode, int bufsize)
1024 return ext2fs_write_inode2(fs, ino, inode, bufsize, 0);
1027 errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino,
1028 struct ext2_inode *inode)
1030 return ext2fs_write_inode2(fs, ino, inode,
1031 sizeof(struct ext2_inode), 0);
1035 * This function should be called when writing a new inode. It makes
1036 * sure that extra part of large inodes is initialized properly.
1038 errcode_t ext2fs_write_new_inode(ext2_filsys fs, ext2_ino_t ino,
1039 struct ext2_inode *inode)
1041 struct ext2_inode *buf;
1042 int size = EXT2_INODE_SIZE(fs->super);
1043 struct ext2_inode_large *large_inode;
1045 __u32 t = fs->now ? fs->now : time(NULL);
1047 if (!inode->i_ctime)
1049 if (!inode->i_mtime)
1051 if (!inode->i_atime)
1054 if (size == sizeof(struct ext2_inode))
1055 return ext2fs_write_inode_full(fs, ino, inode,
1056 sizeof(struct ext2_inode));
1062 memset(buf, 0, size);
1065 large_inode = (struct ext2_inode_large *) buf;
1066 large_inode->i_extra_isize = sizeof(struct ext2_inode_large) -
1067 EXT2_GOOD_OLD_INODE_SIZE;
1068 if (!large_inode->i_crtime)
1069 large_inode->i_crtime = t;
1071 retval = ext2fs_write_inode_full(fs, ino, buf, size);
1077 errcode_t ext2fs_get_blocks(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks)
1079 struct ext2_inode inode;
1083 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
1085 if (ino > fs->super->s_inodes_count)
1086 return EXT2_ET_BAD_INODE_NUM;
1088 if (fs->get_blocks) {
1089 if (!(*fs->get_blocks)(fs, ino, blocks))
1092 retval = ext2fs_read_inode(fs, ino, &inode);
1095 for (i=0; i < EXT2_N_BLOCKS; i++)
1096 blocks[i] = inode.i_block[i];
1100 errcode_t ext2fs_check_directory(ext2_filsys fs, ext2_ino_t ino)
1102 struct ext2_inode inode;
1105 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
1107 if (ino > fs->super->s_inodes_count)
1108 return EXT2_ET_BAD_INODE_NUM;
1110 if (fs->check_directory) {
1111 retval = (fs->check_directory)(fs, ino);
1112 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
1115 retval = ext2fs_read_inode(fs, ino, &inode);
1118 if (!LINUX_S_ISDIR(inode.i_mode))
1119 return EXT2_ET_NO_DIRECTORY;