#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/pagemap.h> /* PAGE_CACHE_SIZE */
#include <linux/fs.h> /* This is where libfs stuff is declared */
#include <asm/atomic.h>
#include <asm/uaccess.h> /* copy_to_user */
#include <linux/pagemap.h>
#include <linux/buffer_head.h>
/*
* Wen Hui.
* Just for linux 2.6
*
* desp:
* innux 3.2, get_sb --> mount,
* get_sb_single --> mount_single.
*/
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Wen Hui");
#define XORFS_MAGIC 0x20120418
#define FILE_INODE_NUMBER 2
/**
* system_file_type
*/
/* get_sb */
static struct super_block *
xorfs_get_sb(struct file_system_type *fs_type, int flags, const char *devname, void *data, struct vfsmount* mnt);
/* kill_sb */
static void
xorfs_kill_sb(struct super_block *super);
static int
xorfs_fill_super(struct super_block* sb, void *data, int silent);
/**
* super_operations
*/
static int
xorfs_super_write_inode(struct inode *inode, struct writeback_control *wbc);
/*static void
xorfs_super_read_inode(struct inode *inode);*/
/**
* inode_operations
*/
/* lookup */
static struct dentry*
xorfs_inode_lookup(struct inode*, struct dentry *, struct nameidata *);
static struct inode*
xorfs_iget(struct super_block *sp, unsigned long ino);
/**
* file_operations
*/
static int
xorfs_file_open(struct inode *inode, struct file *file);
static int
xorfs_file_readdir(struct file *file, void *dirent, filldir_t filldir);
static int
xorfs_file_release(struct inode* ino, struct file *file);
static ssize_t
xorfs_file_read(struct file *file, char *buf, size_t max, loff_t* offset);
static ssize_t
xorfs_file_write(struct file *file, const char *buf, size_t max, loff_t* offset);
/**
* address_space_operations
*/
/* readpage
* old version: ->prepare_write(),->commit_write(),
* ->sync_page(),and ->readpage()
* new version (LSF'08'): try use vm_operations
* instead of address_space_operations,
* and a small/dummpy ->readpage is still needed because
* ->generic_file_mmap, still check
* for the existence of the ->readpage method.
*/
static int
xorfs_readpage(struct file *file, struct page *page);
/* write page */
static int
xorfs_writepage(struct page *page, struct writeback_control *wbc);
/* write_beign */
static int
xorfs_write_begin(struct file *file, struct address_space *mapping, loff_t pos, unsigned len, unsigned flags, struct page **pagep, void **fsdata);
/* write_end */
static int
xorfs_write_end(struct file *file, struct address_space *mapping, loff_t pos, unsigned len, unsigned copied, struct page *page, void *fsdata);
/*
* Data declarations
*/
static struct super_operations xorfs_sops =
{
// .read_inode = xorfs_super_read_inode
.statfs = simple_statfs,
.write_inode = &xorfs_super_write_inode
}; // struct xorfs_sops
static struct inode_operations xorfs_iops =
{
.lookup = xorfs_inode_lookup
}; // struct xorfs_iops
static struct file_operations xorfs_fops =
{
.open = xorfs_file_open,
.read = &xorfs_file_read,
.readdir = &xorfs_file_readdir,
.write = &xorfs_file_write,
.release = &xorfs_file_release,
.fsync = &generic_file_fsync
}; // struct xorfs_fops
static struct file_system_type xorfs = {
name : "xorfs",
get_sb : xorfs_get_sb,
kill_sb : xorfs_kill_sb,
owner : THIS_MODULE
}; // struct file_system_type
static struct address_space_operations xorfs_aops =
{
.readpage = xorfs_readpage,
.writepage = xorfs_writepage,
.write_begin = xorfs_write_begin,
.write_end = xorfs_write_end
}; // struct xorfs_aops
static struct inode *xorfs_root_inode;
static char file_buf[PAGE_SIZE] = "Hello World\n";
static int file_size = 12;
/**
* system_file_type
*/
static struct super_block *
xorfs_get_sb(struct file_system_type *fs_type,
int flags, const char *devname, void *data, struct vfsmount* mnt)
{
printk("XORFS:xorfs_get_sb\n");
return get_sb_single(fs_type, flags, data, &xorfs_fill_super, mnt);
} // xorfs_get_sb
static void
xorfs_kill_sb(struct super_block *super)
{
printk("XORFS: xorfs_kill_sb\n");
kill_anon_super(super);
} // xorfs_kill_sb
/* call the get_sb_single(), and callback the fn() */
static int
xorfs_fill_super(struct super_block* sb, void *data, int silent)
{
printk("XORFS: xorfs_fill_super\n");
sb->s_blocksize = 1024;
sb->s_blocksize_bits = 10;
sb->s_magic = XORFS_MAGIC;
sb->s_op = &xorfs_sops; // super block operation
sb->s_type = &xorfs; // file_system_type
// xorfs_root_inode = iget_locked(sb, 1); // allocate 1 node
xorfs_root_inode = xorfs_iget(sb, 1); // allocate 1 node
xorfs_root_inode->i_op = &xorfs_iops; // set the inode ops
xorfs_root_inode->i_mode = S_IFDIR | S_IRWXU;
xorfs_root_inode->i_fop = &xorfs_fops; // set the inode file operations
// xorfs_root_inode->i_mapping->a_ops = &xorfs_aops;
if(!(sb->s_root = d_alloc_root(xorfs_root_inode)))
{
iput( xorfs_root_inode );
return -ENOMEM;
} // if
return 0;
} // xorfs_fill_super
/**
* super_operations
*/
static int
xorfs_super_write_inode(struct inode *inode, struct writeback_control *wbc)
{
printk("XORFS: xorfs_super_write_inode (i_ino=%d) = %d\n",
(int) inode->i_ino,
(int) i_size_read(inode));
if(inode->i_ino == FILE_INODE_NUMBER)
{
file_size = i_size_read(inode);
}
return 0;
} // xorfs_super_write_inode
/**
static void*
xorfs_super_read_inode(struct super_block *sp, struct inode *inode)
{
inode->i_mapping->a_ops = &xorfs_aops;
} // xorfs_super_read_inode
*/
/**
* inode_operations
*/
static char filename[] = "hello.txt";
static int filename_len = sizeof(filename) - 1;
static struct dentry*
xorfs_inode_lookup(struct inode* parent_inode, struct dentry *dentry, struct nameidata *nameidata)
{
struct inode *file_inode;
printk("XORFS: xorfs_inode_lookup\n");
if(parent_inode->i_ino != xorfs_root_inode->i_ino ||
dentry->d_name.len != filename_len ||
strncmp(dentry->d_name.name, filename, dentry->d_name.len))
{
d_add(dentry, NULL);
return NULL;
} // if
file_inode = xorfs_iget(parent_inode->i_sb, FILE_INODE_NUMBER);
if(!file_inode)
return ERR_PTR(-EACCES);
file_inode->i_size = file_size;
file_inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
file_inode->i_fop = &xorfs_fops;
d_add(dentry, file_inode);
return NULL;
} // xorfs_inode_lookup
static struct inode*
xorfs_iget(struct super_block *sb, unsigned long ino)
{
struct inode *inode;
int ret;
printk("XORFS: xorfs_iget\n");
inode = iget_locked(sb, ino);
if(!inode)
return ERR_PTR(-ENOMEM);
if(!(inode->i_state & I_NEW))
return inode;
inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
inode->i_mapping->a_ops = &xorfs_aops;
unlock_new_inode(inode);
return inode;
} // xorfs_iget
/**
* file_operations
*/
static int
xorfs_file_open(struct inode *inode, struct file *file)
{
printk("XORFS: @xorfs_file_open max_readahead = %d (size = %d)\n",
(int) file->f_ra.ra_pages, file_size);
file->f_ra.ra_pages = 0; /* No read-ahead */
return generic_file_open(inode, file);
} // xorfs_file_open
static ssize_t
xorfs_file_write(struct file *file, const char *buf, size_t maxlen, loff_t* offset)
{
int count;
if(*offset > 0)
{
printk("XORFS: @xorfs_file_write Positive offset %d\n", *offset);
return 0;
} // if
count = maxlen > sizeof(file_buf) ? sizeof(file_buf) : maxlen;
//__generic_copy_from_user(file_buf, buf, count);
copy_from_user(file_buf, buf, maxlen);
printk("XORFS: xorfs_file_write called with maxlen=%d, offset=%d\n", maxlen, *offset);
*offset += count;
if(*offset > file_size)
file_size = *offset;
return count;
} // xorfs_file_write
static ssize_t
xorfs_file_read(struct file *file, char *buf, size_t max, loff_t* offset){
int i;
int buflen;
if(*offset > 0)
return 0;
printk("XORFS: xorfs_file_read called [%d] [%d]\n", max, *offset);
buflen = (file_size > max) ? max : file_size;
copy_to_user(buf, file_buf, buflen);
*offset += buflen;
return buflen;
} // xorfs_file_read
static int
xorfs_file_readdir(struct file *f
Linux下一个简单的文件系统实现
5星 · 超过95%的资源 需积分: 45 130 浏览量
2012-04-18
10:21:36
上传
评论 1
收藏 3KB RAR 举报
软件真理与光
- 粉丝: 2w+
- 资源: 18
最新资源
- 修复windows系统库软件
- OpenCv 使用fffffffff
- 正点原子开拓者FPGA多人表决器代码项目
- EOP-Last5Years.txt
- windows 32位、64位系统常见缺少的库
- 毕业设计基于springboot+vue实现的求职招聘类型网站源码+数据库(高分项目).zip
- 535springboot + vue 体质测试数据分析及可视化设计.zip(可运行源码+数据库文件+文档)
- python毕业设计-基于Django+OpenCV的二维码生成与识别系统源码.zip
- 基于springboot+vue实现的求职招聘类型网站源代码+数据库(优质毕设项目).zip
- iOS APP提审checklist
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
前往页