Chinaunix首页 | 论坛 | 博客
  • 博客访问: 360914
  • 博文数量: 168
  • 博客积分: 6895
  • 博客等级: 准将
  • 技术积分: 1726
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-12 23:01
文章分类

全部博文(168)

文章存档

2011年(6)

2010年(162)

我的朋友

分类: LINUX

2010-11-04 22:40:17

    I am not a lucky dog. It is a terrible day but my xylgkgfs. Okay following is code. I just make it by learning from brother Wuting's.


/*
   Copyleft( c ), 2010, @ xiyou linux group.
   All rights reserved.

   File name: xylgkg.c.
   Programmers' name: Licong, Wangxiang, Zhaoqiao, Liurhyme, Harry Wei, Wuyun, Zhangbo, Lanxiangyu, Dubobo.
   Start time: 2010, 8, 1.
   Finish time: 2010, 11, 7.

   Programme introduction: Just program a simple virtual fs.

   Modify time:
   Modify programmers:

   Modify introduction:
*/
#include       /*U know it is a module inserted in kernel :) */
#include         /*some important strcture in it, like inode structure.*/
#include       /*notify some information between dentry and inode*/
#include        /*module init and exit*/
#include     /*lookup_one_len in it*/
#include     /*some fs's magic number in it*/
#include     /*mount infor mation*/
#include
#include

#define        XYLGKGFS_MAGIC        0x3966477

static    struct    vfsmount    *xylgkgfs_mount;
static    int    xylgkgfs_mount_count;
static    bool    xylgkgfs_registered;

static    const    struct    super_operations    xylgkgfs =
{
    .statfs        = simple_statfs,
    .drop_inode    = generic_delete_inode,
};

static    ssize_t    xylgkgfs_read_file( struct file *file, const char __user *buf, size_t    count, loff_t *ppos )
{
    return( 0 );
}

static    ssize_t    xylgkgfs_write_file( struct file *file, const char __user *buf, size_t count, loff_t *ppos )
{
    return( count );
}

static    ssize_t    xylgkgfs_open_file( struct inode *inode, struct file *file )
{
    if( inode->i_private )
    {
        file->private_data = inode->i_private;
    }

    return( 0 );
}

const    struct    file_operations    xylgkg_file_operations =
{
    .read        = xylgkgfs_read_file,
    .write        = xylgkgfs_write_file,
    .open        = xylgkgfs_open_file,
};

static    struct    inode *xylgkgfs_get_inode( struct super_block *sb, int mode, dev_t dev )
{
    struct    inode    *inode = new_inode( sb );

    if( inode )
    {
        inode->i_mode        = mode;
        inode->i_uid        = 0;
        inode->i_gid        = 0;
        inode->i_blocks        = 0;
        inode->i_atime        = inode->i_mtime = CURRENT_TIME;
        inode->i_fop        = &xylgkg_file_operations;
    }

    return( inode );
}

static    int    xylgkgfs_mknod( struct inode *dir, struct dentry *dentry, int mode, dev_t dev )
{
    struct    inode    *inode;
    int    error    = -EPERM;

    if( dentry->d_inode )
    {
        return( -EEXIST );
    }

    inode = xylgkgfs_get_inode( dir->i_sb, mode, dev );

    if( inode )
    {
        d_instantiate( dentry, inode );

        dget( dentry );
        error = 0;
    }
   
    return( error );
}

static    int    xylgkgfs_creat( struct inode *dir, struct dentry *dentry, int mode )
{
    int    res;

    mode        = ( mode & S_IALLUGO ) | S_IFREG;
    res        = xylgkgfs_mknod( dir, dentry, mode, 0 );

    if( !res )
    {
        fsnotify_create( dir, dentry );
    }

    return( res );
}

static    inline    int    xylgkgfs_fill_super( struct super_block *sb, void *data, int silent )
{
    static    struct    tree_descr    xylgkgfs_files[] =  {{""}};

    return( simple_fill_super( sb, XYLGKGFS_MAGIC, xylgkgfs_files ) );
}

static    int    xylgkgfs_get_sb( struct    file_system_type *xylgkgfs_fs_type, int flags, const char *dev_name, void *data, struct vfsmount *mnt )
{
    return( get_sb_single( xylgkgfs_fs_type, flags, data, xylgkgfs_fill_super, mnt ) );
}

