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 * Temporarly 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 : 8;
179 scan->current_block = ext2fs_inode_table_loc(scan->fs,
180 scan->current_group);
181 scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super);
182 scan->blocks_left = scan->fs->inode_blocks_per_group;
183 if (ext2fs_has_group_desc_csum(fs)) {
185 ext2fs_bg_itable_unused(fs, scan->current_group);
188 (fs->blocksize / scan->inode_size - 1)) *
189 scan->inode_size / fs->blocksize;
191 retval = io_channel_alloc_buf(fs->io, scan->inode_buffer_blocks,
192 &scan->inode_buffer);
193 scan->done_group = 0;
194 scan->done_group_data = 0;
195 scan->bad_block_ptr = 0;
197 ext2fs_free_mem(&scan);
200 retval = ext2fs_get_mem(scan->inode_size + scan->inode_buffer_blocks,
203 ext2fs_free_mem(&scan->inode_buffer);
204 ext2fs_free_mem(&scan);
207 memset(SCAN_BLOCK_STATUS(scan), 0, scan->inode_buffer_blocks);
208 if (scan->fs->badblocks && scan->fs->badblocks->num)
209 scan->scan_flags |= EXT2_SF_CHK_BADBLOCKS;
210 if (ext2fs_has_group_desc_csum(fs))
211 scan->scan_flags |= EXT2_SF_DO_LAZY;
216 void ext2fs_close_inode_scan(ext2_inode_scan scan)
218 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
221 ext2fs_free_mem(&scan->inode_buffer);
222 scan->inode_buffer = NULL;
223 ext2fs_free_mem(&scan->temp_buffer);
224 scan->temp_buffer = NULL;
225 ext2fs_free_mem(&scan);
229 void ext2fs_set_inode_callback(ext2_inode_scan scan,
230 errcode_t (*done_group)(ext2_filsys fs,
231 ext2_inode_scan scan,
234 void *done_group_data)
236 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
239 scan->done_group = done_group;
240 scan->done_group_data = done_group_data;
243 int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags,
248 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
251 old_flags = scan->scan_flags;
252 scan->scan_flags &= ~clear_flags;
253 scan->scan_flags |= set_flags;
258 * This function is called by ext2fs_get_next_inode when it needs to
259 * get ready to read in a new blockgroup.
261 static errcode_t get_next_blockgroup(ext2_inode_scan scan)
263 ext2_filsys fs = scan->fs;
265 scan->current_group++;
268 scan->current_block = ext2fs_inode_table_loc(scan->fs,
269 scan->current_group);
270 scan->current_inode = scan->current_group *
271 EXT2_INODES_PER_GROUP(fs->super);
273 scan->bytes_left = 0;
274 scan->inodes_left = EXT2_INODES_PER_GROUP(fs->super);
275 scan->blocks_left = fs->inode_blocks_per_group;
276 if (ext2fs_has_group_desc_csum(fs)) {
278 ext2fs_bg_itable_unused(fs, scan->current_group);
281 (fs->blocksize / scan->inode_size - 1)) *
282 scan->inode_size / fs->blocksize;
288 errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,
291 scan->current_group = group - 1;
292 scan->groups_left = scan->fs->group_desc_count - group;
293 return get_next_blockgroup(scan);
297 * This function is called by get_next_blocks() to check for bad
298 * blocks in the inode table.
300 * This function assumes that badblocks_list->list is sorted in
303 static errcode_t check_for_inode_bad_blocks(ext2_inode_scan scan,
306 blk64_t blk = scan->current_block;
307 badblocks_list bb = scan->fs->badblocks;
310 * If the inode table is missing, then obviously there are no
317 * If the current block is greater than the bad block listed
318 * in the bad block list, then advance the pointer until this
319 * is no longer the case. If we run out of bad blocks, then
320 * we don't need to do any more checking!
322 while (blk > bb->list[scan->bad_block_ptr]) {
323 if (++scan->bad_block_ptr >= bb->num) {
324 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
330 * If the current block is equal to the bad block listed in
331 * the bad block list, then handle that one block specially.
332 * (We could try to handle runs of bad blocks, but that
333 * only increases CPU efficiency by a small amount, at the
334 * expense of a huge expense of code complexity, and for an
335 * uncommon case at that.)
337 if (blk == bb->list[scan->bad_block_ptr]) {
338 scan->scan_flags |= EXT2_SF_BAD_INODE_BLK;
340 if (++scan->bad_block_ptr >= bb->num)
341 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
346 * If there is a bad block in the range that we're about to
347 * read in, adjust the number of blocks to read so that we we
348 * don't read in the bad block. (Then the next block to read
349 * will be the bad block, which is handled in the above case.)
351 if ((blk + *num_blocks) > bb->list[scan->bad_block_ptr])
352 *num_blocks = (int) (bb->list[scan->bad_block_ptr] - blk);
357 static int block_map_looks_insane(ext2_filsys fs,
358 struct ext2_inode_large *inode)
362 /* We're only interested in block mapped files, dirs, and symlinks */
363 if ((inode->i_flags & EXT4_INLINE_DATA_FL) ||
364 (inode->i_flags & EXT4_EXTENTS_FL))
366 if (!LINUX_S_ISREG(inode->i_mode) &&
367 !LINUX_S_ISLNK(inode->i_mode) &&
368 !LINUX_S_ISDIR(inode->i_mode))
370 if (LINUX_S_ISLNK(inode->i_mode) &&
371 EXT2_I_SIZE(inode) <= sizeof(inode->i_block))
374 /* Unused inodes probably aren't insane */
375 if (inode->i_links_count == 0)
378 /* See if more than half the block maps are insane */
379 for (i = 0, bad = 0; i < EXT2_N_BLOCKS; i++)
380 if (inode->i_block[i] != 0 &&
381 (inode->i_block[i] < fs->super->s_first_data_block ||
382 inode->i_block[i] >= ext2fs_blocks_count(fs->super)))
384 return bad > EXT2_N_BLOCKS / 2;
387 static int extent_head_looks_insane(struct ext2_inode_large *inode)
389 if (!(inode->i_flags & EXT4_EXTENTS_FL) ||
390 ext2fs_extent_header_verify(inode->i_block,
391 sizeof(inode->i_block)) == 0)
397 * Check all the inodes that we just read into the buffer. Record what we
398 * find here -- currently, we can observe that all checksums are ok; more
399 * than half the inodes are insane; or no conclusions at all.
401 static void check_inode_block_sanity(ext2_inode_scan scan, blk64_t num_blocks)
403 ext2_ino_t ino, inodes_to_scan;
404 unsigned int badness, checksum_failures;
405 unsigned int inodes_in_buf, inodes_per_block;
407 struct ext2_inode_large *inode;
411 if (!(scan->scan_flags & EXT2_SF_WARN_GARBAGE_INODES))
414 inodes_to_scan = scan->inodes_left;
415 inodes_in_buf = num_blocks * scan->fs->blocksize / scan->inode_size;
416 if (inodes_to_scan > inodes_in_buf)
417 inodes_to_scan = inodes_in_buf;
419 p = scan->inode_buffer;
420 ino = scan->current_inode + 1;
421 checksum_failures = badness = 0;
422 block_status = SCAN_BLOCK_STATUS(scan);
423 memset(block_status, 0, scan->inode_buffer_blocks);
424 inodes_per_block = EXT2_INODES_PER_BLOCK(scan->fs->super);
426 while (inodes_to_scan > 0) {
427 blk = (p - (void *)scan->inode_buffer) / scan->fs->blocksize;
430 /* Is this inode insane? */
431 if (!ext2fs_inode_csum_verify(scan->fs, ino, inode)) {
434 } else if (extent_head_looks_insane(inode) ||
435 block_map_looks_insane(scan->fs, inode))
438 /* If more than half are insane, declare the whole block bad */
439 if (badness > inodes_per_block / 2) {
440 unsigned int ino_adj;
442 block_status[blk] |= IBLOCK_STATUS_INSANE;
443 ino_adj = inodes_per_block -
444 ((ino - 1) % inodes_per_block);
445 if (ino_adj > inodes_to_scan)
446 ino_adj = inodes_to_scan;
447 inodes_to_scan -= ino_adj;
448 p += scan->inode_size * ino_adj;
450 checksum_failures = badness = 0;
454 if ((ino % inodes_per_block) == 0) {
455 if (checksum_failures == 0)
456 block_status[blk] |= IBLOCK_STATUS_CSUMS_OK;
457 checksum_failures = badness = 0;
460 p += scan->inode_size;
466 * This function is called by ext2fs_get_next_inode when it needs to
467 * read in more blocks from the current blockgroup's inode table.
469 static errcode_t get_next_blocks(ext2_inode_scan scan)
475 * Figure out how many blocks to read; we read at most
476 * inode_buffer_blocks, and perhaps less if there aren't that
477 * many blocks left to read.
479 num_blocks = scan->inode_buffer_blocks;
480 if (num_blocks > scan->blocks_left)
481 num_blocks = scan->blocks_left;
484 * If the past block "read" was a bad block, then mark the
485 * left-over extra bytes as also being bad.
487 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) {
488 if (scan->bytes_left)
489 scan->scan_flags |= EXT2_SF_BAD_EXTRA_BYTES;
490 scan->scan_flags &= ~EXT2_SF_BAD_INODE_BLK;
494 * Do inode bad block processing, if necessary.
496 if (scan->scan_flags & EXT2_SF_CHK_BADBLOCKS) {
497 retval = check_for_inode_bad_blocks(scan, &num_blocks);
502 if ((scan->scan_flags & EXT2_SF_BAD_INODE_BLK) ||
503 (scan->current_block == 0)) {
504 memset(scan->inode_buffer, 0,
505 (size_t) num_blocks * scan->fs->blocksize);
507 retval = io_channel_read_blk64(scan->fs->io,
512 return EXT2_ET_NEXT_INODE_READ;
514 check_inode_block_sanity(scan, num_blocks);
516 scan->ptr = scan->inode_buffer;
517 scan->bytes_left = num_blocks * scan->fs->blocksize;
519 scan->blocks_left -= num_blocks;
520 if (scan->current_block)
521 scan->current_block += num_blocks;
528 * Returns 1 if the entire inode_buffer has a non-zero size and
529 * contains all zeros. (Not just deleted inodes, since that means
530 * that part of the inode table was used at one point; we want all
531 * zeros, which means that the inode table is pristine.)
533 static inline int is_empty_scan(ext2_inode_scan scan)
537 if (scan->bytes_left == 0)
540 for (i=0; i < scan->bytes_left; i++)
547 errcode_t ext2fs_get_next_inode_full(ext2_inode_scan scan, ext2_ino_t *ino,
548 struct ext2_inode *inode, int bufsize)
553 struct ext2_inode_large *iptr = (struct ext2_inode_large *)inode;
557 EXT2_CHECK_MAGIC(scan, EXT2_ET_MAGIC_INODE_SCAN);
558 length = EXT2_INODE_SIZE(scan->fs->super);
559 iblock_status = SCAN_BLOCK_STATUS(scan);
562 * Do we need to start reading a new block group?
564 if (scan->inodes_left <= 0) {
566 if (scan->done_group) {
567 retval = (scan->done_group)
568 (scan->fs, scan, scan->current_group,
569 scan->done_group_data);
573 if (scan->groups_left <= 0) {
577 retval = get_next_blockgroup(scan);
582 * These checks are done outside the above if statement so
583 * they can be done for block group #0.
585 if ((scan->scan_flags & EXT2_SF_DO_LAZY) &&
586 (ext2fs_bg_flags_test(scan->fs, scan->current_group, EXT2_BG_INODE_UNINIT)
588 goto force_new_group;
589 if (scan->inodes_left == 0)
590 goto force_new_group;
591 if (scan->current_block == 0) {
592 if (scan->scan_flags & EXT2_SF_SKIP_MISSING_ITABLE) {
593 goto force_new_group;
595 return EXT2_ET_MISSING_INODE_TABLE;
600 * Have we run out of space in the inode buffer? If so, we
601 * need to read in more blocks.
603 if (scan->bytes_left < scan->inode_size) {
604 memcpy(scan->temp_buffer, scan->ptr, scan->bytes_left);
605 extra_bytes = scan->bytes_left;
607 retval = get_next_blocks(scan);
612 * XXX test Need check for used inode somehow.
613 * (Note: this is hard.)
615 if (is_empty_scan(scan))
616 goto force_new_group;
620 if (bufsize < length) {
621 retval = ext2fs_get_mem(length, &iptr);
627 iblk = scan->current_inode % EXT2_INODES_PER_GROUP(scan->fs->super) /
628 EXT2_INODES_PER_BLOCK(scan->fs->super) %
629 scan->inode_buffer_blocks;
631 memcpy(scan->temp_buffer+extra_bytes, scan->ptr,
632 scan->inode_size - extra_bytes);
633 scan->ptr += scan->inode_size - extra_bytes;
634 scan->bytes_left -= scan->inode_size - extra_bytes;
636 /* Verify the inode checksum. */
637 if (!(iblock_status[iblk] & IBLOCK_STATUS_CSUMS_OK) &&
638 !(scan->fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
639 !ext2fs_inode_csum_verify(scan->fs, scan->current_inode + 1,
640 (struct ext2_inode_large *)scan->temp_buffer))
641 retval = EXT2_ET_INODE_CSUM_INVALID;
643 #ifdef WORDS_BIGENDIAN
644 memset(iptr, 0, length);
645 ext2fs_swap_inode_full(scan->fs,
646 (struct ext2_inode_large *) iptr,
647 (struct ext2_inode_large *) scan->temp_buffer,
650 memcpy(iptr, scan->temp_buffer, length);
652 if (scan->scan_flags & EXT2_SF_BAD_EXTRA_BYTES)
653 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
654 scan->scan_flags &= ~EXT2_SF_BAD_EXTRA_BYTES;
656 /* Verify the inode checksum. */
657 if (!(iblock_status[iblk] & IBLOCK_STATUS_CSUMS_OK) &&
658 !(scan->fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
659 !ext2fs_inode_csum_verify(scan->fs, scan->current_inode + 1,
660 (struct ext2_inode_large *)scan->ptr))
661 retval = EXT2_ET_INODE_CSUM_INVALID;
663 #ifdef WORDS_BIGENDIAN
664 memset(iptr, 0, length);
665 ext2fs_swap_inode_full(scan->fs,
666 (struct ext2_inode_large *) iptr,
667 (struct ext2_inode_large *) scan->ptr,
670 memcpy(iptr, scan->ptr, length);
672 scan->ptr += scan->inode_size;
673 scan->bytes_left -= scan->inode_size;
674 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK)
675 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
677 if ((iblock_status[iblk] & IBLOCK_STATUS_INSANE) &&
678 (retval == 0 || retval == EXT2_ET_INODE_CSUM_INVALID))
679 retval = EXT2_ET_INODE_IS_GARBAGE;
682 scan->current_inode++;
683 *ino = scan->current_inode;
684 if (iptr != (struct ext2_inode_large *)inode) {
685 memcpy(inode, iptr, bufsize);
686 ext2fs_free_mem(&iptr);
691 errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ext2_ino_t *ino,
692 struct ext2_inode *inode)
694 return ext2fs_get_next_inode_full(scan, ino, inode,
695 sizeof(struct ext2_inode));
699 * Functions to read and write a single inode.
701 errcode_t ext2fs_read_inode_full(ext2_filsys fs, ext2_ino_t ino,
702 struct ext2_inode * inode, int bufsize)
705 unsigned long group, block, offset;
708 int clen, i, inodes_per_block;
710 int length = EXT2_INODE_SIZE(fs->super);
711 struct ext2_inode_large *iptr;
714 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
716 /* Check to see if user has an override function */
717 if (fs->read_inode &&
718 ((bufsize == sizeof(struct ext2_inode)) ||
719 (EXT2_INODE_SIZE(fs->super) == sizeof(struct ext2_inode)))) {
720 retval = (fs->read_inode)(fs, ino, inode);
721 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
724 if ((ino == 0) || (ino > fs->super->s_inodes_count))
725 return EXT2_ET_BAD_INODE_NUM;
726 /* Create inode cache if not present */
728 retval = ext2fs_create_inode_cache(fs, 4);
732 /* Check to see if it's in the inode cache */
733 for (i = 0; i < fs->icache->cache_size; i++) {
734 if (fs->icache->cache[i].ino == ino) {
735 memcpy(inode, fs->icache->cache[i].inode,
736 (bufsize > length) ? length : bufsize);
740 if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
741 inodes_per_block = fs->blocksize / EXT2_INODE_SIZE(fs->super);
742 block_nr = fs->image_header->offset_inode / fs->blocksize;
743 block_nr += (ino - 1) / inodes_per_block;
744 offset = ((ino - 1) % inodes_per_block) *
745 EXT2_INODE_SIZE(fs->super);
748 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
749 if (group > fs->group_desc_count)
750 return EXT2_ET_BAD_INODE_NUM;
751 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
752 EXT2_INODE_SIZE(fs->super);
753 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
754 if (!ext2fs_inode_table_loc(fs, (unsigned) group))
755 return EXT2_ET_MISSING_INODE_TABLE;
756 block_nr = ext2fs_inode_table_loc(fs, group) +
760 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
762 cache_slot = (fs->icache->cache_last + 1) % fs->icache->cache_size;
763 iptr = (struct ext2_inode_large *)fs->icache->cache[cache_slot].inode;
768 if ((offset + length) > fs->blocksize)
769 clen = fs->blocksize - offset;
771 if (block_nr != fs->icache->buffer_blk) {
772 retval = io_channel_read_blk64(io, block_nr, 1,
776 fs->icache->buffer_blk = block_nr;
779 memcpy(ptr, ((char *) fs->icache->buffer) + (unsigned) offset,
787 length = EXT2_INODE_SIZE(fs->super);
789 /* Verify the inode checksum. */
790 if (!(fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
791 !ext2fs_inode_csum_verify(fs, ino, iptr))
792 return EXT2_ET_INODE_CSUM_INVALID;
794 #ifdef WORDS_BIGENDIAN
795 ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) iptr,
796 (struct ext2_inode_large *) iptr,
800 /* Update the inode cache bookkeeping */
801 fs->icache->cache_last = cache_slot;
802 fs->icache->cache[cache_slot].ino = ino;
803 memcpy(inode, iptr, (bufsize > length) ? length : bufsize);
808 errcode_t ext2fs_read_inode(ext2_filsys fs, ext2_ino_t ino,
809 struct ext2_inode * inode)
811 return ext2fs_read_inode_full(fs, ino, inode,
812 sizeof(struct ext2_inode));
815 errcode_t ext2fs_write_inode_full(ext2_filsys fs, ext2_ino_t ino,
816 struct ext2_inode * inode, int bufsize)
819 unsigned long group, block, offset;
820 errcode_t retval = 0;
821 struct ext2_inode_large *w_inode;
824 int length = EXT2_INODE_SIZE(fs->super);
826 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
828 /* Check to see if user provided an override function */
829 if (fs->write_inode) {
830 retval = (fs->write_inode)(fs, ino, inode);
831 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
835 if ((ino == 0) || (ino > fs->super->s_inodes_count))
836 return EXT2_ET_BAD_INODE_NUM;
838 /* Prepare our shadow buffer for read/modify/byteswap/write */
839 retval = ext2fs_get_mem(length, &w_inode);
843 if (bufsize < length) {
844 int old_flags = fs->flags;
845 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
846 retval = ext2fs_read_inode_full(fs, ino,
847 (struct ext2_inode *)w_inode,
849 fs->flags = old_flags;
854 /* Check to see if the inode cache needs to be updated */
856 for (i=0; i < fs->icache->cache_size; i++) {
857 if (fs->icache->cache[i].ino == ino) {
858 memcpy(fs->icache->cache[i].inode, inode,
859 (bufsize > length) ? length : bufsize);
864 retval = ext2fs_create_inode_cache(fs, 4);
868 memcpy(w_inode, inode, (bufsize > length) ? length : bufsize);
870 if (!(fs->flags & EXT2_FLAG_RW)) {
871 retval = EXT2_ET_RO_FILSYS;
875 #ifdef WORDS_BIGENDIAN
876 ext2fs_swap_inode_full(fs, w_inode, w_inode, 1, length);
879 retval = ext2fs_inode_csum_set(fs, ino, w_inode);
883 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
884 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
885 EXT2_INODE_SIZE(fs->super);
886 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
887 if (!ext2fs_inode_table_loc(fs, (unsigned) group)) {
888 retval = EXT2_ET_MISSING_INODE_TABLE;
891 block_nr = ext2fs_inode_table_loc(fs, (unsigned) group) + block;
893 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
895 ptr = (char *) w_inode;
899 if ((offset + length) > fs->blocksize)
900 clen = fs->blocksize - offset;
902 if (fs->icache->buffer_blk != block_nr) {
903 retval = io_channel_read_blk64(fs->io, block_nr, 1,
907 fs->icache->buffer_blk = block_nr;
911 memcpy((char *) fs->icache->buffer + (unsigned) offset,
914 retval = io_channel_write_blk64(fs->io, block_nr, 1,
925 fs->flags |= EXT2_FLAG_CHANGED;
927 ext2fs_free_mem(&w_inode);
931 errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino,
932 struct ext2_inode *inode)
934 return ext2fs_write_inode_full(fs, ino, inode,
935 sizeof(struct ext2_inode));
939 * This function should be called when writing a new inode. It makes
940 * sure that extra part of large inodes is initialized properly.
942 errcode_t ext2fs_write_new_inode(ext2_filsys fs, ext2_ino_t ino,
943 struct ext2_inode *inode)
945 struct ext2_inode *buf;
946 int size = EXT2_INODE_SIZE(fs->super);
947 struct ext2_inode_large *large_inode;
949 __u32 t = fs->now ? fs->now : time(NULL);
958 if (size == sizeof(struct ext2_inode))
959 return ext2fs_write_inode_full(fs, ino, inode,
960 sizeof(struct ext2_inode));
966 memset(buf, 0, size);
969 large_inode = (struct ext2_inode_large *) buf;
970 large_inode->i_extra_isize = sizeof(struct ext2_inode_large) -
971 EXT2_GOOD_OLD_INODE_SIZE;
972 if (!large_inode->i_crtime)
973 large_inode->i_crtime = t;
975 retval = ext2fs_write_inode_full(fs, ino, buf, size);
981 errcode_t ext2fs_get_blocks(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks)
983 struct ext2_inode inode;
987 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
989 if (ino > fs->super->s_inodes_count)
990 return EXT2_ET_BAD_INODE_NUM;
992 if (fs->get_blocks) {
993 if (!(*fs->get_blocks)(fs, ino, blocks))
996 retval = ext2fs_read_inode(fs, ino, &inode);
999 for (i=0; i < EXT2_N_BLOCKS; i++)
1000 blocks[i] = inode.i_block[i];
1004 errcode_t ext2fs_check_directory(ext2_filsys fs, ext2_ino_t ino)
1006 struct ext2_inode inode;
1009 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
1011 if (ino > fs->super->s_inodes_count)
1012 return EXT2_ET_BAD_INODE_NUM;
1014 if (fs->check_directory) {
1015 retval = (fs->check_directory)(fs, ino);
1016 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
1019 retval = ext2fs_read_inode(fs, ino, &inode);
1022 if (!LINUX_S_ISDIR(inode.i_mode))
1023 return EXT2_ET_NO_DIRECTORY;