#include <endian.h>
#include <error.h>
#include <pmap.h>
-#include <arch/bitmask.h>
+#include <bitmask.h>
/* These structs are declared again and initialized farther down */
struct page_map_operations ext2_pm_op;
return (inode->i_ino - 1) % le32_to_cpu(e2sbi->e2sb->s_inodes_per_group);
}
+/* Returns the inode number given a 0-index of an inode within a block group */
+static unsigned long ext2_bgidx2ino(struct super_block *sb,
+ struct ext2_block_group *bg,
+ unsigned int ino_idx)
+{
+ struct ext2_sb_info *e2sbi = (struct ext2_sb_info*)sb->s_fs_info;
+ struct ext2_sb *e2sb = e2sbi->e2sb;
+ struct ext2_block_group *e2bg = e2sbi->e2bg;
+ return (bg - e2bg) * le32_to_cpu(e2sb->s_inodes_per_group) + ino_idx + 1;
+}
+
/* Returns an uncounted reference to the BG in the BG table, which is pinned,
* hanging off the sb. Note, the BGs cover the blocks starting from the first
* data block, not from 0. So if the FDB is 1, BG 0 covers 1 through 1024, and
/* One-time init for all ext2 instances */
void ext2_init(void)
{
- ext2_i_kcache = kmem_cache_create("ext2_i_info", sizeof(struct ext2_i_info),
- __alignof__(struct ext2_i_info), 0, 0, 0);
+ ext2_i_kcache = kmem_cache_create("ext2_i_info",
+ sizeof(struct ext2_i_info),
+ __alignof__(struct ext2_i_info), 0,
+ NULL, 0, 0, NULL);
}
/* Block management */
void *__ext2_get_metablock(struct block_device *bdev, unsigned long blk_num,
unsigned int blk_sz)
{
- struct page *page;
- struct page_map *pm = &bdev->b_pm;
- unsigned int blk_per_pg = PGSIZE / blk_sz;
- unsigned int blk_offset = (blk_num % blk_per_pg) * blk_sz;
- int error;
- assert(blk_offset < PGSIZE);
- error = pm_load_page(pm, blk_num / blk_per_pg, &page);
- if (error) {
- warn("Failed to read metablock! (%d)", error);
- return 0;
- }
- /* return where we are within the page for the given block */
- return page2kva(page) + blk_offset;
+ return bdev_get_buffer(bdev, blk_num, blk_sz)->bh_buffer;
}
/* Convenience wrapper */
return __ext2_get_metablock(sb->s_bdev, block_num, sb->s_blocksize);
}
+/* Helper to figure out the BH for any address within it's buffer */
+static struct buffer_head *ext2_my_bh(struct super_block *sb, void *addr)
+{
+ struct page *page = kva2page(addr);
+ struct buffer_head *bh = (struct buffer_head*)page->pg_private;
+ /* This case is for when we try do decref a non-BH'd 'metablock'. It's tied
+ * to e2ii->i_block[]. */
+ if (!bh)
+ return 0;
+ void *my_buf = (void*)ROUNDDOWN((uintptr_t)addr, sb->s_blocksize);
+ while (bh) {
+ if (bh->bh_buffer == my_buf)
+ break;
+ bh = bh->bh_next;
+ }
+ assert(bh && bh->bh_buffer == my_buf);
+ return bh;
+}
+
/* Decrefs the buffer from get_metablock(). Call this when you no longer
- * reference your metadata block/buffer */
-void ext2_put_metablock(void *buffer)
+ * reference your metadata block/buffer. Yes, we could just decref the page,
+ * but this will work if we end up changing how bdev_put_buffer() works. */
+void ext2_put_metablock(struct super_block *sb, void *buffer)
{
- page_decref(kva2page(buffer));
+ struct buffer_head *bh = ext2_my_bh(sb, buffer);
+ if (bh)
+ bdev_put_buffer(bh);
}
-/* Will dirty the block/BH/page for the given metadata block/buffer. Will have
- * to be careful with the page reclaimer - if someone holds a reference, they
- * can still dirty it. */
-void ext2_dirty_metablock(void *buffer)
+/* Will dirty the block/BH/page for the given metadata block/buffer. */
+void ext2_dirty_metablock(struct super_block *sb, void *buffer)
{
- struct page *page = kva2page(buffer);
- /* TODO: race on flag modification, and consider dirtying the BH. */
- page->pg_flags |= PG_DIRTY;
+ struct buffer_head *bh = ext2_my_bh(sb, buffer);
+ if (bh)
+ bdev_dirty_buffer(bh);
}
/* Helper for alloc_block. It will try to alloc a block from the BG, starting
if (!(GET_BITMASK_BIT(blk_bitmap, blk_idx))) {
SET_BITMASK_BIT(blk_bitmap, blk_idx);
bg->bg_free_blocks_cnt--;
- ext2_dirty_metablock(blk_bitmap);
+ ext2_dirty_metablock(sb, blk_bitmap);
found = TRUE;
break;
}
/* Note: the wrap-around hasn't been tested yet */
blk_idx = (blk_idx + 1) % blks_per_bg;
}
- ext2_put_metablock(blk_bitmap);
+ ext2_put_metablock(sb, blk_bitmap);
if (found)
*block_num = ext2_bgidx2block(sb, bg, blk_idx);
return found;
struct ext2_sb_info *e2sbi = (struct ext2_sb_info*)inode->i_sb->s_fs_info;
struct ext2_block_group *fetish_bg, *bg_i = e2sbi->e2bg;
unsigned int blk_idx;
- uint8_t *blk_bitmap;
bool found = FALSE;
uint32_t retval = 0;
return retval;
}
-/* Inode Table Management */
+/* Inode Management */
+
+/* Helper for alloc_diskinode. It will try to alloc a disk inode from the BG.
+ * If successful, it will return the inode number in *ino_num. TODO:
+ * concurrency protection */
+static bool ext2_tryalloc_diskinode(struct super_block *sb,
+ struct ext2_block_group *bg,
+ unsigned long *ino_num)
+{
+ uint8_t *ino_bitmap;
+ struct ext2_sb_info *e2sbi = (struct ext2_sb_info*)sb->s_fs_info;
+ unsigned int i, ino_per_bg = le32_to_cpu(e2sbi->e2sb->s_inodes_per_group);
+ bool found = FALSE;
+
+ /* Check to see if there are any free inodes */
+ if (!le32_to_cpu(bg->bg_free_inodes_cnt))
+ return FALSE;
+ /* Check the bitmap for the free inode */
+ ino_bitmap = ext2_get_metablock(sb, bg->bg_inode_bitmap);
+ for (i = 0; i < ino_per_bg; i++) {
+ if (!(GET_BITMASK_BIT(ino_bitmap, i))) {
+ SET_BITMASK_BIT(ino_bitmap, i);
+ bg->bg_free_inodes_cnt--;
+ ext2_dirty_metablock(sb, ino_bitmap);
+ found = TRUE;
+ break;
+ }
+ }
+ ext2_put_metablock(sb, ino_bitmap);
+ /* Convert the i (a 0-index bit) within the BG to a real inode number. */
+ if (found)
+ *ino_num = ext2_bgidx2ino(sb, bg, i);
+ return found;
+}
+
+/* This allocates a fresh ino number for inode, given the parent's BG. Make
+ * sure you set the inode's type before calling this, since it matters if we a
+ * making a directory or not. This disk inode is reserved on disk in the bitmap
+ * (at least the bitmap is changed and dirtied). Note the lack of concurrency
+ * protections here. Consider returning the BG too. */
+unsigned long ext2_alloc_diskinode(struct inode *inode,
+ struct ext2_block_group *dir_bg)
+{
+ struct ext2_sb_info *e2sbi = (struct ext2_sb_info*)inode->i_sb->s_fs_info;
+ struct ext2_block_group *bg = dir_bg;
+ struct ext2_block_group *bg_i = e2sbi->e2bg;
+ bool found = FALSE;
+ unsigned long retval = 0;
+
+ if (S_ISDIR(inode->i_mode)) {
+ /* TODO: intelligently pick a different bg to use than the current one.
+ * Right now, we just jump to the next one, though you should do things
+ * like take into account the ratio of directories to files. */
+ bg += 1;
+ }
+ /* Try to find a free inode in the chosen BG */
+ found = ext2_tryalloc_diskinode(inode->i_sb, bg, &retval);
+ if (found)
+ return retval;
+
+ warn("This part hasn't been tested yet.");
+ /* Find an inode anywhere else (perhaps using the log trick, but for now just
+ * linearly scanning). */
+ for (int i = 0; i < e2sbi->nr_bgs; i++, bg_i++) {
+ if (bg_i == bg)
+ continue;
+ found = ext2_tryalloc_diskinode(inode->i_sb, bg_i, &retval);
+ if (found)
+ break;
+ }
+ if (!found)
+ panic("Ran out of inodes! (probably a bug)");
+ return retval;
+}
/* Helper for ino table management. blkid is the inode table block we are
* looking in, rel_blkid is the block we want, relative to the current
/* Actually read in the block we alloc'd */
new_blk = ext2_get_metablock(inode->i_sb, new_blkid);
memset(new_blk, 0, inode->i_sb->s_blocksize);
- ext2_dirty_metablock(new_blk);
+ ext2_dirty_metablock(inode->i_sb, new_blk);
/* We put it, despite it getting relooked up in the next walk */
- ext2_put_metablock(new_blk);
+ ext2_put_metablock(inode->i_sb, new_blk);
/* Now write the new block into its slot */
*blk_slot = cpu_to_le32(new_blkid);
- ext2_dirty_metablock(blk_slot);
+ ext2_dirty_metablock(inode->i_sb, blk_slot);
}
/* This walks a table stored at block 'blkid', returning which block you should
ext2_fill_inotable_slot(inode, blk_slot);
*blkid = le32_to_cpu(*blk_slot);
*rel_inoblk = *rel_inoblk % reach;
- ext2_put_metablock(blk_slot); /* the ref for the block we looked in */
+ ext2_put_metablock(inode->i_sb, blk_slot); /* ref for the one looked in */
}
/* Finds the slot of the FS block corresponding to a specific block number of an
* tables (1x, 2x, or 3x (triply indirect) tables). Block numbers start at 0.
*
* This returns a pointer within a metablock, which needs to be decref'd (and
- * possibly dirtied) when you are done.
+ * possibly dirtied) when you are done. Note, it can return a pointer to
+ * something that is NOT in a metablock (e2ii->i_block[]), but put_metablock can
+ * handle it for now.
*
* Horrendously untested, btw. */
uint32_t *ext2_lookup_inotable_slot(struct inode *inode, uint32_t ino_block)
} else {
/* Direct block, straight out of the inode */
blk_slot = &e2ii->i_block[ino_block];
- /* need to incref, since the i_block isn't a real metablock (it's just a
- * random page!), and the caller is going to end up decreffing it */
- page_incref(kva2page(blk_slot));
}
return blk_slot;
}
{
uint32_t retval, *buf = ext2_lookup_inotable_slot(inode, ino_block);
retval = *buf;
- ext2_put_metablock(buf);
+ ext2_put_metablock(inode->i_sb, buf);
return retval;
}
/* Returns an incref'd metadata block for the contents of the ino block. Don't
- * use this for regular files - use their inode's page cache instead. */
+ * use this for regular files - use their inode's page cache instead (used for
+ * directories for now). If there isn't a block allocated yet, it will provide
+ * a zeroed one. */
void *ext2_get_ino_metablock(struct inode *inode, unsigned long ino_block)
{
- uint32_t blkid = ext2_find_inoblock(inode, ino_block);
- return ext2_get_metablock(inode->i_sb, blkid);
+ uint32_t blkid, *retval, *blk_slot;
+ blk_slot = ext2_lookup_inotable_slot(inode, ino_block);
+ blkid = le32_to_cpu(*blk_slot);
+ if (blkid) {
+ ext2_put_metablock(inode->i_sb, blk_slot);
+ return ext2_get_metablock(inode->i_sb, blkid);
+ }
+ /* If there isn't a block there, alloc and insert one. This block will be
+ * the next big chunk of "file" data for this inode. */
+ blkid = ext2_alloc_block(inode, ext2_bgidx2block(inode->i_sb,
+ ext2_inode2bg(inode),
+ 0));
+ *blk_slot = cpu_to_le32(blkid);
+ ext2_dirty_metablock(inode->i_sb, blk_slot);
+ ext2_put_metablock(inode->i_sb, blk_slot);
+ inode->i_blocks += inode->i_sb->s_blocksize >> 9; /* inc by 1 FS block */
+ inode->i_size += inode->i_sb->s_blocksize;
+ retval = ext2_get_metablock(inode->i_sb, blkid);
+ memset(retval, 0, inode->i_sb->s_blocksize); /* 0 the new block */
+ return retval;
}
/* This should help with degubbing. In read_inode(), print out the i_block, and
* the 2x and 3x walks are jacked up. */
void ext2_print_ino_blocks(struct inode *inode)
{
- printk("Inode %08p, Size: %d, 512B 'blocks;: %d\n-------------\n", inode,
+ printk("Inode %p, Size: %d, 512B 'blocks': %d\n-------------\n", inode,
inode->i_size, inode->i_blocks);
- for (int i = 0; i < inode->i_blocks; i++)
+ for (int i = 0; i < inode->i_blocks * (inode->i_sb->s_blocksize / 512); i++)
printk("# %03d, Block %03d\n", i, ext2_find_inoblock(inode, i));
}
blksize = 1024 << le32_to_cpu(e2sb->s_log_block_size);
blks_per_group = le32_to_cpu(e2sb->s_blocks_per_group);
num_blk_group = num_blks / blks_per_group + (num_blks % blks_per_group ? 1 : 0);
-
+
if (print) {
printk("EXT2 info:\n-------------------------\n");
printk("Total Inodes: %8d\n", le32_to_cpu(e2sb->s_inodes_cnt));
printk("Volume name: %s\n", e2sb->s_volume_name);
printk("\nBlock Group Info:\n----------------------\n");
}
-
+
for (int i = 0; i < num_blk_group; i++) {
sum_blks += le16_to_cpu(bg[i].bg_free_blocks_cnt);
sum_inodes += le16_to_cpu(bg[i].bg_free_inodes_cnt);
if (print) {
- printk("*** BG %d at %08p\n", i, &bg[i]);
+ printk("*** BG %d at %p\n", i, &bg[i]);
printk("Block bitmap:%8d\n", le32_to_cpu(bg[i].bg_block_bitmap));
printk("Inode bitmap:%8d\n", le32_to_cpu(bg[i].bg_inode_bitmap));
printk("Inode table: %8d\n", le32_to_cpu(bg[i].bg_inode_table));
printk("Used Dirs: %8d\n", le16_to_cpu(bg[i].bg_used_dirs_cnt));
}
}
-
+
/* Sanity Assertions. A good ext2 will always pass these. */
inodes_per_grp = le32_to_cpu(e2sb->s_inodes_per_group);
blks_per_group = le32_to_cpu(e2sb->s_blocks_per_group);
fs_blk_num = ext2_alloc_block(inode, fs_blk_num + 1);
/* Link it, and dirty the inode indirect block */
*fs_blk_slot = cpu_to_le32(fs_blk_num);
- ext2_dirty_metablock(fs_blk_slot);
+ ext2_dirty_metablock(inode->i_sb, fs_blk_slot);
/* the block is still on disk, and we don't want its contents */
bh->bh_flags = BH_NEEDS_ZEROED; /* talking to readpage */
/* update our num blocks, with 512B each "block" (ext2-style) */
} else { /* there is a block there already */
fs_blk_num = *fs_blk_slot;
}
- ext2_put_metablock(fs_blk_slot);
+ ext2_put_metablock(inode->i_sb, fs_blk_slot);
bh->bh_sector = fs_blk_num * sct_per_blk;
bh->bh_nr_sector = sct_per_blk;
/* Stop if we're the last block in the page. We could be going beyond
struct block_request *breq;
void *eobh;
- assert(page->pg_flags & PG_BUFFER);
+ atomic_or(&page->pg_flags, PG_BUFFER);
retval = ext2_mappage(pm, page);
- if (retval) {
- unlock_page(page);
+ if (retval)
return retval;
- }
/* Build and submit the request */
breq = kmem_cache_alloc(breq_kcache, 0);
- if (!breq) {
- unlock_page(page);
+ if (!breq)
return -ENOMEM;
- }
breq->flags = BREQ_READ;
+ breq->callback = generic_breq_done;
+ breq->data = 0;
+ sem_init_irqsave(&breq->sem, 0);
breq->bhs = breq->local_bhs;
breq->nr_bhs = 0;
/* Pack the BH pointers in the block request */
} else {
memset(bh->bh_buffer, 0, pm->pm_host->i_sb->s_blocksize);
bh->bh_flags |= BH_DIRTY;
- bh->bh_page->pg_flags |= PG_DIRTY;
+ atomic_or(&bh->bh_page->pg_flags, PG_DIRTY);
}
}
- /* TODO: (BLK) this assumes we slept til the request was done */
- retval = make_request(bdev, breq);
+ retval = bdev_submit_request(bdev, breq);
assert(!retval);
+ sleep_on_breq(breq);
+ kmem_cache_free(breq_kcache, breq);
/* zero out whatever is beyond the EOF. we could do this by figuring out
* where the BHs end and zeroing from there, but I'd rather zero from where
* the file ends (which could be in the middle of an FS block */
/* at this point, eof_off is the offset into the page of the EOF, or 0 */
if (eof_off)
memset(eof_off + page2kva(page), 0, PGSIZE - eof_off);
- /* after the data is read, we mark it up to date and unlock the page. */
- page->pg_flags |= PG_UPTODATE;
- unlock_page(page);
- kmem_cache_free(breq_kcache, breq);
+ /* Now the page is up to date */
+ atomic_or(&page->pg_flags, PG_UPTODATE);
/* Useful debugging. Put one higher up if the page is not getting mapped */
//print_pageinfo(page);
return 0;
}
+int ext2_writepage(struct page_map *pm, struct page *page)
+{
+ return -1;
+}
+
/* Super Operations */
/* Creates and initializes a new inode. FS specific, yet inode-generic fields
kmem_cache_free(ext2_i_kcache, inode->i_fs_info);
}
-/* reads the inode data on disk specified by inode->i_ino into the inode.
- * basically, it's a "make this inode the one for i_ino (i number)" */
-void ext2_read_inode(struct inode *inode)
+/* Returns a pointer within a metablock for the disk inode specified by inode.
+ * Be sure to 'put' your reference (and/or dirty it). */
+struct ext2_inode *ext2_get_diskinode(struct inode *inode)
{
- unsigned int bg_idx, ino_per_blk, my_ino_blk;
+ uint32_t my_bg_idx, ino_per_blk, my_ino_blk;
struct ext2_sb_info *e2sbi = (struct ext2_sb_info*)inode->i_sb->s_fs_info;
struct ext2_block_group *my_bg;
- struct ext2_inode *ino_tbl_chunk, *my_ino;
+ struct ext2_inode *ino_tbl_chunk;
+ assert(inode->i_ino); /* ino == 0 is a bug */
/* Need to compute the blockgroup and index of the requested inode */
ino_per_blk = inode->i_sb->s_blocksize /
le16_to_cpu(e2sbi->e2sb->s_inode_size);
- bg_idx = ext2_inode2bgidx(inode);
+ my_bg_idx = ext2_inode2bgidx(inode);
my_bg = ext2_inode2bg(inode);
/* Figure out which FS block of the inode table we want and read in that
* chunk */
- my_ino_blk = le32_to_cpu(my_bg->bg_inode_table) + bg_idx / ino_per_blk;
+ my_ino_blk = le32_to_cpu(my_bg->bg_inode_table) + my_bg_idx / ino_per_blk;
ino_tbl_chunk = ext2_get_metablock(inode->i_sb, my_ino_blk);
- my_ino = &ino_tbl_chunk[bg_idx % ino_per_blk];
+ return &ino_tbl_chunk[my_bg_idx % ino_per_blk];
+}
+
+/* reads the inode data on disk specified by inode->i_ino into the inode.
+ * basically, it's a "make this inode the one for i_ino (i number)" */
+void ext2_read_inode(struct inode *inode)
+{
+ struct ext2_inode *my_ino;
+ my_ino = ext2_get_diskinode(inode);
/* Have the disk inode now, let's put its info into the VFS inode: */
inode->i_mode = le16_to_cpu(my_ino->i_mode);
/* TODO: (HASH) unused: inode->i_hash add to hash (saves on disc reading) */
/* TODO: we could consider saving a pointer to the disk inode and pinning
* its buffer in memory, but for now we'll just free it. */
- ext2_put_metablock(ino_tbl_chunk);
+ ext2_put_metablock(inode->i_sb, my_ino);
}
/* called when an inode in memory is modified (journalling FS's care) */
#endif
}
+/* Initializes a new/empty disk inode, according to inode. If you end up not
+ * zeroing this stuff, be careful of endianness. */
+static void ext2_init_diskinode(struct ext2_inode *e2i, struct inode *inode)
+{
+ assert(inode->i_size == 0);
+ e2i->i_mode = cpu_to_le16(inode->i_mode);
+ e2i->i_uid = cpu_to_le16(inode->i_uid);
+ e2i->i_size = 0;
+ e2i->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
+ e2i->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
+ e2i->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
+ e2i->i_dtime = 0;
+ e2i->i_gid = cpu_to_le16(inode->i_gid);
+ e2i->i_links_cnt = cpu_to_le16(inode->i_nlink);
+ e2i->i_blocks = 0;
+ e2i->i_flags = cpu_to_le32(inode->i_flags);
+ e2i->i_osd1 = 0;
+ e2i->i_generation = 0;
+ e2i->i_file_acl = 0;
+ e2i->i_dir_acl = 0;
+ e2i->i_faddr = 0;
+ for (int i = 0; i < 15; i++)
+ e2i->i_block[i] = 0;
+ for (int i = 0; i < 12; i++)
+ e2i->i_osd2[i] = 0;
+}
+
+/* These should return true if foreach_dirent should stop working on the
+ * dirents. */
+typedef bool (*each_func_t) (struct ext2_dirent *dir_i, long a1, long a2,
+ long a3);
+
+/* Loads the buffer and performs my_work on each dirent, stopping and returning
+ * 0 if one of the calls succeeded, or returning the dir block num of what would
+ * be the next dir block otherwise (aka, how many blocks we went through). */
+static uint32_t ext2_foreach_dirent(struct inode *dir, each_func_t my_work,
+ long a1, long a2, long a3)
+{
+ struct ext2_dirent *dir_buf, *dir_i;
+ uint32_t dir_block = 0;
+ dir_buf = ext2_get_ino_metablock(dir, dir_block++);
+ dir_i = dir_buf;
+ /* now we have the first block worth of dirents. We'll get another block if
+ * dir_i hits a block boundary */
+ for (unsigned int bytes = 0; bytes < dir->i_size; ) {
+ /* On subsequent loops, we might need to advance to the next block.
+ * This is where a file abstraction for a dir might be easier. */
+ if ((void*)dir_i >= (void*)dir_buf + dir->i_sb->s_blocksize) {
+ ext2_put_metablock(dir->i_sb, dir_buf);
+ dir_buf = ext2_get_ino_metablock(dir, dir_block++);
+ dir_i = dir_buf;
+ assert(dir_buf);
+ }
+ if (my_work(dir_i, a1, a2, a3)) {
+ ext2_put_metablock(dir->i_sb, dir_buf);
+ return 0;
+ }
+ /* Get ready for the next loop */
+ bytes += dir_i->dir_reclen;
+ dir_i = (void*)dir_i + dir_i->dir_reclen;
+ }
+ ext2_put_metablock(dir->i_sb, dir_buf);
+ return dir_block;
+}
+
+/* Returns the actual length of a dirent, not just how far to the next entry.
+ * If there is no inode, the entry is unused, and it has no length (as far as
+ * users of this should care). */
+static unsigned int ext2_dirent_len(struct ext2_dirent *e2dir)
+{
+ /* arguably, we don't need the le32_to_cpu */
+ if (le32_to_cpu(e2dir->dir_inode))
+ return ROUNDUP(e2dir->dir_namelen + 8, 4); /* no such le8_to_cpu */
+ else
+ return 0;
+}
+
+/* Helper for writing the contents of a dentry to a disk dirent. Zeroes the
+ * contents of the dirent so that we don't write random data to disk. */
+static void ext2_write_dirent(struct ext2_dirent *e2dir, struct dentry *dentry,
+ unsigned int rec_len)
+{
+ memset(e2dir, 0, sizeof(*e2dir));
+ e2dir->dir_inode = cpu_to_le32(dentry->d_inode->i_ino);
+ e2dir->dir_reclen = cpu_to_le16(rec_len);
+ e2dir->dir_namelen = dentry->d_name.len;
+ switch (dentry->d_inode->i_mode & __S_IFMT) {
+ case (__S_IFDIR):
+ e2dir->dir_filetype = EXT2_FT_DIR;
+ break;
+ case (__S_IFREG):
+ e2dir->dir_filetype = EXT2_FT_REG_FILE;
+ break;
+ case (__S_IFLNK):
+ e2dir->dir_filetype = EXT2_FT_SYMLINK;
+ break;
+ case (__S_IFCHR):
+ e2dir->dir_filetype = EXT2_FT_CHRDEV;
+ break;
+ case (__S_IFBLK):
+ e2dir->dir_filetype = EXT2_FT_BLKDEV;
+ break;
+ case (__S_IFSOCK):
+ e2dir->dir_filetype = EXT2_FT_SOCK;
+ break;
+ default:
+ warn("[Calm British Accent] Look around you: Unknown filetype.");
+ e2dir->dir_filetype = EXT2_FT_UNKNOWN;
+ }
+ assert(dentry->d_name.len <= 255);
+ strlcpy((char*)e2dir->dir_name, dentry->d_name.name,
+ sizeof(e2dir->dir_name));
+}
+
+/* Helper for ext2_create(). This tries to squeeze a dirent in the slack space
+ * after an existing dirent, returning TRUE if it succeeded (to break out). */
+static bool create_each_func(struct ext2_dirent *dir_i, long a1, long a2,
+ long a3)
+{
+ struct dentry *dentry = (struct dentry*)a1;
+ unsigned int our_rec_len = (unsigned int)a2;
+ unsigned int mode = (unsigned int)a3;
+ struct ext2_dirent *dir_new;
+ unsigned int real_len = ext2_dirent_len(dir_i);
+ /* How much room is available after this dir_i before the next one */
+ unsigned int record_slack = le16_to_cpu(dir_i->dir_reclen) - real_len;
+ /* TODO: Note that this technique will clobber any directory indexing. They
+ * exist after the .. entry with an inode of 0. Check the docs for
+ * specifics and think up a nice way to tell the diff between a reserved
+ * entry and an unused one, when inode == 0. */
+ if (record_slack < our_rec_len)
+ return FALSE;
+ /* At this point, there is enough room for us. Stick our new one in right
+ * after the real len, making sure our reclen goes to the old end. Note
+ * that it is possible to have a real_len of 0 (an unused entry). In this
+ * case, we just end up taking over the spot in the dir_blk. Be sure to set
+ * dir_i's reclen before dir_new's (in case they are the same). */
+ dir_new = ((void*)dir_i + real_len);
+ dir_i->dir_reclen = cpu_to_le16(real_len);
+ ext2_write_dirent(dir_new, dentry, record_slack);
+ ext2_dirty_metablock(dentry->d_sb, dir_new);
+ return TRUE;
+}
+
/* Called when creating a new disk inode in dir associated with dentry. We need
* to fill out the i_ino, set the type, and do whatever else we need */
int ext2_create(struct inode *dir, struct dentry *dentry, int mode,
struct nameidata *nd)
{
-I_AM_HERE;
- #if 0
struct inode *inode = dentry->d_inode;
- ext2_init_inode(dir, dentry);
+ struct ext2_block_group *dir_bg = ext2_inode2bg(dir);
+ struct ext2_inode *disk_inode;
+ struct ext2_i_info *e2ii;
+ uint32_t dir_block;
+ unsigned int our_rec_len;
+ struct ext2_dirent *new_dirent;
+ /* Set basic inode stuff for files, get a disk inode, etc */
SET_FTYPE(inode->i_mode, __S_IFREG);
inode->i_fop = &ext2_f_op_file;
- /* fs_info->filestart is set by the caller, or else when first written (for
- * new files. it was set to 0 in alloc_inode(). */
- #endif
+ inode->i_ino = ext2_alloc_diskinode(inode, dir_bg);
+ /* Initialize disk inode (this will be different for short symlinks) */
+ disk_inode = ext2_get_diskinode(inode);
+ ext2_init_diskinode(disk_inode, inode);
+ /* Initialize the e2ii (might get rid of this cache of block info) */
+ inode->i_fs_info = kmem_cache_alloc(ext2_i_kcache, 0);
+ e2ii = (struct ext2_i_info*)inode->i_fs_info;
+ for (int i = 0; i < 15; i++)
+ e2ii->i_block[i] = le32_to_cpu(disk_inode->i_block[i]);
+ /* Dirty and put the disk inode */
+ ext2_dirty_metablock(dentry->d_sb, disk_inode);
+ ext2_put_metablock(dentry->d_sb, disk_inode);
+ /* Insert it in the directory (make a dirent, might expand the dir too) */
+ /* Note the disk dir_name is not null terminated */
+ our_rec_len = ROUNDUP(8 + dentry->d_name.len, 4);
+ assert(our_rec_len <= 8 + 256);
+ /* Consider caching the start point for future dirent ops. Or even using
+ * the indexed directory.... */
+ dir_block = ext2_foreach_dirent(dir, create_each_func, (long)dentry,
+ (long)our_rec_len, (long)mode);
+ /* If this returned a block number, we didn't find room in any of the
+ * existing directory blocks, so we need to make a new one, stick it in the
+ * dir inode, and stick our dirent at the beginning. The reclen is the
+ * whole blocksize (since it's the last entry in this block) */
+ if (dir_block) {
+ new_dirent = ext2_get_ino_metablock(dir, dir_block);
+ ext2_write_dirent(new_dirent, dentry, dentry->d_sb->s_blocksize);
+ ext2_dirty_metablock(dentry->d_sb, new_dirent);
+ ext2_put_metablock(dentry->d_sb, new_dirent);
+ }
return 0;
}
+/* If we match, this loads the inode for the dentry and returns true (so we
+ * break out) */
+static bool lookup_each_func(struct ext2_dirent *dir_i, long a1, long a2,
+ long a3)
+{
+ struct dentry *dentry = (struct dentry*)a1;
+ /* Test if we're the one (TODO: use d_compare). Note, dir_name is not
+ * null terminated, hence the && test. */
+ if (!strncmp((char*)dir_i->dir_name, dentry->d_name.name,
+ dir_i->dir_namelen) &&
+ (dentry->d_name.name[dir_i->dir_namelen] == '\0')) {
+ load_inode(dentry, (long)le32_to_cpu(dir_i->dir_inode));
+ /* TODO: (HASH) add dentry to dcache (maybe the caller should) */
+ return TRUE;
+ }
+ return FALSE;
+}
+
/* Searches the directory for the filename in the dentry, filling in the dentry
* with the FS specific info of this file. If it succeeds, it will pass back
* the *dentry you should use (which might be the same as the one you passed in).
{
assert(S_ISDIR(dir->i_mode));
struct ext2_dirent *dir_buf, *dir_i;
- unsigned int dir_block = 0;
- bool found = FALSE;
- dir_buf = ext2_get_ino_metablock(dir, dir_block++);
- dir_i = dir_buf;
- /* now we have the first block worth of dirents. We'll get another block if
- * dir_i hits a block boundary */
- for (unsigned int bytes = 0; bytes < dir->i_size; ) {
- /* On subsequent loops, we might need to advance to the next block.
- * This is where a file abstraction for a dir might be easier. */
- if ((void*)dir_i >= (void*)dir_buf + dir->i_sb->s_blocksize) {
- ext2_put_metablock(dir_buf);
- dir_buf = ext2_get_ino_metablock(dir, dir_block++);
- dir_i = dir_buf;
- assert(dir_buf);
- }
- /* Test if we're the one (TODO: use d_compare). Note, dir_name is not
- * null terminated, hence the && test. */
- if (!strncmp((char*)dir_i->dir_name, dentry->d_name.name,
- dir_i->dir_namelen) &&
- (dentry->d_name.name[dir_i->dir_namelen] == '\0')) {
- load_inode(dentry, le32_to_cpu(dir_i->dir_inode));
- /* TODO: (HASH) add dentry to dcache (maybe the caller should) */
- ext2_put_metablock(dir_buf);
- return dentry;
- }
- /* Get ready for the next loop */
- bytes += dir_i->dir_reclen;
- dir_i = (void*)dir_i + dir_i->dir_reclen;
- }
- printd("EXT2: Not Found, %s\n", dentry->d_name.name);
- ext2_put_metablock(dir_buf);
+ if (!ext2_foreach_dirent(dir, lookup_each_func, (long)dentry, 0, 0))
+ return dentry;
+ printd("EXT2: Not Found, %s\n", dentry->d_name.name);
return 0;
}
struct inode *inode = dentry->d_inode;
SET_FTYPE(inode->i_mode, __S_IFLNK);
inode->i_fop = &ext2_f_op_sym;
- strncpy(string, symname, len);
- string[len] = '\0'; /* symname should be \0d anyway, but just in case */
+ strlcpy(string, symname, len + 1);
#endif
return 0;
}
return -1;
}
-/* Produces the hash to lookup this dentry from the dcache */
-int ext2_d_hash(struct dentry *dentry, struct qstr *name)
-{
- return -1;
-}
-
/* Compares name1 and name2. name1 should be a member of dir. */
int ext2_d_compare(struct dentry *dir, struct qstr *name1, struct qstr *name2)
{ // default, string comp (case sensitive)
/* Updates the file pointer. TODO: think about locking, and putting this in the
* VFS. */
#include <syscall.h> /* just for set_errno, may go away later */
-off_t ext2_llseek(struct file *file, off_t offset, int whence)
+int ext2_llseek(struct file *file, off64_t offset, off64_t *ret, int whence)
{
- off_t temp_off = 0;
+ off64_t temp_off = 0;
switch (whence) {
case SEEK_SET:
temp_off = offset;
return -1;
}
file->f_pos = temp_off;
- return temp_off;
+ *ret = temp_off;
+ return 0;
}
/* Fills in the next directory entry (dirent), starting with d_off. Like with
{
void *blk_buf;
/* Not enough data at the end of the directory */
- if (dir->f_dentry->d_inode->i_size <
- dirent->d_off + sizeof(struct ext2_dirent))
+ if (dir->f_dentry->d_inode->i_size < dirent->d_off + 8)
return -ENOENT;
-
/* Figure out which block we need to read in for dirent->d_off */
int block = dirent->d_off / dir->f_dentry->d_sb->s_blocksize;
blk_buf = ext2_get_ino_metablock(dir->f_dentry->d_inode, block);
assert(blk_buf);
- off_t f_off = dirent->d_off % dir->f_dentry->d_sb->s_blocksize;
+ off64_t f_off = dirent->d_off % dir->f_dentry->d_sb->s_blocksize;
/* Copy out the dirent info */
struct ext2_dirent *e2dir = (struct ext2_dirent*)(blk_buf + f_off);
dirent->d_ino = le32_to_cpu(e2dir->dir_inode);
dirent->d_off += le16_to_cpu(e2dir->dir_reclen);
+ if (dir->f_dentry->d_inode->i_size < dirent->d_off)
+ panic("Something is jacked with the dirent going beyond the dir/file");
/* note, dir_namelen doesn't include the \0 */
dirent->d_reclen = e2dir->dir_namelen;
- strncpy(dirent->d_name, (char*)e2dir->dir_name, e2dir->dir_namelen);
assert(e2dir->dir_namelen <= MAX_FILENAME_SZ);
- dirent->d_name[e2dir->dir_namelen] = '\0';
- ext2_put_metablock(blk_buf);
-
+ strlcpy(dirent->d_name, (char*)e2dir->dir_name, e2dir->dir_namelen + 1);
+ ext2_put_metablock(dir->f_dentry->d_sb, blk_buf);
+
/* At the end of the directory, sort of. ext2 often preallocates blocks, so
* this will cause us to walk along til the end, which isn't quite right. */
if (dir->f_dentry->d_inode->i_size == dirent->d_off)
/* Reads count bytes from a file, starting from (and modifiying) offset, and
* putting the bytes into buffers described by vector */
ssize_t ext2_readv(struct file *file, const struct iovec *vector,
- unsigned long count, off_t *offset)
+ unsigned long count, off64_t *offset)
{
return -1;
}
/* Writes count bytes to a file, starting from (and modifiying) offset, and
* taking the bytes from buffers described by vector */
ssize_t ext2_writev(struct file *file, const struct iovec *vector,
- unsigned long count, off_t *offset)
+ unsigned long count, off64_t *offset)
{
return -1;
}
/* Write the contents of file to the page. Will sort the params later */
ssize_t ext2_sendpage(struct file *file, struct page *page, int offset,
- size_t size, off_t pos, int more)
+ size_t size, off64_t pos, int more)
{
return -1;
}
/* Redeclaration and initialization of the FS ops structures */
struct page_map_operations ext2_pm_op = {
ext2_readpage,
+ ext2_writepage,
};
struct super_operations ext2_s_op = {
struct dentry_operations ext2_d_op = {
ext2_d_revalidate,
- ext2_d_hash,
+ generic_dentry_hash,
ext2_d_compare,
ext2_d_delete,
ext2_d_release,