static    struct    file_system_type xylgkgfs_fs_type =
{
    .owner        = THIS_MODULE,
    .name        = "xylgkgfs",
    .get_sb        = xylgkgfs_get_sb,
    .kill_sb    = kill_litter_super,
};

static    int    xylgkgfs_create_by_name( const char *name, mode_t mode, struct dentry *parent, struct dentry **dentry )
{
    int error    = 0;

    if( !parent )
    {
        if( xylgkgfs_mount && xylgkgfs_mount-> mnt_sb )
        {
            parent = xylgkgfs_mount->mnt_sb->s_root;
        }
    }

    if( !parent )
    {
        return( -EFAULT );
    }

    *dentry        = NULL;
    *dentry        = lookup_one_len( name, parent, strlen( name ) );
    error        = xylgkgfs_creat( parent->d_inode, *dentry, mode );
    dput( *dentry );

    return( error );
}

struct    dentry    *xylgkgfs_create_file( const char *name, mode_t mode, struct dentry *parent, void *data, const struct file_operations *fops )
{
    struct    dentry *dentry = NULL;
    int    error;

    error = simple_pin_fs( &xylgkgfs_fs_type, &xylgkgfs_mount, &xylgkgfs_mount_count );
    if( error )
    {
        goto exit;
    }

    error    = xylgkgfs_create_by_name( name, mode, parent, &dentry );
    if( error )
    {
        dentry    = NULL;
        simple_release_fs( &xylgkgfs_mount, &xylgkgfs_mount_count );

        goto    exit;
    }

    if( dentry->d_inode )
    {
        if( data )
        {
            dentry->d_inode->i_private = data;
        }

        if( fops )
        {
            dentry->d_inode->i_fop = fops;
        }
    }

exit:
    return( dentry );
}
EXPORT_SYMBOL_GPL( xylgkgfs_create_file );

struct    dentry    *xylgkgfs_create_dir( const char *name, struct dentry *parent )
{
    return( xylgkgfs_create_file( name, S_IFDIR | S_IRWXU| S_IRUGO | S_IXUGO, parent, NULL, NULL ) );
}
EXPORT_SYMBOL_GPL( xylgkgfs_create_dir );

static    int __init    xylgkgfs_init( void )
{
    int    retval;

    retval        = register_filesystem( &xylgkgfs_fs_type );
    if( !retval )
    {
        xylgkgfs_mount    = vfs_kern_mount( &xylgkgfs_fs_type, MS_KERNMOUNT, ( &xylgkgfs_fs_type )->name, NULL );

        if( IS_ERR( xylgkgfs_mount ) )
        {
            printk( KERN_ERR "xylgkgfs: can not mount!\n" );
            retval = PTR_ERR( xylgkgfs_mount );
            xylgkgfs_mount = NULL;
            unregister_filesystem( &xylgkgfs_fs_type );
            return( retval );
        }

    }
   
    return( 0 );
}

static    void __exit    xylgkgfs_exit( void )
{
    xylgkgfs_registered        = false;

    simple_release_fs( &xylgkgfs_mount, &xylgkgfs_mount_count );
    unregister_filesystem( &xylgkgfs_fs_type );
}

module_init( xylgkgfs_init );
module_exit( xylgkgfs_exit );

MODULE_LICENSE( "GPL" );

Following are in /proc/filesystems

nodev    sysfs
nodev    rootfs
nodev    bdev
nodev    proc
nodev    cgroup
nodev    cpuset
nodev    debugfs
nodev    securityfs
nodev    sockfs
nodev    usbfs
nodev    pipefs
nodev    anon_inodefs
nodev    tmpfs
nodev    inotifyfs
nodev    devpts
    ext3
    ext4
    ext2
    cramfs
nodev    ramfs
nodev    hugetlbfs
nodev    ecryptfs
nodev    fuse
    fuseblk
nodev    fusectl
nodev    mqueue
nodev    binfmt_misc
    vfat
nodev    xylgkgfs  :)mine



Following are my problems in it:
1, creat is not create.
2, some spelling mistakes.

etc.


Okay, all for today, Bye.

阅读(296) | 评论(0) | 转发(0) |
0

上一篇:11 3

下一篇:11 5

给主人留下些什么吧!~~