ext2: fix inode size and calculations
[oweals/u-boot.git] / fs / ext2 / ext2fs.c
1 /*
2  * (C) Copyright 2004
3  *  esd gmbh <www.esd-electronics.com>
4  *  Reinhard Arlt <reinhard.arlt@esd-electronics.com>
5  *
6  *  based on code from grub2 fs/ext2.c and fs/fshelp.c by
7  *
8  *  GRUB  --  GRand Unified Bootloader
9  *  Copyright (C) 2003, 2004  Free Software Foundation, Inc.
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26 #include <common.h>
27 #include <ext2fs.h>
28 #include <malloc.h>
29 #include <asm/byteorder.h>
30
31 extern int ext2fs_devread (int sector, int byte_offset, int byte_len,
32                            char *buf);
33
34 /* Magic value used to identify an ext2 filesystem.  */
35 #define EXT2_MAGIC              0xEF53
36 /* Amount of indirect blocks in an inode.  */
37 #define INDIRECT_BLOCKS         12
38 /* Maximum lenght of a pathname.  */
39 #define EXT2_PATH_MAX           4096
40 /* Maximum nesting of symlinks, used to prevent a loop.  */
41 #define EXT2_MAX_SYMLINKCNT     8
42
43 /* Filetype used in directory entry.  */
44 #define FILETYPE_UNKNOWN        0
45 #define FILETYPE_REG            1
46 #define FILETYPE_DIRECTORY      2
47 #define FILETYPE_SYMLINK        7
48
49 /* Filetype information as used in inodes.  */
50 #define FILETYPE_INO_MASK       0170000
51 #define FILETYPE_INO_REG        0100000
52 #define FILETYPE_INO_DIRECTORY  0040000
53 #define FILETYPE_INO_SYMLINK    0120000
54
55 /* Bits used as offset in sector */
56 #define DISK_SECTOR_BITS        9
57
58 /* Log2 size of ext2 block in 512 blocks.  */
59 #define LOG2_EXT2_BLOCK_SIZE(data) (__le32_to_cpu (data->sblock.log2_block_size) + 1)
60
61 /* Log2 size of ext2 block in bytes.  */
62 #define LOG2_BLOCK_SIZE(data)      (__le32_to_cpu (data->sblock.log2_block_size) + 10)
63
64 /* The size of an ext2 block in bytes.  */
65 #define EXT2_BLOCK_SIZE(data)      (1 << LOG2_BLOCK_SIZE(data))
66
67 /* The ext2 superblock.  */
68 struct ext2_sblock {
69         uint32_t total_inodes;
70         uint32_t total_blocks;
71         uint32_t reserved_blocks;
72         uint32_t free_blocks;
73         uint32_t free_inodes;
74         uint32_t first_data_block;
75         uint32_t log2_block_size;
76         uint32_t log2_fragment_size;
77         uint32_t blocks_per_group;
78         uint32_t fragments_per_group;
79         uint32_t inodes_per_group;
80         uint32_t mtime;
81         uint32_t utime;
82         uint16_t mnt_count;
83         uint16_t max_mnt_count;
84         uint16_t magic;
85         uint16_t fs_state;
86         uint16_t error_handling;
87         uint16_t minor_revision_level;
88         uint32_t lastcheck;
89         uint32_t checkinterval;
90         uint32_t creator_os;
91         uint32_t revision_level;
92         uint16_t uid_reserved;
93         uint16_t gid_reserved;
94         uint32_t first_inode;
95         uint16_t inode_size;
96         uint16_t block_group_number;
97         uint32_t feature_compatibility;
98         uint32_t feature_incompat;
99         uint32_t feature_ro_compat;
100         uint32_t unique_id[4];
101         char volume_name[16];
102         char last_mounted_on[64];
103         uint32_t compression_info;
104 };
105
106 /* The ext2 blockgroup.  */
107 struct ext2_block_group {
108         uint32_t block_id;
109         uint32_t inode_id;
110         uint32_t inode_table_id;
111         uint16_t free_blocks;
112         uint16_t free_inodes;
113         uint16_t used_dir_cnt;
114         uint32_t reserved[3];
115 };
116
117 /* The ext2 inode.  */
118 struct ext2_inode {
119         uint16_t mode;
120         uint16_t uid;
121         uint32_t size;
122         uint32_t atime;
123         uint32_t ctime;
124         uint32_t mtime;
125         uint32_t dtime;
126         uint16_t gid;
127         uint16_t nlinks;
128         uint32_t blockcnt;      /* Blocks of 512 bytes!! */
129         uint32_t flags;
130         uint32_t osd1;
131         union {
132                 struct datablocks {
133                         uint32_t dir_blocks[INDIRECT_BLOCKS];
134                         uint32_t indir_block;
135                         uint32_t double_indir_block;
136                         uint32_t tripple_indir_block;
137                 } blocks;
138                 char symlink[60];
139         } b;
140         uint32_t version;
141         uint32_t acl;
142         uint32_t dir_acl;
143         uint32_t fragment_addr;
144         uint32_t osd2[3];
145 };
146
147 /* The header of an ext2 directory entry.  */
148 struct ext2_dirent {
149         uint32_t inode;
150         uint16_t direntlen;
151         uint8_t namelen;
152         uint8_t filetype;
153 };
154
155 struct ext2fs_node {
156         struct ext2_data *data;
157         struct ext2_inode inode;
158         int ino;
159         int inode_read;
160 };
161
162 /* Information about a "mounted" ext2 filesystem.  */
163 struct ext2_data {
164         struct ext2_sblock sblock;
165         struct ext2_inode *inode;
166         struct ext2fs_node diropen;
167 };
168
169
170 typedef struct ext2fs_node *ext2fs_node_t;
171
172 struct ext2_data *ext2fs_root = NULL;
173 ext2fs_node_t ext2fs_file = NULL;
174 int symlinknest = 0;
175 uint32_t *indir1_block = NULL;
176 int indir1_size = 0;
177 int indir1_blkno = -1;
178 uint32_t *indir2_block = NULL;
179 int indir2_size = 0;
180 int indir2_blkno = -1;
181
182
183 static int ext2fs_blockgroup
184         (struct ext2_data *data, int group, struct ext2_block_group *blkgrp) {
185         unsigned int blkno;
186         unsigned int blkoff;
187         unsigned int desc_per_blk;
188
189         desc_per_blk = EXT2_BLOCK_SIZE(data) / sizeof(struct ext2_block_group);
190
191         blkno = __le32_to_cpu(data->sblock.first_data_block) + 1 +
192         group / desc_per_blk;
193         blkoff = (group % desc_per_blk) * sizeof(struct ext2_block_group);
194 #ifdef DEBUG
195         printf ("ext2fs read %d group descriptor (blkno %d blkoff %d)\n",
196                 group, blkno, blkoff);
197 #endif
198         return (ext2fs_devread (blkno << LOG2_EXT2_BLOCK_SIZE(data),
199                 blkoff, sizeof(struct ext2_block_group), (char *)blkgrp));
200
201 }
202
203
204 static int ext2fs_read_inode
205         (struct ext2_data *data, int ino, struct ext2_inode *inode) {
206         struct ext2_block_group blkgrp;
207         struct ext2_sblock *sblock = &data->sblock;
208         int inodes_per_block;
209         int status;
210
211         unsigned int blkno;
212         unsigned int blkoff;
213
214 #ifdef DEBUG
215         printf ("ext2fs read inode %d\n", ino);
216 #endif
217         /* It is easier to calculate if the first inode is 0.  */
218         ino--;
219         status = ext2fs_blockgroup (data, ino / __le32_to_cpu
220                                     (sblock->inodes_per_group), &blkgrp);
221         if (status == 0) {
222                 return (0);
223         }
224
225         inodes_per_block = EXT2_BLOCK_SIZE(data) / __le16_to_cpu(sblock->inode_size);
226
227 #ifdef DEBUG
228         printf ("ext2fs read inode blkno %d blkoff %d\n", blkno, blkoff);
229 #endif
230
231         blkno = __le32_to_cpu (blkgrp.inode_table_id) +
232                 (ino % __le32_to_cpu (sblock->inodes_per_group))
233                 / inodes_per_block;
234         blkoff = (ino % inodes_per_block) * __le16_to_cpu (sblock->inode_size);
235 #ifdef DEBUG
236         printf ("ext2fs read inode blkno %d blkoff %d\n", blkno, blkoff);
237 #endif
238         /* Read the inode.  */
239         status = ext2fs_devread (blkno << LOG2_EXT2_BLOCK_SIZE (data), blkoff,
240                                  sizeof (struct ext2_inode), (char *) inode);
241         if (status == 0) {
242                 return (0);
243         }
244
245         return (1);
246 }
247
248
249 void ext2fs_free_node (ext2fs_node_t node, ext2fs_node_t currroot) {
250         if ((node != &ext2fs_root->diropen) && (node != currroot)) {
251                 free (node);
252         }
253 }
254
255
256 static int ext2fs_read_block (ext2fs_node_t node, int fileblock) {
257         struct ext2_data *data = node->data;
258         struct ext2_inode *inode = &node->inode;
259         int blknr;
260         int blksz = EXT2_BLOCK_SIZE (data);
261         int log2_blksz = LOG2_EXT2_BLOCK_SIZE (data);
262         int status;
263
264         /* Direct blocks.  */
265         if (fileblock < INDIRECT_BLOCKS) {
266                 blknr = __le32_to_cpu (inode->b.blocks.dir_blocks[fileblock]);
267         }
268         /* Indirect.  */
269         else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
270                 if (indir1_block == NULL) {
271                         indir1_block = (uint32_t *) malloc (blksz);
272                         if (indir1_block == NULL) {
273                                 printf ("** ext2fs read block (indir 1) malloc failed. **\n");
274                                 return (-1);
275                         }
276                         indir1_size = blksz;
277                         indir1_blkno = -1;
278                 }
279                 if (blksz != indir1_size) {
280                         free (indir1_block);
281                         indir1_block = NULL;
282                         indir1_size = 0;
283                         indir1_blkno = -1;
284                         indir1_block = (uint32_t *) malloc (blksz);
285                         if (indir1_block == NULL) {
286                                 printf ("** ext2fs read block (indir 1) malloc failed. **\n");
287                                 return (-1);
288                         }
289                         indir1_size = blksz;
290                 }
291                 if ((__le32_to_cpu (inode->b.blocks.indir_block) <<
292                      log2_blksz) != indir1_blkno) {
293                         status = ext2fs_devread (__le32_to_cpu(inode->b.blocks.indir_block) << log2_blksz,
294                                                  0, blksz,
295                                                  (char *) indir1_block);
296                         if (status == 0) {
297                                 printf ("** ext2fs read block (indir 1) failed. **\n");
298                                 return (0);
299                         }
300                         indir1_blkno =
301                                 __le32_to_cpu (inode->b.blocks.
302                                                indir_block) << log2_blksz;
303                 }
304                 blknr = __le32_to_cpu (indir1_block
305                                        [fileblock - INDIRECT_BLOCKS]);
306         }
307         /* Double indirect.  */
308         else if (fileblock <
309                  (INDIRECT_BLOCKS + (blksz / 4 * (blksz / 4 + 1)))) {
310                 unsigned int perblock = blksz / 4;
311                 unsigned int rblock = fileblock - (INDIRECT_BLOCKS
312                                                    + blksz / 4);
313
314                 if (indir1_block == NULL) {
315                         indir1_block = (uint32_t *) malloc (blksz);
316                         if (indir1_block == NULL) {
317                                 printf ("** ext2fs read block (indir 2 1) malloc failed. **\n");
318                                 return (-1);
319                         }
320                         indir1_size = blksz;
321                         indir1_blkno = -1;
322                 }
323                 if (blksz != indir1_size) {
324                         free (indir1_block);
325                         indir1_block = NULL;
326                         indir1_size = 0;
327                         indir1_blkno = -1;
328                         indir1_block = (uint32_t *) malloc (blksz);
329                         if (indir1_block == NULL) {
330                                 printf ("** ext2fs read block (indir 2 1) malloc failed. **\n");
331                                 return (-1);
332                         }
333                         indir1_size = blksz;
334                 }
335                 if ((__le32_to_cpu (inode->b.blocks.double_indir_block) <<
336                      log2_blksz) != indir1_blkno) {
337                         status = ext2fs_devread (__le32_to_cpu(inode->b.blocks.double_indir_block) << log2_blksz,
338                                                 0, blksz,
339                                                 (char *) indir1_block);
340                         if (status == 0) {
341                                 printf ("** ext2fs read block (indir 2 1) failed. **\n");
342                                 return (-1);
343                         }
344                         indir1_blkno =
345                                 __le32_to_cpu (inode->b.blocks.double_indir_block) << log2_blksz;
346                 }
347
348                 if (indir2_block == NULL) {
349                         indir2_block = (uint32_t *) malloc (blksz);
350                         if (indir2_block == NULL) {
351                                 printf ("** ext2fs read block (indir 2 2) malloc failed. **\n");
352                                 return (-1);
353                         }
354                         indir2_size = blksz;
355                         indir2_blkno = -1;
356                 }
357                 if (blksz != indir2_size) {
358                         free (indir2_block);
359                         indir2_block = NULL;
360                         indir2_size = 0;
361                         indir2_blkno = -1;
362                         indir2_block = (uint32_t *) malloc (blksz);
363                         if (indir2_block == NULL) {
364                                 printf ("** ext2fs read block (indir 2 2) malloc failed. **\n");
365                                 return (-1);
366                         }
367                         indir2_size = blksz;
368                 }
369                 if ((__le32_to_cpu (indir1_block[rblock / perblock]) <<
370                      log2_blksz) != indir1_blkno) {
371                         status = ext2fs_devread (__le32_to_cpu(indir1_block[rblock / perblock]) << log2_blksz,
372                                                  0, blksz,
373                                                  (char *) indir2_block);
374                         if (status == 0) {
375                                 printf ("** ext2fs read block (indir 2 2) failed. **\n");
376                                 return (-1);
377                         }
378                         indir2_blkno =
379                                 __le32_to_cpu (indir1_block[rblock / perblock]) << log2_blksz;
380                 }
381                 blknr = __le32_to_cpu (indir2_block[rblock % perblock]);
382         }
383         /* Tripple indirect.  */
384         else {
385                 printf ("** ext2fs doesn't support tripple indirect blocks. **\n");
386                 return (-1);
387         }
388 #ifdef DEBUG
389         printf ("ext2fs_read_block %08x\n", blknr);
390 #endif
391         return (blknr);
392 }
393
394
395 int ext2fs_read_file
396         (ext2fs_node_t node, int pos, unsigned int len, char *buf) {
397         int i;
398         int blockcnt;
399         int log2blocksize = LOG2_EXT2_BLOCK_SIZE (node->data);
400         int blocksize = 1 << (log2blocksize + DISK_SECTOR_BITS);
401         unsigned int filesize = __le32_to_cpu(node->inode.size);
402
403         /* Adjust len so it we can't read past the end of the file.  */
404         if (len > filesize) {
405                 len = filesize;
406         }
407         blockcnt = ((len + pos) + blocksize - 1) / blocksize;
408
409         for (i = pos / blocksize; i < blockcnt; i++) {
410                 int blknr;
411                 int blockoff = pos % blocksize;
412                 int blockend = blocksize;
413
414                 int skipfirst = 0;
415
416                 blknr = ext2fs_read_block (node, i);
417                 if (blknr < 0) {
418                         return (-1);
419                 }
420                 blknr = blknr << log2blocksize;
421
422                 /* Last block.  */
423                 if (i == blockcnt - 1) {
424                         blockend = (len + pos) % blocksize;
425
426                         /* The last portion is exactly blocksize.  */
427                         if (!blockend) {
428                                 blockend = blocksize;
429                         }
430                 }
431
432                 /* First block.  */
433                 if (i == pos / blocksize) {
434                         skipfirst = blockoff;
435                         blockend -= skipfirst;
436                 }
437
438                 /* If the block number is 0 this block is not stored on disk but
439                    is zero filled instead.  */
440                 if (blknr) {
441                         int status;
442
443                         status = ext2fs_devread (blknr, skipfirst, blockend, buf);
444                         if (status == 0) {
445                                 return (-1);
446                         }
447                 } else {
448                         memset (buf, 0, blocksize - skipfirst);
449                 }
450                 buf += blocksize - skipfirst;
451         }
452         return (len);
453 }
454
455
456 static int ext2fs_iterate_dir (ext2fs_node_t dir, char *name, ext2fs_node_t * fnode, int *ftype)
457 {
458         unsigned int fpos = 0;
459         int status;
460         struct ext2fs_node *diro = (struct ext2fs_node *) dir;
461
462 #ifdef DEBUG
463         if (name != NULL)
464                 printf ("Iterate dir %s\n", name);
465 #endif /* of DEBUG */
466         if (!diro->inode_read) {
467                 status = ext2fs_read_inode (diro->data, diro->ino,
468                                             &diro->inode);
469                 if (status == 0) {
470                         return (0);
471                 }
472         }
473         /* Search the file.  */
474         while (fpos < __le32_to_cpu (diro->inode.size)) {
475                 struct ext2_dirent dirent;
476
477                 status = ext2fs_read_file (diro, fpos,
478                                            sizeof (struct ext2_dirent),
479                                            (char *) &dirent);
480                 if (status < 1) {
481                         return (0);
482                 }
483                 if (dirent.namelen != 0) {
484                         char filename[dirent.namelen + 1];
485                         ext2fs_node_t fdiro;
486                         int type = FILETYPE_UNKNOWN;
487
488                         status = ext2fs_read_file (diro,
489                                                    fpos + sizeof (struct ext2_dirent),
490                                                    dirent.namelen, filename);
491                         if (status < 1) {
492                                 return (0);
493                         }
494                         fdiro = malloc (sizeof (struct ext2fs_node));
495                         if (!fdiro) {
496                                 return (0);
497                         }
498
499                         fdiro->data = diro->data;
500                         fdiro->ino = __le32_to_cpu (dirent.inode);
501
502                         filename[dirent.namelen] = '\0';
503
504                         if (dirent.filetype != FILETYPE_UNKNOWN) {
505                                 fdiro->inode_read = 0;
506
507                                 if (dirent.filetype == FILETYPE_DIRECTORY) {
508                                         type = FILETYPE_DIRECTORY;
509                                 } else if (dirent.filetype ==
510                                            FILETYPE_SYMLINK) {
511                                         type = FILETYPE_SYMLINK;
512                                 } else if (dirent.filetype == FILETYPE_REG) {
513                                         type = FILETYPE_REG;
514                                 }
515                         } else {
516                                 /* The filetype can not be read from the dirent, get it from inode */
517
518                                 status = ext2fs_read_inode (diro->data,
519                                                             __le32_to_cpu(dirent.inode),
520                                                             &fdiro->inode);
521                                 if (status == 0) {
522                                         free (fdiro);
523                                         return (0);
524                                 }
525                                 fdiro->inode_read = 1;
526
527                                 if ((__le16_to_cpu (fdiro->inode.mode) &
528                                      FILETYPE_INO_MASK) ==
529                                     FILETYPE_INO_DIRECTORY) {
530                                         type = FILETYPE_DIRECTORY;
531                                 } else if ((__le16_to_cpu (fdiro->inode.mode)
532                                             & FILETYPE_INO_MASK) ==
533                                            FILETYPE_INO_SYMLINK) {
534                                         type = FILETYPE_SYMLINK;
535                                 } else if ((__le16_to_cpu (fdiro->inode.mode)
536                                             & FILETYPE_INO_MASK) ==
537                                            FILETYPE_INO_REG) {
538                                         type = FILETYPE_REG;
539                                 }
540                         }
541 #ifdef DEBUG
542                         printf ("iterate >%s<\n", filename);
543 #endif /* of DEBUG */
544                         if ((name != NULL) && (fnode != NULL)
545                             && (ftype != NULL)) {
546                                 if (strcmp (filename, name) == 0) {
547                                         *ftype = type;
548                                         *fnode = fdiro;
549                                         return (1);
550                                 }
551                         } else {
552                                 if (fdiro->inode_read == 0) {
553                                         status = ext2fs_read_inode (diro->data,
554                                                             __le32_to_cpu (dirent.inode),
555                                                             &fdiro->inode);
556                                         if (status == 0) {
557                                                 free (fdiro);
558                                                 return (0);
559                                         }
560                                         fdiro->inode_read = 1;
561                                 }
562                                 switch (type) {
563                                 case FILETYPE_DIRECTORY:
564                                         printf ("<DIR> ");
565                                         break;
566                                 case FILETYPE_SYMLINK:
567                                         printf ("<SYM> ");
568                                         break;
569                                 case FILETYPE_REG:
570                                         printf ("      ");
571                                         break;
572                                 default:
573                                         printf ("< ? > ");
574                                         break;
575                                 }
576                                 printf ("%10d %s\n",
577                                         __le32_to_cpu (fdiro->inode.size),
578                                         filename);
579                         }
580                         free (fdiro);
581                 }
582                 fpos += __le16_to_cpu (dirent.direntlen);
583         }
584         return (0);
585 }
586
587
588 static char *ext2fs_read_symlink (ext2fs_node_t node) {
589         char *symlink;
590         struct ext2fs_node *diro = node;
591         int status;
592
593         if (!diro->inode_read) {
594                 status = ext2fs_read_inode (diro->data, diro->ino,
595                                             &diro->inode);
596                 if (status == 0) {
597                         return (0);
598                 }
599         }
600         symlink = malloc (__le32_to_cpu (diro->inode.size) + 1);
601         if (!symlink) {
602                 return (0);
603         }
604         /* If the filesize of the symlink is bigger than
605            60 the symlink is stored in a separate block,
606            otherwise it is stored in the inode.  */
607         if (__le32_to_cpu (diro->inode.size) <= 60) {
608                 strncpy (symlink, diro->inode.b.symlink,
609                          __le32_to_cpu (diro->inode.size));
610         } else {
611                 status = ext2fs_read_file (diro, 0,
612                                            __le32_to_cpu (diro->inode.size),
613                                            symlink);
614                 if (status == 0) {
615                         free (symlink);
616                         return (0);
617                 }
618         }
619         symlink[__le32_to_cpu (diro->inode.size)] = '\0';
620         return (symlink);
621 }
622
623
624 int ext2fs_find_file1
625         (const char *currpath,
626          ext2fs_node_t currroot, ext2fs_node_t * currfound, int *foundtype) {
627         char fpath[strlen (currpath) + 1];
628         char *name = fpath;
629         char *next;
630         int status;
631         int type = FILETYPE_DIRECTORY;
632         ext2fs_node_t currnode = currroot;
633         ext2fs_node_t oldnode = currroot;
634
635         strncpy (fpath, currpath, strlen (currpath) + 1);
636
637         /* Remove all leading slashes.  */
638         while (*name == '/') {
639                 name++;
640         }
641         if (!*name) {
642                 *currfound = currnode;
643                 return (1);
644         }
645
646         for (;;) {
647                 int found;
648
649                 /* Extract the actual part from the pathname.  */
650                 next = strchr (name, '/');
651                 if (next) {
652                         /* Remove all leading slashes.  */
653                         while (*next == '/') {
654                                 *(next++) = '\0';
655                         }
656                 }
657
658                 /* At this point it is expected that the current node is a directory, check if this is true.  */
659                 if (type != FILETYPE_DIRECTORY) {
660                         ext2fs_free_node (currnode, currroot);
661                         return (0);
662                 }
663
664                 oldnode = currnode;
665
666                 /* Iterate over the directory.  */
667                 found = ext2fs_iterate_dir (currnode, name, &currnode, &type);
668                 if (found == 0) {
669                         return (0);
670                 }
671                 if (found == -1) {
672                         break;
673                 }
674
675                 /* Read in the symlink and follow it.  */
676                 if (type == FILETYPE_SYMLINK) {
677                         char *symlink;
678
679                         /* Test if the symlink does not loop.  */
680                         if (++symlinknest == 8) {
681                                 ext2fs_free_node (currnode, currroot);
682                                 ext2fs_free_node (oldnode, currroot);
683                                 return (0);
684                         }
685
686                         symlink = ext2fs_read_symlink (currnode);
687                         ext2fs_free_node (currnode, currroot);
688
689                         if (!symlink) {
690                                 ext2fs_free_node (oldnode, currroot);
691                                 return (0);
692                         }
693 #ifdef DEBUG
694                         printf ("Got symlink >%s<\n", symlink);
695 #endif /* of DEBUG */
696                         /* The symlink is an absolute path, go back to the root inode.  */
697                         if (symlink[0] == '/') {
698                                 ext2fs_free_node (oldnode, currroot);
699                                 oldnode = &ext2fs_root->diropen;
700                         }
701
702                         /* Lookup the node the symlink points to.  */
703                         status = ext2fs_find_file1 (symlink, oldnode,
704                                                     &currnode, &type);
705
706                         free (symlink);
707
708                         if (status == 0) {
709                                 ext2fs_free_node (oldnode, currroot);
710                                 return (0);
711                         }
712                 }
713
714                 ext2fs_free_node (oldnode, currroot);
715
716                 /* Found the node!  */
717                 if (!next || *next == '\0') {
718                         *currfound = currnode;
719                         *foundtype = type;
720                         return (1);
721                 }
722                 name = next;
723         }
724         return (-1);
725 }
726
727
728 int ext2fs_find_file
729         (const char *path,
730          ext2fs_node_t rootnode, ext2fs_node_t * foundnode, int expecttype) {
731         int status;
732         int foundtype = FILETYPE_DIRECTORY;
733
734
735         symlinknest = 0;
736         if (!path) {
737                 return (0);
738         }
739
740         status = ext2fs_find_file1 (path, rootnode, foundnode, &foundtype);
741         if (status == 0) {
742                 return (0);
743         }
744         /* Check if the node that was found was of the expected type.  */
745         if ((expecttype == FILETYPE_REG) && (foundtype != expecttype)) {
746                 return (0);
747         } else if ((expecttype == FILETYPE_DIRECTORY)
748                    && (foundtype != expecttype)) {
749                 return (0);
750         }
751         return (1);
752 }
753
754
755 int ext2fs_ls (char *dirname) {
756         ext2fs_node_t dirnode;
757         int status;
758
759         if (ext2fs_root == NULL) {
760                 return (0);
761         }
762
763         status = ext2fs_find_file (dirname, &ext2fs_root->diropen, &dirnode,
764                                    FILETYPE_DIRECTORY);
765         if (status != 1) {
766                 printf ("** Can not find directory. **\n");
767                 return (1);
768         }
769         ext2fs_iterate_dir (dirnode, NULL, NULL, NULL);
770         ext2fs_free_node (dirnode, &ext2fs_root->diropen);
771         return (0);
772 }
773
774
775 int ext2fs_open (char *filename) {
776         ext2fs_node_t fdiro = NULL;
777         int status;
778         int len;
779
780         if (ext2fs_root == NULL) {
781                 return (-1);
782         }
783         ext2fs_file = NULL;
784         status = ext2fs_find_file (filename, &ext2fs_root->diropen, &fdiro,
785                                    FILETYPE_REG);
786         if (status == 0) {
787                 goto fail;
788         }
789         if (!fdiro->inode_read) {
790                 status = ext2fs_read_inode (fdiro->data, fdiro->ino,
791                                             &fdiro->inode);
792                 if (status == 0) {
793                         goto fail;
794                 }
795         }
796         len = __le32_to_cpu (fdiro->inode.size);
797         ext2fs_file = fdiro;
798         return (len);
799
800 fail:
801         ext2fs_free_node (fdiro, &ext2fs_root->diropen);
802         return (-1);
803 }
804
805
806 int ext2fs_close (void
807         ) {
808         if ((ext2fs_file != NULL) && (ext2fs_root != NULL)) {
809                 ext2fs_free_node (ext2fs_file, &ext2fs_root->diropen);
810                 ext2fs_file = NULL;
811         }
812         if (ext2fs_root != NULL) {
813                 free (ext2fs_root);
814                 ext2fs_root = NULL;
815         }
816         if (indir1_block != NULL) {
817                 free (indir1_block);
818                 indir1_block = NULL;
819                 indir1_size = 0;
820                 indir1_blkno = -1;
821         }
822         if (indir2_block != NULL) {
823                 free (indir2_block);
824                 indir2_block = NULL;
825                 indir2_size = 0;
826                 indir2_blkno = -1;
827         }
828         return (0);
829 }
830
831
832 int ext2fs_read (char *buf, unsigned len) {
833         int status;
834
835         if (ext2fs_root == NULL) {
836                 return (0);
837         }
838
839         if (ext2fs_file == NULL) {
840                 return (0);
841         }
842
843         status = ext2fs_read_file (ext2fs_file, 0, len, buf);
844         return (status);
845 }
846
847
848 int ext2fs_mount (unsigned part_length) {
849         struct ext2_data *data;
850         int status;
851
852         data = malloc (sizeof (struct ext2_data));
853         if (!data) {
854                 return (0);
855         }
856         /* Read the superblock.  */
857         status = ext2fs_devread (1 * 2, 0, sizeof (struct ext2_sblock),
858                                  (char *) &data->sblock);
859         if (status == 0) {
860                 goto fail;
861         }
862         /* Make sure this is an ext2 filesystem.  */
863         if (__le16_to_cpu (data->sblock.magic) != EXT2_MAGIC) {
864                 goto fail;
865         }
866         data->diropen.data = data;
867         data->diropen.ino = 2;
868         data->diropen.inode_read = 1;
869         data->inode = &data->diropen.inode;
870
871         status = ext2fs_read_inode (data, 2, data->inode);
872         if (status == 0) {
873                 goto fail;
874         }
875
876         ext2fs_root = data;
877
878         return (1);
879
880 fail:
881         printf ("Failed to mount ext2 filesystem...\n");
882         free (data);
883         ext2fs_root = NULL;
884         return (0);
885 }