[fs][ext2] read only ext2 fs support
diff --git a/include/lib/fs/ext2.h b/include/lib/fs/ext2.h
new file mode 100644
index 0000000..d8ccb54
--- /dev/null
+++ b/include/lib/fs/ext2.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2007 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef __EXT2_H
+#define __EXT2_H
+
+#include <lib/bio.h>
+#include <lib/fs.h>
+
+typedef void *fsfilecookie;
+
+int ext2_mount(bdev_t *dev, fscookie *cookie);
+int ext2_unmount(fscookie cookie);
+
+/* file api */
+int ext2_open_file(fscookie cookie, const char *path, fsfilecookie *fcookie);
+int ext2_read_file(fsfilecookie fcookie, void *buf, off_t offset, size_t len);
+int ext2_close_file(fsfilecookie fcookie);
+int ext2_stat_file(fsfilecookie fcookie, struct file_stat *);
+
+#endif
+
diff --git a/lib/fs/ext2/dir.c b/lib/fs/ext2/dir.c
new file mode 100644
index 0000000..016167f
--- /dev/null
+++ b/lib/fs/ext2/dir.c
@@ -0,0 +1,198 @@
+/*
+ * Copyright (c) 2007 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <stdlib.h>
+#include <debug.h>
+#include <err.h>
+#include <lib/fs/ext2.h>
+#include "ext2_priv.h"
+
+#define LOCAL_TRACE 0
+
+/* read in the dir, look for the entry */
+static int ext2_dir_lookup(ext2_t *ext2, struct ext2_inode *dir_inode, const char *name, inodenum_t *inum)
+{
+ uint file_blocknum;
+ int err;
+ uint8_t *buf;
+ size_t namelen = strlen(name);
+
+ if (!S_ISDIR(dir_inode->i_mode))
+ return ERR_NOT_DIR;
+
+ buf = malloc(EXT2_BLOCK_SIZE(ext2->sb));
+
+ file_blocknum = 0;
+ for (;;) {
+ /* read in the offset */
+ err = ext2_read_inode(ext2, dir_inode, buf, file_blocknum * EXT2_BLOCK_SIZE(ext2->sb), EXT2_BLOCK_SIZE(ext2->sb));
+ if (err <= 0) {
+ free(buf);
+ return -1;
+ }
+
+ /* walk through the directory entries, looking for the one that matches */
+ struct ext2_dir_entry_2 *ent;
+ uint pos = 0;
+ while (pos < EXT2_BLOCK_SIZE(ext2->sb)) {
+ ent = (struct ext2_dir_entry_2 *)&buf[pos];
+
+ LTRACEF("ent %d:%d: inode 0x%x, reclen %d, namelen %d\n",
+ file_blocknum, pos, LE32(ent->inode), LE16(ent->rec_len), ent->name_len/* , ent->name*/);
+
+ /* sanity check the record length */
+ if (LE16(ent->rec_len) == 0)
+ break;
+
+ if (ent->name_len == namelen && memcmp(name, ent->name, ent->name_len) == 0) {
+ // match
+ *inum = LE32(ent->inode);
+ LTRACEF("match: inode %d\n", *inum);
+ free(buf);
+ return 1;
+ }
+
+ pos += ROUNDUP(LE16(ent->rec_len), 4);
+ }
+
+ file_blocknum++;
+
+ /* sanity check the directory. 4MB should be enough */
+ if (file_blocknum > 1024) {
+ free(buf);
+ return -1;
+ }
+ }
+}
+
+/* note, trashes path */
+static int ext2_walk(ext2_t *ext2, char *path, struct ext2_inode *start_inode, inodenum_t *inum, int recurse)
+{
+ char *ptr;
+ struct ext2_inode inode;
+ struct ext2_inode dir_inode;
+ int err;
+ bool done;
+
+ LTRACEF("path '%s', start_inode %p, inum %p, recurse %d\n", path, start_inode, inum, recurse);
+
+ if (recurse > 4)
+ return ERR_RECURSE_TOO_DEEP;
+
+ /* chew up leading slashes */
+ ptr = &path[0];
+ while (*ptr == '/')
+ ptr++;
+
+ done = false;
+ memcpy(&dir_inode, start_inode, sizeof(struct ext2_inode));
+ while (!done) {
+ /* process the first component */
+ char *next_sep = strchr(ptr, '/');
+ if (next_sep) {
+ /* terminate the next component, giving us a substring */
+ *next_sep = 0;
+ } else {
+ /* this is the last component */
+ done = true;
+ }
+
+ LTRACEF("component '%s', done %d\n", ptr, done);
+
+ /* do the lookup on this component */
+ err = ext2_dir_lookup(ext2, &dir_inode, ptr, inum);
+ if (err < 0)
+ return err;
+
+nextcomponent:
+ LTRACEF("inum %u\n", *inum);
+
+ /* load the next inode */
+ err = ext2_load_inode(ext2, *inum, &inode);
+ if (err < 0)
+ return err;
+
+ /* is it a symlink? */
+ if (S_ISLNK(inode.i_mode)) {
+ char link[512];
+
+ LTRACEF("hit symlink\n");
+
+ err = ext2_read_link(ext2, &inode, link, sizeof(link));
+ if (err < 0)
+ return err;
+
+ LTRACEF("symlink read returns %d '%s'\n", err, link);
+
+ /* recurse, parsing the link */
+ if (link[0] == '/') {
+ /* link starts with '/', so start over again at the rootfs */
+ err = ext2_walk(ext2, link, &ext2->root_inode, inum, recurse + 1);
+ } else {
+ err = ext2_walk(ext2, link, &dir_inode, inum, recurse + 1);
+ }
+
+ LTRACEF("recursive walk returns %d\n", err);
+
+ if (err < 0)
+ return err;
+
+ /* if we weren't done with our path parsing, start again with the result of this recurse */
+ if (!done) {
+ goto nextcomponent;
+ }
+ } else if (S_ISDIR(inode.i_mode)) {
+ /* for the next cycle, point the dir inode at our new directory */
+ memcpy(&dir_inode, &inode, sizeof(struct ext2_inode));
+ } else {
+ if (!done) {
+ /* we aren't done and this walked over a nondir, abort */
+ LTRACEF("not finished and component is nondir\n");
+ return ERR_NOT_FOUND;
+ }
+ }
+
+ if (!done) {
+ /* move to the next seperator */
+ ptr = next_sep + 1;
+
+ /* consume multiple seperators */
+ while (*ptr == '/')
+ ptr++;
+ }
+ }
+
+ return 0;
+}
+
+/* do a path parse, looking up each component */
+int ext2_lookup(ext2_t *ext2, const char *_path, inodenum_t *inum)
+{
+ LTRACEF("path '%s', inum %p\n", _path, inum);
+
+ char path[512];
+ strlcpy(path, _path, sizeof(path));
+
+ return ext2_walk(ext2, path, &ext2->root_inode, inum, 1);
+}
+
diff --git a/lib/fs/ext2/ext2.c b/lib/fs/ext2/ext2.c
new file mode 100644
index 0000000..def2c0e
--- /dev/null
+++ b/lib/fs/ext2/ext2.c
@@ -0,0 +1,259 @@
+/*
+ * Copyright (c) 2007 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <string.h>
+#include <stdlib.h>
+#include <debug.h>
+#include <lib/fs/ext2.h>
+#include "ext2_priv.h"
+
+#define LOCAL_TRACE 0
+
+static void endian_swap_superblock(struct ext2_super_block *sb)
+{
+ LE32SWAP(sb->s_inodes_count);
+ LE32SWAP(sb->s_blocks_count);
+ LE32SWAP(sb->s_r_blocks_count);
+ LE32SWAP(sb->s_free_blocks_count);
+ LE32SWAP(sb->s_free_inodes_count);
+ LE32SWAP(sb->s_first_data_block);
+ LE32SWAP(sb->s_log_block_size);
+ LE32SWAP(sb->s_log_frag_size);
+ LE32SWAP(sb->s_blocks_per_group);
+ LE32SWAP(sb->s_frags_per_group);
+ LE32SWAP(sb->s_inodes_per_group);
+ LE32SWAP(sb->s_mtime);
+ LE32SWAP(sb->s_wtime);
+ LE16SWAP(sb->s_mnt_count);
+ LE16SWAP(sb->s_max_mnt_count);
+ LE16SWAP(sb->s_magic);
+ LE16SWAP(sb->s_state);
+ LE16SWAP(sb->s_errors);
+ LE16SWAP(sb->s_minor_rev_level);
+ LE32SWAP(sb->s_lastcheck);
+ LE32SWAP(sb->s_checkinterval);
+ LE32SWAP(sb->s_creator_os);
+ LE32SWAP(sb->s_rev_level);
+ LE16SWAP(sb->s_def_resuid);
+ LE16SWAP(sb->s_def_resgid);
+ LE32SWAP(sb->s_first_ino);
+ LE16SWAP(sb->s_inode_size);
+ LE16SWAP(sb->s_block_group_nr);
+ LE32SWAP(sb->s_feature_compat);
+ LE32SWAP(sb->s_feature_incompat);
+ LE32SWAP(sb->s_feature_ro_compat);
+ LE32SWAP(sb->s_algorithm_usage_bitmap);
+
+ /* ext3 journal stuff */
+ LE32SWAP(sb->s_journal_inum);
+ LE32SWAP(sb->s_journal_dev);
+ LE32SWAP(sb->s_last_orphan);
+ LE32SWAP(sb->s_default_mount_opts);
+ LE32SWAP(sb->s_first_meta_bg);
+}
+
+static void endian_swap_inode(struct ext2_inode *inode)
+{
+ LE16SWAP(inode->i_mode);
+ LE16SWAP(inode->i_uid_low);
+ LE32SWAP(inode->i_size);
+ LE32SWAP(inode->i_atime);
+ LE32SWAP(inode->i_ctime);
+ LE32SWAP(inode->i_mtime);
+ LE32SWAP(inode->i_dtime);
+ LE16SWAP(inode->i_gid_low);
+ LE16SWAP(inode->i_links_count);
+ LE32SWAP(inode->i_blocks);
+ LE32SWAP(inode->i_flags);
+
+ // leave block pointers/symlink data alone
+
+ LE32SWAP(inode->i_generation);
+ LE32SWAP(inode->i_file_acl);
+ LE32SWAP(inode->i_dir_acl);
+ LE32SWAP(inode->i_faddr);
+
+ LE16SWAP(inode->i_uid_high);
+ LE16SWAP(inode->i_gid_high);
+}
+
+static void endian_swap_group_desc(struct ext2_group_desc *gd)
+{
+ LE32SWAP(gd->bg_block_bitmap);
+ LE32SWAP(gd->bg_inode_bitmap);
+ LE32SWAP(gd->bg_inode_table);
+ LE16SWAP(gd->bg_free_blocks_count);
+ LE16SWAP(gd->bg_free_inodes_count);
+ LE16SWAP(gd->bg_used_dirs_count);
+}
+
+int ext2_mount(bdev_t *dev, fscookie *cookie)
+{
+ int err;
+
+ LTRACEF("dev %p\n", dev);
+
+ ext2_t *ext2 = malloc(sizeof(ext2_t));
+ ext2->dev = dev;
+
+ err = bio_read(dev, &ext2->sb, 1024, sizeof(struct ext2_super_block));
+ if (err < 0)
+ goto err;
+
+ endian_swap_superblock(&ext2->sb);
+
+ /* see if the superblock is good */
+ if (ext2->sb.s_magic != EXT2_SUPER_MAGIC) {
+ err = -1;
+ return err;
+ }
+
+ /* calculate group count, rounded up */
+ ext2->s_group_count = (ext2->sb.s_blocks_count + ext2->sb.s_blocks_per_group - 1) / ext2->sb.s_blocks_per_group;
+
+ /* print some info */
+ LTRACEF("rev level %d\n", ext2->sb.s_rev_level);
+ LTRACEF("compat features 0x%x\n", ext2->sb.s_feature_compat);
+ LTRACEF("incompat features 0x%x\n", ext2->sb.s_feature_incompat);
+ LTRACEF("ro compat features 0x%x\n", ext2->sb.s_feature_ro_compat);
+ LTRACEF("block size %d\n", EXT2_BLOCK_SIZE(ext2->sb));
+ LTRACEF("inode size %d\n", EXT2_INODE_SIZE(ext2->sb));
+ LTRACEF("block count %d\n", ext2->sb.s_blocks_count);
+ LTRACEF("blocks per group %d\n", ext2->sb.s_blocks_per_group);
+ LTRACEF("group count %d\n", ext2->s_group_count);
+ LTRACEF("inodes per group %d\n", ext2->sb.s_inodes_per_group);
+
+ /* we only support dynamic revs */
+ if (ext2->sb.s_rev_level > EXT2_DYNAMIC_REV) {
+ err = -2;
+ return err;
+ }
+
+ /* make sure it doesn't have any ro features we don't support */
+ if (ext2->sb.s_feature_ro_compat & ~(EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|EXT2_FEATURE_RO_COMPAT_LARGE_FILE)) {
+ err = -3;
+ return err;
+ }
+
+ /* read in all the group descriptors */
+ ext2->gd = malloc(sizeof(struct ext2_group_desc) * ext2->s_group_count);
+ err = bio_read(ext2->dev, (void *)ext2->gd,
+ (EXT2_BLOCK_SIZE(ext2->sb) == 4096) ? 4096 : 2048,
+ sizeof(struct ext2_group_desc) * ext2->s_group_count);
+ if (err < 0) {
+ err = -4;
+ return err;
+ }
+
+ int i;
+ for (i=0; i < ext2->s_group_count; i++) {
+ endian_swap_group_desc(&ext2->gd[i]);
+ LTRACEF("group %d:\n", i);
+ LTRACEF("\tblock bitmap %d\n", ext2->gd[i].bg_block_bitmap);
+ LTRACEF("\tinode bitmap %d\n", ext2->gd[i].bg_inode_bitmap);
+ LTRACEF("\tinode table %d\n", ext2->gd[i].bg_inode_table);
+ LTRACEF("\tfree blocks %d\n", ext2->gd[i].bg_free_blocks_count);
+ LTRACEF("\tfree inodes %d\n", ext2->gd[i].bg_free_inodes_count);
+ LTRACEF("\tused dirs %d\n", ext2->gd[i].bg_used_dirs_count);
+ }
+
+ /* initialize the block cache */
+ ext2->cache = bcache_create(ext2->dev, EXT2_BLOCK_SIZE(ext2->sb), 4);
+
+ /* load the first inode */
+ err = ext2_load_inode(ext2, EXT2_ROOT_INO, &ext2->root_inode);
+ if (err < 0)
+ goto err;
+
+// TRACE("successfully mounted volume\n");
+
+ *cookie = ext2;
+
+ return 0;
+
+err:
+ LTRACEF("exiting with err code %d\n", err);
+
+ free(ext2);
+ return err;
+}
+
+int ext2_unmount(fscookie cookie)
+{
+ // free it up
+ ext2_t *ext2 = (ext2_t *)cookie;
+
+ bcache_destroy(ext2->cache);
+ free(ext2->gd);
+ free(ext2);
+
+ return 0;
+}
+
+static void get_inode_addr(ext2_t *ext2, inodenum_t num, blocknum_t *block, size_t *block_offset)
+{
+ num--;
+
+ uint32_t group = num / ext2->sb.s_inodes_per_group;
+
+ // calculate the start of the inode table for the group it's in
+ *block = ext2->gd[group].bg_inode_table;
+
+ // add the offset of the inode within the group
+ size_t offset = (num % EXT2_INODES_PER_GROUP(ext2->sb)) * EXT2_INODE_SIZE(ext2->sb);
+ *block_offset = offset % EXT2_BLOCK_SIZE(ext2->sb);
+ *block += offset / EXT2_BLOCK_SIZE(ext2->sb);
+}
+
+int ext2_load_inode(ext2_t *ext2, inodenum_t num, struct ext2_inode *inode)
+{
+ int err;
+
+ LTRACEF("num %d, inode %p\n", num, inode);
+
+ blocknum_t bnum;
+ size_t block_offset;
+ get_inode_addr(ext2, num, &bnum, &block_offset);
+
+ LTRACEF("bnum %u, offset %zd\n", bnum, block_offset);
+
+ /* get a pointer to the cache block */
+ void *cache_ptr;
+ err = bcache_get_block(ext2->cache, &cache_ptr, bnum);
+ if (err < 0)
+ return err;
+
+ /* copy the inode out */
+ memcpy(inode, (uint8_t *)cache_ptr + block_offset, sizeof(struct ext2_inode));
+
+ /* put the cache block */
+ bcache_put_block(ext2->cache, bnum);
+
+ /* endian swap it */
+ endian_swap_inode(inode);
+
+ LTRACEF("read inode: mode 0x%x, size %d\n", inode->i_mode, inode->i_size);
+
+ return 0;
+}
+
diff --git a/lib/fs/ext2/ext2_fs.h b/lib/fs/ext2/ext2_fs.h
new file mode 100644
index 0000000..9429cba
--- /dev/null
+++ b/lib/fs/ext2/ext2_fs.h
@@ -0,0 +1,436 @@
+/*
+ * linux/include/linux/ext2_fs.h
+ *
+ * Copyright (C) 1992, 1993, 1994, 1995
+ * Remy Card (card@masi.ibp.fr)
+ * Laboratoire MASI - Institut Blaise Pascal
+ * Universite Pierre et Marie Curie (Paris VI)
+ *
+ * from
+ *
+ * linux/include/linux/minix_fs.h
+ *
+ * Copyright (C) 1991, 1992 Linus Torvalds
+ */
+
+#ifndef _LINUX_EXT2_FS_H
+#define _LINUX_EXT2_FS_H
+
+#include <sys/types.h>
+#include <stdint.h>
+
+//#include <linux/types.h>
+//#include <linux/magic.h>
+
+/*
+ * The second extended filesystem constants/structures
+ */
+
+/*
+ * Special inode numbers
+ */
+#define EXT2_BAD_INO 1 /* Bad blocks inode */
+#define EXT2_ROOT_INO 2 /* Root inode */
+#define EXT2_BOOT_LOADER_INO 5 /* Boot loader inode */
+#define EXT2_UNDEL_DIR_INO 6 /* Undelete directory inode */
+
+/* First non-reserved inode for old ext2 filesystems */
+#define EXT2_GOOD_OLD_FIRST_INO 11
+
+/*
+ * Maximal count of links to a file
+ */
+#define EXT2_LINK_MAX 32000
+
+/*
+ * Macro-instructions used to manage several block sizes
+ */
+#define EXT2_MIN_BLOCK_SIZE 1024
+#define EXT2_MAX_BLOCK_SIZE 4096
+#define EXT2_MIN_BLOCK_LOG_SIZE 10
+#define EXT2_BLOCK_SIZE(s) ((uint32_t)EXT2_MIN_BLOCK_SIZE << (s).s_log_block_size)
+#define EXT2_ADDR_PER_BLOCK(s) (EXT2_BLOCK_SIZE(s) / sizeof (uint32_t))
+#define EXT2_BLOCK_SIZE_BITS(s) ((s).s_log_block_size + 10)
+#define EXT2_INODE_SIZE(s) (((s).s_rev_level == EXT2_GOOD_OLD_REV) ? \
+ EXT2_GOOD_OLD_INODE_SIZE : \
+ (s).s_inode_size)
+#define EXT2_FIRST_INO(s) (((s).s_rev_level == EXT2_GOOD_OLD_REV) ? \
+ EXT2_GOOD_OLD_FIRST_INO : \
+ (s).s_first_ino)
+
+/*
+ * Macro-instructions used to manage fragments
+ */
+#define EXT2_MIN_FRAG_SIZE 1024
+#define EXT2_MAX_FRAG_SIZE 4096
+#define EXT2_MIN_FRAG_LOG_SIZE 10
+#define EXT2_FRAG_SIZE(s) (EXT2_MIN_FRAG_SIZE << (s).s_log_frag_size)
+#define EXT2_FRAGS_PER_BLOCK(s) (EXT2_BLOCK_SIZE(s) / EXT2_FRAG_SIZE(s))
+
+/*
+ * Structure of a blocks group descriptor
+ */
+struct ext2_group_desc
+{
+ uint32_t bg_block_bitmap; /* Blocks bitmap block */
+ uint32_t bg_inode_bitmap; /* Inodes bitmap block */
+ uint32_t bg_inode_table; /* Inodes table block */
+ uint16_t bg_free_blocks_count; /* Free blocks count */
+ uint16_t bg_free_inodes_count; /* Free inodes count */
+ uint16_t bg_used_dirs_count; /* Directories count */
+ uint16_t bg_pad;
+ uint32_t bg_reserved[3];
+};
+
+/*
+ * Macro-instructions used to manage group descriptors
+ */
+#define EXT2_BLOCKS_PER_GROUP(s) ((s).s_blocks_per_group)
+#define EXT2_DESC_PER_BLOCK(s) (EXT2_BLOCK_SIZE(s) / sizeof (struct ext2_group_desc))
+#define EXT2_INODES_PER_GROUP(s) ((s).s_inodes_per_group)
+
+/*
+ * Constants relative to the data blocks
+ */
+#define EXT2_NDIR_BLOCKS 12
+#define EXT2_IND_BLOCK EXT2_NDIR_BLOCKS
+#define EXT2_DIND_BLOCK (EXT2_IND_BLOCK + 1)
+#define EXT2_TIND_BLOCK (EXT2_DIND_BLOCK + 1)
+#define EXT2_N_BLOCKS (EXT2_TIND_BLOCK + 1)
+
+/*
+ * Structure of an inode on the disk
+ */
+struct ext2_inode {
+ uint16_t i_mode; /* File mode */
+ uint16_t i_uid; /* Low 16 bits of Owner Uid */
+ uint32_t i_size; /* Size in bytes */
+ uint32_t i_atime; /* Access time */
+ uint32_t i_ctime; /* Creation time */
+ uint32_t i_mtime; /* Modification time */
+ uint32_t i_dtime; /* Deletion Time */
+ uint16_t i_gid; /* Low 16 bits of Group Id */
+ uint16_t i_links_count; /* Links count */
+ uint32_t i_blocks; /* Blocks count */
+ uint32_t i_flags; /* File flags */
+ union {
+ struct {
+ uint32_t l_i_reserved1;
+ } linux1;
+ struct {
+ uint32_t h_i_translator;
+ } hurd1;
+ struct {
+ uint32_t m_i_reserved1;
+ } masix1;
+ } osd1; /* OS dependent 1 */
+ uint32_t i_block[EXT2_N_BLOCKS];/* Pointers to blocks */
+ uint32_t i_generation; /* File version (for NFS) */
+ uint32_t i_file_acl; /* File ACL */
+ uint32_t i_dir_acl; /* Directory ACL */
+ uint32_t i_faddr; /* Fragment address */
+ union {
+ struct {
+ uint8_t l_i_frag; /* Fragment number */
+ uint8_t l_i_fsize; /* Fragment size */
+ uint16_t i_pad1;
+ uint16_t l_i_uid_high; /* these 2 fields */
+ uint16_t l_i_gid_high; /* were reserved2[0] */
+ uint32_t l_i_reserved2;
+ } linux2;
+ struct {
+ uint8_t h_i_frag; /* Fragment number */
+ uint8_t h_i_fsize; /* Fragment size */
+ uint16_t h_i_mode_high;
+ uint16_t h_i_uid_high;
+ uint16_t h_i_gid_high;
+ uint32_t h_i_author;
+ } hurd2;
+ struct {
+ uint8_t m_i_frag; /* Fragment number */
+ uint8_t m_i_fsize; /* Fragment size */
+ uint16_t m_pad1;
+ uint32_t m_i_reserved2[2];
+ } masix2;
+ } osd2; /* OS dependent 2 */
+};
+
+#define i_size_high i_dir_acl
+
+#define i_reserved1 osd1.linux1.l_i_reserved1
+#define i_frag osd2.linux2.l_i_frag
+#define i_fsize osd2.linux2.l_i_fsize
+#define i_uid_low i_uid
+#define i_gid_low i_gid
+#define i_uid_high osd2.linux2.l_i_uid_high
+#define i_gid_high osd2.linux2.l_i_gid_high
+#define i_reserved2 osd2.linux2.l_i_reserved2
+
+/*
+ * File system states
+ */
+#define EXT2_VALID_FS 0x0001 /* Unmounted cleanly */
+#define EXT2_ERROR_FS 0x0002 /* Errors detected */
+
+/*
+ * Mount flags
+ */
+#define EXT2_MOUNT_CHECK 0x000001 /* Do mount-time checks */
+#define EXT2_MOUNT_OLDALLOC 0x000002 /* Don't use the new Orlov allocator */
+#define EXT2_MOUNT_GRPID 0x000004 /* Create files with directory's group */
+#define EXT2_MOUNT_DEBUG 0x000008 /* Some debugging messages */
+#define EXT2_MOUNT_ERRORS_CONT 0x000010 /* Continue on errors */
+#define EXT2_MOUNT_ERRORS_RO 0x000020 /* Remount fs ro on errors */
+#define EXT2_MOUNT_ERRORS_PANIC 0x000040 /* Panic on errors */
+#define EXT2_MOUNT_MINIX_DF 0x000080 /* Mimics the Minix statfs */
+#define EXT2_MOUNT_NOBH 0x000100 /* No buffer_heads */
+#define EXT2_MOUNT_NO_UID32 0x000200 /* Disable 32-bit UIDs */
+#define EXT2_MOUNT_XATTR_USER 0x004000 /* Extended user attributes */
+#define EXT2_MOUNT_POSIX_ACL 0x008000 /* POSIX Access Control Lists */
+#define EXT2_MOUNT_XIP 0x010000 /* Execute in place */
+#define EXT2_MOUNT_USRQUOTA 0x020000 /* user quota */
+#define EXT2_MOUNT_GRPQUOTA 0x040000 /* group quota */
+
+
+#define clear_opt(o, opt) o &= ~EXT2_MOUNT_##opt
+#define set_opt(o, opt) o |= EXT2_MOUNT_##opt
+#define test_opt(sb, opt) (EXT2_SB(sb)->s_mount_opt & \
+ EXT2_MOUNT_##opt)
+/*
+ * Maximal mount counts between two filesystem checks
+ */
+#define EXT2_DFL_MAX_MNT_COUNT 20 /* Allow 20 mounts */
+#define EXT2_DFL_CHECKINTERVAL 0 /* Don't use interval check */
+
+/*
+ * Behaviour when detecting errors
+ */
+#define EXT2_ERRORS_CONTINUE 1 /* Continue execution */
+#define EXT2_ERRORS_RO 2 /* Remount fs read-only */
+#define EXT2_ERRORS_PANIC 3 /* Panic */
+#define EXT2_ERRORS_DEFAULT EXT2_ERRORS_CONTINUE
+
+#define EXT2_SUPER_MAGIC 0xEF53
+#define EXT3_SUPER_MAGIC 0xEF53
+#define EXT4_SUPER_MAGIC 0xEF53
+
+/*
+ * Structure of the super block
+ */
+struct ext2_super_block {
+ uint32_t s_inodes_count; /* Inodes count */
+ uint32_t s_blocks_count; /* Blocks count */
+ uint32_t s_r_blocks_count; /* Reserved blocks count */
+ uint32_t s_free_blocks_count; /* Free blocks count */
+ uint32_t s_free_inodes_count; /* Free inodes count */
+ uint32_t s_first_data_block; /* First Data Block */
+ uint32_t s_log_block_size; /* Block size */
+ uint32_t s_log_frag_size; /* Fragment size */
+ uint32_t s_blocks_per_group; /* # Blocks per group */
+ uint32_t s_frags_per_group; /* # Fragments per group */
+ uint32_t s_inodes_per_group; /* # Inodes per group */
+ uint32_t s_mtime; /* Mount time */
+ uint32_t s_wtime; /* Write time */
+ uint16_t s_mnt_count; /* Mount count */
+ uint16_t s_max_mnt_count; /* Maximal mount count */
+ uint16_t s_magic; /* Magic signature */
+ uint16_t s_state; /* File system state */
+ uint16_t s_errors; /* Behaviour when detecting errors */
+ uint16_t s_minor_rev_level; /* minor revision level */
+ uint32_t s_lastcheck; /* time of last check */
+ uint32_t s_checkinterval; /* max. time between checks */
+ uint32_t s_creator_os; /* OS */
+ uint32_t s_rev_level; /* Revision level */
+ uint16_t s_def_resuid; /* Default uid for reserved blocks */
+ uint16_t s_def_resgid; /* Default gid for reserved blocks */
+ /*
+ * These fields are for EXT2_DYNAMIC_REV superblocks only.
+ *
+ * Note: the difference between the compatible feature set and
+ * the incompatible feature set is that if there is a bit set
+ * in the incompatible feature set that the kernel doesn't
+ * know about, it should refuse to mount the filesystem.
+ *
+ * e2fsck's requirements are more strict; if it doesn't know
+ * about a feature in either the compatible or incompatible
+ * feature set, it must abort and not try to meddle with
+ * things it doesn't understand...
+ */
+ uint32_t s_first_ino; /* First non-reserved inode */
+ uint16_t s_inode_size; /* size of inode structure */
+ uint16_t s_block_group_nr; /* block group # of this superblock */
+ uint32_t s_feature_compat; /* compatible feature set */
+ uint32_t s_feature_incompat; /* incompatible feature set */
+ uint32_t s_feature_ro_compat; /* readonly-compatible feature set */
+ uint8_t s_uuid[16]; /* 128-bit uuid for volume */
+ char s_volume_name[16]; /* volume name */
+ char s_last_mounted[64]; /* directory where last mounted */
+ uint32_t s_algorithm_usage_bitmap; /* For compression */
+ /*
+ * Performance hints. Directory preallocation should only
+ * happen if the EXT2_COMPAT_PREALLOC flag is on.
+ */
+ uint8_t s_prealloc_blocks; /* Nr of blocks to try to preallocate*/
+ uint8_t s_prealloc_dir_blocks; /* Nr to preallocate for dirs */
+ uint16_t s_padding1;
+ /*
+ * Journaling support valid if EXT3_FEATURE_COMPAT_HAS_JOURNAL set.
+ */
+ uint8_t s_journal_uuid[16]; /* uuid of journal superblock */
+ uint32_t s_journal_inum; /* inode number of journal file */
+ uint32_t s_journal_dev; /* device number of journal file */
+ uint32_t s_last_orphan; /* start of list of inodes to delete */
+ uint32_t s_hash_seed[4]; /* HTREE hash seed */
+ uint8_t s_def_hash_version; /* Default hash version to use */
+ uint8_t s_reserved_char_pad;
+ uint16_t s_reserved_word_pad;
+ uint32_t s_default_mount_opts;
+ uint32_t s_first_meta_bg; /* First metablock block group */
+ uint32_t s_reserved[190]; /* Padding to the end of the block */
+};
+
+/*
+ * Codes for operating systems
+ */
+#define EXT2_OS_LINUX 0
+#define EXT2_OS_HURD 1
+#define EXT2_OS_MASIX 2
+#define EXT2_OS_FREEBSD 3
+#define EXT2_OS_LITES 4
+
+/*
+ * Revision levels
+ */
+#define EXT2_GOOD_OLD_REV 0 /* The good old (original) format */
+#define EXT2_DYNAMIC_REV 1 /* V2 format w/ dynamic inode sizes */
+
+#define EXT2_CURRENT_REV EXT2_GOOD_OLD_REV
+#define EXT2_MAX_SUPP_REV EXT2_DYNAMIC_REV
+
+#define EXT2_GOOD_OLD_INODE_SIZE 128
+
+/*
+ * Feature set definitions
+ */
+
+#define EXT2_HAS_COMPAT_FEATURE(sb,mask) \
+ ( (sb).s_feature_compat & cpu_to_le32(mask) )
+#define EXT2_HAS_RO_COMPAT_FEATURE(sb,mask) \
+ ( (sb).s_feature_ro_compat & cpu_to_le32(mask) )
+#define EXT2_HAS_INCOMPAT_FEATURE(sb,mask) \
+ ( (sb).s_feature_incompat & cpu_to_le32(mask) )
+#define EXT2_SET_COMPAT_FEATURE(sb,mask) \
+ (sb).s_feature_compat |= cpu_to_le32(mask)
+#define EXT2_SET_RO_COMPAT_FEATURE(sb,mask) \
+ (sb).s_feature_ro_compat |= cpu_to_le32(mask)
+#define EXT2_SET_INCOMPAT_FEATURE(sb,mask) \
+ (sb).s_feature_incompat |= cpu_to_le32(mask)
+#define EXT2_CLEAR_COMPAT_FEATURE(sb,mask) \
+ (sb).s_feature_compat &= ~cpu_to_le32(mask)
+#define EXT2_CLEAR_RO_COMPAT_FEATURE(sb,mask) \
+ (sb).s_feature_ro_compat &= ~cpu_to_le32(mask)
+#define EXT2_CLEAR_INCOMPAT_FEATURE(sb,mask) \
+ (sb).s_feature_incompat &= ~cpu_to_le32(mask)
+
+#define EXT2_FEATURE_COMPAT_DIR_PREALLOC 0x0001
+#define EXT2_FEATURE_COMPAT_IMAGIC_INODES 0x0002
+#define EXT3_FEATURE_COMPAT_HAS_JOURNAL 0x0004
+#define EXT2_FEATURE_COMPAT_EXT_ATTR 0x0008
+#define EXT2_FEATURE_COMPAT_RESIZE_INO 0x0010
+#define EXT2_FEATURE_COMPAT_DIR_INDEX 0x0020
+#define EXT2_FEATURE_COMPAT_ANY 0xffffffff
+
+#define EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER 0x0001
+#define EXT2_FEATURE_RO_COMPAT_LARGE_FILE 0x0002
+#define EXT2_FEATURE_RO_COMPAT_BTREE_DIR 0x0004
+#define EXT2_FEATURE_RO_COMPAT_ANY 0xffffffff
+
+#define EXT2_FEATURE_INCOMPAT_COMPRESSION 0x0001
+#define EXT2_FEATURE_INCOMPAT_FILETYPE 0x0002
+#define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004
+#define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008
+#define EXT2_FEATURE_INCOMPAT_META_BG 0x0010
+#define EXT2_FEATURE_INCOMPAT_ANY 0xffffffff
+
+#define EXT2_FEATURE_COMPAT_SUPP EXT2_FEATURE_COMPAT_EXT_ATTR
+#define EXT2_FEATURE_INCOMPAT_SUPP (EXT2_FEATURE_INCOMPAT_FILETYPE| \
+ EXT2_FEATURE_INCOMPAT_META_BG)
+#define EXT2_FEATURE_RO_COMPAT_SUPP (EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER| \
+ EXT2_FEATURE_RO_COMPAT_LARGE_FILE| \
+ EXT2_FEATURE_RO_COMPAT_BTREE_DIR)
+#define EXT2_FEATURE_RO_COMPAT_UNSUPPORTED ~EXT2_FEATURE_RO_COMPAT_SUPP
+#define EXT2_FEATURE_INCOMPAT_UNSUPPORTED ~EXT2_FEATURE_INCOMPAT_SUPP
+
+/*
+ * Default values for user and/or group using reserved blocks
+ */
+#define EXT2_DEF_RESUID 0
+#define EXT2_DEF_RESGID 0
+
+/*
+ * Default mount options
+ */
+#define EXT2_DEFM_DEBUG 0x0001
+#define EXT2_DEFM_BSDGROUPS 0x0002
+#define EXT2_DEFM_XATTR_USER 0x0004
+#define EXT2_DEFM_ACL 0x0008
+#define EXT2_DEFM_UID16 0x0010
+ /* Not used by ext2, but reserved for use by ext3 */
+#define EXT3_DEFM_JMODE 0x0060
+#define EXT3_DEFM_JMODE_DATA 0x0020
+#define EXT3_DEFM_JMODE_ORDERED 0x0040
+#define EXT3_DEFM_JMODE_WBACK 0x0060
+
+/*
+ * Structure of a directory entry
+ */
+#define EXT2_NAME_LEN 255
+
+struct ext2_dir_entry {
+ uint32_t inode; /* Inode number */
+ uint16_t rec_len; /* Directory entry length */
+ uint16_t name_len; /* Name length */
+ char name[EXT2_NAME_LEN]; /* File name */
+};
+
+/*
+ * The new version of the directory entry. Since EXT2 structures are
+ * stored in intel byte order, and the name_len field could never be
+ * bigger than 255 chars, it's safe to reclaim the extra byte for the
+ * file_type field.
+ */
+struct ext2_dir_entry_2 {
+ uint32_t inode; /* Inode number */
+ uint16_t rec_len; /* Directory entry length */
+ uint8_t name_len; /* Name length */
+ uint8_t file_type;
+ char name[EXT2_NAME_LEN]; /* File name */
+};
+
+/*
+ * Ext2 directory file types. Only the low 3 bits are used. The
+ * other bits are reserved for now.
+ */
+enum {
+ EXT2_FT_UNKNOWN,
+ EXT2_FT_REG_FILE,
+ EXT2_FT_DIR,
+ EXT2_FT_CHRDEV,
+ EXT2_FT_BLKDEV,
+ EXT2_FT_FIFO,
+ EXT2_FT_SOCK,
+ EXT2_FT_SYMLINK,
+ EXT2_FT_MAX
+};
+
+/*
+ * EXT2_DIR_PAD defines the directory entries boundaries
+ *
+ * NOTE: It must be a multiple of 4
+ */
+#define EXT2_DIR_PAD 4
+#define EXT2_DIR_ROUND (EXT2_DIR_PAD - 1)
+#define EXT2_DIR_REC_LEN(name_len) (((name_len) + 8 + EXT2_DIR_ROUND) & \
+ ~EXT2_DIR_ROUND)
+
+#endif /* _LINUX_EXT2_FS_H */
diff --git a/lib/fs/ext2/ext2_priv.h b/lib/fs/ext2/ext2_priv.h
new file mode 100644
index 0000000..f0fbb35
--- /dev/null
+++ b/lib/fs/ext2/ext2_priv.h
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2007 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef __EXT2_PRIV_H
+#define __EXT2_PRIV_H
+
+#include <lib/bio.h>
+#include <lib/bcache.h>
+#include "ext2_fs.h"
+
+typedef uint32_t blocknum_t;
+typedef uint32_t inodenum_t;
+typedef uint32_t groupnum_t;
+
+typedef struct {
+ bdev_t *dev;
+ bcache_t cache;
+
+ struct ext2_super_block sb;
+ int s_group_count;
+ struct ext2_group_desc *gd;
+ struct ext2_inode root_inode;
+} ext2_t;
+
+struct cache_block {
+ blocknum_t num;
+ void *ptr;
+};
+
+/* open file handle */
+typedef struct {
+ ext2_t *ext2;
+
+ struct cache_block ind_cache[3]; // cache of indirect blocks as they're scanned
+ struct ext2_inode inode;
+} ext2_file_t;
+
+/* internal routines */
+int ext2_load_inode(ext2_t *ext2, inodenum_t num, struct ext2_inode *inode);
+int ext2_lookup(ext2_t *ext2, const char *path, inodenum_t *inum); // path to inode
+
+/* io */
+int ext2_read_block(ext2_t *ext2, void *buf, blocknum_t bnum);
+int ext2_get_block(ext2_t *ext2, void **ptr, blocknum_t bnum);
+int ext2_put_block(ext2_t *ext2, blocknum_t bnum);
+
+off_t ext2_file_len(ext2_t *ext2, struct ext2_inode *inode);
+int ext2_read_inode(ext2_t *ext2, struct ext2_inode *inode, void *buf, off_t offset, size_t len);
+int ext2_read_link(ext2_t *ext2, struct ext2_inode *inode, char *str, size_t len);
+
+/* mode stuff */
+#define S_IFMT 0170000
+#define S_IFIFO 0010000
+#define S_IFCHR 0020000
+#define S_IFDIR 0040000
+#define S_IFBLK 0060000
+#define S_IFREG 0100000
+#define S_IFLNK 0120000
+#define S_IFSOCK 0140000
+
+#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
+#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
+#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
+#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
+#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
+#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
+#define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
+
+#endif
+
diff --git a/lib/fs/ext2/ext3_fs.h b/lib/fs/ext2/ext3_fs.h
new file mode 100644
index 0000000..ece49a8
--- /dev/null
+++ b/lib/fs/ext2/ext3_fs.h
@@ -0,0 +1,886 @@
+/*
+ * linux/include/linux/ext3_fs.h
+ *
+ * Copyright (C) 1992, 1993, 1994, 1995
+ * Remy Card (card@masi.ibp.fr)
+ * Laboratoire MASI - Institut Blaise Pascal
+ * Universite Pierre et Marie Curie (Paris VI)
+ *
+ * from
+ *
+ * linux/include/linux/minix_fs.h
+ *
+ * Copyright (C) 1991, 1992 Linus Torvalds
+ */
+
+#ifndef _LINUX_EXT3_FS_H
+#define _LINUX_EXT3_FS_H
+
+#include <linux/types.h>
+#include <linux/magic.h>
+
+/*
+ * The second extended filesystem constants/structures
+ */
+
+/*
+ * Define EXT3FS_DEBUG to produce debug messages
+ */
+#undef EXT3FS_DEBUG
+
+/*
+ * Define EXT3_RESERVATION to reserve data blocks for expanding files
+ */
+#define EXT3_DEFAULT_RESERVE_BLOCKS 8
+/*max window size: 1024(direct blocks) + 3([t,d]indirect blocks) */
+#define EXT3_MAX_RESERVE_BLOCKS 1027
+#define EXT3_RESERVE_WINDOW_NOT_ALLOCATED 0
+/*
+ * Always enable hashed directories
+ */
+#define CONFIG_EXT3_INDEX
+
+/*
+ * Debug code
+ */
+#ifdef EXT3FS_DEBUG
+#define ext3_debug(f, a...) \
+ do { \
+ printk (KERN_DEBUG "EXT3-fs DEBUG (%s, %d): %s:", \
+ __FILE__, __LINE__, __FUNCTION__); \
+ printk (KERN_DEBUG f, ## a); \
+ } while (0)
+#else
+#define ext3_debug(f, a...) do {} while (0)
+#endif
+
+/*
+ * Special inodes numbers
+ */
+#define EXT3_BAD_INO 1 /* Bad blocks inode */
+#define EXT3_ROOT_INO 2 /* Root inode */
+#define EXT3_BOOT_LOADER_INO 5 /* Boot loader inode */
+#define EXT3_UNDEL_DIR_INO 6 /* Undelete directory inode */
+#define EXT3_RESIZE_INO 7 /* Reserved group descriptors inode */
+#define EXT3_JOURNAL_INO 8 /* Journal inode */
+
+/* First non-reserved inode for old ext3 filesystems */
+#define EXT3_GOOD_OLD_FIRST_INO 11
+
+/*
+ * Maximal count of links to a file
+ */
+#define EXT3_LINK_MAX 32000
+
+/*
+ * Macro-instructions used to manage several block sizes
+ */
+#define EXT3_MIN_BLOCK_SIZE 1024
+#define EXT3_MAX_BLOCK_SIZE 4096
+#define EXT3_MIN_BLOCK_LOG_SIZE 10
+#ifdef __KERNEL__
+# define EXT3_BLOCK_SIZE(s) ((s)->s_blocksize)
+#else
+# define EXT3_BLOCK_SIZE(s) (EXT3_MIN_BLOCK_SIZE << (s)->s_log_block_size)
+#endif
+#define EXT3_ADDR_PER_BLOCK(s) (EXT3_BLOCK_SIZE(s) / sizeof (__u32))
+#ifdef __KERNEL__
+# define EXT3_BLOCK_SIZE_BITS(s) ((s)->s_blocksize_bits)
+#else
+# define EXT3_BLOCK_SIZE_BITS(s) ((s)->s_log_block_size + 10)
+#endif
+#ifdef __KERNEL__
+#define EXT3_ADDR_PER_BLOCK_BITS(s) (EXT3_SB(s)->s_addr_per_block_bits)
+#define EXT3_INODE_SIZE(s) (EXT3_SB(s)->s_inode_size)
+#define EXT3_FIRST_INO(s) (EXT3_SB(s)->s_first_ino)
+#else
+#define EXT3_INODE_SIZE(s) (((s)->s_rev_level == EXT3_GOOD_OLD_REV) ? \
+ EXT3_GOOD_OLD_INODE_SIZE : \
+ (s)->s_inode_size)
+#define EXT3_FIRST_INO(s) (((s)->s_rev_level == EXT3_GOOD_OLD_REV) ? \
+ EXT3_GOOD_OLD_FIRST_INO : \
+ (s)->s_first_ino)
+#endif
+
+/*
+ * Macro-instructions used to manage fragments
+ */
+#define EXT3_MIN_FRAG_SIZE 1024
+#define EXT3_MAX_FRAG_SIZE 4096
+#define EXT3_MIN_FRAG_LOG_SIZE 10
+#ifdef __KERNEL__
+# define EXT3_FRAG_SIZE(s) (EXT3_SB(s)->s_frag_size)
+# define EXT3_FRAGS_PER_BLOCK(s) (EXT3_SB(s)->s_frags_per_block)
+#else
+# define EXT3_FRAG_SIZE(s) (EXT3_MIN_FRAG_SIZE << (s)->s_log_frag_size)
+# define EXT3_FRAGS_PER_BLOCK(s) (EXT3_BLOCK_SIZE(s) / EXT3_FRAG_SIZE(s))
+#endif
+
+/*
+ * Structure of a blocks group descriptor
+ */
+struct ext3_group_desc
+{
+ __le32 bg_block_bitmap; /* Blocks bitmap block */
+ __le32 bg_inode_bitmap; /* Inodes bitmap block */
+ __le32 bg_inode_table; /* Inodes table block */
+ __le16 bg_free_blocks_count; /* Free blocks count */
+ __le16 bg_free_inodes_count; /* Free inodes count */
+ __le16 bg_used_dirs_count; /* Directories count */
+ __u16 bg_pad;
+ __le32 bg_reserved[3];
+};
+
+/*
+ * Macro-instructions used to manage group descriptors
+ */
+#ifdef __KERNEL__
+# define EXT3_BLOCKS_PER_GROUP(s) (EXT3_SB(s)->s_blocks_per_group)
+# define EXT3_DESC_PER_BLOCK(s) (EXT3_SB(s)->s_desc_per_block)
+# define EXT3_INODES_PER_GROUP(s) (EXT3_SB(s)->s_inodes_per_group)
+# define EXT3_DESC_PER_BLOCK_BITS(s) (EXT3_SB(s)->s_desc_per_block_bits)
+#else
+# define EXT3_BLOCKS_PER_GROUP(s) ((s)->s_blocks_per_group)
+# define EXT3_DESC_PER_BLOCK(s) (EXT3_BLOCK_SIZE(s) / sizeof (struct ext3_group_desc))
+# define EXT3_INODES_PER_GROUP(s) ((s)->s_inodes_per_group)
+#endif
+
+/*
+ * Constants relative to the data blocks
+ */
+#define EXT3_NDIR_BLOCKS 12
+#define EXT3_IND_BLOCK EXT3_NDIR_BLOCKS
+#define EXT3_DIND_BLOCK (EXT3_IND_BLOCK + 1)
+#define EXT3_TIND_BLOCK (EXT3_DIND_BLOCK + 1)
+#define EXT3_N_BLOCKS (EXT3_TIND_BLOCK + 1)
+
+/*
+ * Inode flags
+ */
+#define EXT3_SECRM_FL 0x00000001 /* Secure deletion */
+#define EXT3_UNRM_FL 0x00000002 /* Undelete */
+#define EXT3_COMPR_FL 0x00000004 /* Compress file */
+#define EXT3_SYNC_FL 0x00000008 /* Synchronous updates */
+#define EXT3_IMMUTABLE_FL 0x00000010 /* Immutable file */
+#define EXT3_APPEND_FL 0x00000020 /* writes to file may only append */
+#define EXT3_NODUMP_FL 0x00000040 /* do not dump file */
+#define EXT3_NOATIME_FL 0x00000080 /* do not update atime */
+/* Reserved for compression usage... */
+#define EXT3_DIRTY_FL 0x00000100
+#define EXT3_COMPRBLK_FL 0x00000200 /* One or more compressed clusters */
+#define EXT3_NOCOMPR_FL 0x00000400 /* Don't compress */
+#define EXT3_ECOMPR_FL 0x00000800 /* Compression error */
+/* End compression flags --- maybe not all used */
+#define EXT3_INDEX_FL 0x00001000 /* hash-indexed directory */
+#define EXT3_IMAGIC_FL 0x00002000 /* AFS directory */
+#define EXT3_JOURNAL_DATA_FL 0x00004000 /* file data should be journaled */
+#define EXT3_NOTAIL_FL 0x00008000 /* file tail should not be merged */
+#define EXT3_DIRSYNC_FL 0x00010000 /* dirsync behaviour (directories only) */
+#define EXT3_TOPDIR_FL 0x00020000 /* Top of directory hierarchies*/
+#define EXT3_RESERVED_FL 0x80000000 /* reserved for ext3 lib */
+
+#define EXT3_FL_USER_VISIBLE 0x0003DFFF /* User visible flags */
+#define EXT3_FL_USER_MODIFIABLE 0x000380FF /* User modifiable flags */
+
+/*
+ * Inode dynamic state flags
+ */
+#define EXT3_STATE_JDATA 0x00000001 /* journaled data exists */
+#define EXT3_STATE_NEW 0x00000002 /* inode is newly created */
+#define EXT3_STATE_XATTR 0x00000004 /* has in-inode xattrs */
+
+/* Used to pass group descriptor data when online resize is done */
+struct ext3_new_group_input {
+ __u32 group; /* Group number for this data */
+ __u32 block_bitmap; /* Absolute block number of block bitmap */
+ __u32 inode_bitmap; /* Absolute block number of inode bitmap */
+ __u32 inode_table; /* Absolute block number of inode table start */
+ __u32 blocks_count; /* Total number of blocks in this group */
+ __u16 reserved_blocks; /* Number of reserved blocks in this group */
+ __u16 unused;
+};
+
+/* The struct ext3_new_group_input in kernel space, with free_blocks_count */
+struct ext3_new_group_data {
+ __u32 group;
+ __u32 block_bitmap;
+ __u32 inode_bitmap;
+ __u32 inode_table;
+ __u32 blocks_count;
+ __u16 reserved_blocks;
+ __u16 unused;
+ __u32 free_blocks_count;
+};
+
+
+/*
+ * ioctl commands
+ */
+#define EXT3_IOC_GETFLAGS FS_IOC_GETFLAGS
+#define EXT3_IOC_SETFLAGS FS_IOC_SETFLAGS
+#define EXT3_IOC_GETVERSION _IOR('f', 3, long)
+#define EXT3_IOC_SETVERSION _IOW('f', 4, long)
+#define EXT3_IOC_GROUP_EXTEND _IOW('f', 7, unsigned long)
+#define EXT3_IOC_GROUP_ADD _IOW('f', 8,struct ext3_new_group_input)
+#define EXT3_IOC_GETVERSION_OLD FS_IOC_GETVERSION
+#define EXT3_IOC_SETVERSION_OLD FS_IOC_SETVERSION
+#ifdef CONFIG_JBD_DEBUG
+#define EXT3_IOC_WAIT_FOR_READONLY _IOR('f', 99, long)
+#endif
+#define EXT3_IOC_GETRSVSZ _IOR('f', 5, long)
+#define EXT3_IOC_SETRSVSZ _IOW('f', 6, long)
+
+/*
+ * ioctl commands in 32 bit emulation
+ */
+#define EXT3_IOC32_GETFLAGS FS_IOC32_GETFLAGS
+#define EXT3_IOC32_SETFLAGS FS_IOC32_SETFLAGS
+#define EXT3_IOC32_GETVERSION _IOR('f', 3, int)
+#define EXT3_IOC32_SETVERSION _IOW('f', 4, int)
+#define EXT3_IOC32_GETRSVSZ _IOR('f', 5, int)
+#define EXT3_IOC32_SETRSVSZ _IOW('f', 6, int)
+#define EXT3_IOC32_GROUP_EXTEND _IOW('f', 7, unsigned int)
+#ifdef CONFIG_JBD_DEBUG
+#define EXT3_IOC32_WAIT_FOR_READONLY _IOR('f', 99, int)
+#endif
+#define EXT3_IOC32_GETVERSION_OLD FS_IOC32_GETVERSION
+#define EXT3_IOC32_SETVERSION_OLD FS_IOC32_SETVERSION
+
+
+/*
+ * Mount options
+ */
+struct ext3_mount_options {
+ unsigned long s_mount_opt;
+ uid_t s_resuid;
+ gid_t s_resgid;
+ unsigned long s_commit_interval;
+#ifdef CONFIG_QUOTA
+ int s_jquota_fmt;
+ char *s_qf_names[MAXQUOTAS];
+#endif
+};
+
+/*
+ * Structure of an inode on the disk
+ */
+struct ext3_inode {
+ __le16 i_mode; /* File mode */
+ __le16 i_uid; /* Low 16 bits of Owner Uid */
+ __le32 i_size; /* Size in bytes */
+ __le32 i_atime; /* Access time */
+ __le32 i_ctime; /* Creation time */
+ __le32 i_mtime; /* Modification time */
+ __le32 i_dtime; /* Deletion Time */
+ __le16 i_gid; /* Low 16 bits of Group Id */
+ __le16 i_links_count; /* Links count */
+ __le32 i_blocks; /* Blocks count */
+ __le32 i_flags; /* File flags */
+ union {
+ struct {
+ __u32 l_i_reserved1;
+ } linux1;
+ struct {
+ __u32 h_i_translator;
+ } hurd1;
+ struct {
+ __u32 m_i_reserved1;
+ } masix1;
+ } osd1; /* OS dependent 1 */
+ __le32 i_block[EXT3_N_BLOCKS];/* Pointers to blocks */
+ __le32 i_generation; /* File version (for NFS) */
+ __le32 i_file_acl; /* File ACL */
+ __le32 i_dir_acl; /* Directory ACL */
+ __le32 i_faddr; /* Fragment address */
+ union {
+ struct {
+ __u8 l_i_frag; /* Fragment number */
+ __u8 l_i_fsize; /* Fragment size */
+ __u16 i_pad1;
+ __le16 l_i_uid_high; /* these 2 fields */
+ __le16 l_i_gid_high; /* were reserved2[0] */
+ __u32 l_i_reserved2;
+ } linux2;
+ struct {
+ __u8 h_i_frag; /* Fragment number */
+ __u8 h_i_fsize; /* Fragment size */
+ __u16 h_i_mode_high;
+ __u16 h_i_uid_high;
+ __u16 h_i_gid_high;
+ __u32 h_i_author;
+ } hurd2;
+ struct {
+ __u8 m_i_frag; /* Fragment number */
+ __u8 m_i_fsize; /* Fragment size */
+ __u16 m_pad1;
+ __u32 m_i_reserved2[2];
+ } masix2;
+ } osd2; /* OS dependent 2 */
+ __le16 i_extra_isize;
+ __le16 i_pad1;
+};
+
+#define i_size_high i_dir_acl
+
+#if defined(__KERNEL__) || defined(__linux__)
+#define i_reserved1 osd1.linux1.l_i_reserved1
+#define i_frag osd2.linux2.l_i_frag
+#define i_fsize osd2.linux2.l_i_fsize
+#define i_uid_low i_uid
+#define i_gid_low i_gid
+#define i_uid_high osd2.linux2.l_i_uid_high
+#define i_gid_high osd2.linux2.l_i_gid_high
+#define i_reserved2 osd2.linux2.l_i_reserved2
+
+#elif defined(__GNU__)
+
+#define i_translator osd1.hurd1.h_i_translator
+#define i_frag osd2.hurd2.h_i_frag;
+#define i_fsize osd2.hurd2.h_i_fsize;
+#define i_uid_high osd2.hurd2.h_i_uid_high
+#define i_gid_high osd2.hurd2.h_i_gid_high
+#define i_author osd2.hurd2.h_i_author
+
+#elif defined(__masix__)
+
+#define i_reserved1 osd1.masix1.m_i_reserved1
+#define i_frag osd2.masix2.m_i_frag
+#define i_fsize osd2.masix2.m_i_fsize
+#define i_reserved2 osd2.masix2.m_i_reserved2
+
+#endif /* defined(__KERNEL__) || defined(__linux__) */
+
+/*
+ * File system states
+ */
+#define EXT3_VALID_FS 0x0001 /* Unmounted cleanly */
+#define EXT3_ERROR_FS 0x0002 /* Errors detected */
+#define EXT3_ORPHAN_FS 0x0004 /* Orphans being recovered */
+
+/*
+ * Mount flags
+ */
+#define EXT3_MOUNT_CHECK 0x00001 /* Do mount-time checks */
+#define EXT3_MOUNT_OLDALLOC 0x00002 /* Don't use the new Orlov allocator */
+#define EXT3_MOUNT_GRPID 0x00004 /* Create files with directory's group */
+#define EXT3_MOUNT_DEBUG 0x00008 /* Some debugging messages */
+#define EXT3_MOUNT_ERRORS_CONT 0x00010 /* Continue on errors */
+#define EXT3_MOUNT_ERRORS_RO 0x00020 /* Remount fs ro on errors */
+#define EXT3_MOUNT_ERRORS_PANIC 0x00040 /* Panic on errors */
+#define EXT3_MOUNT_MINIX_DF 0x00080 /* Mimics the Minix statfs */
+#define EXT3_MOUNT_NOLOAD 0x00100 /* Don't use existing journal*/
+#define EXT3_MOUNT_ABORT 0x00200 /* Fatal error detected */
+#define EXT3_MOUNT_DATA_FLAGS 0x00C00 /* Mode for data writes: */
+#define EXT3_MOUNT_JOURNAL_DATA 0x00400 /* Write data to journal */
+#define EXT3_MOUNT_ORDERED_DATA 0x00800 /* Flush data before commit */
+#define EXT3_MOUNT_WRITEBACK_DATA 0x00C00 /* No data ordering */
+#define EXT3_MOUNT_UPDATE_JOURNAL 0x01000 /* Update the journal format */
+#define EXT3_MOUNT_NO_UID32 0x02000 /* Disable 32-bit UIDs */
+#define EXT3_MOUNT_XATTR_USER 0x04000 /* Extended user attributes */
+#define EXT3_MOUNT_POSIX_ACL 0x08000 /* POSIX Access Control Lists */
+#define EXT3_MOUNT_RESERVATION 0x10000 /* Preallocation */
+#define EXT3_MOUNT_BARRIER 0x20000 /* Use block barriers */
+#define EXT3_MOUNT_NOBH 0x40000 /* No bufferheads */
+#define EXT3_MOUNT_QUOTA 0x80000 /* Some quota option set */
+#define EXT3_MOUNT_USRQUOTA 0x100000 /* "old" user quota */
+#define EXT3_MOUNT_GRPQUOTA 0x200000 /* "old" group quota */
+
+/* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
+#ifndef _LINUX_EXT2_FS_H
+#define clear_opt(o, opt) o &= ~EXT3_MOUNT_##opt
+#define set_opt(o, opt) o |= EXT3_MOUNT_##opt
+#define test_opt(sb, opt) (EXT3_SB(sb)->s_mount_opt & \
+ EXT3_MOUNT_##opt)
+#else
+#define EXT2_MOUNT_NOLOAD EXT3_MOUNT_NOLOAD
+#define EXT2_MOUNT_ABORT EXT3_MOUNT_ABORT
+#define EXT2_MOUNT_DATA_FLAGS EXT3_MOUNT_DATA_FLAGS
+#endif
+
+#define ext3_set_bit ext2_set_bit
+#define ext3_set_bit_atomic ext2_set_bit_atomic
+#define ext3_clear_bit ext2_clear_bit
+#define ext3_clear_bit_atomic ext2_clear_bit_atomic
+#define ext3_test_bit ext2_test_bit
+#define ext3_find_first_zero_bit ext2_find_first_zero_bit
+#define ext3_find_next_zero_bit ext2_find_next_zero_bit
+
+/*
+ * Maximal mount counts between two filesystem checks
+ */
+#define EXT3_DFL_MAX_MNT_COUNT 20 /* Allow 20 mounts */
+#define EXT3_DFL_CHECKINTERVAL 0 /* Don't use interval check */
+
+/*
+ * Behaviour when detecting errors
+ */
+#define EXT3_ERRORS_CONTINUE 1 /* Continue execution */
+#define EXT3_ERRORS_RO 2 /* Remount fs read-only */
+#define EXT3_ERRORS_PANIC 3 /* Panic */
+#define EXT3_ERRORS_DEFAULT EXT3_ERRORS_CONTINUE
+
+/*
+ * Structure of the super block
+ */
+struct ext3_super_block {
+/*00*/ __le32 s_inodes_count; /* Inodes count */
+ __le32 s_blocks_count; /* Blocks count */
+ __le32 s_r_blocks_count; /* Reserved blocks count */
+ __le32 s_free_blocks_count; /* Free blocks count */
+/*10*/ __le32 s_free_inodes_count; /* Free inodes count */
+ __le32 s_first_data_block; /* First Data Block */
+ __le32 s_log_block_size; /* Block size */
+ __le32 s_log_frag_size; /* Fragment size */
+/*20*/ __le32 s_blocks_per_group; /* # Blocks per group */
+ __le32 s_frags_per_group; /* # Fragments per group */
+ __le32 s_inodes_per_group; /* # Inodes per group */
+ __le32 s_mtime; /* Mount time */
+/*30*/ __le32 s_wtime; /* Write time */
+ __le16 s_mnt_count; /* Mount count */
+ __le16 s_max_mnt_count; /* Maximal mount count */
+ __le16 s_magic; /* Magic signature */
+ __le16 s_state; /* File system state */
+ __le16 s_errors; /* Behaviour when detecting errors */
+ __le16 s_minor_rev_level; /* minor revision level */
+/*40*/ __le32 s_lastcheck; /* time of last check */
+ __le32 s_checkinterval; /* max. time between checks */
+ __le32 s_creator_os; /* OS */
+ __le32 s_rev_level; /* Revision level */
+/*50*/ __le16 s_def_resuid; /* Default uid for reserved blocks */
+ __le16 s_def_resgid; /* Default gid for reserved blocks */
+ /*
+ * These fields are for EXT3_DYNAMIC_REV superblocks only.
+ *
+ * Note: the difference between the compatible feature set and
+ * the incompatible feature set is that if there is a bit set
+ * in the incompatible feature set that the kernel doesn't
+ * know about, it should refuse to mount the filesystem.
+ *
+ * e2fsck's requirements are more strict; if it doesn't know
+ * about a feature in either the compatible or incompatible
+ * feature set, it must abort and not try to meddle with
+ * things it doesn't understand...
+ */
+ __le32 s_first_ino; /* First non-reserved inode */
+ __le16 s_inode_size; /* size of inode structure */
+ __le16 s_block_group_nr; /* block group # of this superblock */
+ __le32 s_feature_compat; /* compatible feature set */
+/*60*/ __le32 s_feature_incompat; /* incompatible feature set */
+ __le32 s_feature_ro_compat; /* readonly-compatible feature set */
+/*68*/ __u8 s_uuid[16]; /* 128-bit uuid for volume */
+/*78*/ char s_volume_name[16]; /* volume name */
+/*88*/ char s_last_mounted[64]; /* directory where last mounted */
+/*C8*/ __le32 s_algorithm_usage_bitmap; /* For compression */
+ /*
+ * Performance hints. Directory preallocation should only
+ * happen if the EXT3_FEATURE_COMPAT_DIR_PREALLOC flag is on.
+ */
+ __u8 s_prealloc_blocks; /* Nr of blocks to try to preallocate*/
+ __u8 s_prealloc_dir_blocks; /* Nr to preallocate for dirs */
+ __le16 s_reserved_gdt_blocks; /* Per group desc for online growth */
+ /*
+ * Journaling support valid if EXT3_FEATURE_COMPAT_HAS_JOURNAL set.
+ */
+/*D0*/ __u8 s_journal_uuid[16]; /* uuid of journal superblock */
+/*E0*/ __le32 s_journal_inum; /* inode number of journal file */
+ __le32 s_journal_dev; /* device number of journal file */
+ __le32 s_last_orphan; /* start of list of inodes to delete */
+ __le32 s_hash_seed[4]; /* HTREE hash seed */
+ __u8 s_def_hash_version; /* Default hash version to use */
+ __u8 s_reserved_char_pad;
+ __u16 s_reserved_word_pad;
+ __le32 s_default_mount_opts;
+ __le32 s_first_meta_bg; /* First metablock block group */
+ __u32 s_reserved[190]; /* Padding to the end of the block */
+};
+
+#ifdef __KERNEL__
+#include <linux/ext3_fs_i.h>
+#include <linux/ext3_fs_sb.h>
+static inline struct ext3_sb_info * EXT3_SB(struct super_block *sb)
+{
+ return sb->s_fs_info;
+}
+static inline struct ext3_inode_info *EXT3_I(struct inode *inode)
+{
+ return container_of(inode, struct ext3_inode_info, vfs_inode);
+}
+
+static inline int ext3_valid_inum(struct super_block *sb, unsigned long ino)
+{
+ return ino == EXT3_ROOT_INO ||
+ ino == EXT3_JOURNAL_INO ||
+ ino == EXT3_RESIZE_INO ||
+ (ino >= EXT3_FIRST_INO(sb) &&
+ ino <= le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count));
+}
+#else
+/* Assume that user mode programs are passing in an ext3fs superblock, not
+ * a kernel struct super_block. This will allow us to call the feature-test
+ * macros from user land. */
+#define EXT3_SB(sb) (sb)
+#endif
+
+#define NEXT_ORPHAN(inode) EXT3_I(inode)->i_dtime
+
+/*
+ * Codes for operating systems
+ */
+#define EXT3_OS_LINUX 0
+#define EXT3_OS_HURD 1
+#define EXT3_OS_MASIX 2
+#define EXT3_OS_FREEBSD 3
+#define EXT3_OS_LITES 4
+
+/*
+ * Revision levels
+ */
+#define EXT3_GOOD_OLD_REV 0 /* The good old (original) format */
+#define EXT3_DYNAMIC_REV 1 /* V2 format w/ dynamic inode sizes */
+
+#define EXT3_CURRENT_REV EXT3_GOOD_OLD_REV
+#define EXT3_MAX_SUPP_REV EXT3_DYNAMIC_REV
+
+#define EXT3_GOOD_OLD_INODE_SIZE 128
+
+/*
+ * Feature set definitions
+ */
+
+#define EXT3_HAS_COMPAT_FEATURE(sb,mask) \
+ ( EXT3_SB(sb)->s_es->s_feature_compat & cpu_to_le32(mask) )
+#define EXT3_HAS_RO_COMPAT_FEATURE(sb,mask) \
+ ( EXT3_SB(sb)->s_es->s_feature_ro_compat & cpu_to_le32(mask) )
+#define EXT3_HAS_INCOMPAT_FEATURE(sb,mask) \
+ ( EXT3_SB(sb)->s_es->s_feature_incompat & cpu_to_le32(mask) )
+#define EXT3_SET_COMPAT_FEATURE(sb,mask) \
+ EXT3_SB(sb)->s_es->s_feature_compat |= cpu_to_le32(mask)
+#define EXT3_SET_RO_COMPAT_FEATURE(sb,mask) \
+ EXT3_SB(sb)->s_es->s_feature_ro_compat |= cpu_to_le32(mask)
+#define EXT3_SET_INCOMPAT_FEATURE(sb,mask) \
+ EXT3_SB(sb)->s_es->s_feature_incompat |= cpu_to_le32(mask)
+#define EXT3_CLEAR_COMPAT_FEATURE(sb,mask) \
+ EXT3_SB(sb)->s_es->s_feature_compat &= ~cpu_to_le32(mask)
+#define EXT3_CLEAR_RO_COMPAT_FEATURE(sb,mask) \
+ EXT3_SB(sb)->s_es->s_feature_ro_compat &= ~cpu_to_le32(mask)
+#define EXT3_CLEAR_INCOMPAT_FEATURE(sb,mask) \
+ EXT3_SB(sb)->s_es->s_feature_incompat &= ~cpu_to_le32(mask)
+
+#define EXT3_FEATURE_COMPAT_DIR_PREALLOC 0x0001
+#define EXT3_FEATURE_COMPAT_IMAGIC_INODES 0x0002
+#define EXT3_FEATURE_COMPAT_HAS_JOURNAL 0x0004
+#define EXT3_FEATURE_COMPAT_EXT_ATTR 0x0008
+#define EXT3_FEATURE_COMPAT_RESIZE_INODE 0x0010
+#define EXT3_FEATURE_COMPAT_DIR_INDEX 0x0020
+
+#define EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER 0x0001
+#define EXT3_FEATURE_RO_COMPAT_LARGE_FILE 0x0002
+#define EXT3_FEATURE_RO_COMPAT_BTREE_DIR 0x0004
+
+#define EXT3_FEATURE_INCOMPAT_COMPRESSION 0x0001
+#define EXT3_FEATURE_INCOMPAT_FILETYPE 0x0002
+#define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004 /* Needs recovery */
+#define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 /* Journal device */
+#define EXT3_FEATURE_INCOMPAT_META_BG 0x0010
+
+#define EXT3_FEATURE_COMPAT_SUPP EXT2_FEATURE_COMPAT_EXT_ATTR
+#define EXT3_FEATURE_INCOMPAT_SUPP (EXT3_FEATURE_INCOMPAT_FILETYPE| \
+ EXT3_FEATURE_INCOMPAT_RECOVER| \
+ EXT3_FEATURE_INCOMPAT_META_BG)
+#define EXT3_FEATURE_RO_COMPAT_SUPP (EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER| \
+ EXT3_FEATURE_RO_COMPAT_LARGE_FILE| \
+ EXT3_FEATURE_RO_COMPAT_BTREE_DIR)
+
+/*
+ * Default values for user and/or group using reserved blocks
+ */
+#define EXT3_DEF_RESUID 0
+#define EXT3_DEF_RESGID 0
+
+/*
+ * Default mount options
+ */
+#define EXT3_DEFM_DEBUG 0x0001
+#define EXT3_DEFM_BSDGROUPS 0x0002
+#define EXT3_DEFM_XATTR_USER 0x0004
+#define EXT3_DEFM_ACL 0x0008
+#define EXT3_DEFM_UID16 0x0010
+#define EXT3_DEFM_JMODE 0x0060
+#define EXT3_DEFM_JMODE_DATA 0x0020
+#define EXT3_DEFM_JMODE_ORDERED 0x0040
+#define EXT3_DEFM_JMODE_WBACK 0x0060
+
+/*
+ * Structure of a directory entry
+ */
+#define EXT3_NAME_LEN 255
+
+struct ext3_dir_entry {
+ __le32 inode; /* Inode number */
+ __le16 rec_len; /* Directory entry length */
+ __le16 name_len; /* Name length */
+ char name[EXT3_NAME_LEN]; /* File name */
+};
+
+/*
+ * The new version of the directory entry. Since EXT3 structures are
+ * stored in intel byte order, and the name_len field could never be
+ * bigger than 255 chars, it's safe to reclaim the extra byte for the
+ * file_type field.
+ */
+struct ext3_dir_entry_2 {
+ __le32 inode; /* Inode number */
+ __le16 rec_len; /* Directory entry length */
+ __u8 name_len; /* Name length */
+ __u8 file_type;
+ char name[EXT3_NAME_LEN]; /* File name */
+};
+
+/*
+ * Ext3 directory file types. Only the low 3 bits are used. The
+ * other bits are reserved for now.
+ */
+#define EXT3_FT_UNKNOWN 0
+#define EXT3_FT_REG_FILE 1
+#define EXT3_FT_DIR 2
+#define EXT3_FT_CHRDEV 3
+#define EXT3_FT_BLKDEV 4
+#define EXT3_FT_FIFO 5
+#define EXT3_FT_SOCK 6
+#define EXT3_FT_SYMLINK 7
+
+#define EXT3_FT_MAX 8
+
+/*
+ * EXT3_DIR_PAD defines the directory entries boundaries
+ *
+ * NOTE: It must be a multiple of 4
+ */
+#define EXT3_DIR_PAD 4
+#define EXT3_DIR_ROUND (EXT3_DIR_PAD - 1)
+#define EXT3_DIR_REC_LEN(name_len) (((name_len) + 8 + EXT3_DIR_ROUND) & \
+ ~EXT3_DIR_ROUND)
+/*
+ * Hash Tree Directory indexing
+ * (c) Daniel Phillips, 2001
+ */
+
+#ifdef CONFIG_EXT3_INDEX
+ #define is_dx(dir) (EXT3_HAS_COMPAT_FEATURE(dir->i_sb, \
+ EXT3_FEATURE_COMPAT_DIR_INDEX) && \
+ (EXT3_I(dir)->i_flags & EXT3_INDEX_FL))
+#define EXT3_DIR_LINK_MAX(dir) (!is_dx(dir) && (dir)->i_nlink >= EXT3_LINK_MAX)
+#define EXT3_DIR_LINK_EMPTY(dir) ((dir)->i_nlink == 2 || (dir)->i_nlink == 1)
+#else
+ #define is_dx(dir) 0
+#define EXT3_DIR_LINK_MAX(dir) ((dir)->i_nlink >= EXT3_LINK_MAX)
+#define EXT3_DIR_LINK_EMPTY(dir) ((dir)->i_nlink == 2)
+#endif
+
+/* Legal values for the dx_root hash_version field: */
+
+#define DX_HASH_LEGACY 0
+#define DX_HASH_HALF_MD4 1
+#define DX_HASH_TEA 2
+
+#ifdef __KERNEL__
+
+/* hash info structure used by the directory hash */
+struct dx_hash_info
+{
+ u32 hash;
+ u32 minor_hash;
+ int hash_version;
+ u32 *seed;
+};
+
+#define EXT3_HTREE_EOF 0x7fffffff
+
+/*
+ * Control parameters used by ext3_htree_next_block
+ */
+#define HASH_NB_ALWAYS 1
+
+
+/*
+ * Describe an inode's exact location on disk and in memory
+ */
+struct ext3_iloc
+{
+ struct buffer_head *bh;
+ unsigned long offset;
+ unsigned long block_group;
+};
+
+static inline struct ext3_inode *ext3_raw_inode(struct ext3_iloc *iloc)
+{
+ return (struct ext3_inode *) (iloc->bh->b_data + iloc->offset);
+}
+
+/*
+ * This structure is stuffed into the struct file's private_data field
+ * for directories. It is where we put information so that we can do
+ * readdir operations in hash tree order.
+ */
+struct dir_private_info {
+ struct rb_root root;
+ struct rb_node *curr_node;
+ struct fname *extra_fname;
+ loff_t last_pos;
+ __u32 curr_hash;
+ __u32 curr_minor_hash;
+ __u32 next_hash;
+};
+
+/* calculate the first block number of the group */
+static inline ext3_fsblk_t
+ext3_group_first_block_no(struct super_block *sb, unsigned long group_no)
+{
+ return group_no * (ext3_fsblk_t)EXT3_BLOCKS_PER_GROUP(sb) +
+ le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block);
+}
+
+/*
+ * Special error return code only used by dx_probe() and its callers.
+ */
+#define ERR_BAD_DX_DIR -75000
+
+/*
+ * Function prototypes
+ */
+
+/*
+ * Ok, these declarations are also in <linux/kernel.h> but none of the
+ * ext3 source programs needs to include it so they are duplicated here.
+ */
+# define NORET_TYPE /**/
+# define ATTRIB_NORET __attribute__((noreturn))
+# define NORET_AND noreturn,
+
+/* balloc.c */
+extern int ext3_bg_has_super(struct super_block *sb, int group);
+extern unsigned long ext3_bg_num_gdb(struct super_block *sb, int group);
+extern ext3_fsblk_t ext3_new_block (handle_t *handle, struct inode *inode,
+ ext3_fsblk_t goal, int *errp);
+extern ext3_fsblk_t ext3_new_blocks (handle_t *handle, struct inode *inode,
+ ext3_fsblk_t goal, unsigned long *count, int *errp);
+extern void ext3_free_blocks (handle_t *handle, struct inode *inode,
+ ext3_fsblk_t block, unsigned long count);
+extern void ext3_free_blocks_sb (handle_t *handle, struct super_block *sb,
+ ext3_fsblk_t block, unsigned long count,
+ unsigned long *pdquot_freed_blocks);
+extern ext3_fsblk_t ext3_count_free_blocks (struct super_block *);
+extern void ext3_check_blocks_bitmap (struct super_block *);
+extern struct ext3_group_desc * ext3_get_group_desc(struct super_block * sb,
+ unsigned int block_group,
+ struct buffer_head ** bh);
+extern int ext3_should_retry_alloc(struct super_block *sb, int *retries);
+extern void ext3_init_block_alloc_info(struct inode *);
+extern void ext3_rsv_window_add(struct super_block *sb, struct ext3_reserve_window_node *rsv);
+
+/* dir.c */
+extern int ext3_check_dir_entry(const char *, struct inode *,
+ struct ext3_dir_entry_2 *,
+ struct buffer_head *, unsigned long);
+extern int ext3_htree_store_dirent(struct file *dir_file, __u32 hash,
+ __u32 minor_hash,
+ struct ext3_dir_entry_2 *dirent);
+extern void ext3_htree_free_dir_info(struct dir_private_info *p);
+
+/* fsync.c */
+extern int ext3_sync_file (struct file *, struct dentry *, int);
+
+/* hash.c */
+extern int ext3fs_dirhash(const char *name, int len, struct
+ dx_hash_info *hinfo);
+
+/* ialloc.c */
+extern struct inode * ext3_new_inode (handle_t *, struct inode *, int);
+extern void ext3_free_inode (handle_t *, struct inode *);
+extern struct inode * ext3_orphan_get (struct super_block *, unsigned long);
+extern unsigned long ext3_count_free_inodes (struct super_block *);
+extern unsigned long ext3_count_dirs (struct super_block *);
+extern void ext3_check_inodes_bitmap (struct super_block *);
+extern unsigned long ext3_count_free (struct buffer_head *, unsigned);
+
+
+/* inode.c */
+int ext3_forget(handle_t *handle, int is_metadata, struct inode *inode,
+ struct buffer_head *bh, ext3_fsblk_t blocknr);
+struct buffer_head * ext3_getblk (handle_t *, struct inode *, long, int, int *);
+struct buffer_head * ext3_bread (handle_t *, struct inode *, int, int, int *);
+int ext3_get_blocks_handle(handle_t *handle, struct inode *inode,
+ sector_t iblock, unsigned long maxblocks, struct buffer_head *bh_result,
+ int create, int extend_disksize);
+
+extern void ext3_read_inode (struct inode *);
+extern int ext3_write_inode (struct inode *, int);
+extern int ext3_setattr (struct dentry *, struct iattr *);
+extern void ext3_delete_inode (struct inode *);
+extern int ext3_sync_inode (handle_t *, struct inode *);
+extern void ext3_discard_reservation (struct inode *);
+extern void ext3_dirty_inode(struct inode *);
+extern int ext3_change_inode_journal_flag(struct inode *, int);
+extern int ext3_get_inode_loc(struct inode *, struct ext3_iloc *);
+extern void ext3_truncate (struct inode *);
+extern void ext3_set_inode_flags(struct inode *);
+extern void ext3_get_inode_flags(struct ext3_inode_info *);
+extern void ext3_set_aops(struct inode *inode);
+
+/* ioctl.c */
+extern int ext3_ioctl (struct inode *, struct file *, unsigned int,
+ unsigned long);
+extern long ext3_compat_ioctl (struct file *, unsigned int, unsigned long);
+
+/* namei.c */
+extern int ext3_orphan_add(handle_t *, struct inode *);
+extern int ext3_orphan_del(handle_t *, struct inode *);
+extern int ext3_htree_fill_tree(struct file *dir_file, __u32 start_hash,
+ __u32 start_minor_hash, __u32 *next_hash);
+
+/* resize.c */
+extern int ext3_group_add(struct super_block *sb,
+ struct ext3_new_group_data *input);
+extern int ext3_group_extend(struct super_block *sb,
+ struct ext3_super_block *es,
+ ext3_fsblk_t n_blocks_count);
+
+/* super.c */
+extern void ext3_error (struct super_block *, const char *, const char *, ...)
+ __attribute__ ((format (printf, 3, 4)));
+extern void __ext3_std_error (struct super_block *, const char *, int);
+extern void ext3_abort (struct super_block *, const char *, const char *, ...)
+ __attribute__ ((format (printf, 3, 4)));
+extern void ext3_warning (struct super_block *, const char *, const char *, ...)
+ __attribute__ ((format (printf, 3, 4)));
+extern void ext3_update_dynamic_rev (struct super_block *sb);
+
+#define ext3_std_error(sb, errno) \
+do { \
+ if ((errno)) \
+ __ext3_std_error((sb), __FUNCTION__, (errno)); \
+} while (0)
+
+/*
+ * Inodes and files operations
+ */
+
+/* dir.c */
+extern const struct file_operations ext3_dir_operations;
+
+/* file.c */
+extern const struct inode_operations ext3_file_inode_operations;
+extern const struct file_operations ext3_file_operations;
+
+/* namei.c */
+extern const struct inode_operations ext3_dir_inode_operations;
+extern const struct inode_operations ext3_special_inode_operations;
+
+/* symlink.c */
+extern const struct inode_operations ext3_symlink_inode_operations;
+extern const struct inode_operations ext3_fast_symlink_inode_operations;
+
+
+#endif /* __KERNEL__ */
+
+#endif /* _LINUX_EXT3_FS_H */
diff --git a/lib/fs/ext2/file.c b/lib/fs/ext2/file.c
new file mode 100644
index 0000000..9f49c0b
--- /dev/null
+++ b/lib/fs/ext2/file.c
@@ -0,0 +1,145 @@
+/*
+ * Copyright (c) 2007 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdlib.h>
+#include <err.h>
+#include <debug.h>
+#include <lib/fs/ext2.h>
+#include "ext2_priv.h"
+
+#define LOCAL_TRACE 0
+
+int ext2_open_file(fscookie cookie, const char *path, fsfilecookie *fcookie)
+{
+ ext2_t *ext2 = (ext2_t *)cookie;
+ int err;
+
+ /* do a path lookup */
+ inodenum_t inum;
+ err = ext2_lookup(ext2, path, &inum);
+ if (err < 0)
+ return err;
+
+ /* create the file object */
+ ext2_file_t *file = malloc(sizeof(ext2_file_t));
+ memset(file, 0, sizeof(ext2_file_t));
+
+ /* read in the inode */
+ err = ext2_load_inode(ext2, inum, &file->inode);
+ if (err < 0) {
+ free(file);
+ return err;
+ }
+
+ file->ext2 = ext2;
+ *fcookie = file;
+
+ return 0;
+}
+
+int ext2_read_file(fsfilecookie fcookie, void *buf, off_t offset, size_t len)
+{
+ ext2_file_t *file = (ext2_file_t *)fcookie;
+ int err;
+
+ // test that it's a file
+ if (!S_ISREG(file->inode.i_mode)) {
+ dprintf(INFO, "ext2_read_file: not a file\n");
+ return -1;
+ }
+
+ // read from the inode
+ err = ext2_read_inode(file->ext2, &file->inode, buf, offset, len);
+
+ return err;
+}
+
+int ext2_close_file(fsfilecookie fcookie)
+{
+ ext2_file_t *file = (ext2_file_t *)fcookie;
+
+ // see if we need to free any of the cache blocks
+ int i;
+ for (i=0; i < 3; i++) {
+ if (file->ind_cache[i].num != 0) {
+ free(file->ind_cache[i].ptr);
+ }
+ }
+
+ free(file);
+
+ return 0;
+}
+
+off_t ext2_file_len(ext2_t *ext2, struct ext2_inode *inode)
+{
+ /* calculate the file size */
+ off_t len = inode->i_size;
+ if ((ext2->sb.s_feature_ro_compat & EXT2_FEATURE_RO_COMPAT_LARGE_FILE) && (S_ISREG(inode->i_mode))) {
+ /* can potentially be a large file */
+ len |= (off_t)inode->i_size_high << 32;
+ }
+
+ return len;
+}
+
+int ext2_stat_file(fsfilecookie fcookie, struct file_stat *stat)
+{
+ ext2_file_t *file = (ext2_file_t *)fcookie;
+
+ stat->size = ext2_file_len(file->ext2, &file->inode);
+
+ /* is it a dir? */
+ stat->is_dir = false;
+ if (S_ISDIR(file->inode.i_mode))
+ stat->is_dir = true;
+
+ return 0;
+}
+
+int ext2_read_link(ext2_t *ext2, struct ext2_inode *inode, char *str, size_t len)
+{
+ LTRACEF("inode %p, str %p, len %d\n", inode, str, len);
+
+ off_t linklen = ext2_file_len(ext2, inode);
+
+ if ((linklen < 0) || (linklen + 1 > len))
+ return ERR_NO_MEMORY;
+
+ if (linklen > 60) {
+ int err = ext2_read_inode(ext2, inode, str, 0, linklen);
+ if (err < 0)
+ return err;
+ str[linklen] = 0;
+ } else {
+ memcpy(str, &inode->i_block[0], linklen);
+ str[linklen] = 0;
+ }
+
+ LTRACEF("read link '%s'\n", str);
+
+ return linklen;
+}
+
diff --git a/lib/fs/ext2/io.c b/lib/fs/ext2/io.c
new file mode 100644
index 0000000..76e9ce4
--- /dev/null
+++ b/lib/fs/ext2/io.c
@@ -0,0 +1,269 @@
+/*
+ * Copyright (c) 2007 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <string.h>
+#include <stdlib.h>
+#include <debug.h>
+#include <lib/fs/ext2.h>
+#include "ext2_priv.h"
+
+#define LOCAL_TRACE 0
+
+int ext2_read_block(ext2_t *ext2, void *buf, blocknum_t bnum)
+{
+ return bcache_read_block(ext2->cache, buf, bnum);
+}
+
+int ext2_get_block(ext2_t *ext2, void **ptr, blocknum_t bnum)
+{
+ return bcache_get_block(ext2->cache, ptr, bnum);
+}
+
+int ext2_put_block(ext2_t *ext2, blocknum_t bnum)
+{
+ return bcache_put_block(ext2->cache, bnum);
+}
+
+static int ext2_calculate_block_pointer_pos(ext2_t *ext2, blocknum_t block_to_find, uint32_t *level, uint32_t pos[])
+{
+ uint32_t block_ptr_per_block, block_ptr_per_2nd_block;
+
+ // XXX optimize this
+
+ // See if it's in the direct blocks
+ if(block_to_find < EXT2_NDIR_BLOCKS) {
+ *level = 0;
+ pos[0] = block_to_find;
+ return 0;
+ }
+
+ block_ptr_per_block = EXT2_ADDR_PER_BLOCK(ext2->sb);
+ block_to_find -= EXT2_NDIR_BLOCKS;
+ // See if it's in the first indirect block
+ if(block_to_find < block_ptr_per_block) {
+ *level = 1;
+ pos[0] = EXT2_IND_BLOCK;
+ pos[1] = block_to_find;
+ return 0;
+ }
+
+ block_to_find -= block_ptr_per_block;
+ block_ptr_per_2nd_block = block_ptr_per_block * block_ptr_per_block;
+ // See if it's in the second indirect block
+ if(block_to_find < (block_ptr_per_2nd_block)) {
+ *level = 2;
+ pos[0] = EXT2_DIND_BLOCK;
+ pos[1] = block_to_find / block_ptr_per_block;
+ pos[2] = block_to_find % block_ptr_per_block;
+ return 0;
+ }
+
+ block_to_find -= block_ptr_per_2nd_block;
+ // See if it's in the third indirect block
+ if(block_to_find < (block_ptr_per_2nd_block * block_ptr_per_block)) {
+ *level = 3;
+ pos[0] = EXT2_TIND_BLOCK;
+ pos[1] = block_to_find / block_ptr_per_2nd_block;
+ pos[2] = (block_to_find % block_ptr_per_2nd_block) / block_ptr_per_block;
+ pos[3] = (block_to_find % block_ptr_per_2nd_block) % block_ptr_per_block;
+ return 0;
+ }
+
+ // The block requested must be too big.
+ return -1;
+}
+
+// This function returns a pointer to the cache block that corresponds to the indirect block pointer.
+int ext2_get_indirect_block_pointer_cache_block(ext2_t *ext2, struct ext2_inode *inode, blocknum_t **cache_block, uint32_t level, uint32_t pos[], uint *block_loaded)
+{
+ uint32_t current_level = 0;
+ uint current_block = 0, last_block;
+ blocknum_t *block = NULL;
+ int err;
+
+ if ((level > 3) || (level == 0)) {
+ err = -1;
+ goto error;
+ }
+
+ // Dig down into the indirect blocks. When done, current_block should point to the target.
+ while (current_level < level) {
+ if (current_level == 0) {
+ // read the direct block, simulates a prior loop
+ current_block = LE32(inode->i_block[pos[0]]);
+ }
+
+ if (current_block == 0) {
+ err = -1;
+ goto error;
+ }
+
+ last_block = current_block;
+ current_level++;
+ *block_loaded = current_block;
+
+ err = ext2_get_block(ext2, (void **)(void *)&block, current_block);
+ if (err < 0) {
+ goto error;
+ }
+
+ if (current_level < level) {
+ current_block = LE32(block[pos[current_level]]);
+ ext2_put_block(ext2, last_block);
+ }
+ }
+
+ *cache_block = block;
+ return 0;
+
+error:
+ *cache_block = NULL;
+ *block_loaded = 0;
+ return err;
+}
+
+/* translate a file block to a physical block */
+static blocknum_t file_block_to_fs_block(ext2_t *ext2, struct ext2_inode *inode, uint fileblock)
+{
+ int err;
+ blocknum_t block;
+
+ LTRACEF("inode %p, fileblock %u\n", inode, fileblock);
+
+ uint32_t pos[4];
+ uint32_t level = 0;
+ ext2_calculate_block_pointer_pos(ext2, fileblock, &level, pos);
+
+ LTRACEF("level %d, pos 0x%x 0x%x 0x%x 0x%x\n", level, pos[0], pos[1], pos[2], pos[3]);
+
+ if (level == 0) {
+ /* direct block, just return it directly */
+ block = LE32(inode->i_block[fileblock]);
+ } else {
+ /* at least one level of indirection, get a pointer to the final indirect block table and dereference it */
+ blocknum_t *ind_table;
+ blocknum_t phys_block;
+ err = ext2_get_indirect_block_pointer_cache_block(ext2, inode, &ind_table, level, pos, &phys_block);
+ if (err < 0)
+ return 0;
+
+ /* dereference the final entry in the final table */
+ block = LE32(ind_table[pos[level]]);
+ LTRACEF("block %u, indirect_block %u\n", block, phys_block);
+
+ /* release the ref on the cache block */
+ ext2_put_block(ext2, phys_block);
+ }
+
+ LTRACEF("returning %u\n", block);
+
+ return block;
+}
+
+int ext2_read_inode(ext2_t *ext2, struct ext2_inode *inode, void *_buf, off_t offset, size_t len)
+{
+ int err = 0;
+ int bytes_read = 0;
+ uint8_t *buf = _buf;
+
+ /* calculate the file size */
+ off_t file_size = ext2_file_len(ext2, inode);
+
+ LTRACEF("inode %p, offset %lld, len %zd, file_size %lld\n", inode, offset, len, file_size);
+
+ /* trim the read */
+ if (offset > file_size)
+ return 0;
+ if (offset + len >= file_size)
+ len = file_size - offset;
+ if (len == 0)
+ return 0;
+
+ /* calculate the starting file block */
+ uint file_block = offset / EXT2_BLOCK_SIZE(ext2->sb);
+
+ /* handle partial first block */
+ if ((offset % EXT2_BLOCK_SIZE(ext2->sb)) != 0) {
+ uint8_t temp[EXT2_BLOCK_SIZE(ext2->sb)];
+
+ /* calculate the block and read it */
+ blocknum_t phys_block = file_block_to_fs_block(ext2, inode, file_block);
+ if (phys_block == 0) {
+ memset(temp, 0, EXT2_BLOCK_SIZE(ext2->sb));
+ } else {
+ ext2_read_block(ext2, temp, phys_block);
+ }
+
+ /* copy out what we need */
+ size_t block_offset = offset % EXT2_BLOCK_SIZE(ext2->sb);
+ size_t tocopy = MIN(len, EXT2_BLOCK_SIZE(ext2->sb) - block_offset);
+ memcpy(buf, temp + block_offset, tocopy);
+
+ /* increment our stuff */
+ file_block++;
+ len -= tocopy;
+ bytes_read += tocopy;
+ buf += tocopy;
+ }
+
+ /* handle middle blocks */
+ while (len >= EXT2_BLOCK_SIZE(ext2->sb)) {
+ /* calculate the block and read it */
+ blocknum_t phys_block = file_block_to_fs_block(ext2, inode, file_block);
+ if (phys_block == 0) {
+ memset(buf, 0, EXT2_BLOCK_SIZE(ext2->sb));
+ } else {
+ ext2_read_block(ext2, buf, phys_block);
+ }
+
+ /* increment our stuff */
+ file_block++;
+ len -= EXT2_BLOCK_SIZE(ext2->sb);
+ bytes_read += EXT2_BLOCK_SIZE(ext2->sb);
+ buf += EXT2_BLOCK_SIZE(ext2->sb);
+ }
+
+ /* handle partial last block */
+ if (len > 0) {
+ uint8_t temp[EXT2_BLOCK_SIZE(ext2->sb)];
+
+ /* calculate the block and read it */
+ blocknum_t phys_block = file_block_to_fs_block(ext2, inode, file_block);
+ if (phys_block == 0) {
+ memset(temp, 0, EXT2_BLOCK_SIZE(ext2->sb));
+ } else {
+ ext2_read_block(ext2, temp, phys_block);
+ }
+
+ /* copy out what we need */
+ memcpy(buf, temp, len);
+
+ /* increment our stuff */
+ bytes_read += len;
+ }
+
+ LTRACEF("err %d, bytes_read %d\n", err, bytes_read);
+
+ return (err < 0) ? err : bytes_read;
+}
+
diff --git a/lib/fs/ext2/rules.mk b/lib/fs/ext2/rules.mk
new file mode 100644
index 0000000..a4bb383
--- /dev/null
+++ b/lib/fs/ext2/rules.mk
@@ -0,0 +1,12 @@
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+MODULES += \
+ lib/fs \
+ lib/bcache \
+ lib/bio
+
+OBJS += \
+ $(LOCAL_DIR)/ext2.o \
+ $(LOCAL_DIR)/dir.o \
+ $(LOCAL_DIR)/io.o \
+ $(LOCAL_DIR)/file.o