1 /* 2 * (C) Copyright 2011 - 2012 Samsung Electronics 3 * EXT4 filesystem implementation in Uboot by 4 * Uma Shankar <uma.shankar@samsung.com> 5 * Manjunatha C Achar <a.manjunatha@samsung.com> 6 * 7 * ext4ls and ext4load : Based on ext2 ls load support in Uboot. 8 * 9 * (C) Copyright 2004 10 * esd gmbh <www.esd-electronics.com> 11 * Reinhard Arlt <reinhard.arlt@esd-electronics.com> 12 * 13 * based on code from grub2 fs/ext2.c and fs/fshelp.c by 14 * GRUB -- GRand Unified Bootloader 15 * Copyright (C) 2003, 2004 Free Software Foundation, Inc. 16 * 17 * ext4write : Based on generic ext4 protocol. 18 * 19 * SPDX-License-Identifier: GPL-2.0+ 20 */ 21 22 #include <common.h> 23 #include <ext_common.h> 24 #include <ext4fs.h> 25 #include <inttypes.h> 26 #include <malloc.h> 27 #include <memalign.h> 28 #include <stddef.h> 29 #include <linux/stat.h> 30 #include <linux/time.h> 31 #include <asm/byteorder.h> 32 #include "ext4_common.h" 33 34 struct ext2_data *ext4fs_root; 35 struct ext2fs_node *ext4fs_file; 36 uint32_t *ext4fs_indir1_block; 37 int ext4fs_indir1_size; 38 int ext4fs_indir1_blkno = -1; 39 uint32_t *ext4fs_indir2_block; 40 int ext4fs_indir2_size; 41 int ext4fs_indir2_blkno = -1; 42 43 uint32_t *ext4fs_indir3_block; 44 int ext4fs_indir3_size; 45 int ext4fs_indir3_blkno = -1; 46 struct ext2_inode *g_parent_inode; 47 static int symlinknest; 48 49 #if defined(CONFIG_EXT4_WRITE) 50 uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n) 51 { 52 uint32_t res = size / n; 53 if (res * n != size) 54 res++; 55 56 return res; 57 } 58 59 void put_ext4(uint64_t off, void *buf, uint32_t size) 60 { 61 uint64_t startblock; 62 uint64_t remainder; 63 unsigned char *temp_ptr = NULL; 64 struct ext_filesystem *fs = get_fs(); 65 int log2blksz = fs->dev_desc->log2blksz; 66 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf, fs->dev_desc->blksz); 67 68 startblock = off >> log2blksz; 69 startblock += part_offset; 70 remainder = off & (uint64_t)(fs->dev_desc->blksz - 1); 71 72 if (fs->dev_desc == NULL) 73 return; 74 75 if ((startblock + (size >> log2blksz)) > 76 (part_offset + fs->total_sect)) { 77 printf("part_offset is " LBAFU "\n", part_offset); 78 printf("total_sector is %" PRIu64 "\n", fs->total_sect); 79 printf("error: overflow occurs\n"); 80 return; 81 } 82 83 if (remainder) { 84 if (fs->dev_desc->block_read) { 85 fs->dev_desc->block_read(fs->dev_desc->dev, 86 startblock, 1, sec_buf); 87 temp_ptr = sec_buf; 88 memcpy((temp_ptr + remainder), 89 (unsigned char *)buf, size); 90 fs->dev_desc->block_write(fs->dev_desc->dev, 91 startblock, 1, sec_buf); 92 } 93 } else { 94 if (size >> log2blksz != 0) { 95 fs->dev_desc->block_write(fs->dev_desc->dev, 96 startblock, 97 size >> log2blksz, 98 (unsigned long *)buf); 99 } else { 100 fs->dev_desc->block_read(fs->dev_desc->dev, 101 startblock, 1, sec_buf); 102 temp_ptr = sec_buf; 103 memcpy(temp_ptr, buf, size); 104 fs->dev_desc->block_write(fs->dev_desc->dev, 105 startblock, 1, 106 (unsigned long *)sec_buf); 107 } 108 } 109 } 110 111 static int _get_new_inode_no(unsigned char *buffer) 112 { 113 struct ext_filesystem *fs = get_fs(); 114 unsigned char input; 115 int operand, status; 116 int count = 1; 117 int j = 0; 118 119 /* get the blocksize of the filesystem */ 120 unsigned char *ptr = buffer; 121 while (*ptr == 255) { 122 ptr++; 123 count += 8; 124 if (count > ext4fs_root->sblock.inodes_per_group) 125 return -1; 126 } 127 128 for (j = 0; j < fs->blksz; j++) { 129 input = *ptr; 130 int i = 0; 131 while (i <= 7) { 132 operand = 1 << i; 133 status = input & operand; 134 if (status) { 135 i++; 136 count++; 137 } else { 138 *ptr |= operand; 139 return count; 140 } 141 } 142 ptr = ptr + 1; 143 } 144 145 return -1; 146 } 147 148 static int _get_new_blk_no(unsigned char *buffer) 149 { 150 unsigned char input; 151 int operand, status; 152 int count = 0; 153 int j = 0; 154 unsigned char *ptr = buffer; 155 struct ext_filesystem *fs = get_fs(); 156 157 if (fs->blksz != 1024) 158 count = 0; 159 else 160 count = 1; 161 162 while (*ptr == 255) { 163 ptr++; 164 count += 8; 165 if (count == (fs->blksz * 8)) 166 return -1; 167 } 168 169 for (j = 0; j < fs->blksz; j++) { 170 input = *ptr; 171 int i = 0; 172 while (i <= 7) { 173 operand = 1 << i; 174 status = input & operand; 175 if (status) { 176 i++; 177 count++; 178 } else { 179 *ptr |= operand; 180 return count; 181 } 182 } 183 ptr = ptr + 1; 184 } 185 186 return -1; 187 } 188 189 int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index) 190 { 191 int i, remainder, status; 192 unsigned char *ptr = buffer; 193 unsigned char operand; 194 i = blockno / 8; 195 remainder = blockno % 8; 196 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root); 197 198 i = i - (index * blocksize); 199 if (blocksize != 1024) { 200 ptr = ptr + i; 201 operand = 1 << remainder; 202 status = *ptr & operand; 203 if (status) 204 return -1; 205 206 *ptr = *ptr | operand; 207 return 0; 208 } else { 209 if (remainder == 0) { 210 ptr = ptr + i - 1; 211 operand = (1 << 7); 212 } else { 213 ptr = ptr + i; 214 operand = (1 << (remainder - 1)); 215 } 216 status = *ptr & operand; 217 if (status) 218 return -1; 219 220 *ptr = *ptr | operand; 221 return 0; 222 } 223 } 224 225 void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, int index) 226 { 227 int i, remainder, status; 228 unsigned char *ptr = buffer; 229 unsigned char operand; 230 i = blockno / 8; 231 remainder = blockno % 8; 232 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root); 233 234 i = i - (index * blocksize); 235 if (blocksize != 1024) { 236 ptr = ptr + i; 237 operand = (1 << remainder); 238 status = *ptr & operand; 239 if (status) 240 *ptr = *ptr & ~(operand); 241 } else { 242 if (remainder == 0) { 243 ptr = ptr + i - 1; 244 operand = (1 << 7); 245 } else { 246 ptr = ptr + i; 247 operand = (1 << (remainder - 1)); 248 } 249 status = *ptr & operand; 250 if (status) 251 *ptr = *ptr & ~(operand); 252 } 253 } 254 255 int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index) 256 { 257 int i, remainder, status; 258 unsigned char *ptr = buffer; 259 unsigned char operand; 260 261 inode_no -= (index * ext4fs_root->sblock.inodes_per_group); 262 i = inode_no / 8; 263 remainder = inode_no % 8; 264 if (remainder == 0) { 265 ptr = ptr + i - 1; 266 operand = (1 << 7); 267 } else { 268 ptr = ptr + i; 269 operand = (1 << (remainder - 1)); 270 } 271 status = *ptr & operand; 272 if (status) 273 return -1; 274 275 *ptr = *ptr | operand; 276 277 return 0; 278 } 279 280 void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index) 281 { 282 int i, remainder, status; 283 unsigned char *ptr = buffer; 284 unsigned char operand; 285 286 inode_no -= (index * ext4fs_root->sblock.inodes_per_group); 287 i = inode_no / 8; 288 remainder = inode_no % 8; 289 if (remainder == 0) { 290 ptr = ptr + i - 1; 291 operand = (1 << 7); 292 } else { 293 ptr = ptr + i; 294 operand = (1 << (remainder - 1)); 295 } 296 status = *ptr & operand; 297 if (status) 298 *ptr = *ptr & ~(operand); 299 } 300 301 int ext4fs_checksum_update(unsigned int i) 302 { 303 struct ext2_block_group *desc; 304 struct ext_filesystem *fs = get_fs(); 305 __u16 crc = 0; 306 307 desc = (struct ext2_block_group *)&fs->bgd[i]; 308 if (fs->sb->feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) { 309 int offset = offsetof(struct ext2_block_group, bg_checksum); 310 311 crc = ext2fs_crc16(~0, fs->sb->unique_id, 312 sizeof(fs->sb->unique_id)); 313 crc = ext2fs_crc16(crc, &i, sizeof(i)); 314 crc = ext2fs_crc16(crc, desc, offset); 315 offset += sizeof(desc->bg_checksum); /* skip checksum */ 316 assert(offset == sizeof(*desc)); 317 } 318 319 return crc; 320 } 321 322 static int check_void_in_dentry(struct ext2_dirent *dir, char *filename) 323 { 324 int dentry_length; 325 int sizeof_void_space; 326 int new_entry_byte_reqd; 327 short padding_factor = 0; 328 329 if (dir->namelen % 4 != 0) 330 padding_factor = 4 - (dir->namelen % 4); 331 332 dentry_length = sizeof(struct ext2_dirent) + 333 dir->namelen + padding_factor; 334 sizeof_void_space = dir->direntlen - dentry_length; 335 if (sizeof_void_space == 0) 336 return 0; 337 338 padding_factor = 0; 339 if (strlen(filename) % 4 != 0) 340 padding_factor = 4 - (strlen(filename) % 4); 341 342 new_entry_byte_reqd = strlen(filename) + 343 sizeof(struct ext2_dirent) + padding_factor; 344 if (sizeof_void_space >= new_entry_byte_reqd) { 345 dir->direntlen = dentry_length; 346 return sizeof_void_space; 347 } 348 349 return 0; 350 } 351 352 void ext4fs_update_parent_dentry(char *filename, int *p_ino, int file_type) 353 { 354 unsigned int *zero_buffer = NULL; 355 char *root_first_block_buffer = NULL; 356 int direct_blk_idx; 357 long int root_blknr; 358 long int first_block_no_of_root = 0; 359 long int previous_blknr = -1; 360 int totalbytes = 0; 361 short int padding_factor = 0; 362 unsigned int new_entry_byte_reqd; 363 unsigned int last_entry_dirlen; 364 int sizeof_void_space = 0; 365 int templength = 0; 366 int inodeno; 367 int status; 368 struct ext_filesystem *fs = get_fs(); 369 /* directory entry */ 370 struct ext2_dirent *dir; 371 char *temp_dir = NULL; 372 373 zero_buffer = zalloc(fs->blksz); 374 if (!zero_buffer) { 375 printf("No Memory\n"); 376 return; 377 } 378 root_first_block_buffer = zalloc(fs->blksz); 379 if (!root_first_block_buffer) { 380 free(zero_buffer); 381 printf("No Memory\n"); 382 return; 383 } 384 restart: 385 386 /* read the block no allocated to a file */ 387 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS; 388 direct_blk_idx++) { 389 root_blknr = read_allocated_block(g_parent_inode, 390 direct_blk_idx); 391 if (root_blknr == 0) { 392 first_block_no_of_root = previous_blknr; 393 break; 394 } 395 previous_blknr = root_blknr; 396 } 397 398 status = ext4fs_devread((lbaint_t)first_block_no_of_root 399 * fs->sect_perblk, 400 0, fs->blksz, root_first_block_buffer); 401 if (status == 0) 402 goto fail; 403 404 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root)) 405 goto fail; 406 dir = (struct ext2_dirent *)root_first_block_buffer; 407 totalbytes = 0; 408 while (dir->direntlen > 0) { 409 /* 410 * blocksize-totalbytes because last directory length 411 * i.e. dir->direntlen is free availble space in the 412 * block that means it is a last entry of directory 413 * entry 414 */ 415 416 /* traversing the each directory entry */ 417 if (fs->blksz - totalbytes == dir->direntlen) { 418 if (strlen(filename) % 4 != 0) 419 padding_factor = 4 - (strlen(filename) % 4); 420 421 new_entry_byte_reqd = strlen(filename) + 422 sizeof(struct ext2_dirent) + padding_factor; 423 padding_factor = 0; 424 /* 425 * update last directory entry length to its 426 * length because we are creating new directory 427 * entry 428 */ 429 if (dir->namelen % 4 != 0) 430 padding_factor = 4 - (dir->namelen % 4); 431 432 last_entry_dirlen = dir->namelen + 433 sizeof(struct ext2_dirent) + padding_factor; 434 if ((fs->blksz - totalbytes - last_entry_dirlen) < 435 new_entry_byte_reqd) { 436 printf("1st Block Full:Allocate new block\n"); 437 438 if (direct_blk_idx == INDIRECT_BLOCKS - 1) { 439 printf("Directory exceeds limit\n"); 440 goto fail; 441 } 442 g_parent_inode->b.blocks.dir_blocks 443 [direct_blk_idx] = ext4fs_get_new_blk_no(); 444 if (g_parent_inode->b.blocks.dir_blocks 445 [direct_blk_idx] == -1) { 446 printf("no block left to assign\n"); 447 goto fail; 448 } 449 put_ext4(((uint64_t) 450 ((uint64_t)g_parent_inode->b. 451 blocks.dir_blocks[direct_blk_idx] * 452 (uint64_t)fs->blksz)), zero_buffer, fs->blksz); 453 g_parent_inode->size = 454 g_parent_inode->size + fs->blksz; 455 g_parent_inode->blockcnt = 456 g_parent_inode->blockcnt + fs->sect_perblk; 457 if (ext4fs_put_metadata 458 (root_first_block_buffer, 459 first_block_no_of_root)) 460 goto fail; 461 goto restart; 462 } 463 dir->direntlen = last_entry_dirlen; 464 break; 465 } 466 467 templength = dir->direntlen; 468 totalbytes = totalbytes + templength; 469 sizeof_void_space = check_void_in_dentry(dir, filename); 470 if (sizeof_void_space) 471 break; 472 473 dir = (struct ext2_dirent *)((char *)dir + templength); 474 } 475 476 /* make a pointer ready for creating next directory entry */ 477 templength = dir->direntlen; 478 totalbytes = totalbytes + templength; 479 dir = (struct ext2_dirent *)((char *)dir + templength); 480 481 /* get the next available inode number */ 482 inodeno = ext4fs_get_new_inode_no(); 483 if (inodeno == -1) { 484 printf("no inode left to assign\n"); 485 goto fail; 486 } 487 dir->inode = inodeno; 488 if (sizeof_void_space) 489 dir->direntlen = sizeof_void_space; 490 else 491 dir->direntlen = fs->blksz - totalbytes; 492 493 dir->namelen = strlen(filename); 494 dir->filetype = FILETYPE_REG; /* regular file */ 495 temp_dir = (char *)dir; 496 temp_dir = temp_dir + sizeof(struct ext2_dirent); 497 memcpy(temp_dir, filename, strlen(filename)); 498 499 *p_ino = inodeno; 500 501 /* update or write the 1st block of root inode */ 502 if (ext4fs_put_metadata(root_first_block_buffer, 503 first_block_no_of_root)) 504 goto fail; 505 506 fail: 507 free(zero_buffer); 508 free(root_first_block_buffer); 509 } 510 511 static int search_dir(struct ext2_inode *parent_inode, char *dirname) 512 { 513 int status; 514 int inodeno; 515 int totalbytes; 516 int templength; 517 int direct_blk_idx; 518 long int blknr; 519 int found = 0; 520 char *ptr = NULL; 521 unsigned char *block_buffer = NULL; 522 struct ext2_dirent *dir = NULL; 523 struct ext2_dirent *previous_dir = NULL; 524 struct ext_filesystem *fs = get_fs(); 525 526 /* read the block no allocated to a file */ 527 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS; 528 direct_blk_idx++) { 529 blknr = read_allocated_block(parent_inode, direct_blk_idx); 530 if (blknr == 0) 531 goto fail; 532 533 /* read the blocks of parenet inode */ 534 block_buffer = zalloc(fs->blksz); 535 if (!block_buffer) 536 goto fail; 537 538 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 539 0, fs->blksz, (char *)block_buffer); 540 if (status == 0) 541 goto fail; 542 543 dir = (struct ext2_dirent *)block_buffer; 544 ptr = (char *)dir; 545 totalbytes = 0; 546 while (dir->direntlen >= 0) { 547 /* 548 * blocksize-totalbytes because last directory 549 * length i.e.,*dir->direntlen is free availble 550 * space in the block that means 551 * it is a last entry of directory entry 552 */ 553 if (strlen(dirname) == dir->namelen) { 554 if (strncmp(dirname, ptr + 555 sizeof(struct ext2_dirent), 556 dir->namelen) == 0) { 557 previous_dir->direntlen += 558 dir->direntlen; 559 inodeno = dir->inode; 560 dir->inode = 0; 561 found = 1; 562 break; 563 } 564 } 565 566 if (fs->blksz - totalbytes == dir->direntlen) 567 break; 568 569 /* traversing the each directory entry */ 570 templength = dir->direntlen; 571 totalbytes = totalbytes + templength; 572 previous_dir = dir; 573 dir = (struct ext2_dirent *)((char *)dir + templength); 574 ptr = (char *)dir; 575 } 576 577 if (found == 1) { 578 free(block_buffer); 579 block_buffer = NULL; 580 return inodeno; 581 } 582 583 free(block_buffer); 584 block_buffer = NULL; 585 } 586 587 fail: 588 free(block_buffer); 589 590 return -1; 591 } 592 593 static int find_dir_depth(char *dirname) 594 { 595 char *token = strtok(dirname, "/"); 596 int count = 0; 597 while (token != NULL) { 598 token = strtok(NULL, "/"); 599 count++; 600 } 601 return count + 1 + 1; 602 /* 603 * for example for string /home/temp 604 * depth=home(1)+temp(1)+1 extra for NULL; 605 * so count is 4; 606 */ 607 } 608 609 static int parse_path(char **arr, char *dirname) 610 { 611 char *token = strtok(dirname, "/"); 612 int i = 0; 613 614 /* add root */ 615 arr[i] = zalloc(strlen("/") + 1); 616 if (!arr[i]) 617 return -ENOMEM; 618 memcpy(arr[i++], "/", strlen("/")); 619 620 /* add each path entry after root */ 621 while (token != NULL) { 622 arr[i] = zalloc(strlen(token) + 1); 623 if (!arr[i]) 624 return -ENOMEM; 625 memcpy(arr[i++], token, strlen(token)); 626 token = strtok(NULL, "/"); 627 } 628 arr[i] = NULL; 629 630 return 0; 631 } 632 633 int ext4fs_iget(int inode_no, struct ext2_inode *inode) 634 { 635 if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0) 636 return -1; 637 638 return 0; 639 } 640 641 /* 642 * Function: ext4fs_get_parent_inode_num 643 * Return Value: inode Number of the parent directory of file/Directory to be 644 * created 645 * dirname : Input parmater, input path name of the file/directory to be created 646 * dname : Output parameter, to be filled with the name of the directory 647 * extracted from dirname 648 */ 649 int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags) 650 { 651 int i; 652 int depth = 0; 653 int matched_inode_no; 654 int result_inode_no = -1; 655 char **ptr = NULL; 656 char *depth_dirname = NULL; 657 char *parse_dirname = NULL; 658 struct ext2_inode *parent_inode = NULL; 659 struct ext2_inode *first_inode = NULL; 660 struct ext2_inode temp_inode; 661 662 if (*dirname != '/') { 663 printf("Please supply Absolute path\n"); 664 return -1; 665 } 666 667 /* TODO: input validation make equivalent to linux */ 668 depth_dirname = zalloc(strlen(dirname) + 1); 669 if (!depth_dirname) 670 return -ENOMEM; 671 672 memcpy(depth_dirname, dirname, strlen(dirname)); 673 depth = find_dir_depth(depth_dirname); 674 parse_dirname = zalloc(strlen(dirname) + 1); 675 if (!parse_dirname) 676 goto fail; 677 memcpy(parse_dirname, dirname, strlen(dirname)); 678 679 /* allocate memory for each directory level */ 680 ptr = zalloc((depth) * sizeof(char *)); 681 if (!ptr) 682 goto fail; 683 if (parse_path(ptr, parse_dirname)) 684 goto fail; 685 parent_inode = zalloc(sizeof(struct ext2_inode)); 686 if (!parent_inode) 687 goto fail; 688 first_inode = zalloc(sizeof(struct ext2_inode)); 689 if (!first_inode) 690 goto fail; 691 memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode)); 692 memcpy(first_inode, parent_inode, sizeof(struct ext2_inode)); 693 if (flags & F_FILE) 694 result_inode_no = EXT2_ROOT_INO; 695 for (i = 1; i < depth; i++) { 696 matched_inode_no = search_dir(parent_inode, ptr[i]); 697 if (matched_inode_no == -1) { 698 if (ptr[i + 1] == NULL && i == 1) { 699 result_inode_no = EXT2_ROOT_INO; 700 goto end; 701 } else { 702 if (ptr[i + 1] == NULL) 703 break; 704 printf("Invalid path\n"); 705 result_inode_no = -1; 706 goto fail; 707 } 708 } else { 709 if (ptr[i + 1] != NULL) { 710 memset(parent_inode, '\0', 711 sizeof(struct ext2_inode)); 712 if (ext4fs_iget(matched_inode_no, 713 parent_inode)) { 714 result_inode_no = -1; 715 goto fail; 716 } 717 result_inode_no = matched_inode_no; 718 } else { 719 break; 720 } 721 } 722 } 723 724 end: 725 if (i == 1) 726 matched_inode_no = search_dir(first_inode, ptr[i]); 727 else 728 matched_inode_no = search_dir(parent_inode, ptr[i]); 729 730 if (matched_inode_no != -1) { 731 ext4fs_iget(matched_inode_no, &temp_inode); 732 if (temp_inode.mode & S_IFDIR) { 733 printf("It is a Directory\n"); 734 result_inode_no = -1; 735 goto fail; 736 } 737 } 738 739 if (strlen(ptr[i]) > 256) { 740 result_inode_no = -1; 741 goto fail; 742 } 743 memcpy(dname, ptr[i], strlen(ptr[i])); 744 745 fail: 746 free(depth_dirname); 747 free(parse_dirname); 748 for (i = 0; i < depth; i++) { 749 if (!ptr[i]) 750 break; 751 free(ptr[i]); 752 } 753 free(ptr); 754 free(parent_inode); 755 free(first_inode); 756 757 return result_inode_no; 758 } 759 760 static int check_filename(char *filename, unsigned int blknr) 761 { 762 unsigned int first_block_no_of_root; 763 int totalbytes = 0; 764 int templength = 0; 765 int status, inodeno; 766 int found = 0; 767 char *root_first_block_buffer = NULL; 768 char *root_first_block_addr = NULL; 769 struct ext2_dirent *dir = NULL; 770 struct ext2_dirent *previous_dir = NULL; 771 char *ptr = NULL; 772 struct ext_filesystem *fs = get_fs(); 773 774 /* get the first block of root */ 775 first_block_no_of_root = blknr; 776 root_first_block_buffer = zalloc(fs->blksz); 777 if (!root_first_block_buffer) 778 return -ENOMEM; 779 root_first_block_addr = root_first_block_buffer; 780 status = ext4fs_devread((lbaint_t)first_block_no_of_root * 781 fs->sect_perblk, 0, 782 fs->blksz, root_first_block_buffer); 783 if (status == 0) 784 goto fail; 785 786 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root)) 787 goto fail; 788 dir = (struct ext2_dirent *)root_first_block_buffer; 789 ptr = (char *)dir; 790 totalbytes = 0; 791 while (dir->direntlen >= 0) { 792 /* 793 * blocksize-totalbytes because last 794 * directory length i.e., *dir->direntlen 795 * is free availble space in the block that 796 * means it is a last entry of directory entry 797 */ 798 if (strlen(filename) == dir->namelen) { 799 if (strncmp(filename, ptr + sizeof(struct ext2_dirent), 800 dir->namelen) == 0) { 801 printf("file found deleting\n"); 802 previous_dir->direntlen += dir->direntlen; 803 inodeno = dir->inode; 804 dir->inode = 0; 805 found = 1; 806 break; 807 } 808 } 809 810 if (fs->blksz - totalbytes == dir->direntlen) 811 break; 812 813 /* traversing the each directory entry */ 814 templength = dir->direntlen; 815 totalbytes = totalbytes + templength; 816 previous_dir = dir; 817 dir = (struct ext2_dirent *)((char *)dir + templength); 818 ptr = (char *)dir; 819 } 820 821 822 if (found == 1) { 823 if (ext4fs_put_metadata(root_first_block_addr, 824 first_block_no_of_root)) 825 goto fail; 826 return inodeno; 827 } 828 fail: 829 free(root_first_block_buffer); 830 831 return -1; 832 } 833 834 int ext4fs_filename_check(char *filename) 835 { 836 short direct_blk_idx = 0; 837 long int blknr = -1; 838 int inodeno = -1; 839 840 /* read the block no allocated to a file */ 841 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS; 842 direct_blk_idx++) { 843 blknr = read_allocated_block(g_parent_inode, direct_blk_idx); 844 if (blknr == 0) 845 break; 846 inodeno = check_filename(filename, blknr); 847 if (inodeno != -1) 848 return inodeno; 849 } 850 851 return -1; 852 } 853 854 long int ext4fs_get_new_blk_no(void) 855 { 856 short i; 857 short status; 858 int remainder; 859 unsigned int bg_idx; 860 static int prev_bg_bitmap_index = -1; 861 unsigned int blk_per_grp = ext4fs_root->sblock.blocks_per_group; 862 struct ext_filesystem *fs = get_fs(); 863 char *journal_buffer = zalloc(fs->blksz); 864 char *zero_buffer = zalloc(fs->blksz); 865 if (!journal_buffer || !zero_buffer) 866 goto fail; 867 struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable; 868 869 if (fs->first_pass_bbmap == 0) { 870 for (i = 0; i < fs->no_blkgrp; i++) { 871 if (bgd[i].free_blocks) { 872 if (bgd[i].bg_flags & EXT4_BG_BLOCK_UNINIT) { 873 put_ext4(((uint64_t) ((uint64_t)bgd[i].block_id * 874 (uint64_t)fs->blksz)), 875 zero_buffer, fs->blksz); 876 bgd[i].bg_flags = 877 bgd[i]. 878 bg_flags & ~EXT4_BG_BLOCK_UNINIT; 879 memcpy(fs->blk_bmaps[i], zero_buffer, 880 fs->blksz); 881 } 882 fs->curr_blkno = 883 _get_new_blk_no(fs->blk_bmaps[i]); 884 if (fs->curr_blkno == -1) 885 /* if block bitmap is completely fill */ 886 continue; 887 fs->curr_blkno = fs->curr_blkno + 888 (i * fs->blksz * 8); 889 fs->first_pass_bbmap++; 890 bgd[i].free_blocks--; 891 fs->sb->free_blocks--; 892 status = ext4fs_devread((lbaint_t) 893 bgd[i].block_id * 894 fs->sect_perblk, 0, 895 fs->blksz, 896 journal_buffer); 897 if (status == 0) 898 goto fail; 899 if (ext4fs_log_journal(journal_buffer, 900 bgd[i].block_id)) 901 goto fail; 902 goto success; 903 } else { 904 debug("no space left on block group %d\n", i); 905 } 906 } 907 908 goto fail; 909 } else { 910 restart: 911 fs->curr_blkno++; 912 /* get the blockbitmap index respective to blockno */ 913 bg_idx = fs->curr_blkno / blk_per_grp; 914 if (fs->blksz == 1024) { 915 remainder = fs->curr_blkno % blk_per_grp; 916 if (!remainder) 917 bg_idx--; 918 } 919 920 /* 921 * To skip completely filled block group bitmaps 922 * Optimize the block allocation 923 */ 924 if (bg_idx >= fs->no_blkgrp) 925 goto fail; 926 927 if (bgd[bg_idx].free_blocks == 0) { 928 debug("block group %u is full. Skipping\n", bg_idx); 929 fs->curr_blkno = fs->curr_blkno + blk_per_grp; 930 fs->curr_blkno--; 931 goto restart; 932 } 933 934 if (bgd[bg_idx].bg_flags & EXT4_BG_BLOCK_UNINIT) { 935 memset(zero_buffer, '\0', fs->blksz); 936 put_ext4(((uint64_t) ((uint64_t)bgd[bg_idx].block_id * 937 (uint64_t)fs->blksz)), zero_buffer, fs->blksz); 938 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz); 939 bgd[bg_idx].bg_flags = bgd[bg_idx].bg_flags & 940 ~EXT4_BG_BLOCK_UNINIT; 941 } 942 943 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx], 944 bg_idx) != 0) { 945 debug("going for restart for the block no %ld %u\n", 946 fs->curr_blkno, bg_idx); 947 goto restart; 948 } 949 950 /* journal backup */ 951 if (prev_bg_bitmap_index != bg_idx) { 952 memset(journal_buffer, '\0', fs->blksz); 953 status = ext4fs_devread((lbaint_t)bgd[bg_idx].block_id 954 * fs->sect_perblk, 955 0, fs->blksz, journal_buffer); 956 if (status == 0) 957 goto fail; 958 if (ext4fs_log_journal(journal_buffer, 959 bgd[bg_idx].block_id)) 960 goto fail; 961 962 prev_bg_bitmap_index = bg_idx; 963 } 964 bgd[bg_idx].free_blocks--; 965 fs->sb->free_blocks--; 966 goto success; 967 } 968 success: 969 free(journal_buffer); 970 free(zero_buffer); 971 972 return fs->curr_blkno; 973 fail: 974 free(journal_buffer); 975 free(zero_buffer); 976 977 return -1; 978 } 979 980 int ext4fs_get_new_inode_no(void) 981 { 982 short i; 983 short status; 984 unsigned int ibmap_idx; 985 static int prev_inode_bitmap_index = -1; 986 unsigned int inodes_per_grp = ext4fs_root->sblock.inodes_per_group; 987 struct ext_filesystem *fs = get_fs(); 988 char *journal_buffer = zalloc(fs->blksz); 989 char *zero_buffer = zalloc(fs->blksz); 990 if (!journal_buffer || !zero_buffer) 991 goto fail; 992 struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable; 993 994 if (fs->first_pass_ibmap == 0) { 995 for (i = 0; i < fs->no_blkgrp; i++) { 996 if (bgd[i].free_inodes) { 997 if (bgd[i].bg_itable_unused != 998 bgd[i].free_inodes) 999 bgd[i].bg_itable_unused = 1000 bgd[i].free_inodes; 1001 if (bgd[i].bg_flags & EXT4_BG_INODE_UNINIT) { 1002 put_ext4(((uint64_t) 1003 ((uint64_t)bgd[i].inode_id * 1004 (uint64_t)fs->blksz)), 1005 zero_buffer, fs->blksz); 1006 bgd[i].bg_flags = bgd[i].bg_flags & 1007 ~EXT4_BG_INODE_UNINIT; 1008 memcpy(fs->inode_bmaps[i], 1009 zero_buffer, fs->blksz); 1010 } 1011 fs->curr_inode_no = 1012 _get_new_inode_no(fs->inode_bmaps[i]); 1013 if (fs->curr_inode_no == -1) 1014 /* if block bitmap is completely fill */ 1015 continue; 1016 fs->curr_inode_no = fs->curr_inode_no + 1017 (i * inodes_per_grp); 1018 fs->first_pass_ibmap++; 1019 bgd[i].free_inodes--; 1020 bgd[i].bg_itable_unused--; 1021 fs->sb->free_inodes--; 1022 status = ext4fs_devread((lbaint_t) 1023 bgd[i].inode_id * 1024 fs->sect_perblk, 0, 1025 fs->blksz, 1026 journal_buffer); 1027 if (status == 0) 1028 goto fail; 1029 if (ext4fs_log_journal(journal_buffer, 1030 bgd[i].inode_id)) 1031 goto fail; 1032 goto success; 1033 } else 1034 debug("no inode left on block group %d\n", i); 1035 } 1036 goto fail; 1037 } else { 1038 restart: 1039 fs->curr_inode_no++; 1040 /* get the blockbitmap index respective to blockno */ 1041 ibmap_idx = fs->curr_inode_no / inodes_per_grp; 1042 if (bgd[ibmap_idx].bg_flags & EXT4_BG_INODE_UNINIT) { 1043 memset(zero_buffer, '\0', fs->blksz); 1044 put_ext4(((uint64_t) ((uint64_t)bgd[ibmap_idx].inode_id * 1045 (uint64_t)fs->blksz)), zero_buffer, 1046 fs->blksz); 1047 bgd[ibmap_idx].bg_flags = 1048 bgd[ibmap_idx].bg_flags & ~EXT4_BG_INODE_UNINIT; 1049 memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer, 1050 fs->blksz); 1051 } 1052 1053 if (ext4fs_set_inode_bmap(fs->curr_inode_no, 1054 fs->inode_bmaps[ibmap_idx], 1055 ibmap_idx) != 0) { 1056 debug("going for restart for the block no %d %u\n", 1057 fs->curr_inode_no, ibmap_idx); 1058 goto restart; 1059 } 1060 1061 /* journal backup */ 1062 if (prev_inode_bitmap_index != ibmap_idx) { 1063 memset(journal_buffer, '\0', fs->blksz); 1064 status = ext4fs_devread((lbaint_t) 1065 bgd[ibmap_idx].inode_id 1066 * fs->sect_perblk, 1067 0, fs->blksz, journal_buffer); 1068 if (status == 0) 1069 goto fail; 1070 if (ext4fs_log_journal(journal_buffer, 1071 bgd[ibmap_idx].inode_id)) 1072 goto fail; 1073 prev_inode_bitmap_index = ibmap_idx; 1074 } 1075 if (bgd[ibmap_idx].bg_itable_unused != 1076 bgd[ibmap_idx].free_inodes) 1077 bgd[ibmap_idx].bg_itable_unused = 1078 bgd[ibmap_idx].free_inodes; 1079 bgd[ibmap_idx].free_inodes--; 1080 bgd[ibmap_idx].bg_itable_unused--; 1081 fs->sb->free_inodes--; 1082 goto success; 1083 } 1084 1085 success: 1086 free(journal_buffer); 1087 free(zero_buffer); 1088 1089 return fs->curr_inode_no; 1090 fail: 1091 free(journal_buffer); 1092 free(zero_buffer); 1093 1094 return -1; 1095 1096 } 1097 1098 1099 static void alloc_single_indirect_block(struct ext2_inode *file_inode, 1100 unsigned int *total_remaining_blocks, 1101 unsigned int *no_blks_reqd) 1102 { 1103 short i; 1104 short status; 1105 long int actual_block_no; 1106 long int si_blockno; 1107 /* si :single indirect */ 1108 unsigned int *si_buffer = NULL; 1109 unsigned int *si_start_addr = NULL; 1110 struct ext_filesystem *fs = get_fs(); 1111 1112 if (*total_remaining_blocks != 0) { 1113 si_buffer = zalloc(fs->blksz); 1114 if (!si_buffer) { 1115 printf("No Memory\n"); 1116 return; 1117 } 1118 si_start_addr = si_buffer; 1119 si_blockno = ext4fs_get_new_blk_no(); 1120 if (si_blockno == -1) { 1121 printf("no block left to assign\n"); 1122 goto fail; 1123 } 1124 (*no_blks_reqd)++; 1125 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks); 1126 1127 status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk, 1128 0, fs->blksz, (char *)si_buffer); 1129 memset(si_buffer, '\0', fs->blksz); 1130 if (status == 0) 1131 goto fail; 1132 1133 for (i = 0; i < (fs->blksz / sizeof(int)); i++) { 1134 actual_block_no = ext4fs_get_new_blk_no(); 1135 if (actual_block_no == -1) { 1136 printf("no block left to assign\n"); 1137 goto fail; 1138 } 1139 *si_buffer = actual_block_no; 1140 debug("SIAB %u: %u\n", *si_buffer, 1141 *total_remaining_blocks); 1142 1143 si_buffer++; 1144 (*total_remaining_blocks)--; 1145 if (*total_remaining_blocks == 0) 1146 break; 1147 } 1148 1149 /* write the block to disk */ 1150 put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)), 1151 si_start_addr, fs->blksz); 1152 file_inode->b.blocks.indir_block = si_blockno; 1153 } 1154 fail: 1155 free(si_start_addr); 1156 } 1157 1158 static void alloc_double_indirect_block(struct ext2_inode *file_inode, 1159 unsigned int *total_remaining_blocks, 1160 unsigned int *no_blks_reqd) 1161 { 1162 short i; 1163 short j; 1164 short status; 1165 long int actual_block_no; 1166 /* di:double indirect */ 1167 long int di_blockno_parent; 1168 long int di_blockno_child; 1169 unsigned int *di_parent_buffer = NULL; 1170 unsigned int *di_child_buff = NULL; 1171 unsigned int *di_block_start_addr = NULL; 1172 unsigned int *di_child_buff_start = NULL; 1173 struct ext_filesystem *fs = get_fs(); 1174 1175 if (*total_remaining_blocks != 0) { 1176 /* double indirect parent block connecting to inode */ 1177 di_blockno_parent = ext4fs_get_new_blk_no(); 1178 if (di_blockno_parent == -1) { 1179 printf("no block left to assign\n"); 1180 goto fail; 1181 } 1182 di_parent_buffer = zalloc(fs->blksz); 1183 if (!di_parent_buffer) 1184 goto fail; 1185 1186 di_block_start_addr = di_parent_buffer; 1187 (*no_blks_reqd)++; 1188 debug("DIPB %ld: %u\n", di_blockno_parent, 1189 *total_remaining_blocks); 1190 1191 status = ext4fs_devread((lbaint_t)di_blockno_parent * 1192 fs->sect_perblk, 0, 1193 fs->blksz, (char *)di_parent_buffer); 1194 1195 if (!status) { 1196 printf("%s: Device read error!\n", __func__); 1197 goto fail; 1198 } 1199 memset(di_parent_buffer, '\0', fs->blksz); 1200 1201 /* 1202 * start:for each double indirect parent 1203 * block create one more block 1204 */ 1205 for (i = 0; i < (fs->blksz / sizeof(int)); i++) { 1206 di_blockno_child = ext4fs_get_new_blk_no(); 1207 if (di_blockno_child == -1) { 1208 printf("no block left to assign\n"); 1209 goto fail; 1210 } 1211 di_child_buff = zalloc(fs->blksz); 1212 if (!di_child_buff) 1213 goto fail; 1214 1215 di_child_buff_start = di_child_buff; 1216 *di_parent_buffer = di_blockno_child; 1217 di_parent_buffer++; 1218 (*no_blks_reqd)++; 1219 debug("DICB %ld: %u\n", di_blockno_child, 1220 *total_remaining_blocks); 1221 1222 status = ext4fs_devread((lbaint_t)di_blockno_child * 1223 fs->sect_perblk, 0, 1224 fs->blksz, 1225 (char *)di_child_buff); 1226 1227 if (!status) { 1228 printf("%s: Device read error!\n", __func__); 1229 goto fail; 1230 } 1231 memset(di_child_buff, '\0', fs->blksz); 1232 /* filling of actual datablocks for each child */ 1233 for (j = 0; j < (fs->blksz / sizeof(int)); j++) { 1234 actual_block_no = ext4fs_get_new_blk_no(); 1235 if (actual_block_no == -1) { 1236 printf("no block left to assign\n"); 1237 goto fail; 1238 } 1239 *di_child_buff = actual_block_no; 1240 debug("DIAB %ld: %u\n", actual_block_no, 1241 *total_remaining_blocks); 1242 1243 di_child_buff++; 1244 (*total_remaining_blocks)--; 1245 if (*total_remaining_blocks == 0) 1246 break; 1247 } 1248 /* write the block table */ 1249 put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)), 1250 di_child_buff_start, fs->blksz); 1251 free(di_child_buff_start); 1252 di_child_buff_start = NULL; 1253 1254 if (*total_remaining_blocks == 0) 1255 break; 1256 } 1257 put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)), 1258 di_block_start_addr, fs->blksz); 1259 file_inode->b.blocks.double_indir_block = di_blockno_parent; 1260 } 1261 fail: 1262 free(di_block_start_addr); 1263 } 1264 1265 static void alloc_triple_indirect_block(struct ext2_inode *file_inode, 1266 unsigned int *total_remaining_blocks, 1267 unsigned int *no_blks_reqd) 1268 { 1269 short i; 1270 short j; 1271 short k; 1272 long int actual_block_no; 1273 /* ti: Triple Indirect */ 1274 long int ti_gp_blockno; 1275 long int ti_parent_blockno; 1276 long int ti_child_blockno; 1277 unsigned int *ti_gp_buff = NULL; 1278 unsigned int *ti_parent_buff = NULL; 1279 unsigned int *ti_child_buff = NULL; 1280 unsigned int *ti_gp_buff_start_addr = NULL; 1281 unsigned int *ti_pbuff_start_addr = NULL; 1282 unsigned int *ti_cbuff_start_addr = NULL; 1283 struct ext_filesystem *fs = get_fs(); 1284 if (*total_remaining_blocks != 0) { 1285 /* triple indirect grand parent block connecting to inode */ 1286 ti_gp_blockno = ext4fs_get_new_blk_no(); 1287 if (ti_gp_blockno == -1) { 1288 printf("no block left to assign\n"); 1289 goto fail; 1290 } 1291 ti_gp_buff = zalloc(fs->blksz); 1292 if (!ti_gp_buff) 1293 goto fail; 1294 1295 ti_gp_buff_start_addr = ti_gp_buff; 1296 (*no_blks_reqd)++; 1297 debug("TIGPB %ld: %u\n", ti_gp_blockno, 1298 *total_remaining_blocks); 1299 1300 /* for each 4 byte grand parent entry create one more block */ 1301 for (i = 0; i < (fs->blksz / sizeof(int)); i++) { 1302 ti_parent_blockno = ext4fs_get_new_blk_no(); 1303 if (ti_parent_blockno == -1) { 1304 printf("no block left to assign\n"); 1305 goto fail; 1306 } 1307 ti_parent_buff = zalloc(fs->blksz); 1308 if (!ti_parent_buff) 1309 goto fail; 1310 1311 ti_pbuff_start_addr = ti_parent_buff; 1312 *ti_gp_buff = ti_parent_blockno; 1313 ti_gp_buff++; 1314 (*no_blks_reqd)++; 1315 debug("TIPB %ld: %u\n", ti_parent_blockno, 1316 *total_remaining_blocks); 1317 1318 /* for each 4 byte entry parent create one more block */ 1319 for (j = 0; j < (fs->blksz / sizeof(int)); j++) { 1320 ti_child_blockno = ext4fs_get_new_blk_no(); 1321 if (ti_child_blockno == -1) { 1322 printf("no block left assign\n"); 1323 goto fail; 1324 } 1325 ti_child_buff = zalloc(fs->blksz); 1326 if (!ti_child_buff) 1327 goto fail; 1328 1329 ti_cbuff_start_addr = ti_child_buff; 1330 *ti_parent_buff = ti_child_blockno; 1331 ti_parent_buff++; 1332 (*no_blks_reqd)++; 1333 debug("TICB %ld: %u\n", ti_parent_blockno, 1334 *total_remaining_blocks); 1335 1336 /* fill actual datablocks for each child */ 1337 for (k = 0; k < (fs->blksz / sizeof(int)); 1338 k++) { 1339 actual_block_no = 1340 ext4fs_get_new_blk_no(); 1341 if (actual_block_no == -1) { 1342 printf("no block left\n"); 1343 goto fail; 1344 } 1345 *ti_child_buff = actual_block_no; 1346 debug("TIAB %ld: %u\n", actual_block_no, 1347 *total_remaining_blocks); 1348 1349 ti_child_buff++; 1350 (*total_remaining_blocks)--; 1351 if (*total_remaining_blocks == 0) 1352 break; 1353 } 1354 /* write the child block */ 1355 put_ext4(((uint64_t) ((uint64_t)ti_child_blockno * 1356 (uint64_t)fs->blksz)), 1357 ti_cbuff_start_addr, fs->blksz); 1358 free(ti_cbuff_start_addr); 1359 1360 if (*total_remaining_blocks == 0) 1361 break; 1362 } 1363 /* write the parent block */ 1364 put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)), 1365 ti_pbuff_start_addr, fs->blksz); 1366 free(ti_pbuff_start_addr); 1367 1368 if (*total_remaining_blocks == 0) 1369 break; 1370 } 1371 /* write the grand parent block */ 1372 put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)), 1373 ti_gp_buff_start_addr, fs->blksz); 1374 file_inode->b.blocks.triple_indir_block = ti_gp_blockno; 1375 } 1376 fail: 1377 free(ti_gp_buff_start_addr); 1378 } 1379 1380 void ext4fs_allocate_blocks(struct ext2_inode *file_inode, 1381 unsigned int total_remaining_blocks, 1382 unsigned int *total_no_of_block) 1383 { 1384 short i; 1385 long int direct_blockno; 1386 unsigned int no_blks_reqd = 0; 1387 1388 /* allocation of direct blocks */ 1389 for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) { 1390 direct_blockno = ext4fs_get_new_blk_no(); 1391 if (direct_blockno == -1) { 1392 printf("no block left to assign\n"); 1393 return; 1394 } 1395 file_inode->b.blocks.dir_blocks[i] = direct_blockno; 1396 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks); 1397 1398 total_remaining_blocks--; 1399 } 1400 1401 alloc_single_indirect_block(file_inode, &total_remaining_blocks, 1402 &no_blks_reqd); 1403 alloc_double_indirect_block(file_inode, &total_remaining_blocks, 1404 &no_blks_reqd); 1405 alloc_triple_indirect_block(file_inode, &total_remaining_blocks, 1406 &no_blks_reqd); 1407 *total_no_of_block += no_blks_reqd; 1408 } 1409 1410 #endif 1411 1412 static struct ext4_extent_header *ext4fs_get_extent_block 1413 (struct ext2_data *data, char *buf, 1414 struct ext4_extent_header *ext_block, 1415 uint32_t fileblock, int log2_blksz) 1416 { 1417 struct ext4_extent_idx *index; 1418 unsigned long long block; 1419 int blksz = EXT2_BLOCK_SIZE(data); 1420 int i; 1421 1422 while (1) { 1423 index = (struct ext4_extent_idx *)(ext_block + 1); 1424 1425 if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC) 1426 return 0; 1427 1428 if (ext_block->eh_depth == 0) 1429 return ext_block; 1430 i = -1; 1431 do { 1432 i++; 1433 if (i >= le16_to_cpu(ext_block->eh_entries)) 1434 break; 1435 } while (fileblock >= le32_to_cpu(index[i].ei_block)); 1436 1437 if (--i < 0) 1438 return 0; 1439 1440 block = le16_to_cpu(index[i].ei_leaf_hi); 1441 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo); 1442 1443 if (ext4fs_devread((lbaint_t)block << log2_blksz, 0, blksz, 1444 buf)) 1445 ext_block = (struct ext4_extent_header *)buf; 1446 else 1447 return 0; 1448 } 1449 } 1450 1451 static int ext4fs_blockgroup 1452 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp) 1453 { 1454 long int blkno; 1455 unsigned int blkoff, desc_per_blk; 1456 int log2blksz = get_fs()->dev_desc->log2blksz; 1457 1458 desc_per_blk = EXT2_BLOCK_SIZE(data) / sizeof(struct ext2_block_group); 1459 1460 blkno = __le32_to_cpu(data->sblock.first_data_block) + 1 + 1461 group / desc_per_blk; 1462 blkoff = (group % desc_per_blk) * sizeof(struct ext2_block_group); 1463 1464 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n", 1465 group, blkno, blkoff); 1466 1467 return ext4fs_devread((lbaint_t)blkno << 1468 (LOG2_BLOCK_SIZE(data) - log2blksz), 1469 blkoff, sizeof(struct ext2_block_group), 1470 (char *)blkgrp); 1471 } 1472 1473 int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode) 1474 { 1475 struct ext2_block_group blkgrp; 1476 struct ext2_sblock *sblock = &data->sblock; 1477 struct ext_filesystem *fs = get_fs(); 1478 int log2blksz = get_fs()->dev_desc->log2blksz; 1479 int inodes_per_block, status; 1480 long int blkno; 1481 unsigned int blkoff; 1482 1483 /* It is easier to calculate if the first inode is 0. */ 1484 ino--; 1485 status = ext4fs_blockgroup(data, ino / __le32_to_cpu 1486 (sblock->inodes_per_group), &blkgrp); 1487 if (status == 0) 1488 return 0; 1489 1490 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz; 1491 blkno = __le32_to_cpu(blkgrp.inode_table_id) + 1492 (ino % __le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block; 1493 blkoff = (ino % inodes_per_block) * fs->inodesz; 1494 /* Read the inode. */ 1495 status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) - 1496 log2blksz), blkoff, 1497 sizeof(struct ext2_inode), (char *)inode); 1498 if (status == 0) 1499 return 0; 1500 1501 return 1; 1502 } 1503 1504 long int read_allocated_block(struct ext2_inode *inode, int fileblock) 1505 { 1506 long int blknr; 1507 int blksz; 1508 int log2_blksz; 1509 int status; 1510 long int rblock; 1511 long int perblock_parent; 1512 long int perblock_child; 1513 unsigned long long start; 1514 /* get the blocksize of the filesystem */ 1515 blksz = EXT2_BLOCK_SIZE(ext4fs_root); 1516 log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root) 1517 - get_fs()->dev_desc->log2blksz; 1518 1519 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) { 1520 char *buf = zalloc(blksz); 1521 if (!buf) 1522 return -ENOMEM; 1523 struct ext4_extent_header *ext_block; 1524 struct ext4_extent *extent; 1525 int i = -1; 1526 ext_block = 1527 ext4fs_get_extent_block(ext4fs_root, buf, 1528 (struct ext4_extent_header *) 1529 inode->b.blocks.dir_blocks, 1530 fileblock, log2_blksz); 1531 if (!ext_block) { 1532 printf("invalid extent block\n"); 1533 free(buf); 1534 return -EINVAL; 1535 } 1536 1537 extent = (struct ext4_extent *)(ext_block + 1); 1538 1539 do { 1540 i++; 1541 if (i >= le16_to_cpu(ext_block->eh_entries)) 1542 break; 1543 } while (fileblock >= le32_to_cpu(extent[i].ee_block)); 1544 if (--i >= 0) { 1545 fileblock -= le32_to_cpu(extent[i].ee_block); 1546 if (fileblock >= le16_to_cpu(extent[i].ee_len)) { 1547 free(buf); 1548 return 0; 1549 } 1550 1551 start = le16_to_cpu(extent[i].ee_start_hi); 1552 start = (start << 32) + 1553 le32_to_cpu(extent[i].ee_start_lo); 1554 free(buf); 1555 return fileblock + start; 1556 } 1557 1558 printf("Extent Error\n"); 1559 free(buf); 1560 return -1; 1561 } 1562 1563 /* Direct blocks. */ 1564 if (fileblock < INDIRECT_BLOCKS) 1565 blknr = __le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]); 1566 1567 /* Indirect. */ 1568 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) { 1569 if (ext4fs_indir1_block == NULL) { 1570 ext4fs_indir1_block = zalloc(blksz); 1571 if (ext4fs_indir1_block == NULL) { 1572 printf("** SI ext2fs read block (indir 1)" 1573 "malloc failed. **\n"); 1574 return -1; 1575 } 1576 ext4fs_indir1_size = blksz; 1577 ext4fs_indir1_blkno = -1; 1578 } 1579 if (blksz != ext4fs_indir1_size) { 1580 free(ext4fs_indir1_block); 1581 ext4fs_indir1_block = NULL; 1582 ext4fs_indir1_size = 0; 1583 ext4fs_indir1_blkno = -1; 1584 ext4fs_indir1_block = zalloc(blksz); 1585 if (ext4fs_indir1_block == NULL) { 1586 printf("** SI ext2fs read block (indir 1):" 1587 "malloc failed. **\n"); 1588 return -1; 1589 } 1590 ext4fs_indir1_size = blksz; 1591 } 1592 if ((__le32_to_cpu(inode->b.blocks.indir_block) << 1593 log2_blksz) != ext4fs_indir1_blkno) { 1594 status = 1595 ext4fs_devread((lbaint_t)__le32_to_cpu 1596 (inode->b.blocks. 1597 indir_block) << log2_blksz, 0, 1598 blksz, (char *)ext4fs_indir1_block); 1599 if (status == 0) { 1600 printf("** SI ext2fs read block (indir 1)" 1601 "failed. **\n"); 1602 return 0; 1603 } 1604 ext4fs_indir1_blkno = 1605 __le32_to_cpu(inode->b.blocks. 1606 indir_block) << log2_blksz; 1607 } 1608 blknr = __le32_to_cpu(ext4fs_indir1_block 1609 [fileblock - INDIRECT_BLOCKS]); 1610 } 1611 /* Double indirect. */ 1612 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 * 1613 (blksz / 4 + 1)))) { 1614 1615 long int perblock = blksz / 4; 1616 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4); 1617 1618 if (ext4fs_indir1_block == NULL) { 1619 ext4fs_indir1_block = zalloc(blksz); 1620 if (ext4fs_indir1_block == NULL) { 1621 printf("** DI ext2fs read block (indir 2 1)" 1622 "malloc failed. **\n"); 1623 return -1; 1624 } 1625 ext4fs_indir1_size = blksz; 1626 ext4fs_indir1_blkno = -1; 1627 } 1628 if (blksz != ext4fs_indir1_size) { 1629 free(ext4fs_indir1_block); 1630 ext4fs_indir1_block = NULL; 1631 ext4fs_indir1_size = 0; 1632 ext4fs_indir1_blkno = -1; 1633 ext4fs_indir1_block = zalloc(blksz); 1634 if (ext4fs_indir1_block == NULL) { 1635 printf("** DI ext2fs read block (indir 2 1)" 1636 "malloc failed. **\n"); 1637 return -1; 1638 } 1639 ext4fs_indir1_size = blksz; 1640 } 1641 if ((__le32_to_cpu(inode->b.blocks.double_indir_block) << 1642 log2_blksz) != ext4fs_indir1_blkno) { 1643 status = 1644 ext4fs_devread((lbaint_t)__le32_to_cpu 1645 (inode->b.blocks. 1646 double_indir_block) << log2_blksz, 1647 0, blksz, 1648 (char *)ext4fs_indir1_block); 1649 if (status == 0) { 1650 printf("** DI ext2fs read block (indir 2 1)" 1651 "failed. **\n"); 1652 return -1; 1653 } 1654 ext4fs_indir1_blkno = 1655 __le32_to_cpu(inode->b.blocks.double_indir_block) << 1656 log2_blksz; 1657 } 1658 1659 if (ext4fs_indir2_block == NULL) { 1660 ext4fs_indir2_block = zalloc(blksz); 1661 if (ext4fs_indir2_block == NULL) { 1662 printf("** DI ext2fs read block (indir 2 2)" 1663 "malloc failed. **\n"); 1664 return -1; 1665 } 1666 ext4fs_indir2_size = blksz; 1667 ext4fs_indir2_blkno = -1; 1668 } 1669 if (blksz != ext4fs_indir2_size) { 1670 free(ext4fs_indir2_block); 1671 ext4fs_indir2_block = NULL; 1672 ext4fs_indir2_size = 0; 1673 ext4fs_indir2_blkno = -1; 1674 ext4fs_indir2_block = zalloc(blksz); 1675 if (ext4fs_indir2_block == NULL) { 1676 printf("** DI ext2fs read block (indir 2 2)" 1677 "malloc failed. **\n"); 1678 return -1; 1679 } 1680 ext4fs_indir2_size = blksz; 1681 } 1682 if ((__le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) << 1683 log2_blksz) != ext4fs_indir2_blkno) { 1684 status = ext4fs_devread((lbaint_t)__le32_to_cpu 1685 (ext4fs_indir1_block 1686 [rblock / 1687 perblock]) << log2_blksz, 0, 1688 blksz, 1689 (char *)ext4fs_indir2_block); 1690 if (status == 0) { 1691 printf("** DI ext2fs read block (indir 2 2)" 1692 "failed. **\n"); 1693 return -1; 1694 } 1695 ext4fs_indir2_blkno = 1696 __le32_to_cpu(ext4fs_indir1_block[rblock 1697 / 1698 perblock]) << 1699 log2_blksz; 1700 } 1701 blknr = __le32_to_cpu(ext4fs_indir2_block[rblock % perblock]); 1702 } 1703 /* Tripple indirect. */ 1704 else { 1705 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 + 1706 (blksz / 4 * blksz / 4)); 1707 perblock_child = blksz / 4; 1708 perblock_parent = ((blksz / 4) * (blksz / 4)); 1709 1710 if (ext4fs_indir1_block == NULL) { 1711 ext4fs_indir1_block = zalloc(blksz); 1712 if (ext4fs_indir1_block == NULL) { 1713 printf("** TI ext2fs read block (indir 2 1)" 1714 "malloc failed. **\n"); 1715 return -1; 1716 } 1717 ext4fs_indir1_size = blksz; 1718 ext4fs_indir1_blkno = -1; 1719 } 1720 if (blksz != ext4fs_indir1_size) { 1721 free(ext4fs_indir1_block); 1722 ext4fs_indir1_block = NULL; 1723 ext4fs_indir1_size = 0; 1724 ext4fs_indir1_blkno = -1; 1725 ext4fs_indir1_block = zalloc(blksz); 1726 if (ext4fs_indir1_block == NULL) { 1727 printf("** TI ext2fs read block (indir 2 1)" 1728 "malloc failed. **\n"); 1729 return -1; 1730 } 1731 ext4fs_indir1_size = blksz; 1732 } 1733 if ((__le32_to_cpu(inode->b.blocks.triple_indir_block) << 1734 log2_blksz) != ext4fs_indir1_blkno) { 1735 status = ext4fs_devread 1736 ((lbaint_t) 1737 __le32_to_cpu(inode->b.blocks.triple_indir_block) 1738 << log2_blksz, 0, blksz, 1739 (char *)ext4fs_indir1_block); 1740 if (status == 0) { 1741 printf("** TI ext2fs read block (indir 2 1)" 1742 "failed. **\n"); 1743 return -1; 1744 } 1745 ext4fs_indir1_blkno = 1746 __le32_to_cpu(inode->b.blocks.triple_indir_block) << 1747 log2_blksz; 1748 } 1749 1750 if (ext4fs_indir2_block == NULL) { 1751 ext4fs_indir2_block = zalloc(blksz); 1752 if (ext4fs_indir2_block == NULL) { 1753 printf("** TI ext2fs read block (indir 2 2)" 1754 "malloc failed. **\n"); 1755 return -1; 1756 } 1757 ext4fs_indir2_size = blksz; 1758 ext4fs_indir2_blkno = -1; 1759 } 1760 if (blksz != ext4fs_indir2_size) { 1761 free(ext4fs_indir2_block); 1762 ext4fs_indir2_block = NULL; 1763 ext4fs_indir2_size = 0; 1764 ext4fs_indir2_blkno = -1; 1765 ext4fs_indir2_block = zalloc(blksz); 1766 if (ext4fs_indir2_block == NULL) { 1767 printf("** TI ext2fs read block (indir 2 2)" 1768 "malloc failed. **\n"); 1769 return -1; 1770 } 1771 ext4fs_indir2_size = blksz; 1772 } 1773 if ((__le32_to_cpu(ext4fs_indir1_block[rblock / 1774 perblock_parent]) << 1775 log2_blksz) 1776 != ext4fs_indir2_blkno) { 1777 status = ext4fs_devread((lbaint_t)__le32_to_cpu 1778 (ext4fs_indir1_block 1779 [rblock / 1780 perblock_parent]) << 1781 log2_blksz, 0, blksz, 1782 (char *)ext4fs_indir2_block); 1783 if (status == 0) { 1784 printf("** TI ext2fs read block (indir 2 2)" 1785 "failed. **\n"); 1786 return -1; 1787 } 1788 ext4fs_indir2_blkno = 1789 __le32_to_cpu(ext4fs_indir1_block[rblock / 1790 perblock_parent]) 1791 << log2_blksz; 1792 } 1793 1794 if (ext4fs_indir3_block == NULL) { 1795 ext4fs_indir3_block = zalloc(blksz); 1796 if (ext4fs_indir3_block == NULL) { 1797 printf("** TI ext2fs read block (indir 2 2)" 1798 "malloc failed. **\n"); 1799 return -1; 1800 } 1801 ext4fs_indir3_size = blksz; 1802 ext4fs_indir3_blkno = -1; 1803 } 1804 if (blksz != ext4fs_indir3_size) { 1805 free(ext4fs_indir3_block); 1806 ext4fs_indir3_block = NULL; 1807 ext4fs_indir3_size = 0; 1808 ext4fs_indir3_blkno = -1; 1809 ext4fs_indir3_block = zalloc(blksz); 1810 if (ext4fs_indir3_block == NULL) { 1811 printf("** TI ext2fs read block (indir 2 2)" 1812 "malloc failed. **\n"); 1813 return -1; 1814 } 1815 ext4fs_indir3_size = blksz; 1816 } 1817 if ((__le32_to_cpu(ext4fs_indir2_block[rblock 1818 / 1819 perblock_child]) << 1820 log2_blksz) != ext4fs_indir3_blkno) { 1821 status = 1822 ext4fs_devread((lbaint_t)__le32_to_cpu 1823 (ext4fs_indir2_block 1824 [(rblock / perblock_child) 1825 % (blksz / 4)]) << log2_blksz, 0, 1826 blksz, (char *)ext4fs_indir3_block); 1827 if (status == 0) { 1828 printf("** TI ext2fs read block (indir 2 2)" 1829 "failed. **\n"); 1830 return -1; 1831 } 1832 ext4fs_indir3_blkno = 1833 __le32_to_cpu(ext4fs_indir2_block[(rblock / 1834 perblock_child) % 1835 (blksz / 1836 4)]) << 1837 log2_blksz; 1838 } 1839 1840 blknr = __le32_to_cpu(ext4fs_indir3_block 1841 [rblock % perblock_child]); 1842 } 1843 debug("read_allocated_block %ld\n", blknr); 1844 1845 return blknr; 1846 } 1847 1848 /** 1849 * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's 1850 * global pointers 1851 * 1852 * This function assures that for a file with the same name but different size 1853 * the sequential store on the ext4 filesystem will be correct. 1854 * 1855 * In this function the global data, responsible for internal representation 1856 * of the ext4 data are initialized to the reset state. Without this, during 1857 * replacement of the smaller file with the bigger truncation of new file was 1858 * performed. 1859 */ 1860 void ext4fs_reinit_global(void) 1861 { 1862 if (ext4fs_indir1_block != NULL) { 1863 free(ext4fs_indir1_block); 1864 ext4fs_indir1_block = NULL; 1865 ext4fs_indir1_size = 0; 1866 ext4fs_indir1_blkno = -1; 1867 } 1868 if (ext4fs_indir2_block != NULL) { 1869 free(ext4fs_indir2_block); 1870 ext4fs_indir2_block = NULL; 1871 ext4fs_indir2_size = 0; 1872 ext4fs_indir2_blkno = -1; 1873 } 1874 if (ext4fs_indir3_block != NULL) { 1875 free(ext4fs_indir3_block); 1876 ext4fs_indir3_block = NULL; 1877 ext4fs_indir3_size = 0; 1878 ext4fs_indir3_blkno = -1; 1879 } 1880 } 1881 void ext4fs_close(void) 1882 { 1883 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) { 1884 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen); 1885 ext4fs_file = NULL; 1886 } 1887 if (ext4fs_root != NULL) { 1888 free(ext4fs_root); 1889 ext4fs_root = NULL; 1890 } 1891 1892 ext4fs_reinit_global(); 1893 } 1894 1895 int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name, 1896 struct ext2fs_node **fnode, int *ftype) 1897 { 1898 unsigned int fpos = 0; 1899 int status; 1900 loff_t actread; 1901 struct ext2fs_node *diro = (struct ext2fs_node *) dir; 1902 1903 #ifdef DEBUG 1904 if (name != NULL) 1905 printf("Iterate dir %s\n", name); 1906 #endif /* of DEBUG */ 1907 if (!diro->inode_read) { 1908 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode); 1909 if (status == 0) 1910 return 0; 1911 } 1912 /* Search the file. */ 1913 while (fpos < __le32_to_cpu(diro->inode.size)) { 1914 struct ext2_dirent dirent; 1915 1916 status = ext4fs_read_file(diro, fpos, 1917 sizeof(struct ext2_dirent), 1918 (char *)&dirent, &actread); 1919 if (status < 0) 1920 return 0; 1921 1922 if (dirent.namelen != 0) { 1923 char filename[dirent.namelen + 1]; 1924 struct ext2fs_node *fdiro; 1925 int type = FILETYPE_UNKNOWN; 1926 1927 status = ext4fs_read_file(diro, 1928 fpos + 1929 sizeof(struct ext2_dirent), 1930 dirent.namelen, filename, 1931 &actread); 1932 if (status < 0) 1933 return 0; 1934 1935 fdiro = zalloc(sizeof(struct ext2fs_node)); 1936 if (!fdiro) 1937 return 0; 1938 1939 fdiro->data = diro->data; 1940 fdiro->ino = __le32_to_cpu(dirent.inode); 1941 1942 filename[dirent.namelen] = '\0'; 1943 1944 if (dirent.filetype != FILETYPE_UNKNOWN) { 1945 fdiro->inode_read = 0; 1946 1947 if (dirent.filetype == FILETYPE_DIRECTORY) 1948 type = FILETYPE_DIRECTORY; 1949 else if (dirent.filetype == FILETYPE_SYMLINK) 1950 type = FILETYPE_SYMLINK; 1951 else if (dirent.filetype == FILETYPE_REG) 1952 type = FILETYPE_REG; 1953 } else { 1954 status = ext4fs_read_inode(diro->data, 1955 __le32_to_cpu 1956 (dirent.inode), 1957 &fdiro->inode); 1958 if (status == 0) { 1959 free(fdiro); 1960 return 0; 1961 } 1962 fdiro->inode_read = 1; 1963 1964 if ((__le16_to_cpu(fdiro->inode.mode) & 1965 FILETYPE_INO_MASK) == 1966 FILETYPE_INO_DIRECTORY) { 1967 type = FILETYPE_DIRECTORY; 1968 } else if ((__le16_to_cpu(fdiro->inode.mode) 1969 & FILETYPE_INO_MASK) == 1970 FILETYPE_INO_SYMLINK) { 1971 type = FILETYPE_SYMLINK; 1972 } else if ((__le16_to_cpu(fdiro->inode.mode) 1973 & FILETYPE_INO_MASK) == 1974 FILETYPE_INO_REG) { 1975 type = FILETYPE_REG; 1976 } 1977 } 1978 #ifdef DEBUG 1979 printf("iterate >%s<\n", filename); 1980 #endif /* of DEBUG */ 1981 if ((name != NULL) && (fnode != NULL) 1982 && (ftype != NULL)) { 1983 if (strcmp(filename, name) == 0) { 1984 *ftype = type; 1985 *fnode = fdiro; 1986 return 1; 1987 } 1988 } else { 1989 if (fdiro->inode_read == 0) { 1990 status = ext4fs_read_inode(diro->data, 1991 __le32_to_cpu( 1992 dirent.inode), 1993 &fdiro->inode); 1994 if (status == 0) { 1995 free(fdiro); 1996 return 0; 1997 } 1998 fdiro->inode_read = 1; 1999 } 2000 switch (type) { 2001 case FILETYPE_DIRECTORY: 2002 printf("<DIR> "); 2003 break; 2004 case FILETYPE_SYMLINK: 2005 printf("<SYM> "); 2006 break; 2007 case FILETYPE_REG: 2008 printf(" "); 2009 break; 2010 default: 2011 printf("< ? > "); 2012 break; 2013 } 2014 printf("%10u %s\n", 2015 __le32_to_cpu(fdiro->inode.size), 2016 filename); 2017 } 2018 free(fdiro); 2019 } 2020 fpos += __le16_to_cpu(dirent.direntlen); 2021 } 2022 return 0; 2023 } 2024 2025 static char *ext4fs_read_symlink(struct ext2fs_node *node) 2026 { 2027 char *symlink; 2028 struct ext2fs_node *diro = node; 2029 int status; 2030 loff_t actread; 2031 2032 if (!diro->inode_read) { 2033 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode); 2034 if (status == 0) 2035 return 0; 2036 } 2037 symlink = zalloc(__le32_to_cpu(diro->inode.size) + 1); 2038 if (!symlink) 2039 return 0; 2040 2041 if (__le32_to_cpu(diro->inode.size) <= 60) { 2042 strncpy(symlink, diro->inode.b.symlink, 2043 __le32_to_cpu(diro->inode.size)); 2044 } else { 2045 status = ext4fs_read_file(diro, 0, 2046 __le32_to_cpu(diro->inode.size), 2047 symlink, &actread); 2048 if (status == 0) { 2049 free(symlink); 2050 return 0; 2051 } 2052 } 2053 symlink[__le32_to_cpu(diro->inode.size)] = '\0'; 2054 return symlink; 2055 } 2056 2057 static int ext4fs_find_file1(const char *currpath, 2058 struct ext2fs_node *currroot, 2059 struct ext2fs_node **currfound, int *foundtype) 2060 { 2061 char fpath[strlen(currpath) + 1]; 2062 char *name = fpath; 2063 char *next; 2064 int status; 2065 int type = FILETYPE_DIRECTORY; 2066 struct ext2fs_node *currnode = currroot; 2067 struct ext2fs_node *oldnode = currroot; 2068 2069 strncpy(fpath, currpath, strlen(currpath) + 1); 2070 2071 /* Remove all leading slashes. */ 2072 while (*name == '/') 2073 name++; 2074 2075 if (!*name) { 2076 *currfound = currnode; 2077 return 1; 2078 } 2079 2080 for (;;) { 2081 int found; 2082 2083 /* Extract the actual part from the pathname. */ 2084 next = strchr(name, '/'); 2085 if (next) { 2086 /* Remove all leading slashes. */ 2087 while (*next == '/') 2088 *(next++) = '\0'; 2089 } 2090 2091 if (type != FILETYPE_DIRECTORY) { 2092 ext4fs_free_node(currnode, currroot); 2093 return 0; 2094 } 2095 2096 oldnode = currnode; 2097 2098 /* Iterate over the directory. */ 2099 found = ext4fs_iterate_dir(currnode, name, &currnode, &type); 2100 if (found == 0) 2101 return 0; 2102 2103 if (found == -1) 2104 break; 2105 2106 /* Read in the symlink and follow it. */ 2107 if (type == FILETYPE_SYMLINK) { 2108 char *symlink; 2109 2110 /* Test if the symlink does not loop. */ 2111 if (++symlinknest == 8) { 2112 ext4fs_free_node(currnode, currroot); 2113 ext4fs_free_node(oldnode, currroot); 2114 return 0; 2115 } 2116 2117 symlink = ext4fs_read_symlink(currnode); 2118 ext4fs_free_node(currnode, currroot); 2119 2120 if (!symlink) { 2121 ext4fs_free_node(oldnode, currroot); 2122 return 0; 2123 } 2124 2125 debug("Got symlink >%s<\n", symlink); 2126 2127 if (symlink[0] == '/') { 2128 ext4fs_free_node(oldnode, currroot); 2129 oldnode = &ext4fs_root->diropen; 2130 } 2131 2132 /* Lookup the node the symlink points to. */ 2133 status = ext4fs_find_file1(symlink, oldnode, 2134 &currnode, &type); 2135 2136 free(symlink); 2137 2138 if (status == 0) { 2139 ext4fs_free_node(oldnode, currroot); 2140 return 0; 2141 } 2142 } 2143 2144 ext4fs_free_node(oldnode, currroot); 2145 2146 /* Found the node! */ 2147 if (!next || *next == '\0') { 2148 *currfound = currnode; 2149 *foundtype = type; 2150 return 1; 2151 } 2152 name = next; 2153 } 2154 return -1; 2155 } 2156 2157 int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode, 2158 struct ext2fs_node **foundnode, int expecttype) 2159 { 2160 int status; 2161 int foundtype = FILETYPE_DIRECTORY; 2162 2163 symlinknest = 0; 2164 if (!path) 2165 return 0; 2166 2167 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype); 2168 if (status == 0) 2169 return 0; 2170 2171 /* Check if the node that was found was of the expected type. */ 2172 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype)) 2173 return 0; 2174 else if ((expecttype == FILETYPE_DIRECTORY) 2175 && (foundtype != expecttype)) 2176 return 0; 2177 2178 return 1; 2179 } 2180 2181 int ext4fs_open(const char *filename, loff_t *len) 2182 { 2183 struct ext2fs_node *fdiro = NULL; 2184 int status; 2185 2186 if (ext4fs_root == NULL) 2187 return -1; 2188 2189 ext4fs_file = NULL; 2190 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro, 2191 FILETYPE_REG); 2192 if (status == 0) 2193 goto fail; 2194 2195 if (!fdiro->inode_read) { 2196 status = ext4fs_read_inode(fdiro->data, fdiro->ino, 2197 &fdiro->inode); 2198 if (status == 0) 2199 goto fail; 2200 } 2201 *len = __le32_to_cpu(fdiro->inode.size); 2202 ext4fs_file = fdiro; 2203 2204 return 0; 2205 fail: 2206 ext4fs_free_node(fdiro, &ext4fs_root->diropen); 2207 2208 return -1; 2209 } 2210 2211 int ext4fs_mount(unsigned part_length) 2212 { 2213 struct ext2_data *data; 2214 int status; 2215 struct ext_filesystem *fs = get_fs(); 2216 data = zalloc(SUPERBLOCK_SIZE); 2217 if (!data) 2218 return 0; 2219 2220 /* Read the superblock. */ 2221 status = ext4_read_superblock((char *)&data->sblock); 2222 2223 if (status == 0) 2224 goto fail; 2225 2226 /* Make sure this is an ext2 filesystem. */ 2227 if (__le16_to_cpu(data->sblock.magic) != EXT2_MAGIC) 2228 goto fail; 2229 2230 if (__le32_to_cpu(data->sblock.revision_level == 0)) 2231 fs->inodesz = 128; 2232 else 2233 fs->inodesz = __le16_to_cpu(data->sblock.inode_size); 2234 2235 debug("EXT2 rev %d, inode_size %d\n", 2236 __le32_to_cpu(data->sblock.revision_level), fs->inodesz); 2237 2238 data->diropen.data = data; 2239 data->diropen.ino = 2; 2240 data->diropen.inode_read = 1; 2241 data->inode = &data->diropen.inode; 2242 2243 status = ext4fs_read_inode(data, 2, data->inode); 2244 if (status == 0) 2245 goto fail; 2246 2247 ext4fs_root = data; 2248 2249 return 1; 2250 fail: 2251 printf("Failed to mount ext2 filesystem...\n"); 2252 free(data); 2253 ext4fs_root = NULL; 2254 2255 return 0; 2256 } 2257