inode.c revision 6f19f44a4c180079b402070efa6733da02eaac3e
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 (scan->inodes_left + 243 (fs->blocksize / scan->inode_size - 1)) * 244 scan->inode_size / fs->blocksize; 245 } 246 247 return 0; 248} 249 250errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan, 251 int group) 252{ 253 scan->current_group = group - 1; 254 scan->groups_left = scan->fs->group_desc_count - group; 255 return get_next_blockgroup(scan); 256} 257 258/* 259 * This function is called by get_next_blocks() to check for bad 260 * blocks in the inode table. 261 * 262 * This function assumes that badblocks_list->list is sorted in 263 * increasing order. 264 */ 265static errcode_t check_for_inode_bad_blocks(ext2_inode_scan scan, 266 blk_t *num_blocks) 267{ 268 blk_t blk = scan->current_block; 269 badblocks_list bb = scan->fs->badblocks; 270 271 /* 272 * If the inode table is missing, then obviously there are no 273 * bad blocks. :-) 274 */ 275 if (blk == 0) 276 return 0; 277 278 /* 279 * If the current block is greater than the bad block listed 280 * in the bad block list, then advance the pointer until this 281 * is no longer the case. If we run out of bad blocks, then 282 * we don't need to do any more checking! 283 */ 284 while (blk > bb->list[scan->bad_block_ptr]) { 285 if (++scan->bad_block_ptr >= bb->num) { 286 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS; 287 return 0; 288 } 289 } 290 291 /* 292 * If the current block is equal to the bad block listed in 293 * the bad block list, then handle that one block specially. 294 * (We could try to handle runs of bad blocks, but that 295 * only increases CPU efficiency by a small amount, at the 296 * expense of a huge expense of code complexity, and for an 297 * uncommon case at that.) 298 */ 299 if (blk == bb->list[scan->bad_block_ptr]) { 300 scan->scan_flags |= EXT2_SF_BAD_INODE_BLK; 301 *num_blocks = 1; 302 if (++scan->bad_block_ptr >= bb->num) 303 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS; 304 return 0; 305 } 306 307 /* 308 * If there is a bad block in the range that we're about to 309 * read in, adjust the number of blocks to read so that we we 310 * don't read in the bad block. (Then the next block to read 311 * will be the bad block, which is handled in the above case.) 312 */ 313 if ((blk + *num_blocks) > bb->list[scan->bad_block_ptr]) 314 *num_blocks = (int) (bb->list[scan->bad_block_ptr] - blk); 315 316 return 0; 317} 318 319/* 320 * This function is called by ext2fs_get_next_inode when it needs to 321 * read in more blocks from the current blockgroup's inode table. 322 */ 323static errcode_t get_next_blocks(ext2_inode_scan scan) 324{ 325 blk_t num_blocks; 326 errcode_t retval; 327 328 /* 329 * Figure out how many blocks to read; we read at most 330 * inode_buffer_blocks, and perhaps less if there aren't that 331 * many blocks left to read. 332 */ 333 num_blocks = scan->inode_buffer_blocks; 334 if (num_blocks > scan->blocks_left) 335 num_blocks = scan->blocks_left; 336 337 /* 338 * If the past block "read" was a bad block, then mark the 339 * left-over extra bytes as also being bad. 340 */ 341 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) { 342 if (scan->bytes_left) 343 scan->scan_flags |= EXT2_SF_BAD_EXTRA_BYTES; 344 scan->scan_flags &= ~EXT2_SF_BAD_INODE_BLK; 345 } 346 347 /* 348 * Do inode bad block processing, if necessary. 349 */ 350 if (scan->scan_flags & EXT2_SF_CHK_BADBLOCKS) { 351 retval = check_for_inode_bad_blocks(scan, &num_blocks); 352 if (retval) 353 return retval; 354 } 355 356 if ((scan->scan_flags & EXT2_SF_BAD_INODE_BLK) || 357 (scan->current_block == 0)) { 358 memset(scan->inode_buffer, 0, 359 (size_t) num_blocks * scan->fs->blocksize); 360 } else { 361 retval = io_channel_read_blk(scan->fs->io, 362 scan->current_block, 363 (int) num_blocks, 364 scan->inode_buffer); 365 if (retval) 366 return EXT2_ET_NEXT_INODE_READ; 367 } 368 scan->ptr = scan->inode_buffer; 369 scan->bytes_left = num_blocks * scan->fs->blocksize; 370 371 scan->blocks_left -= num_blocks; 372 if (scan->current_block) 373 scan->current_block += num_blocks; 374 return 0; 375} 376 377#if 0 378/* 379 * Returns 1 if the entire inode_buffer has a non-zero size and 380 * contains all zeros. (Not just deleted inodes, since that means 381 * that part of the inode table was used at one point; we want all 382 * zeros, which means that the inode table is pristine.) 383 */ 384static inline int is_empty_scan(ext2_inode_scan scan) 385{ 386 int i; 387 388 if (scan->bytes_left == 0) 389 return 0; 390 391 for (i=0; i < scan->bytes_left; i++) 392 if (scan->ptr[i]) 393 return 0; 394 return 1; 395} 396#endif 397 398errcode_t ext2fs_get_next_inode_full(ext2_inode_scan scan, ext2_ino_t *ino, 399 struct ext2_inode *inode, int bufsize) 400{ 401 errcode_t retval; 402 int extra_bytes = 0; 403 404 EXT2_CHECK_MAGIC(scan, EXT2_ET_MAGIC_INODE_SCAN); 405 406 /* 407 * Do we need to start reading a new block group? 408 */ 409 if (scan->inodes_left <= 0) { 410 force_new_group: 411 if (scan->done_group) { 412 retval = (scan->done_group) 413 (scan->fs, scan, scan->current_group, 414 scan->done_group_data); 415 if (retval) 416 return retval; 417 } 418 if (scan->groups_left <= 0) { 419 *ino = 0; 420 return 0; 421 } 422 retval = get_next_blockgroup(scan); 423 if (retval) 424 return retval; 425 } 426 /* 427 * These checks are done outside the above if statement so 428 * they can be done for block group #0. 429 */ 430 if ((scan->scan_flags & EXT2_SF_DO_LAZY) && 431 (scan->fs->group_desc[scan->current_group].bg_flags & 432 EXT2_BG_INODE_UNINIT)) 433 goto force_new_group; 434 if (scan->inodes_left == 0) 435 goto force_new_group; 436 if (scan->current_block == 0) { 437 if (scan->scan_flags & EXT2_SF_SKIP_MISSING_ITABLE) { 438 goto force_new_group; 439 } else 440 return EXT2_ET_MISSING_INODE_TABLE; 441 } 442 443 444 /* 445 * Have we run out of space in the inode buffer? If so, we 446 * need to read in more blocks. 447 */ 448 if (scan->bytes_left < scan->inode_size) { 449 memcpy(scan->temp_buffer, scan->ptr, scan->bytes_left); 450 extra_bytes = scan->bytes_left; 451 452 retval = get_next_blocks(scan); 453 if (retval) 454 return retval; 455#if 0 456 /* 457 * XXX test Need check for used inode somehow. 458 * (Note: this is hard.) 459 */ 460 if (is_empty_scan(scan)) 461 goto force_new_group; 462#endif 463 } 464 465 retval = 0; 466 if (extra_bytes) { 467 memcpy(scan->temp_buffer+extra_bytes, scan->ptr, 468 scan->inode_size - extra_bytes); 469 scan->ptr += scan->inode_size - extra_bytes; 470 scan->bytes_left -= scan->inode_size - extra_bytes; 471 472#ifdef WORDS_BIGENDIAN 473 memset(inode, 0, bufsize); 474 ext2fs_swap_inode_full(scan->fs, 475 (struct ext2_inode_large *) inode, 476 (struct ext2_inode_large *) scan->temp_buffer, 477 0, bufsize); 478#else 479 *inode = *((struct ext2_inode *) scan->temp_buffer); 480#endif 481 if (scan->scan_flags & EXT2_SF_BAD_EXTRA_BYTES) 482 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE; 483 scan->scan_flags &= ~EXT2_SF_BAD_EXTRA_BYTES; 484 } else { 485#ifdef WORDS_BIGENDIAN 486 memset(inode, 0, bufsize); 487 ext2fs_swap_inode_full(scan->fs, 488 (struct ext2_inode_large *) inode, 489 (struct ext2_inode_large *) scan->ptr, 490 0, bufsize); 491#else 492 memcpy(inode, scan->ptr, bufsize); 493#endif 494 scan->ptr += scan->inode_size; 495 scan->bytes_left -= scan->inode_size; 496 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) 497 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE; 498 } 499 500 scan->inodes_left--; 501 scan->current_inode++; 502 *ino = scan->current_inode; 503 return retval; 504} 505 506errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ext2_ino_t *ino, 507 struct ext2_inode *inode) 508{ 509 return ext2fs_get_next_inode_full(scan, ino, inode, 510 sizeof(struct ext2_inode)); 511} 512 513/* 514 * Functions to read and write a single inode. 515 */ 516errcode_t ext2fs_read_inode_full(ext2_filsys fs, ext2_ino_t ino, 517 struct ext2_inode * inode, int bufsize) 518{ 519 unsigned long group, block, block_nr, offset; 520 char *ptr; 521 errcode_t retval; 522 int clen, i, inodes_per_block, length; 523 io_channel io; 524 525 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); 526 527 /* Check to see if user has an override function */ 528 if (fs->read_inode) { 529 retval = (fs->read_inode)(fs, ino, inode); 530 if (retval != EXT2_ET_CALLBACK_NOTHANDLED) 531 return retval; 532 } 533 /* Create inode cache if not present */ 534 if (!fs->icache) { 535 retval = create_icache(fs); 536 if (retval) 537 return retval; 538 } 539 /* Check to see if it's in the inode cache */ 540 if (bufsize == sizeof(struct ext2_inode)) { 541 /* only old good inode can be retrieve from the cache */ 542 for (i=0; i < fs->icache->cache_size; i++) { 543 if (fs->icache->cache[i].ino == ino) { 544 *inode = fs->icache->cache[i].inode; 545 return 0; 546 } 547 } 548 } 549 if ((ino == 0) || (ino > fs->super->s_inodes_count)) 550 return EXT2_ET_BAD_INODE_NUM; 551 if (fs->flags & EXT2_FLAG_IMAGE_FILE) { 552 inodes_per_block = fs->blocksize / EXT2_INODE_SIZE(fs->super); 553 block_nr = fs->image_header->offset_inode / fs->blocksize; 554 block_nr += (ino - 1) / inodes_per_block; 555 offset = ((ino - 1) % inodes_per_block) * 556 EXT2_INODE_SIZE(fs->super); 557 io = fs->image_io; 558 } else { 559 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super); 560 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) * 561 EXT2_INODE_SIZE(fs->super); 562 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super); 563 if (!fs->group_desc[(unsigned)group].bg_inode_table) 564 return EXT2_ET_MISSING_INODE_TABLE; 565 block_nr = fs->group_desc[(unsigned)group].bg_inode_table + 566 block; 567 io = fs->io; 568 } 569 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1); 570 571 length = EXT2_INODE_SIZE(fs->super); 572 if (bufsize < length) 573 length = bufsize; 574 575 ptr = (char *) inode; 576 while (length) { 577 clen = length; 578 if ((offset + length) > fs->blocksize) 579 clen = fs->blocksize - offset; 580 581 if (block_nr != fs->icache->buffer_blk) { 582 retval = io_channel_read_blk(io, block_nr, 1, 583 fs->icache->buffer); 584 if (retval) 585 return retval; 586 fs->icache->buffer_blk = block_nr; 587 } 588 589 memcpy(ptr, ((char *) fs->icache->buffer) + (unsigned) offset, 590 clen); 591 592 offset = 0; 593 length -= clen; 594 ptr += clen; 595 block_nr++; 596 } 597 598#ifdef WORDS_BIGENDIAN 599 ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) inode, 600 (struct ext2_inode_large *) inode, 601 0, bufsize); 602#endif 603 604 /* Update the inode cache */ 605 fs->icache->cache_last = (fs->icache->cache_last + 1) % 606 fs->icache->cache_size; 607 fs->icache->cache[fs->icache->cache_last].ino = ino; 608 fs->icache->cache[fs->icache->cache_last].inode = *inode; 609 610 return 0; 611} 612 613errcode_t ext2fs_read_inode(ext2_filsys fs, ext2_ino_t ino, 614 struct ext2_inode * inode) 615{ 616 return ext2fs_read_inode_full(fs, ino, inode, 617 sizeof(struct ext2_inode)); 618} 619 620errcode_t ext2fs_write_inode_full(ext2_filsys fs, ext2_ino_t ino, 621 struct ext2_inode * inode, int bufsize) 622{ 623 unsigned long group, block, block_nr, offset; 624 errcode_t retval = 0; 625 struct ext2_inode_large temp_inode, *w_inode; 626 char *ptr; 627 int clen, i, length; 628 629 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); 630 631 /* Check to see if user provided an override function */ 632 if (fs->write_inode) { 633 retval = (fs->write_inode)(fs, ino, inode); 634 if (retval != EXT2_ET_CALLBACK_NOTHANDLED) 635 return retval; 636 } 637 638 /* Check to see if the inode cache needs to be updated */ 639 if (fs->icache) { 640 for (i=0; i < fs->icache->cache_size; i++) { 641 if (fs->icache->cache[i].ino == ino) { 642 fs->icache->cache[i].inode = *inode; 643 break; 644 } 645 } 646 } else { 647 retval = create_icache(fs); 648 if (retval) 649 return retval; 650 } 651 652 if (!(fs->flags & EXT2_FLAG_RW)) 653 return EXT2_ET_RO_FILSYS; 654 655 if ((ino == 0) || (ino > fs->super->s_inodes_count)) 656 return EXT2_ET_BAD_INODE_NUM; 657 658 length = bufsize; 659 if (length < EXT2_INODE_SIZE(fs->super)) 660 length = EXT2_INODE_SIZE(fs->super); 661 662 if (length > (int) sizeof(struct ext2_inode_large)) { 663 w_inode = malloc(length); 664 if (!w_inode) 665 return ENOMEM; 666 } else 667 w_inode = &temp_inode; 668 memset(w_inode, 0, length); 669 670#ifdef WORDS_BIGENDIAN 671 ext2fs_swap_inode_full(fs, w_inode, 672 (struct ext2_inode_large *) inode, 673 1, bufsize); 674#else 675 memcpy(w_inode, inode, bufsize); 676#endif 677 678 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super); 679 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) * 680 EXT2_INODE_SIZE(fs->super); 681 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super); 682 if (!fs->group_desc[(unsigned) group].bg_inode_table) { 683 retval = EXT2_ET_MISSING_INODE_TABLE; 684 goto errout; 685 } 686 block_nr = fs->group_desc[(unsigned) group].bg_inode_table + block; 687 688 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1); 689 690 length = EXT2_INODE_SIZE(fs->super); 691 if (length > bufsize) 692 length = bufsize; 693 694 ptr = (char *) w_inode; 695 696 while (length) { 697 clen = length; 698 if ((offset + length) > fs->blocksize) 699 clen = fs->blocksize - offset; 700 701 if (fs->icache->buffer_blk != block_nr) { 702 retval = io_channel_read_blk(fs->io, block_nr, 1, 703 fs->icache->buffer); 704 if (retval) 705 goto errout; 706 fs->icache->buffer_blk = block_nr; 707 } 708 709 710 memcpy((char *) fs->icache->buffer + (unsigned) offset, 711 ptr, clen); 712 713 retval = io_channel_write_blk(fs->io, block_nr, 1, 714 fs->icache->buffer); 715 if (retval) 716 goto errout; 717 718 offset = 0; 719 ptr += clen; 720 length -= clen; 721 block_nr++; 722 } 723 724 fs->flags |= EXT2_FLAG_CHANGED; 725errout: 726 if (w_inode && w_inode != &temp_inode) 727 free(w_inode); 728 return retval; 729} 730 731errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino, 732 struct ext2_inode *inode) 733{ 734 return ext2fs_write_inode_full(fs, ino, inode, 735 sizeof(struct ext2_inode)); 736} 737 738/* 739 * This function should be called when writing a new inode. It makes 740 * sure that extra part of large inodes is initialized properly. 741 */ 742errcode_t ext2fs_write_new_inode(ext2_filsys fs, ext2_ino_t ino, 743 struct ext2_inode *inode) 744{ 745 struct ext2_inode *buf; 746 int size = EXT2_INODE_SIZE(fs->super); 747 struct ext2_inode_large *large_inode; 748 errcode_t retval; 749 750 if (size == sizeof(struct ext2_inode)) 751 return ext2fs_write_inode_full(fs, ino, inode, 752 sizeof(struct ext2_inode)); 753 754 buf = malloc(size); 755 if (!buf) 756 return ENOMEM; 757 758 memset(buf, 0, size); 759 *buf = *inode; 760 761 large_inode = (struct ext2_inode_large *) buf; 762 large_inode->i_extra_isize = sizeof(struct ext2_inode_large) - 763 EXT2_GOOD_OLD_INODE_SIZE; 764 765 retval = ext2fs_write_inode_full(fs, ino, buf, size); 766 free(buf); 767 return retval; 768} 769 770 771errcode_t ext2fs_get_blocks(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks) 772{ 773 struct ext2_inode inode; 774 int i; 775 errcode_t retval; 776 777 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); 778 779 if (ino > fs->super->s_inodes_count) 780 return EXT2_ET_BAD_INODE_NUM; 781 782 if (fs->get_blocks) { 783 if (!(*fs->get_blocks)(fs, ino, blocks)) 784 return 0; 785 } 786 retval = ext2fs_read_inode(fs, ino, &inode); 787 if (retval) 788 return retval; 789 for (i=0; i < EXT2_N_BLOCKS; i++) 790 blocks[i] = inode.i_block[i]; 791 return 0; 792} 793 794errcode_t ext2fs_check_directory(ext2_filsys fs, ext2_ino_t ino) 795{ 796 struct ext2_inode inode; 797 errcode_t retval; 798 799 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); 800 801 if (ino > fs->super->s_inodes_count) 802 return EXT2_ET_BAD_INODE_NUM; 803 804 if (fs->check_directory) { 805 retval = (fs->check_directory)(fs, ino); 806 if (retval != EXT2_ET_CALLBACK_NOTHANDLED) 807 return retval; 808 } 809 retval = ext2fs_read_inode(fs, ino, &inode); 810 if (retval) 811 return retval; 812 if (!LINUX_S_ISDIR(inode.i_mode)) 813 return EXT2_ET_NO_DIRECTORY; 814 return 0; 815} 816 817