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