Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1746693
  • 博文数量: 141
  • 博客积分: 3552
  • 博客等级: 中校
  • 技术积分: 4101
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-15 14:39
个人简介

熟悉Linux下程序设计及各种应用程序 熟悉C Language 熟悉Glusterfs、FFmpeg、CDN 系统设计,计算机图形系统设计、分布式程序设计 目前主要研究方向:流媒体

文章分类

全部博文(141)

分类: LINUX

2010-01-18 10:49:17

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/major.h>
#include <linux/vfs.h>


#define ROMFS_MAGIC 0xEBAF3421

static struct kmem_cache *tbagfs_inode_cachep;

struct tbagfs_inode_info {
    struct inode vfs_inode;
    unsigned long i_metasize; /* size of non-data area */
    unsigned long i_dataoffset; /* from the start of fs */
};



/*
 * * fill in the superblock
 * */

static int tbagfs_fill_super(struct super_block *sb, void *data, int silent)
{
    return 0;
}

/*
 * * get a superblock for mounting
 * */

static int tbagfs_get_sb(struct file_system_type *fs_type,
        int flags, const char *dev_name,
        void *data, struct vfsmount *mnt)
{
    int ret = -EINVAL;

    if (ret == -EINVAL)
        ret = get_sb_bdev(fs_type, flags, dev_name, data,
                tbagfs_fill_super, mnt);
    return ret;
}

static struct file_system_type tbagfs_fs_type = {
    .owner = THIS_MODULE,
    .name = "tbagfs",
    .get_sb = tbagfs_get_sb,
    .kill_sb = kill_block_super,
    .fs_flags = FS_REQUIRES_DEV,
};

static void tbagfs_i_init_once(void *_inode)
{
    struct tbagfs_inode_info *inode = _inode;

    inode_init_once(&inode->vfs_inode);
}


static int __init init_tbagfs_fs(void)
{
    int ret = 0;
    tbagfs_inode_cachep =
        kmem_cache_create("tbagfs", sizeof(struct tbagfs_inode_info), 0,
                SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
                tbagfs_i_init_once);

    if(!tbagfs_inode_cachep){
        printk(KERN_ERR "TBAGFS error: Failed to initialise inode cache\n");
        return -ENOMEM;
    }

    ret = register_filesystem(&tbagfs_fs_type);
    if(ret){
        printk(KERN_ERR "TBAGFS error: Failed to register filesystem\n");
        goto error_register;
    }
    return 0;

error_register:
    kmem_cache_destroy(tbagfs_inode_cachep);
    return ret;
}

static void __exit exit_tbagfs_fs(void)
{
    unregister_filesystem(&tbagfs_fs_type);
    kmem_cache_destroy(tbagfs_inode_cachep);
}

module_init(init_tbagfs_fs)
module_exit(exit_tbagfs_fs)
MODULE_LICENSE("GPL");



KERNELDIR = /lib/modules/`uname -r`/build/
PWD := $(shell pwd)
obj-m := super.o

modules:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
    rm -rf *.o *.ko test_chrdev Module.* module* *.mod.c



[liuqi@Tbag Tbagfs]$ sudo make
[sudo] password for liuqi:
make -C /lib/modules/`uname -r`/build/ M=/home/liuqi/drivercode/fs/Tbagfs modules
make[1]: Entering directory `/opt/linux-2.6'
  CC [M] /home/liuqi/drivercode/fs/Tbagfs/super.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC /home/liuqi/drivercode/fs/Tbagfs/super.mod.o
  LD [M] /home/liuqi/drivercode/fs/Tbagfs/super.ko
make[1]: Leaving directory `/opt/linux-2.6'

[liuqi@Tbag Tbagfs]$ sudo insmod super.ko
[liuqi@Tbag Tbagfs]$ sudo lsmod | grep super
super 1464 0
[liuqi@Tbag Tbagfs]$



[liuqi@Tbag Tbagfs]$ cat /proc/filesystems
nodev    sysfs
nodev    rootfs
nodev    bdev
nodev    proc
nodev    cgroup
nodev    cpuset
nodev    tmpfs
nodev    binfmt_misc
nodev    debugfs
nodev    securityfs
nodev    sockfs
nodev    usbfs
nodev    pipefs
nodev    anon_inodefs
nodev    rpc_pipefs
nodev    inotifyfs
nodev    configfs
nodev    devpts
    reiserfs
    ext3
    ext2
    ext4
    cramfs
    squashfs
nodev    ramfs
nodev    hugetlbfs
    minix
    vfat
    msdos
    iso9660
nodev    nfs
nodev    nfs4
nodev    nfsd
nodev    smbfs
nodev    cifs
    ntfs
    romfs
nodev    autofs
nodev    fuse
    fuseblk
nodev    fusectl
    jfs
    xfs
    nilfs2
    ocfs2
nodev    ocfs2_dlmfs
    btrfs
    gfs2
    gfs2meta
nodev    mqueue
    tbagfs
[liuqi@Tbag Tbagfs]$


简单的实现了一下,原理就在register_filesystem,先找到文件系统的list,然后find一下,没有找到
static struct file_system_type tbagfs_fs_type = {
    .owner = THIS_MODULE,
    .name = "tbagfs",
    .get_sb = tbagfs_get_sb,
    .kill_sb = kill_block_super,
    .fs_flags = FS_REQUIRES_DEV,
};
的话就加一个,否则就提示busy

阅读(2337) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~