2 * inode.c --- utility routines to read and write inodes
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
17 #include <sys/types.h>
22 #include <linux/ext2_fs.h>
26 struct ext2_struct_inode_scan {
32 int inodes_left, blocks_left, groups_left;
33 int inode_buffer_blocks;
39 errcode_t (*done_group)(ext2_filsys fs,
43 void * done_group_data;
49 static errcode_t create_icache(ext2_filsys fs)
55 fs->icache = malloc(sizeof(struct ext2_inode_cache));
56 memset(fs->icache, 0, sizeof(struct ext2_inode_cache));
57 fs->icache->buffer = malloc(fs->blocksize);
58 if (!fs->icache->buffer) {
62 fs->icache->buffer_blk = 0;
63 fs->icache->cache_last = -1;
64 fs->icache->cache_size = 4;
65 fs->icache->refcount = 1;
66 fs->icache->cache = malloc(sizeof(struct ext2_inode_cache_ent)
67 * fs->icache->cache_size);
68 if (!fs->icache->cache) {
69 free(fs->icache->buffer);
73 for (i=0; i < fs->icache->cache_size; i++)
74 fs->icache->cache[i].ino = 0;
78 errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks,
79 ext2_inode_scan *ret_scan)
83 errcode_t (*save_get_blocks)(ext2_filsys fs, ino_t ino, blk_t *blocks);
85 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
88 * If fs->badblocks isn't set, then set it --- since the inode
89 * scanning functions require it.
91 if (fs->badblocks == 0) {
93 * Temporarly save fs->get_blocks and set it to zero,
94 * for compatibility with old e2fsck's.
96 save_get_blocks = fs->get_blocks;
98 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
99 if (retval && fs->badblocks) {
100 badblocks_list_free(fs->badblocks);
103 fs->get_blocks = save_get_blocks;
106 scan = (ext2_inode_scan) malloc(sizeof(struct ext2_struct_inode_scan));
109 memset(scan, 0, sizeof(struct ext2_struct_inode_scan));
111 scan->magic = EXT2_ET_MAGIC_INODE_SCAN;
113 scan->inode_size = EXT2_INODE_SIZE(fs->super);
114 scan->bytes_left = 0;
115 scan->current_group = -1;
116 scan->inode_buffer_blocks = buffer_blocks ? buffer_blocks : 8;
117 scan->groups_left = fs->group_desc_count;
118 scan->inode_buffer = malloc(scan->inode_buffer_blocks * fs->blocksize);
119 scan->done_group = 0;
120 scan->done_group_data = 0;
121 scan->bad_block_ptr = 0;
122 if (!scan->inode_buffer) {
126 scan->temp_buffer = malloc(scan->inode_size);
127 if (!scan->temp_buffer) {
128 free(scan->inode_buffer);
132 if (scan->fs->badblocks && scan->fs->badblocks->num)
133 scan->scan_flags |= EXT2_SF_CHK_BADBLOCKS;
138 void ext2fs_close_inode_scan(ext2_inode_scan scan)
140 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
143 free(scan->inode_buffer);
144 scan->inode_buffer = NULL;
145 free(scan->temp_buffer);
146 scan->temp_buffer = NULL;
151 void ext2fs_set_inode_callback(ext2_inode_scan scan,
152 errcode_t (*done_group)(ext2_filsys fs,
153 ext2_inode_scan scan,
156 void *done_group_data)
158 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
161 scan->done_group = done_group;
162 scan->done_group_data = done_group_data;
165 int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags,
170 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
173 old_flags = scan->scan_flags;
174 scan->scan_flags &= ~clear_flags;
175 scan->scan_flags |= set_flags;
180 * This function is called by ext2fs_get_next_inode when it needs to
181 * get ready to read in a new blockgroup.
183 static errcode_t get_next_blockgroup(ext2_inode_scan scan)
185 scan->current_group++;
188 scan->current_block = scan->fs->
189 group_desc[scan->current_group].bg_inode_table;
191 scan->bytes_left = 0;
192 scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super);
193 scan->blocks_left = scan->fs->inode_blocks_per_group;
197 errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,
200 scan->current_group = group - 1;
201 scan->groups_left = scan->fs->group_desc_count - group;
202 return get_next_blockgroup(scan);
206 * This function is called by get_next_blocks() to check for bad
207 * blocks in the inode table.
209 * This function assumes that badblocks_list->list is sorted in
212 static errcode_t check_for_inode_bad_blocks(ext2_inode_scan scan,
215 blk_t blk = scan->current_block;
216 badblocks_list bb = scan->fs->badblocks;
219 * If the inode table is missing, then obviously there are no
226 * If the current block is greater than the bad block listed
227 * in the bad block list, then advance the pointer until this
228 * is no longer the case. If we run out of bad blocks, then
229 * we don't need to do any more checking!
231 while (blk > bb->list[scan->bad_block_ptr]) {
232 if (++scan->bad_block_ptr >= bb->num) {
233 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
239 * If the current block is equal to the bad block listed in
240 * the bad block list, then handle that one block specially.
241 * (We could try to handle runs of bad blocks, but that
242 * only increases CPU efficiency by a small amount, at the
243 * expense of a huge expense of code complexity, and for an
244 * uncommon case at that.)
246 if (blk == bb->list[scan->bad_block_ptr]) {
247 scan->scan_flags |= EXT2_SF_BAD_INODE_BLK;
249 if (++scan->bad_block_ptr >= bb->num)
250 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
255 * If there is a bad block in the range that we're about to
256 * read in, adjust the number of blocks to read so that we we
257 * don't read in the bad block. (Then the next block to read
258 * will be the bad block, which is handled in the above case.)
260 if ((blk + *num_blocks) > bb->list[scan->bad_block_ptr])
261 *num_blocks = bb->list[scan->bad_block_ptr] - blk;
267 * This function is called by ext2fs_get_next_inode when it needs to
268 * read in more blocks from the current blockgroup's inode table.
270 static errcode_t get_next_blocks(ext2_inode_scan scan)
276 * Figure out how many blocks to read; we read at most
277 * inode_buffer_blocks, and perhaps less if there aren't that
278 * many blocks left to read.
280 num_blocks = scan->inode_buffer_blocks;
281 if (num_blocks > scan->blocks_left)
282 num_blocks = scan->blocks_left;
285 * If the past block "read" was a bad block, then mark the
286 * left-over extra bytes as also being bad.
288 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) {
289 if (scan->bytes_left)
290 scan->scan_flags |= EXT2_SF_BAD_EXTRA_BYTES;
291 scan->scan_flags &= ~EXT2_SF_BAD_INODE_BLK;
295 * Do inode bad block processing, if necessary.
297 if (scan->scan_flags & EXT2_SF_CHK_BADBLOCKS) {
298 retval = check_for_inode_bad_blocks(scan, &num_blocks);
303 if ((scan->scan_flags & EXT2_SF_BAD_INODE_BLK) ||
304 (scan->current_block == 0)) {
305 memset(scan->inode_buffer, 0,
306 num_blocks * scan->fs->blocksize);
308 retval = io_channel_read_blk(scan->fs->io,
310 num_blocks, scan->inode_buffer);
312 return EXT2_ET_NEXT_INODE_READ;
314 scan->ptr = scan->inode_buffer;
315 scan->bytes_left = num_blocks * scan->fs->blocksize;
317 scan->blocks_left -= num_blocks;
318 if (scan->current_block)
319 scan->current_block += num_blocks;
323 errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ino_t *ino,
324 struct ext2_inode *inode)
329 EXT2_CHECK_MAGIC(scan, EXT2_ET_MAGIC_INODE_SCAN);
332 * Do we need to start reading a new block group?
334 if (scan->inodes_left <= 0) {
336 if (scan->done_group) {
337 retval = (scan->done_group)
338 (scan->fs, scan, scan->current_group,
339 scan->done_group_data);
343 if (scan->groups_left <= 0) {
347 retval = get_next_blockgroup(scan);
350 if (scan->current_block == 0) {
351 if (scan->scan_flags & EXT2_SF_SKIP_MISSING_ITABLE) {
354 return EXT2_ET_MISSING_INODE_TABLE;
359 * Have we run out of space in the inode buffer? If so, we
360 * need to read in more blocks.
362 if (scan->bytes_left < scan->inode_size) {
363 memcpy(scan->temp_buffer, scan->ptr, scan->bytes_left);
364 extra_bytes = scan->bytes_left;
366 retval = get_next_blocks(scan);
373 memcpy(scan->temp_buffer+extra_bytes, scan->ptr,
374 scan->inode_size - extra_bytes);
375 scan->ptr += scan->inode_size - extra_bytes;
376 scan->bytes_left -= scan->inode_size - extra_bytes;
378 if ((scan->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
379 (scan->fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
380 ext2fs_swap_inode(scan->fs, inode,
381 (struct ext2_inode *) scan->temp_buffer, 0);
383 *inode = *((struct ext2_inode *) scan->temp_buffer);
384 if (scan->scan_flags & EXT2_SF_BAD_EXTRA_BYTES)
385 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
386 scan->scan_flags &= ~EXT2_SF_BAD_EXTRA_BYTES;
388 if ((scan->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
389 (scan->fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
390 ext2fs_swap_inode(scan->fs, inode,
391 (struct ext2_inode *) scan->ptr, 0);
393 *inode = *((struct ext2_inode *) scan->ptr);
394 scan->ptr += scan->inode_size;
395 scan->bytes_left -= scan->inode_size;
396 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK)
397 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
401 scan->current_inode++;
402 *ino = scan->current_inode;
407 * Functions to read and write a single inode.
409 errcode_t ext2fs_read_inode (ext2_filsys fs, ino_t ino,
410 struct ext2_inode * inode)
412 unsigned long group, block, block_nr, offset;
417 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
419 /* Check to see if user has an override function */
420 if (fs->read_inode) {
421 retval = (fs->read_inode)(fs, ino, inode);
422 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
425 /* Create inode cache if not present */
427 retval = create_icache(fs);
431 /* Check to see if it's in the inode cache */
432 for (i=0; i < fs->icache->cache_size; i++) {
433 if (fs->icache->cache[i].ino == ino) {
434 *inode = fs->icache->cache[i].inode;
438 if (ino > fs->super->s_inodes_count)
439 return EXT2_ET_BAD_INODE_NUM;
440 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
441 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
442 EXT2_INODE_SIZE(fs->super);
443 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
444 if (!fs->group_desc[group].bg_inode_table)
445 return EXT2_ET_MISSING_INODE_TABLE;
446 block_nr = fs->group_desc[group].bg_inode_table + block;
447 if (block_nr != fs->icache->buffer_blk) {
448 retval = io_channel_read_blk(fs->io, block_nr, 1,
452 fs->icache->buffer_blk = block_nr;
454 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
455 ptr = ((char *) fs->icache->buffer) + offset;
457 memset(inode, 0, sizeof(struct ext2_inode));
458 length = EXT2_INODE_SIZE(fs->super);
459 if (length > sizeof(struct ext2_inode))
460 length = sizeof(struct ext2_inode);
462 if ((offset + length) > EXT2_BLOCK_SIZE(fs->super)) {
463 clen = EXT2_BLOCK_SIZE(fs->super) - offset;
464 memcpy((char *) inode, ptr, clen);
467 retval = io_channel_read_blk(fs->io, block_nr+1, 1,
470 fs->icache->buffer_blk = 0;
473 fs->icache->buffer_blk = block_nr+1;
475 memcpy(((char *) inode) + clen,
476 fs->icache->buffer, length);
478 memcpy((char *) inode, ptr, length);
480 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
481 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
482 ext2fs_swap_inode(fs, inode, inode, 0);
484 /* Update the inode cache */
485 fs->icache->cache_last = (fs->icache->cache_last + 1) %
486 fs->icache->cache_size;
487 fs->icache->cache[fs->icache->cache_last].ino = ino;
488 fs->icache->cache[fs->icache->cache_last].inode = *inode;
493 errcode_t ext2fs_write_inode(ext2_filsys fs, ino_t ino,
494 struct ext2_inode * inode)
496 unsigned long group, block, block_nr, offset;
498 struct ext2_inode temp_inode;
502 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
504 /* Check to see if user provided an override function */
505 if (fs->write_inode) {
506 retval = (fs->write_inode)(fs, ino, inode);
507 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
511 /* Check to see if the inode cache needs to be updated */
513 for (i=0; i < fs->icache->cache_size; i++) {
514 if (fs->icache->cache[i].ino == ino) {
515 fs->icache->cache[i].inode = *inode;
520 retval = create_icache(fs);
525 if (!(fs->flags & EXT2_FLAG_RW))
526 return EXT2_ET_RO_FILSYS;
528 if (ino > fs->super->s_inodes_count)
529 return EXT2_ET_BAD_INODE_NUM;
531 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
532 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))
533 ext2fs_swap_inode(fs, &temp_inode, inode, 1);
535 memcpy(&temp_inode, inode, sizeof(struct ext2_inode));
537 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
538 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
539 EXT2_INODE_SIZE(fs->super);
540 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
541 if (!fs->group_desc[group].bg_inode_table)
542 return EXT2_ET_MISSING_INODE_TABLE;
543 block_nr = fs->group_desc[group].bg_inode_table + block;
544 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
545 ptr = (char *) fs->icache->buffer + offset;
547 length = EXT2_INODE_SIZE(fs->super);
549 if (length > sizeof(struct ext2_inode))
550 length = sizeof(struct ext2_inode);
552 if (fs->icache->buffer_blk != block_nr) {
553 retval = io_channel_read_blk(fs->io, block_nr, 1,
557 fs->icache->buffer_blk = block_nr;
560 if ((offset + length) > EXT2_BLOCK_SIZE(fs->super)) {
561 clen = EXT2_BLOCK_SIZE(fs->super) - offset;
566 memcpy(ptr, &temp_inode, clen);
567 retval = io_channel_write_blk(fs->io, block_nr, 1, fs->icache->buffer);
572 retval = io_channel_read_blk(fs->io, ++block_nr, 1,
575 fs->icache->buffer_blk = 0;
578 fs->icache->buffer_blk = block_nr;
579 memcpy(fs->icache->buffer, ((char *) &temp_inode) + clen,
582 retval = io_channel_write_blk(fs->io, block_nr, 1,
588 fs->flags |= EXT2_FLAG_CHANGED;
592 errcode_t ext2fs_get_blocks(ext2_filsys fs, ino_t ino, blk_t *blocks)
594 struct ext2_inode inode;
598 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
600 if (ino > fs->super->s_inodes_count)
601 return EXT2_ET_BAD_INODE_NUM;
603 if (fs->get_blocks) {
604 if (!(*fs->get_blocks)(fs, ino, blocks))
607 retval = ext2fs_read_inode(fs, ino, &inode);
610 for (i=0; i < EXT2_N_BLOCKS; i++)
611 blocks[i] = inode.i_block[i];
615 errcode_t ext2fs_check_directory(ext2_filsys fs, ino_t ino)
617 struct ext2_inode inode;
620 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
622 if (ino > fs->super->s_inodes_count)
623 return EXT2_ET_BAD_INODE_NUM;
625 if (fs->check_directory)
626 return (fs->check_directory)(fs, ino);
627 retval = ext2fs_read_inode(fs, ino, &inode);
630 if (!LINUX_S_ISDIR(inode.i_mode))