summaryrefslogtreecommitdiff
path: root/fs/bfs
diff options
context:
space:
mode:
Diffstat (limited to 'fs/bfs')
-rw-r--r--fs/bfs/Makefile7
-rw-r--r--fs/bfs/bfs.h60
-rw-r--r--fs/bfs/dir.c362
-rw-r--r--fs/bfs/file.c162
-rw-r--r--fs/bfs/inode.c420
5 files changed, 1011 insertions, 0 deletions
diff --git a/fs/bfs/Makefile b/fs/bfs/Makefile
new file mode 100644
index 000000000000..c787b36d940c
--- /dev/null
+++ b/fs/bfs/Makefile
@@ -0,0 +1,7 @@
+#
+# Makefile for BFS filesystem.
+#
+
+obj-$(CONFIG_BFS_FS) += bfs.o
+
+bfs-objs := inode.o file.o dir.o
diff --git a/fs/bfs/bfs.h b/fs/bfs/bfs.h
new file mode 100644
index 000000000000..1020dbc88bec
--- /dev/null
+++ b/fs/bfs/bfs.h
@@ -0,0 +1,60 @@
+/*
+ * fs/bfs/bfs.h
+ * Copyright (C) 1999 Tigran Aivazian <tigran@veritas.com>
+ */
+#ifndef _FS_BFS_BFS_H
+#define _FS_BFS_BFS_H
+
+#include <linux/bfs_fs.h>
+
+/*
+ * BFS file system in-core superblock info
+ */
+struct bfs_sb_info {
+ unsigned long si_blocks;
+ unsigned long si_freeb;
+ unsigned long si_freei;
+ unsigned long si_lf_ioff;
+ unsigned long si_lf_sblk;
+ unsigned long si_lf_eblk;
+ unsigned long si_lasti;
+ unsigned long * si_imap;
+ struct buffer_head * si_sbh; /* buffer header w/superblock */
+ struct bfs_super_block * si_bfs_sb; /* superblock in si_sbh->b_data */
+};
+
+/*
+ * BFS file system in-core inode info
+ */
+struct bfs_inode_info {
+ unsigned long i_dsk_ino; /* inode number from the disk, can be 0 */
+ unsigned long i_sblock;
+ unsigned long i_eblock;
+ struct inode vfs_inode;
+};
+
+static inline struct bfs_sb_info *BFS_SB(struct super_block *sb)
+{
+ return sb->s_fs_info;
+}
+
+static inline struct bfs_inode_info *BFS_I(struct inode *inode)
+{
+ return list_entry(inode, struct bfs_inode_info, vfs_inode);
+}
+
+
+#define printf(format, args...) \
+ printk(KERN_ERR "BFS-fs: %s(): " format, __FUNCTION__, ## args)
+
+
+/* file.c */
+extern struct inode_operations bfs_file_inops;
+extern struct file_operations bfs_file_operations;
+extern struct address_space_operations bfs_aops;
+
+/* dir.c */
+extern struct inode_operations bfs_dir_inops;
+extern struct file_operations bfs_dir_operations;
+
+#endif /* _FS_BFS_BFS_H */
diff --git a/fs/bfs/dir.c b/fs/bfs/dir.c
new file mode 100644
index 000000000000..5a1e5ce057ff
--- /dev/null
+++ b/fs/bfs/dir.c
@@ -0,0 +1,362 @@
+/*
+ * fs/bfs/dir.c
+ * BFS directory operations.
+ * Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com>
+ */
+
+#include <linux/time.h>
+#include <linux/string.h>
+#include <linux/fs.h>
+#include <linux/smp_lock.h>
+#include <linux/buffer_head.h>
+#include <linux/sched.h>
+#include "bfs.h"
+
+#undef DEBUG
+
+#ifdef DEBUG
+#define dprintf(x...) printf(x)
+#else
+#define dprintf(x...)
+#endif
+
+static int bfs_add_entry(struct inode * dir, const char * name, int namelen, int ino);
+static struct buffer_head * bfs_find_entry(struct inode * dir,
+ const char * name, int namelen, struct bfs_dirent ** res_dir);
+
+static int bfs_readdir(struct file * f, void * dirent, filldir_t filldir)
+{
+ struct inode * dir = f->f_dentry->d_inode;
+ struct buffer_head * bh;
+ struct bfs_dirent * de;
+ unsigned int offset;
+ int block;
+
+ lock_kernel();
+
+ if (f->f_pos & (BFS_DIRENT_SIZE-1)) {
+ printf("Bad f_pos=%08lx for %s:%08lx\n", (unsigned long)f->f_pos,
+ dir->i_sb->s_id, dir->i_ino);
+ unlock_kernel();
+ return -EBADF;
+ }
+
+ while (f->f_pos < dir->i_size) {
+ offset = f->f_pos & (BFS_BSIZE-1);
+ block = BFS_I(dir)->i_sblock + (f->f_pos >> BFS_BSIZE_BITS);
+ bh = sb_bread(dir->i_sb, block);
+ if (!bh) {
+ f->f_pos += BFS_BSIZE - offset;
+ continue;
+ }
+ do {
+ de = (struct bfs_dirent *)(bh->b_data + offset);
+ if (de->ino) {
+ int size = strnlen(de->name, BFS_NAMELEN);
+ if (filldir(dirent, de->name, size, f->f_pos, de->ino, DT_UNKNOWN) < 0) {
+ brelse(bh);
+ unlock_kernel();
+ return 0;
+ }
+ }
+ offset += BFS_DIRENT_SIZE;
+ f->f_pos += BFS_DIRENT_SIZE;
+ } while (offset < BFS_BSIZE && f->f_pos < dir->i_size);
+ brelse(bh);
+ }
+
+ unlock_kernel();
+ return 0;
+}
+
+struct file_operations bfs_dir_operations = {
+ .read = generic_read_dir,
+ .readdir = bfs_readdir,
+ .fsync = file_fsync,
+};
+
+extern void dump_imap(const char *, struct super_block *);
+
+static int bfs_create(struct inode * dir, struct dentry * dentry, int mode,
+ struct nameidata *nd)
+{
+ int err;
+ struct inode * inode;
+ struct super_block * s = dir->i_sb;
+ struct bfs_sb_info * info = BFS_SB(s);
+ unsigned long ino;
+
+ inode = new_inode(s);
+ if (!inode)
+ return -ENOSPC;
+ lock_kernel();
+ ino = find_first_zero_bit(info->si_imap, info->si_lasti);
+ if (ino > info->si_lasti) {
+ unlock_kernel();
+ iput(inode);
+ return -ENOSPC;
+ }
+ set_bit(ino, info->si_imap);
+ info->si_freei--;
+ inode->i_uid = current->fsuid;
+ inode->i_gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
+ inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
+ inode->i_blocks = inode->i_blksize = 0;
+ inode->i_op = &bfs_file_inops;
+ inode->i_fop = &bfs_file_operations;
+ inode->i_mapping->a_ops = &bfs_aops;
+ inode->i_mode = mode;
+ inode->i_ino = ino;
+ BFS_I(inode)->i_dsk_ino = ino;
+ BFS_I(inode)->i_sblock = 0;
+ BFS_I(inode)->i_eblock = 0;
+ insert_inode_hash(inode);
+ mark_inode_dirty(inode);
+ dump_imap("create",s);
+
+ err = bfs_add_entry(dir, dentry->d_name.name, dentry->d_name.len, inode->i_ino);
+ if (err) {
+ inode->i_nlink--;
+ mark_inode_dirty(inode);
+ iput(inode);
+ unlock_kernel();
+ return err;
+ }
+ unlock_kernel();
+ d_instantiate(dentry, inode);
+ return 0;
+}
+
+static struct dentry * bfs_lookup(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
+{
+ struct inode * inode = NULL;
+ struct buffer_head * bh;
+ struct bfs_dirent * de;
+
+ if (dentry->d_name.len > BFS_NAMELEN)
+ return ERR_PTR(-ENAMETOOLONG);
+
+ lock_kernel();
+ bh = bfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len, &de);
+ if (bh) {
+ unsigned long ino = le32_to_cpu(de->ino);
+ brelse(bh);
+ inode = iget(dir->i_sb, ino);
+ if (!inode) {
+ unlock_kernel();
+ return ERR_PTR(-EACCES);
+ }
+ }
+ unlock_kernel();
+ d_add(dentry, inode);
+ return NULL;
+}
+
+static int bfs_link(struct dentry * old, struct inode * dir, struct dentry * new)
+{
+ struct inode * inode = old->d_inode;
+ int err;
+
+ lock_kernel();
+ err = bfs_add_entry(dir, new->d_name.name, new->d_name.len, inode->i_ino);
+ if (err) {
+ unlock_kernel();
+ return err;
+ }
+ inode->i_nlink++;
+ inode->i_ctime = CURRENT_TIME_SEC;
+ mark_inode_dirty(inode);
+ atomic_inc(&inode->i_count);
+ d_instantiate(new, inode);
+ unlock_kernel();
+ return 0;
+}
+
+
+static int bfs_unlink(struct inode * dir, struct dentry * dentry)
+{
+ int error = -ENOENT;
+ struct inode * inode;
+ struct buffer_head * bh;
+ struct bfs_dirent * de;
+
+ inode = dentry->d_inode;
+ lock_kernel();
+ bh = bfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len, &de);
+ if (!bh || de->ino != inode->i_ino)
+ goto out_brelse;
+
+ if (!inode->i_nlink) {
+ printf("unlinking non-existent file %s:%lu (nlink=%d)\n", inode->i_sb->s_id,
+ inode->i_ino, inode->i_nlink);
+ inode->i_nlink = 1;
+ }
+ de->ino = 0;
+ mark_buffer_dirty(bh);
+ dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
+ mark_inode_dirty(dir);
+ inode->i_nlink--;
+ inode->i_ctime = dir->i_ctime;
+ mark_inode_dirty(inode);
+ error = 0;
+
+out_brelse:
+ brelse(bh);
+ unlock_kernel();
+ return error;
+}
+
+static int bfs_rename(struct inode * old_dir, struct dentry * old_dentry,
+ struct inode * new_dir, struct dentry * new_dentry)
+{
+ struct inode * old_inode, * new_inode;
+ struct buffer_head * old_bh, * new_bh;
+ struct bfs_dirent * old_de, * new_de;
+ int error = -ENOENT;
+
+ old_bh = new_bh = NULL;
+ old_inode = old_dentry->d_inode;
+ if (S_ISDIR(old_inode->i_mode))
+ return -EINVAL;
+
+ lock_kernel();
+ old_bh = bfs_find_entry(old_dir,
+ old_dentry->d_name.name,
+ old_dentry->d_name.len, &old_de);
+
+ if (!old_bh || old_de->ino != old_inode->i_ino)
+ goto end_rename;
+
+ error = -EPERM;
+ new_inode = new_dentry->d_inode;
+ new_bh = bfs_find_entry(new_dir,
+ new_dentry->d_name.name,
+ new_dentry->d_name.len, &new_de);
+
+ if (new_bh && !new_inode) {
+ brelse(new_bh);
+ new_bh = NULL;
+ }
+ if (!new_bh) {
+ error = bfs_add_entry(new_dir,
+ new_dentry->d_name.name,
+ new_dentry->d_name.len, old_inode->i_ino);
+ if (error)
+ goto end_rename;
+ }
+ old_de->ino = 0;
+ old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME_SEC;
+ mark_inode_dirty(old_dir);
+ if (new_inode) {
+ new_inode->i_nlink--;
+ new_inode->i_ctime = CURRENT_TIME_SEC;
+ mark_inode_dirty(new_inode);
+ }
+ mark_buffer_dirty(old_bh);
+ error = 0;
+
+end_rename:
+ unlock_kernel();
+ brelse(old_bh);
+ brelse(new_bh);
+ return error;
+}
+
+struct inode_operations bfs_dir_inops = {
+ .create = bfs_create,
+ .lookup = bfs_lookup,
+ .link = bfs_link,
+ .unlink = bfs_unlink,
+ .rename = bfs_rename,
+};
+
+static int bfs_add_entry(struct inode * dir, const char * name, int namelen, int ino)
+{
+ struct buffer_head * bh;
+ struct bfs_dirent * de;
+ int block, sblock, eblock, off, eoff;
+ int i;
+
+ dprintf("name=%s, namelen=%d\n", name, namelen);
+
+ if (!namelen)
+ return -ENOENT;
+ if (namelen > BFS_NAMELEN)
+ return -ENAMETOOLONG;
+
+ sblock = BFS_I(dir)->i_sblock;
+ eblock = BFS_I(dir)->i_eblock;
+ eoff = dir->i_size % BFS_BSIZE;
+ for (block=sblock; block<=eblock; block++) {
+ bh = sb_bread(dir->i_sb, block);
+ if(!bh)
+ return -ENOSPC;
+ for (off=0; off<BFS_BSIZE; off+=BFS_DIRENT_SIZE) {
+ de = (struct bfs_dirent *)(bh->b_data + off);
+ if (block==eblock && off>=eoff) {
+ /* Do not read/interpret the garbage in the end of eblock. */
+ de->ino = 0;
+ }
+ if (!de->ino) {
+ if ((block-sblock)*BFS_BSIZE + off >= dir->i_size) {
+ dir->i_size += BFS_DIRENT_SIZE;
+ dir->i_ctime = CURRENT_TIME_SEC;
+ }
+ dir->i_mtime = CURRENT_TIME_SEC;
+ mark_inode_dirty(dir);
+ de->ino = ino;
+ for (i=0; i<BFS_NAMELEN; i++)
+ de->name[i] = (i < namelen) ? name[i] : 0;
+ mark_buffer_dirty(bh);
+ brelse(bh);
+ return 0;
+ }
+ }
+ brelse(bh);
+ }
+ return -ENOSPC;
+}
+
+static inline int bfs_namecmp(int len, const char * name, const char * buffer)
+{
+ if (len < BFS_NAMELEN && buffer[len])
+ return 0;
+ return !memcmp(name, buffer, len);
+}
+
+static struct buffer_head * bfs_find_entry(struct inode * dir,
+ const char * name, int namelen, struct bfs_dirent ** res_dir)
+{
+ unsigned long block, offset;
+ struct buffer_head * bh;
+ struct bfs_dirent * de;
+
+ *res_dir = NULL;
+ if (namelen > BFS_NAMELEN)
+ return NULL;
+ bh = NULL;
+ block = offset = 0;
+ while (block * BFS_BSIZE + offset < dir->i_size) {
+ if (!bh) {
+ bh = sb_bread(dir->i_sb, BFS_I(dir)->i_sblock + block);
+ if (!bh) {
+ block++;
+ continue;
+ }
+ }
+ de = (struct bfs_dirent *)(bh->b_data + offset);
+ offset += BFS_DIRENT_SIZE;
+ if (de->ino && bfs_namecmp(namelen, name, de->name)) {
+ *res_dir = de;
+ return bh;
+ }
+ if (offset < bh->b_size)
+ continue;
+ brelse(bh);
+ bh = NULL;
+ offset = 0;
+ block++;
+ }
+ brelse(bh);
+ return NULL;
+}
diff --git a/fs/bfs/file.c b/fs/bfs/file.c
new file mode 100644
index 000000000000..747fd1ea55e0
--- /dev/null
+++ b/fs/bfs/file.c
@@ -0,0 +1,162 @@
+/*
+ * fs/bfs/file.c
+ * BFS file operations.
+ * Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com>
+ */
+
+#include <linux/fs.h>
+#include <linux/buffer_head.h>
+#include <linux/smp_lock.h>
+#include "bfs.h"
+
+#undef DEBUG
+
+#ifdef DEBUG
+#define dprintf(x...) printf(x)
+#else
+#define dprintf(x...)
+#endif
+
+struct file_operations bfs_file_operations = {
+ .llseek = generic_file_llseek,
+ .read = generic_file_read,
+ .write = generic_file_write,
+ .mmap = generic_file_mmap,
+ .sendfile = generic_file_sendfile,
+};
+
+static int bfs_move_block(unsigned long from, unsigned long to, struct super_block *sb)
+{
+ struct buffer_head *bh, *new;
+
+ bh = sb_bread(sb, from);
+ if (!bh)
+ return -EIO;
+ new = sb_getblk(sb, to);
+ memcpy(new->b_data, bh->b_data, bh->b_size);
+ mark_buffer_dirty(new);
+ bforget(bh);
+ brelse(new);
+ return 0;
+}
+
+static int bfs_move_blocks(struct super_block *sb, unsigned long start, unsigned long end,
+ unsigned long where)
+{
+ unsigned long i;
+
+ dprintf("%08lx-%08lx->%08lx\n", start, end, where);
+ for (i = start; i <= end; i++)
+ if(bfs_move_block(i, where + i, sb)) {
+ dprintf("failed to move block %08lx -> %08lx\n", i, where + i);
+ return -EIO;
+ }
+ return 0;
+}
+
+static int bfs_get_block(struct inode * inode, sector_t block,
+ struct buffer_head * bh_result, int create)
+{
+ long phys;
+ int err;
+ struct super_block *sb = inode->i_sb;
+ struct bfs_sb_info *info = BFS_SB(sb);
+ struct bfs_inode_info *bi = BFS_I(inode);
+ struct buffer_head *sbh = info->si_sbh;
+
+ if (block < 0 || block > info->si_blocks)
+ return -EIO;
+
+ phys = bi->i_sblock + block;
+ if (!create) {
+ if (phys <= bi->i_eblock) {
+ dprintf("c=%d, b=%08lx, phys=%08lx (granted)\n", create, block, phys);
+ map_bh(bh_result, sb, phys);
+ }
+ return 0;
+ }
+
+ /* if the file is not empty and the requested block is within the range
+ of blocks allocated for this file, we can grant it */
+ if (inode->i_size && phys <= bi->i_eblock) {
+ dprintf("c=%d, b=%08lx, phys=%08lx (interim block granted)\n",
+ create, block, phys);
+ map_bh(bh_result, sb, phys);
+ return 0;
+ }
+
+ /* the rest has to be protected against itself */
+ lock_kernel();
+
+ /* if the last data block for this file is the last allocated block, we can
+ extend the file trivially, without moving it anywhere */
+ if (bi->i_eblock == info->si_lf_eblk) {
+ dprintf("c=%d, b=%08lx, phys=%08lx (simple extension)\n",
+ create, block, phys);
+ map_bh(bh_result, sb, phys);
+ info->si_freeb -= phys - bi->i_eblock;
+ info->si_lf_eblk = bi->i_eblock = phys;
+ mark_inode_dirty(inode);
+ mark_buffer_dirty(sbh);
+ err = 0;
+ goto out;
+ }
+
+ /* Ok, we have to move this entire file to the next free block */
+ phys = info->si_lf_eblk + 1;
+ if (bi->i_sblock) { /* if data starts on block 0 then there is no data */
+ err = bfs_move_blocks(inode->i_sb, bi->i_sblock,
+ bi->i_eblock, phys);
+ if (err) {
+ dprintf("failed to move ino=%08lx -> fs corruption\n", inode->i_ino);
+ goto out;
+ }
+ } else
+ err = 0;
+
+ dprintf("c=%d, b=%08lx, phys=%08lx (moved)\n", create, block, phys);
+ bi->i_sblock = phys;
+ phys += block;
+ info->si_lf_eblk = bi->i_eblock = phys;
+
+ /* this assumes nothing can write the inode back while we are here
+ * and thus update inode->i_blocks! (XXX)*/
+ info->si_freeb -= bi->i_eblock - bi->i_sblock + 1 - inode->i_blocks;
+ mark_inode_dirty(inode);
+ mark_buffer_dirty(sbh);
+ map_bh(bh_result, sb, phys);
+out:
+ unlock_kernel();
+ return err;
+}
+
+static int bfs_writepage(struct page *page, struct writeback_control *wbc)
+{
+ return block_write_full_page(page, bfs_get_block, wbc);
+}
+
+static int bfs_readpage(struct file *file, struct page *page)
+{
+ return block_read_full_page(page, bfs_get_block);
+}
+
+static int bfs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
+{
+ return block_prepare_write(page, from, to, bfs_get_block);
+}
+
+static sector_t bfs_bmap(struct address_space *mapping, sector_t block)
+{
+ return generic_block_bmap(mapping, block, bfs_get_block);
+}
+
+struct address_space_operations bfs_aops = {
+ .readpage = bfs_readpage,
+ .writepage = bfs_writepage,
+ .sync_page = block_sync_page,
+ .prepare_write = bfs_prepare_write,
+ .commit_write = generic_commit_write,
+ .bmap = bfs_bmap,
+};
+
+struct inode_operations bfs_file_inops;
diff --git a/fs/bfs/inode.c b/fs/bfs/inode.c
new file mode 100644
index 000000000000..64e0fb33fc0c
--- /dev/null
+++ b/fs/bfs/inode.c
@@ -0,0 +1,420 @@
+/*
+ * fs/bfs/inode.c
+ * BFS superblock and inode operations.
+ * Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com>
+ * From fs/minix, Copyright (C) 1991, 1992 Linus Torvalds.
+ */
+
+#include <linux/module.h>
+#include <linux/mm.h>
+#include <linux/slab.h>
+#include <linux/init.h>
+#include <linux/fs.h>
+#include <linux/smp_lock.h>
+#include <linux/buffer_head.h>
+#include <linux/vfs.h>
+#include <asm/uaccess.h>
+#include "bfs.h"
+
+MODULE_AUTHOR("Tigran A. Aivazian <tigran@veritas.com>");
+MODULE_DESCRIPTION("SCO UnixWare BFS filesystem for Linux");
+MODULE_LICENSE("GPL");
+
+#undef DEBUG
+
+#ifdef DEBUG
+#define dprintf(x...) printf(x)
+#else
+#define dprintf(x...)
+#endif
+
+void dump_imap(const char *prefix, struct super_block * s);
+
+static void bfs_read_inode(struct inode * inode)
+{
+ unsigned long ino = inode->i_ino;
+ struct bfs_inode * di;
+ struct buffer_head * bh;
+ int block, off;
+
+ if (ino < BFS_ROOT_INO || ino > BFS_SB(inode->i_sb)->si_lasti) {
+ printf("Bad inode number %s:%08lx\n", inode->i_sb->s_id, ino);
+ make_bad_inode(inode);
+ return;
+ }
+
+ block = (ino - BFS_ROOT_INO)/BFS_INODES_PER_BLOCK + 1;
+ bh = sb_bread(inode->i_sb, block);
+ if (!bh) {
+ printf("Unable to read inode %s:%08lx\n", inode->i_sb->s_id, ino);
+ make_bad_inode(inode);
+ return;
+ }
+
+ off = (ino - BFS_ROOT_INO) % BFS_INODES_PER_BLOCK;
+ di = (struct bfs_inode *)bh->b_data + off;
+
+ inode->i_mode = 0x0000FFFF & di->i_mode;
+ if (di->i_vtype == BFS_VDIR) {
+ inode->i_mode |= S_IFDIR;
+ inode->i_op = &bfs_dir_inops;
+ inode->i_fop = &bfs_dir_operations;
+ } else if (di->i_vtype == BFS_VREG) {
+ inode->i_mode |= S_IFREG;
+ inode->i_op = &bfs_file_inops;
+ inode->i_fop = &bfs_file_operations;
+ inode->i_mapping->a_ops = &bfs_aops;
+ }
+
+ inode->i_uid = di->i_uid;
+ inode->i_gid = di->i_gid;
+ inode->i_nlink = di->i_nlink;
+ inode->i_size = BFS_FILESIZE(di);
+ inode->i_blocks = BFS_FILEBLOCKS(di);
+ inode->i_blksize = PAGE_SIZE;
+ inode->i_atime.tv_sec = di->i_atime;
+ inode->i_mtime.tv_sec = di->i_mtime;
+ inode->i_ctime.tv_sec = di->i_ctime;
+ inode->i_atime.tv_nsec = 0;
+ inode->i_mtime.tv_nsec = 0;
+ inode->i_ctime.tv_nsec = 0;
+ BFS_I(inode)->i_dsk_ino = di->i_ino; /* can be 0 so we store a copy */
+ BFS_I(inode)->i_sblock = di->i_sblock;
+ BFS_I(inode)->i_eblock = di->i_eblock;
+
+ brelse(bh);
+}
+
+static int bfs_write_inode(struct inode * inode, int unused)
+{
+ unsigned long ino = inode->i_ino;
+ struct bfs_inode * di;
+ struct buffer_head * bh;
+ int block, off;
+
+ if (ino < BFS_ROOT_INO || ino > BFS_SB(inode->i_sb)->si_lasti) {
+ printf("Bad inode number %s:%08lx\n", inode->i_sb->s_id, ino);
+ return -EIO;
+ }
+
+ lock_kernel();
+ block = (ino - BFS_ROOT_INO)/BFS_INODES_PER_BLOCK + 1;
+ bh = sb_bread(inode->i_sb, block);
+ if (!bh) {
+ printf("Unable to read inode %s:%08lx\n", inode->i_sb->s_id, ino);
+ unlock_kernel();
+ return -EIO;
+ }
+
+ off = (ino - BFS_ROOT_INO)%BFS_INODES_PER_BLOCK;
+ di = (struct bfs_inode *)bh->b_data + off;
+
+ if (inode->i_ino == BFS_ROOT_INO)
+ di->i_vtype = BFS_VDIR;
+ else
+ di->i_vtype = BFS_VREG;
+
+ di->i_ino = inode->i_ino;
+ di->i_mode = inode->i_mode;
+ di->i_uid = inode->i_uid;
+ di->i_gid = inode->i_gid;
+ di->i_nlink = inode->i_nlink;
+ di->i_atime = inode->i_atime.tv_sec;
+ di->i_mtime = inode->i_mtime.tv_sec;
+ di->i_ctime = inode->i_ctime.tv_sec;
+ di->i_sblock = BFS_I(inode)->i_sblock;
+ di->i_eblock = BFS_I(inode)->i_eblock;
+ di->i_eoffset = di->i_sblock * BFS_BSIZE + inode->i_size - 1;
+
+ mark_buffer_dirty(bh);
+ brelse(bh);
+ unlock_kernel();
+ return 0;
+}
+
+static void bfs_delete_inode(struct inode * inode)
+{
+ unsigned long ino = inode->i_ino;
+ struct bfs_inode * di;
+ struct buffer_head * bh;
+ int block, off;
+ struct super_block * s = inode->i_sb;
+ struct bfs_sb_info * info = BFS_SB(s);
+
+ dprintf("ino=%08lx\n", inode->i_ino);
+
+ if (inode->i_ino < BFS_ROOT_INO || inode->i_ino > info->si_lasti) {
+ printf("invalid ino=%08lx\n", inode->i_ino);
+ return;
+ }
+
+ inode->i_size = 0;
+ inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
+ lock_kernel();
+ mark_inode_dirty(inode);
+ block = (ino - BFS_ROOT_INO)/BFS_INODES_PER_BLOCK + 1;
+ bh = sb_bread(s, block);
+ if (!bh) {
+ printf("Unable to read inode %s:%08lx\n", inode->i_sb->s_id, ino);
+ unlock_kernel();
+ return;
+ }
+ off = (ino - BFS_ROOT_INO)%BFS_INODES_PER_BLOCK;
+ di = (struct bfs_inode *)bh->b_data + off;
+ if (di->i_ino) {
+ info->si_freeb += BFS_FILEBLOCKS(di);
+ info->si_freei++;
+ clear_bit(di->i_ino, info->si_imap);
+ dump_imap("delete_inode", s);
+ }
+ di->i_ino = 0;
+ di->i_sblock = 0;
+ mark_buffer_dirty(bh);
+ brelse(bh);
+
+ /* if this was the last file, make the previous
+ block "last files last block" even if there is no real file there,
+ saves us 1 gap */
+ if (info->si_lf_eblk == BFS_I(inode)->i_eblock) {
+ info->si_lf_eblk = BFS_I(inode)->i_sblock - 1;
+ mark_buffer_dirty(info->si_sbh);
+ }
+ unlock_kernel();
+ clear_inode(inode);
+}
+
+static void bfs_put_super(struct super_block *s)
+{
+ struct bfs_sb_info *info = BFS_SB(s);
+ brelse(info->si_sbh);
+ kfree(info->si_imap);
+ kfree(info);
+ s->s_fs_info = NULL;
+}
+
+static int bfs_statfs(struct super_block *s, struct kstatfs *buf)
+{
+ struct bfs_sb_info *info = BFS_SB(s);
+ u64 id = huge_encode_dev(s->s_bdev->bd_dev);
+ buf->f_type = BFS_MAGIC;
+ buf->f_bsize = s->s_blocksize;
+ buf->f_blocks = info->si_blocks;
+ buf->f_bfree = buf->f_bavail = info->si_freeb;
+ buf->f_files = info->si_lasti + 1 - BFS_ROOT_INO;
+ buf->f_ffree = info->si_freei;
+ buf->f_fsid.val[0] = (u32)id;
+ buf->f_fsid.val[1] = (u32)(id >> 32);
+ buf->f_namelen = BFS_NAMELEN;
+ return 0;
+}
+
+static void bfs_write_super(struct super_block *s)
+{
+ lock_kernel();
+ if (!(s->s_flags & MS_RDONLY))
+ mark_buffer_dirty(BFS_SB(s)->si_sbh);
+ s->s_dirt = 0;
+ unlock_kernel();
+}
+
+static kmem_cache_t * bfs_inode_cachep;
+
+static struct inode *bfs_alloc_inode(struct super_block *sb)
+{
+ struct bfs_inode_info *bi;
+ bi = kmem_cache_alloc(bfs_inode_cachep, SLAB_KERNEL);
+ if (!bi)
+ return NULL;
+ return &bi->vfs_inode;
+}
+
+static void bfs_destroy_inode(struct inode *inode)
+{
+ kmem_cache_free(bfs_inode_cachep, BFS_I(inode));
+}
+
+static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
+{
+ struct bfs_inode_info *bi = foo;
+
+ if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
+ SLAB_CTOR_CONSTRUCTOR)
+ inode_init_once(&bi->vfs_inode);
+}
+
+static int init_inodecache(void)
+{
+ bfs_inode_cachep = kmem_cache_create("bfs_inode_cache",
+ sizeof(struct bfs_inode_info),
+ 0, SLAB_RECLAIM_ACCOUNT,
+ init_once, NULL);
+ if (bfs_inode_cachep == NULL)
+ return -ENOMEM;
+ return 0;
+}
+
+static void destroy_inodecache(void)
+{
+ if (kmem_cache_destroy(bfs_inode_cachep))
+ printk(KERN_INFO "bfs_inode_cache: not all structures were freed\n");
+}
+
+static struct super_operations bfs_sops = {
+ .alloc_inode = bfs_alloc_inode,
+ .destroy_inode = bfs_destroy_inode,
+ .read_inode = bfs_read_inode,
+ .write_inode = bfs_write_inode,
+ .delete_inode = bfs_delete_inode,
+ .put_super = bfs_put_super,
+ .write_super = bfs_write_super,
+ .statfs = bfs_statfs,
+};
+
+void dump_imap(const char *prefix, struct super_block * s)
+{
+#if 0
+ int i;
+ char *tmpbuf = (char *)get_zeroed_page(GFP_KERNEL);
+
+ if (!tmpbuf)
+ return;
+ for (i=BFS_SB(s)->si_lasti; i>=0; i--) {
+ if (i>PAGE_SIZE-100) break;
+ if (test_bit(i, BFS_SB(s)->si_imap))
+ strcat(tmpbuf, "1");
+ else
+ strcat(tmpbuf, "0");
+ }
+ printk(KERN_ERR "BFS-fs: %s: lasti=%08lx <%s>\n", prefix, BFS_SB(s)->si_lasti, tmpbuf);
+ free_page((unsigned long)tmpbuf);
+#endif
+}
+
+static int bfs_fill_super(struct super_block *s, void *data, int silent)
+{
+ struct buffer_head * bh;
+ struct bfs_super_block * bfs_sb;
+ struct inode * inode;
+ int i, imap_len;
+ struct bfs_sb_info * info;
+
+ info = kmalloc(sizeof(*info), GFP_KERNEL);
+ if (!info)
+ return -ENOMEM;
+ s->s_fs_info = info;
+ memset(info, 0, sizeof(*info));
+
+ sb_set_blocksize(s, BFS_BSIZE);
+
+ bh = sb_bread(s, 0);
+ if(!bh)
+ goto out;
+ bfs_sb = (struct bfs_super_block *)bh->b_data;
+ if (bfs_sb->s_magic != BFS_MAGIC) {
+ if (!silent)
+ printf("No BFS filesystem on %s (magic=%08x)\n",
+ s->s_id, bfs_sb->s_magic);
+ goto out;
+ }
+ if (BFS_UNCLEAN(bfs_sb, s) && !silent)
+ printf("%s is unclean, continuing\n", s->s_id);
+
+ s->s_magic = BFS_MAGIC;
+ info->si_bfs_sb = bfs_sb;
+ info->si_sbh = bh;
+ info->si_lasti = (bfs_sb->s_start - BFS_BSIZE)/sizeof(struct bfs_inode)
+ + BFS_ROOT_INO - 1;
+
+ imap_len = info->si_lasti/8 + 1;
+ info->si_imap = kmalloc(imap_len, GFP_KERNEL);
+ if (!info->si_imap)
+ goto out;
+ memset(info->si_imap, 0, imap_len);
+ for (i=0; i<BFS_ROOT_INO; i++)
+ set_bit(i, info->si_imap);
+
+ s->s_op = &bfs_sops;
+ inode = iget(s, BFS_ROOT_INO);
+ if (!inode) {
+ kfree(info->si_imap);
+ goto out;
+ }
+ s->s_root = d_alloc_root(inode);
+ if (!s->s_root) {
+ iput(inode);
+ kfree(info->si_imap);
+ goto out;
+ }
+
+ info->si_blocks = (bfs_sb->s_end + 1)>>BFS_BSIZE_BITS; /* for statfs(2) */
+ info->si_freeb = (bfs_sb->s_end + 1 - bfs_sb->s_start)>>BFS_BSIZE_BITS;
+ info->si_freei = 0;
+ info->si_lf_eblk = 0;
+ info->si_lf_sblk = 0;
+ info->si_lf_ioff = 0;
+ for (i=BFS_ROOT_INO; i<=info->si_lasti; i++) {
+ inode = iget(s,i);
+ if (BFS_I(inode)->i_dsk_ino == 0)
+ info->si_freei++;
+ else {
+ set_bit(i, info->si_imap);
+ info->si_freeb -= inode->i_blocks;
+ if (BFS_I(inode)->i_eblock > info->si_lf_eblk) {
+ info->si_lf_eblk = BFS_I(inode)->i_eblock;
+ info->si_lf_sblk = BFS_I(inode)->i_sblock;
+ info->si_lf_ioff = BFS_INO2OFF(i);
+ }
+ }
+ iput(inode);
+ }
+ if (!(s->s_flags & MS_RDONLY)) {
+ mark_buffer_dirty(bh);
+ s->s_dirt = 1;
+ }
+ dump_imap("read_super", s);
+ return 0;
+
+out:
+ brelse(bh);
+ kfree(info);
+ s->s_fs_info = NULL;
+ return -EINVAL;
+}
+
+static struct super_block *bfs_get_sb(struct file_system_type *fs_type,
+ int flags, const char *dev_name, void *data)
+{
+ return get_sb_bdev(fs_type, flags, dev_name, data, bfs_fill_super);
+}
+
+static struct file_system_type bfs_fs_type = {
+ .owner = THIS_MODULE,
+ .name = "bfs",
+ .get_sb = bfs_get_sb,
+ .kill_sb = kill_block_super,
+ .fs_flags = FS_REQUIRES_DEV,
+};
+
+static int __init init_bfs_fs(void)
+{
+ int err = init_inodecache();
+ if (err)
+ goto out1;
+ err = register_filesystem(&bfs_fs_type);
+ if (err)
+ goto out;
+ return 0;
+out:
+ destroy_inodecache();
+out1:
+ return err;
+}
+
+static void __exit exit_bfs_fs(void)
+{
+ unregister_filesystem(&bfs_fs_type);
+ destroy_inodecache();
+}
+
+module_init(init_bfs_fs)
+module_exit(exit_bfs_fs)