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