inode.c revision d4f34d41be97e23db07d5ed606fcc1a26f5a3c76
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#if HAVE_SYS_STAT_H 21#include <sys/stat.h> 22#endif 23#if HAVE_SYS_TYPES_H 24#include <sys/types.h> 25#endif 26 27#include "ext2_fs.h" 28#include "ext2fsP.h" 29#include "e2image.h" 30 31struct ext2_struct_inode_scan { 32 errcode_t magic; 33 ext2_filsys fs; 34 ext2_ino_t current_inode; 35 blk_t current_block; 36 dgrp_t current_group; 37 ext2_ino_t inodes_left; 38 blk_t blocks_left; 39 dgrp_t groups_left; 40 blk_t inode_buffer_blocks; 41 char * inode_buffer; 42 int inode_size; 43 char * ptr; 44 int bytes_left; 45 char *temp_buffer; 46 errcode_t (*done_group)(ext2_filsys fs, 47 ext2_inode_scan scan, 48 dgrp_t group, 49 void * priv_data); 50 void * done_group_data; 51 int bad_block_ptr; 52 int scan_flags; 53 int reserved[6]; 54}; 55 56/* 57 * This routine flushes the icache, if it exists. 58 */ 59errcode_t ext2fs_flush_icache(ext2_filsys fs) 60{ 61 int i; 62 63 if (!fs->icache) 64 return 0; 65 66 for (i=0; i < fs->icache->cache_size; i++) 67 fs->icache->cache[i].ino = 0; 68 69 fs->icache->buffer_blk = 0; 70 return 0; 71} 72 73static errcode_t create_icache(ext2_filsys fs) 74{ 75 errcode_t retval; 76 77 if (fs->icache) 78 return 0; 79 retval = ext2fs_get_mem(sizeof(struct ext2_inode_cache), &fs->icache); 80 if (retval) 81 return retval; 82 83 memset(fs->icache, 0, sizeof(struct ext2_inode_cache)); 84 retval = ext2fs_get_mem(fs->blocksize, &fs->icache->buffer); 85 if (retval) { 86 ext2fs_free_mem(&fs->icache); 87 return retval; 88 } 89 fs->icache->buffer_blk = 0; 90 fs->icache->cache_last = -1; 91 fs->icache->cache_size = 4; 92 fs->icache->refcount = 1; 93 retval = ext2fs_get_array(fs->icache->cache_size, 94 sizeof(struct ext2_inode_cache_ent), 95 &fs->icache->cache); 96 if (retval) { 97 ext2fs_free_mem(&fs->icache->buffer); 98 ext2fs_free_mem(&fs->icache); 99 return retval; 100 } 101 ext2fs_flush_icache(fs); 102 return 0; 103} 104 105errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks, 106 ext2_inode_scan *ret_scan) 107{ 108 ext2_inode_scan scan; 109 errcode_t retval; 110 errcode_t (*save_get_blocks)(ext2_filsys f, ext2_ino_t ino, blk_t *blocks); 111 112 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); 113 114 /* 115 * If fs->badblocks isn't set, then set it --- since the inode 116 * scanning functions require it. 117 */ 118 if (fs->badblocks == 0) { 119 /* 120 * Temporarly save fs->get_blocks and set it to zero, 121 * for compatibility with old e2fsck's. 122 */ 123 save_get_blocks = fs->get_blocks; 124 fs->get_blocks = 0; 125 retval = ext2fs_read_bb_inode(fs, &fs->badblocks); 126 if (retval && fs->badblocks) { 127 ext2fs_badblocks_list_free(fs->badblocks); 128 fs->badblocks = 0; 129 } 130 fs->get_blocks = save_get_blocks; 131 } 132 133 retval = ext2fs_get_mem(sizeof(struct ext2_struct_inode_scan), &scan); 134 if (retval) 135 return retval; 136 memset(scan, 0, sizeof(struct ext2_struct_inode_scan)); 137 138 scan->magic = EXT2_ET_MAGIC_INODE_SCAN; 139 scan->fs = fs; 140 scan->inode_size = EXT2_INODE_SIZE(fs->super); 141 scan->bytes_left = 0; 142 scan->current_group = 0; 143 scan->groups_left = fs->group_desc_count - 1; 144 scan->inode_buffer_blocks = buffer_blocks ? buffer_blocks : 8; 145 scan->current_block = scan->fs-> 146 group_desc[scan->current_group].bg_inode_table; 147 scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super); 148 scan->blocks_left = scan->fs->inode_blocks_per_group; 149 retval = ext2fs_get_array(scan->inode_buffer_blocks, 150 fs->blocksize, 151 &scan->inode_buffer); 152 scan->done_group = 0; 153 scan->done_group_data = 0; 154 scan->bad_block_ptr = 0; 155 if (retval) { 156 ext2fs_free_mem(&scan); 157 return retval; 158 } 159 retval = ext2fs_get_mem(scan->inode_size, &scan->temp_buffer); 160 if (retval) { 161 ext2fs_free_mem(&scan->inode_buffer); 162 ext2fs_free_mem(&scan); 163 return retval; 164 } 165 if (scan->fs->badblocks && scan->fs->badblocks->num) 166 scan->scan_flags |= EXT2_SF_CHK_BADBLOCKS; 167 if (EXT2_HAS_COMPAT_FEATURE(fs->super, 168 EXT2_FEATURE_COMPAT_LAZY_BG)) 169 scan->scan_flags |= EXT2_SF_DO_LAZY; 170 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super, 171 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) 172 scan->scan_flags |= EXT2_SF_DO_LAZY; 173 *ret_scan = scan; 174 return 0; 175} 176 177void ext2fs_close_inode_scan(ext2_inode_scan scan) 178{ 179 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN)) 180 return; 181 182 ext2fs_free_mem(&scan->inode_buffer); 183 scan->inode_buffer = NULL; 184 ext2fs_free_mem(&scan->temp_buffer); 185 scan->temp_buffer = NULL; 186 ext2fs_free_mem(&scan); 187 return; 188} 189 190void ext2fs_set_inode_callback(ext2_inode_scan scan, 191 errcode_t (*done_group)(ext2_filsys fs, 192 ext2_inode_scan scan, 193 dgrp_t group, 194 void * priv_data), 195 void *done_group_data) 196{ 197 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN)) 198 return; 199 200 scan->done_group = done_group; 201 scan->done_group_data = done_group_data; 202} 203 204int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags, 205 int clear_flags) 206{ 207 int old_flags; 208 209 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN)) 210 return 0; 211 212 old_flags = scan->scan_flags; 213 scan->scan_flags &= ~clear_flags; 214 scan->scan_flags |= set_flags; 215 return old_flags; 216} 217 218/* 219 * This function is called by ext2fs_get_next_inode when it needs to 220 * get ready to read in a new blockgroup. 221 */ 222static errcode_t get_next_blockgroup(ext2_inode_scan scan) 223{ 224 ext2_filsys fs = scan->fs; 225 226 scan->current_group++; 227 scan->groups_left--; 228 229 scan->current_block =fs->group_desc[scan->current_group].bg_inode_table; 230 231 scan->current_inode = scan->current_group * 232 EXT2_INODES_PER_GROUP(fs->super); 233 234 scan->bytes_left = 0; 235 scan->inodes_left = EXT2_INODES_PER_GROUP(fs->super); 236 scan->blocks_left = fs->inode_blocks_per_group; 237 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super, 238 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) { 239 scan->inodes_left -= 240 fs->group_desc[scan->current_group].bg_itable_unused; 241 scan->blocks_left = 242 (EXT2_INODES_PER_GROUP(fs->super) - 243 fs->group_desc[scan->current_group].bg_itable_unused + 244 fs->blocksize / scan->inode_size - 1) * 245 scan->inode_size / fs->blocksize; 246 } 247 248 return 0; 249} 250 251errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan, 252 int group) 253{ 254 scan->current_group = group - 1; 255 scan->groups_left = scan->fs->group_desc_count - group; 256 return get_next_blockgroup(scan); 257} 258 259/* 260 * This function is called by get_next_blocks() to check for bad 261 * blocks in the inode table. 262 * 263 * This function assumes that badblocks_list->list is sorted in 264 * increasing order. 265 */ 266static errcode_t check_for_inode_bad_blocks(ext2_inode_scan scan, 267 blk_t *num_blocks) 268{ 269 blk_t blk = scan->current_block; 270 badblocks_list bb = scan->fs->badblocks; 271 272 /* 273 * If the inode table is missing, then obviously there are no 274 * bad blocks. :-) 275 */ 276 if (blk == 0) 277 return 0; 278 279 /* 280 * If the current block is greater than the bad block listed 281 * in the bad block list, then advance the pointer until this 282 * is no longer the case. If we run out of bad blocks, then 283 * we don't need to do any more checking! 284 */ 285 while (blk > bb->list[scan->bad_block_ptr]) { 286 if (++scan->bad_block_ptr >= bb->num) { 287 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS; 288 return 0; 289 } 290 } 291 292 /* 293 * If the current block is equal to the bad block listed in 294 * the bad block list, then handle that one block specially. 295 * (We could try to handle runs of bad blocks, but that 296 * only increases CPU efficiency by a small amount, at the 297 * expense of a huge expense of code complexity, and for an 298 * uncommon case at that.) 299 */ 300 if (blk == bb->list[scan->bad_block_ptr]) { 301 scan->scan_flags |= EXT2_SF_BAD_INODE_BLK; 302 *num_blocks = 1; 303 if (++scan->bad_block_ptr >= bb->num) 304 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS; 305 return 0; 306 } 307 308 /* 309 * If there is a bad block in the range that we're about to 310 * read in, adjust the number of blocks to read so that we we 311 * don't read in the bad block. (Then the next block to read 312 * will be the bad block, which is handled in the above case.) 313 */ 314 if ((blk + *num_blocks) > bb->list[scan->bad_block_ptr]) 315 *num_blocks = (int) (bb->list[scan->bad_block_ptr] - blk); 316 317 return 0; 318} 319 320/* 321 * This function is called by ext2fs_get_next_inode when it needs to 322 * read in more blocks from the current blockgroup's inode table. 323 */ 324static errcode_t get_next_blocks(ext2_inode_scan scan) 325{ 326 blk_t num_blocks; 327 errcode_t retval; 328 329 /* 330 * Figure out how many blocks to read; we read at most 331 * inode_buffer_blocks, and perhaps less if there aren't that 332 * many blocks left to read. 333 */ 334 num_blocks = scan->inode_buffer_blocks; 335 if (num_blocks > scan->blocks_left) 336 num_blocks = scan->blocks_left; 337 338 /* 339 * If the past block "read" was a bad block, then mark the 340 * left-over extra bytes as also being bad. 341 */ 342 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) { 343 if (scan->bytes_left) 344 scan->scan_flags |= EXT2_SF_BAD_EXTRA_BYTES; 345 scan->scan_flags &= ~EXT2_SF_BAD_INODE_BLK; 346 } 347 348 /* 349 * Do inode bad block processing, if necessary. 350 */ 351 if (scan->scan_flags & EXT2_SF_CHK_BADBLOCKS) { 352 retval = check_for_inode_bad_blocks(scan, &num_blocks); 353 if (retval) 354 return retval; 355 } 356 357 if ((scan->scan_flags & EXT2_SF_BAD_INODE_BLK) || 358 (scan->current_block == 0)) { 359 memset(scan->inode_buffer, 0, 360 (size_t) num_blocks * scan->fs->blocksize); 361 } else { 362 retval = io_channel_read_blk(scan->fs->io, 363 scan->current_block, 364 (int) num_blocks, 365 scan->inode_buffer); 366 if (retval) 367 return EXT2_ET_NEXT_INODE_READ; 368 } 369 scan->ptr = scan->inode_buffer; 370 scan->bytes_left = num_blocks * scan->fs->blocksize; 371 372 scan->blocks_left -= num_blocks; 373 if (scan->current_block) 374 scan->current_block += num_blocks; 375 return 0; 376} 377 378#if 0 379/* 380 * Returns 1 if the entire inode_buffer has a non-zero size and 381 * contains all zeros. (Not just deleted inodes, since that means 382 * that part of the inode table was used at one point; we want all 383 * zeros, which means that the inode table is pristine.) 384 */ 385static inline int is_empty_scan(ext2_inode_scan scan) 386{ 387 int i; 388 389 if (scan->bytes_left == 0) 390 return 0; 391 392 for (i=0; i < scan->bytes_left; i++) 393 if (scan->ptr[i]) 394 return 0; 395 return 1; 396} 397#endif 398 399errcode_t ext2fs_get_next_inode_full(ext2_inode_scan scan, ext2_ino_t *ino, 400 struct ext2_inode *inode, int bufsize) 401{ 402 errcode_t retval; 403 int extra_bytes = 0; 404 405 EXT2_CHECK_MAGIC(scan, EXT2_ET_MAGIC_INODE_SCAN); 406 407 /* 408 * Do we need to start reading a new block group? 409 */ 410 if (scan->inodes_left <= 0) { 411 force_new_group: 412 if (scan->done_group) { 413 retval = (scan->done_group) 414 (scan->fs, scan, scan->current_group, 415 scan->done_group_data); 416 if (retval) 417 return retval; 418 } 419 if (scan->groups_left <= 0) { 420 *ino = 0; 421 return 0; 422 } 423 retval = get_next_blockgroup(scan); 424 if (retval) 425 return retval; 426 } 427 /* 428 * These checks are done outside the above if statement so 429 * they can be done for block group #0. 430 */ 431 if ((scan->scan_flags & EXT2_SF_DO_LAZY) && 432 (scan->fs->group_desc[scan->current_group].bg_flags & 433 EXT2_BG_INODE_UNINIT)) 434 goto force_new_group; 435 if (scan->inodes_left == 0) 436 goto force_new_group; 437 if (scan->current_block == 0) { 438 if (scan->scan_flags & EXT2_SF_SKIP_MISSING_ITABLE) { 439 goto force_new_group; 440 } else 441 return EXT2_ET_MISSING_INODE_TABLE; 442 } 443 444 445 /* 446 * Have we run out of space in the inode buffer? If so, we 447 * need to read in more blocks. 448 */ 449 if (scan->bytes_left < scan->inode_size) { 450 memcpy(scan->temp_buffer, scan->ptr, scan->bytes_left); 451 extra_bytes = scan->bytes_left; 452 453 retval = get_next_blocks(scan); 454 if (retval) 455 return retval; 456#if 0 457 /* 458 * XXX test Need check for used inode somehow. 459 * (Note: this is hard.) 460 */ 461 if (is_empty_scan(scan)) 462 goto force_new_group; 463#endif 464 } 465 466 retval = 0; 467 if (extra_bytes) { 468 memcpy(scan->temp_buffer+extra_bytes, scan->ptr, 469 scan->inode_size - extra_bytes); 470 scan->ptr += scan->inode_size - extra_bytes; 471 scan->bytes_left -= scan->inode_size - extra_bytes; 472 473#ifdef WORDS_BIGENDIAN 474 memset(inode, 0, bufsize); 475 ext2fs_swap_inode_full(scan->fs, 476 (struct ext2_inode_large *) inode, 477 (struct ext2_inode_large *) scan->temp_buffer, 478 0, bufsize); 479#else 480 *inode = *((struct ext2_inode *) scan->temp_buffer); 481#endif 482 if (scan->scan_flags & EXT2_SF_BAD_EXTRA_BYTES) 483 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE; 484 scan->scan_flags &= ~EXT2_SF_BAD_EXTRA_BYTES; 485 } else { 486#ifdef WORDS_BIGENDIAN 487 memset(inode, 0, bufsize); 488 ext2fs_swap_inode_full(scan->fs, 489 (struct ext2_inode_large *) inode, 490 (struct ext2_inode_large *) scan->ptr, 491 0, bufsize); 492#else 493 memcpy(inode, scan->ptr, bufsize); 494#endif 495 scan->ptr += scan->inode_size; 496 scan->bytes_left -= scan->inode_size; 497 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) 498 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE; 499 } 500 501 scan->inodes_left--; 502 scan->current_inode++; 503 *ino = scan->current_inode; 504 return retval; 505} 506 507errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ext2_ino_t *ino, 508 struct ext2_inode *inode) 509{ 510 return ext2fs_get_next_inode_full(scan, ino, inode, 511 sizeof(struct ext2_inode)); 512} 513 514/* 515 * Functions to read and write a single inode. 516 */ 517errcode_t ext2fs_read_inode_full(ext2_filsys fs, ext2_ino_t ino, 518 struct ext2_inode * inode, int bufsize) 519{ 520 unsigned long group, block, block_nr, offset; 521 char *ptr; 522 errcode_t retval; 523 int clen, i, inodes_per_block, length; 524 io_channel io; 525 526 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); 527 528 /* Check to see if user has an override function */ 529 if (fs->read_inode) { 530 retval = (fs->read_inode)(fs, ino, inode); 531 if (retval != EXT2_ET_CALLBACK_NOTHANDLED) 532 return retval; 533 } 534 /* Create inode cache if not present */ 535 if (!fs->icache) { 536 retval = create_icache(fs); 537 if (retval) 538 return retval; 539 } 540 /* Check to see if it's in the inode cache */ 541 if (bufsize == sizeof(struct ext2_inode)) { 542 /* only old good inode can be retrieve from the cache */ 543 for (i=0; i < fs->icache->cache_size; i++) { 544 if (fs->icache->cache[i].ino == ino) { 545 *inode = fs->icache->cache[i].inode; 546 return 0; 547 } 548 } 549 } 550 if ((ino == 0) || (ino > fs->super->s_inodes_count)) 551 return EXT2_ET_BAD_INODE_NUM; 552 if (fs->flags & EXT2_FLAG_IMAGE_FILE) { 553 inodes_per_block = fs->blocksize / EXT2_INODE_SIZE(fs->super); 554 block_nr = fs->image_header->offset_inode / fs->blocksize; 555 block_nr += (ino - 1) / inodes_per_block; 556 offset = ((ino - 1) % inodes_per_block) * 557 EXT2_INODE_SIZE(fs->super); 558 io = fs->image_io; 559 } else { 560 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super); 561 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) * 562 EXT2_INODE_SIZE(fs->super); 563 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super); 564 if (!fs->group_desc[(unsigned)group].bg_inode_table) 565 return EXT2_ET_MISSING_INODE_TABLE; 566 block_nr = fs->group_desc[(unsigned)group].bg_inode_table + 567 block; 568 io = fs->io; 569 } 570 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1); 571 572 length = EXT2_INODE_SIZE(fs->super); 573 if (bufsize < length) 574 length = bufsize; 575 576 ptr = (char *) inode; 577 while (length) { 578 clen = length; 579 if ((offset + length) > fs->blocksize) 580 clen = fs->blocksize - offset; 581 582 if (block_nr != fs->icache->buffer_blk) { 583 retval = io_channel_read_blk(io, block_nr, 1, 584 fs->icache->buffer); 585 if (retval) 586 return retval; 587 fs->icache->buffer_blk = block_nr; 588 } 589 590 memcpy(ptr, ((char *) fs->icache->buffer) + (unsigned) offset, 591 clen); 592 593 offset = 0; 594 length -= clen; 595 ptr += clen; 596 block_nr++; 597 } 598 599#ifdef WORDS_BIGENDIAN 600 ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) inode, 601 (struct ext2_inode_large *) inode, 602 0, bufsize); 603#endif 604 605 /* Update the inode cache */ 606 fs->icache->cache_last = (fs->icache->cache_last + 1) % 607 fs->icache->cache_size; 608 fs->icache->cache[fs->icache->cache_last].ino = ino; 609 fs->icache->cache[fs->icache->cache_last].inode = *inode; 610 611 return 0; 612} 613 614errcode_t ext2fs_read_inode(ext2_filsys fs, ext2_ino_t ino, 615 struct ext2_inode * inode) 616{ 617 return ext2fs_read_inode_full(fs, ino, inode, 618 sizeof(struct ext2_inode)); 619} 620 621errcode_t ext2fs_write_inode_full(ext2_filsys fs, ext2_ino_t ino, 622 struct ext2_inode * inode, int bufsize) 623{ 624 unsigned long group, block, block_nr, offset; 625 errcode_t retval = 0; 626 struct ext2_inode_large temp_inode, *w_inode; 627 char *ptr; 628 int clen, i, length; 629 630 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); 631 632 /* Check to see if user provided an override function */ 633 if (fs->write_inode) { 634 retval = (fs->write_inode)(fs, ino, inode); 635 if (retval != EXT2_ET_CALLBACK_NOTHANDLED) 636 return retval; 637 } 638 639 /* Check to see if the inode cache needs to be updated */ 640 if (fs->icache) { 641 for (i=0; i < fs->icache->cache_size; i++) { 642 if (fs->icache->cache[i].ino == ino) { 643 fs->icache->cache[i].inode = *inode; 644 break; 645 } 646 } 647 } else { 648 retval = create_icache(fs); 649 if (retval) 650 return retval; 651 } 652 653 if (!(fs->flags & EXT2_FLAG_RW)) 654 return EXT2_ET_RO_FILSYS; 655 656 if ((ino == 0) || (ino > fs->super->s_inodes_count)) 657 return EXT2_ET_BAD_INODE_NUM; 658 659 length = bufsize; 660 if (length < EXT2_INODE_SIZE(fs->super)) 661 length = EXT2_INODE_SIZE(fs->super); 662 663 if (length > (int) sizeof(struct ext2_inode_large)) { 664 w_inode = malloc(length); 665 if (!w_inode) 666 return ENOMEM; 667 } else 668 w_inode = &temp_inode; 669 memset(w_inode, 0, length); 670 671#ifdef WORDS_BIGENDIAN 672 ext2fs_swap_inode_full(fs, w_inode, 673 (struct ext2_inode_large *) inode, 674 1, bufsize); 675#else 676 memcpy(w_inode, inode, bufsize); 677#endif 678 679 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super); 680 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) * 681 EXT2_INODE_SIZE(fs->super); 682 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super); 683 if (!fs->group_desc[(unsigned) group].bg_inode_table) { 684 retval = EXT2_ET_MISSING_INODE_TABLE; 685 goto errout; 686 } 687 block_nr = fs->group_desc[(unsigned) group].bg_inode_table + block; 688 689 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1); 690 691 length = EXT2_INODE_SIZE(fs->super); 692 if (length > bufsize) 693 length = bufsize; 694 695 ptr = (char *) w_inode; 696 697 while (length) { 698 clen = length; 699 if ((offset + length) > fs->blocksize) 700 clen = fs->blocksize - offset; 701 702 if (fs->icache->buffer_blk != block_nr) { 703 retval = io_channel_read_blk(fs->io, block_nr, 1, 704 fs->icache->buffer); 705 if (retval) 706 goto errout; 707 fs->icache->buffer_blk = block_nr; 708 } 709 710 711 memcpy((char *) fs->icache->buffer + (unsigned) offset, 712 ptr, clen); 713 714 retval = io_channel_write_blk(fs->io, block_nr, 1, 715 fs->icache->buffer); 716 if (retval) 717 goto errout; 718 719 offset = 0; 720 ptr += clen; 721 length -= clen; 722 block_nr++; 723 } 724 725 fs->flags |= EXT2_FLAG_CHANGED; 726errout: 727 if (w_inode && w_inode != &temp_inode) 728 free(w_inode); 729 return retval; 730} 731 732errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino, 733 struct ext2_inode *inode) 734{ 735 return ext2fs_write_inode_full(fs, ino, inode, 736 sizeof(struct ext2_inode)); 737} 738 739/* 740 * This function should be called when writing a new inode. It makes 741 * sure that extra part of large inodes is initialized properly. 742 */ 743errcode_t ext2fs_write_new_inode(ext2_filsys fs, ext2_ino_t ino, 744 struct ext2_inode *inode) 745{ 746 struct ext2_inode *buf; 747 int size = EXT2_INODE_SIZE(fs->super); 748 struct ext2_inode_large *large_inode; 749 errcode_t retval; 750 751 if (size == sizeof(struct ext2_inode)) 752 return ext2fs_write_inode_full(fs, ino, inode, 753 sizeof(struct ext2_inode)); 754 755 buf = malloc(size); 756 if (!buf) 757 return ENOMEM; 758 759 memset(buf, 0, size); 760 *buf = *inode; 761 762 large_inode = (struct ext2_inode_large *) buf; 763 large_inode->i_extra_isize = sizeof(struct ext2_inode_large) - 764 EXT2_GOOD_OLD_INODE_SIZE; 765 766 retval = ext2fs_write_inode_full(fs, ino, buf, size); 767 free(buf); 768 return retval; 769} 770 771 772errcode_t ext2fs_get_blocks(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks) 773{ 774 struct ext2_inode inode; 775 int i; 776 errcode_t retval; 777 778 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); 779 780 if (ino > fs->super->s_inodes_count) 781 return EXT2_ET_BAD_INODE_NUM; 782 783 if (fs->get_blocks) { 784 if (!(*fs->get_blocks)(fs, ino, blocks)) 785 return 0; 786 } 787 retval = ext2fs_read_inode(fs, ino, &inode); 788 if (retval) 789 return retval; 790 for (i=0; i < EXT2_N_BLOCKS; i++) 791 blocks[i] = inode.i_block[i]; 792 return 0; 793} 794 795errcode_t ext2fs_check_directory(ext2_filsys fs, ext2_ino_t ino) 796{ 797 struct ext2_inode inode; 798 errcode_t retval; 799 800 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); 801 802 if (ino > fs->super->s_inodes_count) 803 return EXT2_ET_BAD_INODE_NUM; 804 805 if (fs->check_directory) { 806 retval = (fs->check_directory)(fs, ino); 807 if (retval != EXT2_ET_CALLBACK_NOTHANDLED) 808 return retval; 809 } 810 retval = ext2fs_read_inode(fs, ino, &inode); 811 if (retval) 812 return retval; 813 if (!LINUX_S_ISDIR(inode.i_mode)) 814 return EXT2_ET_NO_DIRECTORY; 815 return 0; 816} 817 818