Home | History | Annotate | Download | only in ext2fs
      1 /*
      2  * inode.c --- utility routines to read and write inodes
      3  *
      4  * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
      5  *
      6  * %Begin-Header%
      7  * This file may be redistributed under the terms of the GNU Public
      8  * License.
      9  * %End-Header%
     10  */
     11 
     12 #include <stdio.h>
     13 #include <string.h>
     14 #if HAVE_UNISTD_H
     15 #include <unistd.h>
     16 #endif
     17 #if HAVE_ERRNO_H
     18 #include <errno.h>
     19 #endif
     20 #include <time.h>
     21 #if HAVE_SYS_STAT_H
     22 #include <sys/stat.h>
     23 #endif
     24 #if HAVE_SYS_TYPES_H
     25 #include <sys/types.h>
     26 #endif
     27 
     28 #include "ext2_fs.h"
     29 #include "ext2fsP.h"
     30 #include "e2image.h"
     31 
     32 struct ext2_struct_inode_scan {
     33 	errcode_t		magic;
     34 	ext2_filsys		fs;
     35 	ext2_ino_t		current_inode;
     36 	blk_t			current_block;
     37 	dgrp_t			current_group;
     38 	ext2_ino_t		inodes_left;
     39 	blk_t			blocks_left;
     40 	dgrp_t			groups_left;
     41 	blk_t			inode_buffer_blocks;
     42 	char *			inode_buffer;
     43 	int			inode_size;
     44 	char *			ptr;
     45 	int			bytes_left;
     46 	char			*temp_buffer;
     47 	errcode_t		(*done_group)(ext2_filsys fs,
     48 					      ext2_inode_scan scan,
     49 					      dgrp_t group,
     50 					      void * priv_data);
     51 	void *			done_group_data;
     52 	int			bad_block_ptr;
     53 	int			scan_flags;
     54 	int			reserved[6];
     55 };
     56 
     57 /*
     58  * This routine flushes the icache, if it exists.
     59  */
     60 errcode_t ext2fs_flush_icache(ext2_filsys fs)
     61 {
     62 	int	i;
     63 
     64 	if (!fs->icache)
     65 		return 0;
     66 
     67 	for (i=0; i < fs->icache->cache_size; i++)
     68 		fs->icache->cache[i].ino = 0;
     69 
     70 	fs->icache->buffer_blk = 0;
     71 	return 0;
     72 }
     73 
     74 static errcode_t create_icache(ext2_filsys fs)
     75 {
     76 	errcode_t	retval;
     77 
     78 	if (fs->icache)
     79 		return 0;
     80 	retval = ext2fs_get_mem(sizeof(struct ext2_inode_cache), &fs->icache);
     81 	if (retval)
     82 		return retval;
     83 
     84 	memset(fs->icache, 0, sizeof(struct ext2_inode_cache));
     85 	retval = ext2fs_get_mem(fs->blocksize, &fs->icache->buffer);
     86 	if (retval) {
     87 		ext2fs_free_mem(&fs->icache);
     88 		return retval;
     89 	}
     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),
     96 				  &fs->icache->cache);
     97 	if (retval) {
     98 		ext2fs_free_mem(&fs->icache->buffer);
     99 		ext2fs_free_mem(&fs->icache);
    100 		return retval;
    101 	}
    102 	ext2fs_flush_icache(fs);
    103 	return 0;
    104 }
    105 
    106 errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks,
    107 				 ext2_inode_scan *ret_scan)
    108 {
    109 	ext2_inode_scan	scan;
    110 	errcode_t	retval;
    111 	errcode_t (*save_get_blocks)(ext2_filsys f, ext2_ino_t ino, blk_t *blocks);
    112 
    113 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
    114 
    115 	/*
    116 	 * If fs->badblocks isn't set, then set it --- since the inode
    117 	 * scanning functions require it.
    118 	 */
    119 	if (fs->badblocks == 0) {
    120 		/*
    121 		 * Temporarly save fs->get_blocks and set it to zero,
    122 		 * for compatibility with old e2fsck's.
    123 		 */
    124 		save_get_blocks = fs->get_blocks;
    125 		fs->get_blocks = 0;
    126 		retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
    127 		if (retval && fs->badblocks) {
    128 			ext2fs_badblocks_list_free(fs->badblocks);
    129 			fs->badblocks = 0;
    130 		}
    131 		fs->get_blocks = save_get_blocks;
    132 	}
    133 
    134 	retval = ext2fs_get_mem(sizeof(struct ext2_struct_inode_scan), &scan);
    135 	if (retval)
    136 		return retval;
    137 	memset(scan, 0, sizeof(struct ext2_struct_inode_scan));
    138 
    139 	scan->magic = EXT2_ET_MAGIC_INODE_SCAN;
    140 	scan->fs = fs;
    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)) {
    152 		scan->inodes_left -=
    153 			fs->group_desc[scan->current_group].bg_itable_unused;
    154 		scan->blocks_left =
    155 			(scan->inodes_left +
    156 			 (fs->blocksize / scan->inode_size - 1)) *
    157 			scan->inode_size / fs->blocksize;
    158 	}
    159 	retval = ext2fs_get_array(scan->inode_buffer_blocks,
    160 					  fs->blocksize,
    161 				&scan->inode_buffer);
    162 	scan->done_group = 0;
    163 	scan->done_group_data = 0;
    164 	scan->bad_block_ptr = 0;
    165 	if (retval) {
    166 		ext2fs_free_mem(&scan);
    167 		return retval;
    168 	}
    169 	retval = ext2fs_get_mem(scan->inode_size, &scan->temp_buffer);
    170 	if (retval) {
    171 		ext2fs_free_mem(&scan->inode_buffer);
    172 		ext2fs_free_mem(&scan);
    173 		return retval;
    174 	}
    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;
    180 	*ret_scan = scan;
    181 	return 0;
    182 }
    183 
    184 void ext2fs_close_inode_scan(ext2_inode_scan scan)
    185 {
    186 	if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
    187 		return;
    188 
    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);
    194 	return;
    195 }
    196 
    197 void ext2fs_set_inode_callback(ext2_inode_scan scan,
    198 			       errcode_t (*done_group)(ext2_filsys fs,
    199 						       ext2_inode_scan scan,
    200 						       dgrp_t group,
    201 						       void * priv_data),
    202 			       void *done_group_data)
    203 {
    204 	if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
    205 		return;
    206 
    207 	scan->done_group = done_group;
    208 	scan->done_group_data = done_group_data;
    209 }
    210 
    211 int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags,
    212 			    int clear_flags)
    213 {
    214 	int	old_flags;
    215 
    216 	if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
    217 		return 0;
    218 
    219 	old_flags = scan->scan_flags;
    220 	scan->scan_flags &= ~clear_flags;
    221 	scan->scan_flags |= set_flags;
    222 	return old_flags;
    223 }
    224 
    225 /*
    226  * This function is called by ext2fs_get_next_inode when it needs to
    227  * get ready to read in a new blockgroup.
    228  */
    229 static errcode_t get_next_blockgroup(ext2_inode_scan scan)
    230 {
    231 	ext2_filsys fs = scan->fs;
    232 
    233 	scan->current_group++;
    234 	scan->groups_left--;
    235 
    236 	scan->current_block =fs->group_desc[scan->current_group].bg_inode_table;
    237 
    238 	scan->current_inode = scan->current_group *
    239 		EXT2_INODES_PER_GROUP(fs->super);
    240 
    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)) {
    246 		scan->inodes_left -=
    247 			fs->group_desc[scan->current_group].bg_itable_unused;
    248 		scan->blocks_left =
    249 			(scan->inodes_left +
    250 			 (fs->blocksize / scan->inode_size - 1)) *
    251 			scan->inode_size / fs->blocksize;
    252 	}
    253 
    254 	return 0;
    255 }
    256 
    257 errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,
    258 					    int	group)
    259 {
    260 	scan->current_group = group - 1;
    261 	scan->groups_left = scan->fs->group_desc_count - group;
    262 	return get_next_blockgroup(scan);
    263 }
    264 
    265 /*
    266  * This function is called by get_next_blocks() to check for bad
    267  * blocks in the inode table.
    268  *
    269  * This function assumes that badblocks_list->list is sorted in
    270  * increasing order.
    271  */
    272 static errcode_t check_for_inode_bad_blocks(ext2_inode_scan scan,
    273 					    blk_t *num_blocks)
    274 {
    275 	blk_t	blk = scan->current_block;
    276 	badblocks_list	bb = scan->fs->badblocks;
    277 
    278 	/*
    279 	 * If the inode table is missing, then obviously there are no
    280 	 * bad blocks.  :-)
    281 	 */
    282 	if (blk == 0)
    283 		return 0;
    284 
    285 	/*
    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!
    290 	 */
    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;
    294 			return 0;
    295 		}
    296 	}
    297 
    298 	/*
    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.)
    305 	 */
    306 	if (blk == bb->list[scan->bad_block_ptr]) {
    307 		scan->scan_flags |= EXT2_SF_BAD_INODE_BLK;
    308 		*num_blocks = 1;
    309 		if (++scan->bad_block_ptr >= bb->num)
    310 			scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
    311 		return 0;
    312 	}
    313 
    314 	/*
    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.)
    319 	 */
    320 	if ((blk + *num_blocks) > bb->list[scan->bad_block_ptr])
    321 		*num_blocks = (int) (bb->list[scan->bad_block_ptr] - blk);
    322 
    323 	return 0;
    324 }
    325 
    326 /*
    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.
    329  */
    330 static errcode_t get_next_blocks(ext2_inode_scan scan)
    331 {
    332 	blk_t		num_blocks;
    333 	errcode_t	retval;
    334 
    335 	/*
    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.
    339 	 */
    340 	num_blocks = scan->inode_buffer_blocks;
    341 	if (num_blocks > scan->blocks_left)
    342 		num_blocks = scan->blocks_left;
    343 
    344 	/*
    345 	 * If the past block "read" was a bad block, then mark the
    346 	 * left-over extra bytes as also being bad.
    347 	 */
    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;
    352 	}
    353 
    354 	/*
    355 	 * Do inode bad block processing, if necessary.
    356 	 */
    357 	if (scan->scan_flags & EXT2_SF_CHK_BADBLOCKS) {
    358 		retval = check_for_inode_bad_blocks(scan, &num_blocks);
    359 		if (retval)
    360 			return retval;
    361 	}
    362 
    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);
    367 	} else {
    368 		retval = io_channel_read_blk(scan->fs->io,
    369 					     scan->current_block,
    370 					     (int) num_blocks,
    371 					     scan->inode_buffer);
    372 		if (retval)
    373 			return EXT2_ET_NEXT_INODE_READ;
    374 	}
    375 	scan->ptr = scan->inode_buffer;
    376 	scan->bytes_left = num_blocks * scan->fs->blocksize;
    377 
    378 	scan->blocks_left -= num_blocks;
    379 	if (scan->current_block)
    380 		scan->current_block += num_blocks;
    381 	return 0;
    382 }
    383 
    384 #if 0
    385 /*
    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.)
    390  */
    391 static inline int is_empty_scan(ext2_inode_scan scan)
    392 {
    393 	int	i;
    394 
    395 	if (scan->bytes_left == 0)
    396 		return 0;
    397 
    398 	for (i=0; i < scan->bytes_left; i++)
    399 		if (scan->ptr[i])
    400 			return 0;
    401 	return 1;
    402 }
    403 #endif
    404 
    405 errcode_t ext2fs_get_next_inode_full(ext2_inode_scan scan, ext2_ino_t *ino,
    406 				     struct ext2_inode *inode, int bufsize)
    407 {
    408 	errcode_t	retval;
    409 	int		extra_bytes = 0;
    410 
    411 	EXT2_CHECK_MAGIC(scan, EXT2_ET_MAGIC_INODE_SCAN);
    412 
    413 	/*
    414 	 * Do we need to start reading a new block group?
    415 	 */
    416 	if (scan->inodes_left <= 0) {
    417 	force_new_group:
    418 		if (scan->done_group) {
    419 			retval = (scan->done_group)
    420 				(scan->fs, scan, scan->current_group,
    421 				 scan->done_group_data);
    422 			if (retval)
    423 				return retval;
    424 		}
    425 		if (scan->groups_left <= 0) {
    426 			*ino = 0;
    427 			return 0;
    428 		}
    429 		retval = get_next_blockgroup(scan);
    430 		if (retval)
    431 			return retval;
    432 	}
    433 	/*
    434 	 * These checks are done outside the above if statement so
    435 	 * they can be done for block group #0.
    436 	 */
    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;
    446 		} else
    447 			return EXT2_ET_MISSING_INODE_TABLE;
    448 	}
    449 
    450 
    451 	/*
    452 	 * Have we run out of space in the inode buffer?  If so, we
    453 	 * need to read in more blocks.
    454 	 */
    455 	if (scan->bytes_left < scan->inode_size) {
    456 		memcpy(scan->temp_buffer, scan->ptr, scan->bytes_left);
    457 		extra_bytes = scan->bytes_left;
    458 
    459 		retval = get_next_blocks(scan);
    460 		if (retval)
    461 			return retval;
    462 #if 0
    463 		/*
    464 		 * XXX test  Need check for used inode somehow.
    465 		 * (Note: this is hard.)
    466 		 */
    467 		if (is_empty_scan(scan))
    468 			goto force_new_group;
    469 #endif
    470 	}
    471 
    472 	retval = 0;
    473 	if (extra_bytes) {
    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;
    478 
    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,
    484 			       0, bufsize);
    485 #else
    486 		*inode = *((struct ext2_inode *) scan->temp_buffer);
    487 #endif
    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;
    491 	} else {
    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,
    497 				0, bufsize);
    498 #else
    499 		memcpy(inode, scan->ptr, bufsize);
    500 #endif
    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;
    505 	}
    506 
    507 	scan->inodes_left--;
    508 	scan->current_inode++;
    509 	*ino = scan->current_inode;
    510 	return retval;
    511 }
    512 
    513 errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ext2_ino_t *ino,
    514 				struct ext2_inode *inode)
    515 {
    516 	return ext2fs_get_next_inode_full(scan, ino, inode,
    517 						sizeof(struct ext2_inode));
    518 }
    519 
    520 /*
    521  * Functions to read and write a single inode.
    522  */
    523 errcode_t ext2fs_read_inode_full(ext2_filsys fs, ext2_ino_t ino,
    524 				 struct ext2_inode * inode, int bufsize)
    525 {
    526 	unsigned long 	group, block, block_nr, offset;
    527 	char 		*ptr;
    528 	errcode_t	retval;
    529 	int 		clen, i, inodes_per_block, length;
    530 	io_channel	io;
    531 
    532 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
    533 
    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)
    538 			return retval;
    539 	}
    540 	if ((ino == 0) || (ino > fs->super->s_inodes_count))
    541 		return EXT2_ET_BAD_INODE_NUM;
    542 	/* Create inode cache if not present */
    543 	if (!fs->icache) {
    544 		retval = create_icache(fs);
    545 		if (retval)
    546 			return retval;
    547 	}
    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;
    554 				return 0;
    555 			}
    556 		}
    557 	}
    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);
    564 		io = fs->image_io;
    565 	} else {
    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 +
    575 			block;
    576 		io = fs->io;
    577 	}
    578 	offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
    579 
    580 	length = EXT2_INODE_SIZE(fs->super);
    581 	if (bufsize < length)
    582 		length = bufsize;
    583 
    584 	ptr = (char *) inode;
    585 	while (length) {
    586 		clen = length;
    587 		if ((offset + length) > fs->blocksize)
    588 			clen = fs->blocksize - offset;
    589 
    590 		if (block_nr != fs->icache->buffer_blk) {
    591 			retval = io_channel_read_blk(io, block_nr, 1,
    592 						     fs->icache->buffer);
    593 			if (retval)
    594 				return retval;
    595 			fs->icache->buffer_blk = block_nr;
    596 		}
    597 
    598 		memcpy(ptr, ((char *) fs->icache->buffer) + (unsigned) offset,
    599 		       clen);
    600 
    601 		offset = 0;
    602 		length -= clen;
    603 		ptr += clen;
    604 		block_nr++;
    605 	}
    606 
    607 #ifdef WORDS_BIGENDIAN
    608 	ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) inode,
    609 			       (struct ext2_inode_large *) inode,
    610 			       0, bufsize);
    611 #endif
    612 
    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;
    618 
    619 	return 0;
    620 }
    621 
    622 errcode_t ext2fs_read_inode(ext2_filsys fs, ext2_ino_t ino,
    623 			    struct ext2_inode * inode)
    624 {
    625 	return ext2fs_read_inode_full(fs, ino, inode,
    626 					sizeof(struct ext2_inode));
    627 }
    628 
    629 errcode_t ext2fs_write_inode_full(ext2_filsys fs, ext2_ino_t ino,
    630 				  struct ext2_inode * inode, int bufsize)
    631 {
    632 	unsigned long group, block, block_nr, offset;
    633 	errcode_t retval = 0;
    634 	struct ext2_inode_large temp_inode, *w_inode;
    635 	char *ptr;
    636 	int clen, i, length;
    637 
    638 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
    639 
    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)
    644 			return retval;
    645 	}
    646 
    647 	/* Check to see if the inode cache needs to be updated */
    648 	if (fs->icache) {
    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;
    652 				break;
    653 			}
    654 		}
    655 	} else {
    656 		retval = create_icache(fs);
    657 		if (retval)
    658 			return retval;
    659 	}
    660 
    661 	if (!(fs->flags & EXT2_FLAG_RW))
    662 		return EXT2_ET_RO_FILSYS;
    663 
    664 	if ((ino == 0) || (ino > fs->super->s_inodes_count))
    665 		return EXT2_ET_BAD_INODE_NUM;
    666 
    667 	length = bufsize;
    668 	if (length < EXT2_INODE_SIZE(fs->super))
    669 		length = EXT2_INODE_SIZE(fs->super);
    670 
    671 	if (length > (int) sizeof(struct ext2_inode_large)) {
    672 		w_inode = malloc(length);
    673 		if (!w_inode)
    674 			return ENOMEM;
    675 	} else
    676 		w_inode = &temp_inode;
    677 	memset(w_inode, 0, length);
    678 
    679 #ifdef WORDS_BIGENDIAN
    680 	ext2fs_swap_inode_full(fs, w_inode,
    681 			       (struct ext2_inode_large *) inode,
    682 			       1, bufsize);
    683 #else
    684 	memcpy(w_inode, inode, bufsize);
    685 #endif
    686 
    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;
    693 		goto errout;
    694 	}
    695 	block_nr = fs->group_desc[(unsigned) group].bg_inode_table + block;
    696 
    697 	offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
    698 
    699 	length = EXT2_INODE_SIZE(fs->super);
    700 	if (length > bufsize)
    701 		length = bufsize;
    702 
    703 	ptr = (char *) w_inode;
    704 
    705 	while (length) {
    706 		clen = length;
    707 		if ((offset + length) > fs->blocksize)
    708 			clen = fs->blocksize - offset;
    709 
    710 		if (fs->icache->buffer_blk != block_nr) {
    711 			retval = io_channel_read_blk(fs->io, block_nr, 1,
    712 						     fs->icache->buffer);
    713 			if (retval)
    714 				goto errout;
    715 			fs->icache->buffer_blk = block_nr;
    716 		}
    717 
    718 
    719 		memcpy((char *) fs->icache->buffer + (unsigned) offset,
    720 		       ptr, clen);
    721 
    722 		retval = io_channel_write_blk(fs->io, block_nr, 1,
    723 					      fs->icache->buffer);
    724 		if (retval)
    725 			goto errout;
    726 
    727 		offset = 0;
    728 		ptr += clen;
    729 		length -= clen;
    730 		block_nr++;
    731 	}
    732 
    733 	fs->flags |= EXT2_FLAG_CHANGED;
    734 errout:
    735 	if (w_inode && w_inode != &temp_inode)
    736 		free(w_inode);
    737 	return retval;
    738 }
    739 
    740 errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino,
    741 			     struct ext2_inode *inode)
    742 {
    743 	return ext2fs_write_inode_full(fs, ino, inode,
    744 				       sizeof(struct ext2_inode));
    745 }
    746 
    747 /*
    748  * This function should be called when writing a new inode.  It makes
    749  * sure that extra part of large inodes is initialized properly.
    750  */
    751 errcode_t ext2fs_write_new_inode(ext2_filsys fs, ext2_ino_t ino,
    752 				 struct ext2_inode *inode)
    753 {
    754 	struct ext2_inode	*buf;
    755 	int 			size = EXT2_INODE_SIZE(fs->super);
    756 	struct ext2_inode_large	*large_inode;
    757 	errcode_t		retval;
    758 	__u32 			t = fs->now ? fs->now : time(NULL);
    759 
    760 	if (!inode->i_ctime)
    761 		inode->i_ctime = t;
    762 	if (!inode->i_mtime)
    763 		inode->i_mtime = t;
    764 	if (!inode->i_atime)
    765 		inode->i_atime = t;
    766 
    767 	if (size == sizeof(struct ext2_inode))
    768 		return ext2fs_write_inode_full(fs, ino, inode,
    769 					       sizeof(struct ext2_inode));
    770 
    771 	buf = malloc(size);
    772 	if (!buf)
    773 		return ENOMEM;
    774 
    775 	memset(buf, 0, size);
    776 	*buf = *inode;
    777 
    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;
    783 
    784 	retval = ext2fs_write_inode_full(fs, ino, buf, size);
    785 	free(buf);
    786 	return retval;
    787 }
    788 
    789 
    790 errcode_t ext2fs_get_blocks(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks)
    791 {
    792 	struct ext2_inode	inode;
    793 	int			i;
    794 	errcode_t		retval;
    795 
    796 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
    797 
    798 	if (ino > fs->super->s_inodes_count)
    799 		return EXT2_ET_BAD_INODE_NUM;
    800 
    801 	if (fs->get_blocks) {
    802 		if (!(*fs->get_blocks)(fs, ino, blocks))
    803 			return 0;
    804 	}
    805 	retval = ext2fs_read_inode(fs, ino, &inode);
    806 	if (retval)
    807 		return retval;
    808 	for (i=0; i < EXT2_N_BLOCKS; i++)
    809 		blocks[i] = inode.i_block[i];
    810 	return 0;
    811 }
    812 
    813 errcode_t ext2fs_check_directory(ext2_filsys fs, ext2_ino_t ino)
    814 {
    815 	struct	ext2_inode	inode;
    816 	errcode_t		retval;
    817 
    818 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
    819 
    820 	if (ino > fs->super->s_inodes_count)
    821 		return EXT2_ET_BAD_INODE_NUM;
    822 
    823 	if (fs->check_directory) {
    824 		retval = (fs->check_directory)(fs, ino);
    825 		if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
    826 			return retval;
    827 	}
    828 	retval = ext2fs_read_inode(fs, ino, &inode);
    829 	if (retval)
    830 		return retval;
    831 	if (!LINUX_S_ISDIR(inode.i_mode))
    832 		return EXT2_ET_NO_DIRECTORY;
    833 	return 0;
    834 }
    835 
    836