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);
149 * If fs->badblocks isn't set, then set it --- since the inode
150 * scanning functions require it.
152 if (fs->badblocks == 0) {
154 * Temporarily save fs->get_blocks and set it to zero,
155 * for compatibility with old e2fsck's.
157 save_get_blocks = fs->get_blocks;
159 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
160 if (retval && fs->badblocks) {
161 ext2fs_badblocks_list_free(fs->badblocks);
164 fs->get_blocks = save_get_blocks;
167 retval = ext2fs_get_mem(sizeof(struct ext2_struct_inode_scan), &scan);
170 memset(scan, 0, sizeof(struct ext2_struct_inode_scan));
172 scan->magic = EXT2_ET_MAGIC_INODE_SCAN;
174 scan->inode_size = EXT2_INODE_SIZE(fs->super);
175 scan->bytes_left = 0;
176 scan->current_group = 0;
177 scan->groups_left = fs->group_desc_count - 1;
178 scan->inode_buffer_blocks = buffer_blocks ? buffer_blocks :
179 EXT2_INODE_SCAN_DEFAULT_BUFFER_BLOCKS;
180 scan->current_block = ext2fs_inode_table_loc(scan->fs,
181 scan->current_group);
182 if (scan->current_block &&
183 ((scan->current_block < fs->super->s_first_data_block) ||
184 (scan->current_block + fs->inode_blocks_per_group - 1 >=
185 ext2fs_blocks_count(fs->super)))) {
186 ext2fs_free_mem(&scan);
187 return EXT2_ET_GDESC_BAD_INODE_TABLE;
190 scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super);
191 scan->blocks_left = scan->fs->inode_blocks_per_group;
192 if (ext2fs_has_group_desc_csum(fs)) {
193 __u32 unused = ext2fs_bg_itable_unused(fs, scan->current_group);
194 if (scan->inodes_left > unused)
195 scan->inodes_left -= unused;
197 scan->inodes_left = 0;
200 (fs->blocksize / scan->inode_size - 1)) *
201 scan->inode_size / fs->blocksize;
203 retval = io_channel_alloc_buf(fs->io, scan->inode_buffer_blocks,
204 &scan->inode_buffer);
205 scan->done_group = 0;
206 scan->done_group_data = 0;
207 scan->bad_block_ptr = 0;
209 ext2fs_free_mem(&scan);
212 retval = ext2fs_get_mem(scan->inode_size + scan->inode_buffer_blocks,
215 ext2fs_free_mem(&scan->inode_buffer);
216 ext2fs_free_mem(&scan);
219 memset(SCAN_BLOCK_STATUS(scan), 0, scan->inode_buffer_blocks);
220 if (scan->fs->badblocks && scan->fs->badblocks->num)
221 scan->scan_flags |= EXT2_SF_CHK_BADBLOCKS;
222 if (ext2fs_has_group_desc_csum(fs))
223 scan->scan_flags |= EXT2_SF_DO_LAZY;
228 void ext2fs_close_inode_scan(ext2_inode_scan scan)
230 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
233 ext2fs_free_mem(&scan->inode_buffer);
234 scan->inode_buffer = NULL;
235 ext2fs_free_mem(&scan->temp_buffer);
236 scan->temp_buffer = NULL;
237 ext2fs_free_mem(&scan);
241 void ext2fs_set_inode_callback(ext2_inode_scan scan,
242 errcode_t (*done_group)(ext2_filsys fs,
243 ext2_inode_scan scan,
246 void *done_group_data)
248 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
251 scan->done_group = done_group;
252 scan->done_group_data = done_group_data;
255 int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags,
260 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
263 old_flags = scan->scan_flags;
264 scan->scan_flags &= ~clear_flags;
265 scan->scan_flags |= set_flags;
270 * This function is called by ext2fs_get_next_inode when it needs to
271 * get ready to read in a new blockgroup.
273 static errcode_t get_next_blockgroup(ext2_inode_scan scan)
275 ext2_filsys fs = scan->fs;
277 scan->current_group++;
280 scan->current_block = ext2fs_inode_table_loc(scan->fs,
281 scan->current_group);
282 scan->current_inode = scan->current_group *
283 EXT2_INODES_PER_GROUP(fs->super);
285 scan->bytes_left = 0;
286 scan->inodes_left = EXT2_INODES_PER_GROUP(fs->super);
287 scan->blocks_left = fs->inode_blocks_per_group;
288 if (ext2fs_has_group_desc_csum(fs)) {
289 __u32 unused = ext2fs_bg_itable_unused(fs, scan->current_group);
290 if (scan->inodes_left > unused)
291 scan->inodes_left -= unused;
293 scan->inodes_left = 0;
296 (fs->blocksize / scan->inode_size - 1)) *
297 scan->inode_size / fs->blocksize;
299 if (scan->current_block &&
300 ((scan->current_block < fs->super->s_first_data_block) ||
301 (scan->current_block + fs->inode_blocks_per_group - 1 >=
302 ext2fs_blocks_count(fs->super))))
303 return EXT2_ET_GDESC_BAD_INODE_TABLE;
307 errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,
310 scan->current_group = group - 1;
311 scan->groups_left = scan->fs->group_desc_count - group;
312 return get_next_blockgroup(scan);
316 * This function is called by get_next_blocks() to check for bad
317 * blocks in the inode table.
319 * This function assumes that badblocks_list->list is sorted in
322 static errcode_t check_for_inode_bad_blocks(ext2_inode_scan scan,
325 blk64_t blk = scan->current_block;
326 badblocks_list bb = scan->fs->badblocks;
329 * If the inode table is missing, then obviously there are no
336 * If the current block is greater than the bad block listed
337 * in the bad block list, then advance the pointer until this
338 * is no longer the case. If we run out of bad blocks, then
339 * we don't need to do any more checking!
341 while (blk > bb->list[scan->bad_block_ptr]) {
342 if (++scan->bad_block_ptr >= bb->num) {
343 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
349 * If the current block is equal to the bad block listed in
350 * the bad block list, then handle that one block specially.
351 * (We could try to handle runs of bad blocks, but that
352 * only increases CPU efficiency by a small amount, at the
353 * expense of a huge expense of code complexity, and for an
354 * uncommon case at that.)
356 if (blk == bb->list[scan->bad_block_ptr]) {
357 scan->scan_flags |= EXT2_SF_BAD_INODE_BLK;
359 if (++scan->bad_block_ptr >= bb->num)
360 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
365 * If there is a bad block in the range that we're about to
366 * read in, adjust the number of blocks to read so that we we
367 * don't read in the bad block. (Then the next block to read
368 * will be the bad block, which is handled in the above case.)
370 if ((blk + *num_blocks) > bb->list[scan->bad_block_ptr])
371 *num_blocks = (int) (bb->list[scan->bad_block_ptr] - blk);
376 static int block_map_looks_insane(ext2_filsys fs,
377 struct ext2_inode_large *inode)
381 /* We're only interested in block mapped files, dirs, and symlinks */
382 if ((inode->i_flags & EXT4_INLINE_DATA_FL) ||
383 (inode->i_flags & EXT4_EXTENTS_FL))
385 if (!LINUX_S_ISREG(inode->i_mode) &&
386 !LINUX_S_ISLNK(inode->i_mode) &&
387 !LINUX_S_ISDIR(inode->i_mode))
389 if (LINUX_S_ISLNK(inode->i_mode) &&
390 EXT2_I_SIZE(inode) <= sizeof(inode->i_block))
393 /* Unused inodes probably aren't insane */
394 if (inode->i_links_count == 0)
397 /* See if more than half the block maps are insane */
398 for (i = 0, bad = 0; i < EXT2_N_BLOCKS; i++)
399 if (inode->i_block[i] != 0 &&
400 (inode->i_block[i] < fs->super->s_first_data_block ||
401 inode->i_block[i] >= ext2fs_blocks_count(fs->super)))
403 return bad > EXT2_N_BLOCKS / 2;
406 static int extent_head_looks_insane(struct ext2_inode_large *inode)
408 if (!(inode->i_flags & EXT4_EXTENTS_FL) ||
409 ext2fs_extent_header_verify(inode->i_block,
410 sizeof(inode->i_block)) == 0)
416 * Check all the inodes that we just read into the buffer. Record what we
417 * find here -- currently, we can observe that all checksums are ok; more
418 * than half the inodes are insane; or no conclusions at all.
420 static void check_inode_block_sanity(ext2_inode_scan scan, blk64_t num_blocks)
422 ext2_ino_t ino, inodes_to_scan;
423 unsigned int badness, checksum_failures;
424 unsigned int inodes_in_buf, inodes_per_block;
426 struct ext2_inode_large *inode;
428 unsigned int blk, bad_csum;
430 if (!(scan->scan_flags & EXT2_SF_WARN_GARBAGE_INODES))
433 inodes_to_scan = scan->inodes_left;
434 inodes_in_buf = num_blocks * scan->fs->blocksize / scan->inode_size;
435 if (inodes_to_scan > inodes_in_buf)
436 inodes_to_scan = inodes_in_buf;
438 p = (char *) scan->inode_buffer;
439 ino = scan->current_inode + 1;
440 checksum_failures = badness = 0;
441 block_status = SCAN_BLOCK_STATUS(scan);
442 memset(block_status, 0, scan->inode_buffer_blocks);
443 inodes_per_block = EXT2_INODES_PER_BLOCK(scan->fs->super);
445 if (inodes_per_block < 2)
448 #ifdef WORDS_BIGENDIAN
449 if (ext2fs_get_mem(EXT2_INODE_SIZE(scan->fs->super), &inode))
453 while (inodes_to_scan > 0) {
454 blk = (p - (char *)scan->inode_buffer) / scan->fs->blocksize;
455 bad_csum = ext2fs_inode_csum_verify(scan->fs, ino,
456 (struct ext2_inode_large *) p) == 0;
458 #ifdef WORDS_BIGENDIAN
459 ext2fs_swap_inode_full(scan->fs,
460 (struct ext2_inode_large *) inode,
461 (struct ext2_inode_large *) p,
462 0, EXT2_INODE_SIZE(scan->fs->super));
464 inode = (struct ext2_inode_large *) p;
467 /* Is this inode insane? */
471 } else if (extent_head_looks_insane(inode) ||
472 block_map_looks_insane(scan->fs, inode))
475 /* If more than half are insane, declare the whole block bad */
476 if (badness > inodes_per_block / 2) {
477 unsigned int ino_adj;
479 block_status[blk] |= IBLOCK_STATUS_INSANE;
480 ino_adj = inodes_per_block -
481 ((ino - 1) % inodes_per_block);
482 if (ino_adj > inodes_to_scan)
483 ino_adj = inodes_to_scan;
484 inodes_to_scan -= ino_adj;
485 p += scan->inode_size * ino_adj;
487 checksum_failures = badness = 0;
491 if ((ino % inodes_per_block) == 0) {
492 if (checksum_failures == 0)
493 block_status[blk] |= IBLOCK_STATUS_CSUMS_OK;
494 checksum_failures = badness = 0;
497 p += scan->inode_size;
501 #ifdef WORDS_BIGENDIAN
502 ext2fs_free_mem(&inode);
507 * This function is called by ext2fs_get_next_inode when it needs to
508 * read in more blocks from the current blockgroup's inode table.
510 static errcode_t get_next_blocks(ext2_inode_scan scan)
516 * Figure out how many blocks to read; we read at most
517 * inode_buffer_blocks, and perhaps less if there aren't that
518 * many blocks left to read.
520 num_blocks = scan->inode_buffer_blocks;
521 if (num_blocks > scan->blocks_left)
522 num_blocks = scan->blocks_left;
525 * If the past block "read" was a bad block, then mark the
526 * left-over extra bytes as also being bad.
528 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) {
529 if (scan->bytes_left)
530 scan->scan_flags |= EXT2_SF_BAD_EXTRA_BYTES;
531 scan->scan_flags &= ~EXT2_SF_BAD_INODE_BLK;
535 * Do inode bad block processing, if necessary.
537 if (scan->scan_flags & EXT2_SF_CHK_BADBLOCKS) {
538 retval = check_for_inode_bad_blocks(scan, &num_blocks);
543 if ((scan->scan_flags & EXT2_SF_BAD_INODE_BLK) ||
544 (scan->current_block == 0)) {
545 memset(scan->inode_buffer, 0,
546 (size_t) num_blocks * scan->fs->blocksize);
548 retval = io_channel_read_blk64(scan->fs->io,
553 return EXT2_ET_NEXT_INODE_READ;
555 check_inode_block_sanity(scan, num_blocks);
557 scan->ptr = scan->inode_buffer;
558 scan->bytes_left = num_blocks * scan->fs->blocksize;
560 scan->blocks_left -= num_blocks;
561 if (scan->current_block)
562 scan->current_block += num_blocks;
569 * Returns 1 if the entire inode_buffer has a non-zero size and
570 * contains all zeros. (Not just deleted inodes, since that means
571 * that part of the inode table was used at one point; we want all
572 * zeros, which means that the inode table is pristine.)
574 static inline int is_empty_scan(ext2_inode_scan scan)
578 if (scan->bytes_left == 0)
581 for (i=0; i < scan->bytes_left; i++)
588 errcode_t ext2fs_get_next_inode_full(ext2_inode_scan scan, ext2_ino_t *ino,
589 struct ext2_inode *inode, int bufsize)
594 struct ext2_inode_large *iptr = (struct ext2_inode_large *)inode;
598 EXT2_CHECK_MAGIC(scan, EXT2_ET_MAGIC_INODE_SCAN);
599 length = EXT2_INODE_SIZE(scan->fs->super);
600 iblock_status = SCAN_BLOCK_STATUS(scan);
603 * Do we need to start reading a new block group?
605 if (scan->inodes_left <= 0) {
607 if (scan->done_group) {
608 retval = (scan->done_group)
609 (scan->fs, scan, scan->current_group,
610 scan->done_group_data);
614 if (scan->groups_left <= 0) {
618 retval = get_next_blockgroup(scan);
623 * These checks are done outside the above if statement so
624 * they can be done for block group #0.
626 if ((scan->scan_flags & EXT2_SF_DO_LAZY) &&
627 (ext2fs_bg_flags_test(scan->fs, scan->current_group, EXT2_BG_INODE_UNINIT)
629 goto force_new_group;
630 if (scan->inodes_left == 0)
631 goto force_new_group;
632 if (scan->current_block == 0) {
633 if (scan->scan_flags & EXT2_SF_SKIP_MISSING_ITABLE) {
634 goto force_new_group;
636 return EXT2_ET_MISSING_INODE_TABLE;
641 * Have we run out of space in the inode buffer? If so, we
642 * need to read in more blocks.
644 if (scan->bytes_left < scan->inode_size) {
645 if (scan->bytes_left)
646 memcpy(scan->temp_buffer, scan->ptr, scan->bytes_left);
647 extra_bytes = scan->bytes_left;
649 retval = get_next_blocks(scan);
654 * XXX test Need check for used inode somehow.
655 * (Note: this is hard.)
657 if (is_empty_scan(scan))
658 goto force_new_group;
662 if (bufsize < length) {
663 retval = ext2fs_get_mem(length, &iptr);
669 iblk = scan->current_inode % EXT2_INODES_PER_GROUP(scan->fs->super) /
670 EXT2_INODES_PER_BLOCK(scan->fs->super) %
671 scan->inode_buffer_blocks;
673 memcpy(scan->temp_buffer+extra_bytes, scan->ptr,
674 scan->inode_size - extra_bytes);
675 scan->ptr += scan->inode_size - extra_bytes;
676 scan->bytes_left -= scan->inode_size - extra_bytes;
678 /* Verify the inode checksum. */
679 if (!(iblock_status[iblk] & IBLOCK_STATUS_CSUMS_OK) &&
680 !(scan->fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
681 !ext2fs_inode_csum_verify(scan->fs, scan->current_inode + 1,
682 (struct ext2_inode_large *)scan->temp_buffer))
683 retval = EXT2_ET_INODE_CSUM_INVALID;
685 #ifdef WORDS_BIGENDIAN
686 memset(iptr, 0, length);
687 ext2fs_swap_inode_full(scan->fs,
688 (struct ext2_inode_large *) iptr,
689 (struct ext2_inode_large *) scan->temp_buffer,
692 memcpy(iptr, scan->temp_buffer, length);
694 if (scan->scan_flags & EXT2_SF_BAD_EXTRA_BYTES)
695 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
696 scan->scan_flags &= ~EXT2_SF_BAD_EXTRA_BYTES;
698 /* Verify the inode checksum. */
699 if (!(iblock_status[iblk] & IBLOCK_STATUS_CSUMS_OK) &&
700 !(scan->fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
701 !ext2fs_inode_csum_verify(scan->fs, scan->current_inode + 1,
702 (struct ext2_inode_large *)scan->ptr))
703 retval = EXT2_ET_INODE_CSUM_INVALID;
705 #ifdef WORDS_BIGENDIAN
706 memset(iptr, 0, length);
707 ext2fs_swap_inode_full(scan->fs,
708 (struct ext2_inode_large *) iptr,
709 (struct ext2_inode_large *) scan->ptr,
712 memcpy(iptr, scan->ptr, length);
714 scan->ptr += scan->inode_size;
715 scan->bytes_left -= scan->inode_size;
716 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK)
717 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
719 if ((iblock_status[iblk] & IBLOCK_STATUS_INSANE) &&
720 (retval == 0 || retval == EXT2_ET_INODE_CSUM_INVALID))
721 retval = EXT2_ET_INODE_IS_GARBAGE;
724 scan->current_inode++;
725 *ino = scan->current_inode;
726 if (iptr != (struct ext2_inode_large *)inode) {
727 memcpy(inode, iptr, bufsize);
728 ext2fs_free_mem(&iptr);
733 errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ext2_ino_t *ino,
734 struct ext2_inode *inode)
736 return ext2fs_get_next_inode_full(scan, ino, inode,
737 sizeof(struct ext2_inode));
741 * Functions to read and write a single inode.
743 errcode_t ext2fs_read_inode2(ext2_filsys fs, ext2_ino_t ino,
744 struct ext2_inode * inode, int bufsize,
749 unsigned long block, offset;
753 int clen, inodes_per_block;
755 int length = EXT2_INODE_SIZE(fs->super);
756 struct ext2_inode_large *iptr;
757 int cache_slot, fail_csum;
759 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
761 /* Check to see if user has an override function */
762 if (fs->read_inode &&
763 ((bufsize == sizeof(struct ext2_inode)) ||
764 (EXT2_INODE_SIZE(fs->super) == sizeof(struct ext2_inode)))) {
765 retval = (fs->read_inode)(fs, ino, inode);
766 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
769 if ((ino == 0) || (ino > fs->super->s_inodes_count))
770 return EXT2_ET_BAD_INODE_NUM;
771 /* Create inode cache if not present */
773 retval = ext2fs_create_inode_cache(fs, 4);
777 /* Check to see if it's in the inode cache */
778 for (i = 0; i < fs->icache->cache_size; i++) {
779 if (fs->icache->cache[i].ino == ino) {
780 memcpy(inode, fs->icache->cache[i].inode,
781 (bufsize > length) ? length : bufsize);
785 if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
786 inodes_per_block = fs->blocksize / EXT2_INODE_SIZE(fs->super);
787 block_nr = ext2fs_le32_to_cpu(fs->image_header->offset_inode) / fs->blocksize;
788 block_nr += (ino - 1) / inodes_per_block;
789 offset = ((ino - 1) % inodes_per_block) *
790 EXT2_INODE_SIZE(fs->super);
793 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
794 if (group > fs->group_desc_count)
795 return EXT2_ET_BAD_INODE_NUM;
796 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
797 EXT2_INODE_SIZE(fs->super);
798 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
799 block_nr = ext2fs_inode_table_loc(fs, group);
801 return EXT2_ET_MISSING_INODE_TABLE;
802 if ((block_nr < fs->super->s_first_data_block) ||
803 (block_nr + fs->inode_blocks_per_group - 1 >=
804 ext2fs_blocks_count(fs->super)))
805 return EXT2_ET_GDESC_BAD_INODE_TABLE;
809 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
811 cache_slot = (fs->icache->cache_last + 1) % fs->icache->cache_size;
812 iptr = (struct ext2_inode_large *)fs->icache->cache[cache_slot].inode;
817 if ((offset + length) > fs->blocksize)
818 clen = fs->blocksize - offset;
820 if (block_nr != fs->icache->buffer_blk) {
821 retval = io_channel_read_blk64(io, block_nr, 1,
825 fs->icache->buffer_blk = block_nr;
828 memcpy(ptr, ((char *) fs->icache->buffer) + (unsigned) offset,
836 length = EXT2_INODE_SIZE(fs->super);
838 /* Verify the inode checksum. */
839 fail_csum = !ext2fs_inode_csum_verify(fs, ino, iptr);
841 #ifdef WORDS_BIGENDIAN
842 ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) iptr,
843 (struct ext2_inode_large *) iptr,
847 /* Update the inode cache bookkeeping */
849 fs->icache->cache_last = cache_slot;
850 fs->icache->cache[cache_slot].ino = ino;
852 memcpy(inode, iptr, (bufsize > length) ? length : bufsize);
854 if (!(fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
855 !(flags & READ_INODE_NOCSUM) && fail_csum)
856 return EXT2_ET_INODE_CSUM_INVALID;
861 errcode_t ext2fs_read_inode_full(ext2_filsys fs, ext2_ino_t ino,
862 struct ext2_inode * inode, int bufsize)
864 return ext2fs_read_inode2(fs, ino, inode, bufsize, 0);
867 errcode_t ext2fs_read_inode(ext2_filsys fs, ext2_ino_t ino,
868 struct ext2_inode * inode)
870 return ext2fs_read_inode2(fs, ino, inode,
871 sizeof(struct ext2_inode), 0);
874 errcode_t ext2fs_write_inode2(ext2_filsys fs, ext2_ino_t ino,
875 struct ext2_inode * inode, int bufsize,
880 unsigned long block, offset;
881 errcode_t retval = 0;
882 struct ext2_inode_large *w_inode;
886 int length = EXT2_INODE_SIZE(fs->super);
888 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
890 /* Check to see if user provided an override function */
891 if (fs->write_inode) {
892 retval = (fs->write_inode)(fs, ino, inode);
893 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
897 if ((ino == 0) || (ino > fs->super->s_inodes_count))
898 return EXT2_ET_BAD_INODE_NUM;
900 /* Prepare our shadow buffer for read/modify/byteswap/write */
901 retval = ext2fs_get_mem(length, &w_inode);
905 if (bufsize < length) {
906 retval = ext2fs_read_inode2(fs, ino,
907 (struct ext2_inode *)w_inode,
908 length, READ_INODE_NOCSUM);
913 /* Check to see if the inode cache needs to be updated */
915 for (i=0; i < fs->icache->cache_size; i++) {
916 if (fs->icache->cache[i].ino == ino) {
917 memcpy(fs->icache->cache[i].inode, inode,
918 (bufsize > length) ? length : bufsize);
923 retval = ext2fs_create_inode_cache(fs, 4);
927 memcpy(w_inode, inode, (bufsize > length) ? length : bufsize);
929 if (!(fs->flags & EXT2_FLAG_RW)) {
930 retval = EXT2_ET_RO_FILSYS;
934 #ifdef WORDS_BIGENDIAN
935 ext2fs_swap_inode_full(fs, w_inode, w_inode, 1, length);
938 if ((flags & WRITE_INODE_NOCSUM) == 0) {
939 retval = ext2fs_inode_csum_set(fs, ino, w_inode);
944 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
945 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
946 EXT2_INODE_SIZE(fs->super);
947 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
948 block_nr = ext2fs_inode_table_loc(fs, (unsigned) group);
950 retval = EXT2_ET_MISSING_INODE_TABLE;
953 if ((block_nr < fs->super->s_first_data_block) ||
954 (block_nr + fs->inode_blocks_per_group - 1 >=
955 ext2fs_blocks_count(fs->super))) {
956 retval = EXT2_ET_GDESC_BAD_INODE_TABLE;
961 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
963 ptr = (char *) w_inode;
967 if ((offset + length) > fs->blocksize)
968 clen = fs->blocksize - offset;
970 if (fs->icache->buffer_blk != block_nr) {
971 retval = io_channel_read_blk64(fs->io, block_nr, 1,
975 fs->icache->buffer_blk = block_nr;
979 memcpy((char *) fs->icache->buffer + (unsigned) offset,
982 retval = io_channel_write_blk64(fs->io, block_nr, 1,
993 fs->flags |= EXT2_FLAG_CHANGED;
995 ext2fs_free_mem(&w_inode);
999 errcode_t ext2fs_write_inode_full(ext2_filsys fs, ext2_ino_t ino,
1000 struct ext2_inode * inode, int bufsize)
1002 return ext2fs_write_inode2(fs, ino, inode, bufsize, 0);
1005 errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino,
1006 struct ext2_inode *inode)
1008 return ext2fs_write_inode2(fs, ino, inode,
1009 sizeof(struct ext2_inode), 0);
1013 * This function should be called when writing a new inode. It makes
1014 * sure that extra part of large inodes is initialized properly.
1016 errcode_t ext2fs_write_new_inode(ext2_filsys fs, ext2_ino_t ino,
1017 struct ext2_inode *inode)
1019 struct ext2_inode *buf;
1020 int size = EXT2_INODE_SIZE(fs->super);
1021 struct ext2_inode_large *large_inode;
1023 __u32 t = fs->now ? fs->now : time(NULL);
1025 if (!inode->i_ctime)
1027 if (!inode->i_mtime)
1029 if (!inode->i_atime)
1032 if (size == sizeof(struct ext2_inode))
1033 return ext2fs_write_inode_full(fs, ino, inode,
1034 sizeof(struct ext2_inode));
1040 memset(buf, 0, size);
1043 large_inode = (struct ext2_inode_large *) buf;
1044 large_inode->i_extra_isize = sizeof(struct ext2_inode_large) -
1045 EXT2_GOOD_OLD_INODE_SIZE;
1046 if (!large_inode->i_crtime)
1047 large_inode->i_crtime = t;
1049 retval = ext2fs_write_inode_full(fs, ino, buf, size);
1055 errcode_t ext2fs_get_blocks(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks)
1057 struct ext2_inode inode;
1061 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
1063 if (ino > fs->super->s_inodes_count)
1064 return EXT2_ET_BAD_INODE_NUM;
1066 if (fs->get_blocks) {
1067 if (!(*fs->get_blocks)(fs, ino, blocks))
1070 retval = ext2fs_read_inode(fs, ino, &inode);
1073 for (i=0; i < EXT2_N_BLOCKS; i++)
1074 blocks[i] = inode.i_block[i];
1078 errcode_t ext2fs_check_directory(ext2_filsys fs, ext2_ino_t ino)
1080 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->check_directory) {
1089 retval = (fs->check_directory)(fs, ino);
1090 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
1093 retval = ext2fs_read_inode(fs, ino, &inode);
1096 if (!LINUX_S_ISDIR(inode.i_mode))
1097 return EXT2_ET_NO_DIRECTORY;