Chinaunix首页 | 论坛 | 博客
  • 博客访问: 207640
  • 博文数量: 54
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 230
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-25 13:58
文章分类

全部博文(54)

文章存档

2014年(12)

2013年(42)

分类: 嵌入式

2013-12-20 11:31:34

以下是一个简单的文件系统,该文件系统并没有实际的意义,只是为了帮助读者更好的理解文件系统的基本原理和工作的过程:


点击(此处)折叠或打开

  1. #include <linux/module.h>
  2. #include <linux/fs.h>
  3. #include <linux/pagemap.h>
  4. #include <linux/mount.h>
  5. #include <linux/init.h>
  6. #include <linux/namei.h>


  7. #define AUFS_MAGIC 0x64668735

  8. static struct vfsmount *aufs_mount;
  9. static int aufs_mount_count;

  10. static struct inode *aufs_get_inode(struct super_block *sb, int mode, dev_t dev)
  11. {
  12.     struct inode *inode = new_inode(sb);    
  13.     if (inode) {
  14.     inode->i_mode = mode;
  15.     inode->i_uid = current->fsuid;
  16.     inode->i_gid = current->fsgid;
  17.     inode->i_blksize = PAGE_CACHE_SIZE;
  18.     inode->i_blocks = 0;
  19.     inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;

  20.     switch (mode & S_IFMT) {
  21.     default:
  22.         init_special_inode(inode, mode, dev);
  23.         break;
  24.     case S_IFREG:
  25.         printk("creat a file \n");
  26.         break;
  27.     case S_IFDIR:
  28.         inode->i_op = &simple_dir_inode_operations;
  29.         inode->i_fop = &simple_dir_operations;
  30.         printk("creat a dir file \n");
  31.         inode->i_nlink++;
  32.     break;
  33.         }
  34.     }
  35.     return inode;
  36. }

  37. /* SMP-safe */
  38. static int aufs_mknod(struct inode *dir, struct dentry *dentry,int mode, dev_t dev)
  39. {
  40.     struct inode *inode;
  41.     int error = -EPERM;

  42.     if (dentry->d_inode)
  43.         return -EEXIST;    
  44.     inode = aufs_get_inode(dir->i_sb, mode, dev);
  45.     if (inode) {
  46.         d_instantiate(dentry, inode);
  47.         dget(dentry);
  48.         error = 0;
  49.     }
  50.     return error;
  51. }

  52. static int aufs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  53. {
  54.     int res;

  55.     res = aufs_mknod(dir, dentry, mode |S_IFDIR, 0);
  56.     if (!res)
  57.         dir->i_nlink++;
  58.     return res;
  59. }

  60. static int aufs_create(struct inode *dir, struct dentry *dentry, int mode)
  61. {
  62.     return aufs_mknod(dir, dentry, mode | S_IFREG, 0);
  63. }

  64. static int aufs_fill_super(struct super_block *sb, void *data, int silent)
  65. {
  66.     static struct tree_descr debug_files[] = {{""}};
  67.     return simple_fill_super(sb, AUFS_MAGIC, debug_files);
  68. }
  69. static struct super_block *aufs_get_sb(struct file_system_type *fs_type,int flags, const char *dev_name,void *data)
  70. {
  71.     return get_sb_single(fs_type, flags, data, aufs_fill_super);
  72. }

  73. static struct file_system_type au_fs_type = {
  74.     .owner = THIS_MODULE,
  75.     .name = "aufs",
  76.     .get_sb = aufs_get_sb,
  77.     .kill_sb = kill_litter_super,
  78. };

  79. static int aufs_create_by_name(const char *name, mode_t mode,struct dentry *parent,struct dentry **dentry)
  80. {
  81.     int error = 0;
  82. /* If the parent is not specified, we create it in the root.
  83. * We need the root dentry to do this, which is in the super
  84. * block. A pointer to that is in the struct vfsmount that we
  85. * have around.
  86. */
  87.     if (!parent ) {
  88.         if (aufs_mount && aufs_mount->mnt_sb) {
  89.             parent = aufs_mount->mnt_sb->s_root;
  90.                 }
  91.             }
  92.     if (!parent) {
  93.         printk("Ah! can not find a parent!\n");
  94.         return -EFAULT;
  95.         }
  96.     *dentry = NULL;
  97.     mutex_lock(&parent->d_inode->i_mutex);
  98.     *dentry = lookup_one_len(name, parent, strlen(name));
  99.     if (!IS_ERR(dentry)) {
  100.         if ((mode & S_IFMT) == S_IFDIR)
  101.             error = aufs_mkdir(parent->d_inode, *dentry, mode);
  102.         else
  103.             error = aufs_create(parent->d_inode, *dentry, mode);
  104.     } else
  105.         error = PTR_ERR(dentry);
  106.         mutex_unlock(&parent->d_inode->i_mutex);
  107.         return error;
  108. }

  109. struct dentry *aufs_create_file(const char *name, mode_t mode,struct dentry *parent, void *data,struct file_operations *fops)
  110. {
  111.     struct dentry *dentry = NULL;
  112.     int error;
  113.     printk("aufs: creating file '%s'\n",name);
  114.     error = aufs_create_by_name(name, mode, parent, &dentry);

  115.     if (error) {
  116.         dentry = NULL;
  117.         goto exit;
  118.     }
  119.     if (dentry->d_inode) {
  120.         if (data)
  121.             dentry->d_inode->u.generic_ip = data;
  122.         if (fops)
  123.             dentry->d_inode->i_fop = fops;
  124.     }
  125.     exit:
  126.     return dentry;
  127. }

  128. struct dentry *aufs_create_dir(const char *name, struct dentry *parent)
  129. {
  130.     return aufs_create_file(name,S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,parent, NULL, NULL);
  131. }

  132. static int __init aufs_init(void)
  133. {
  134.     int retval;
  135.     struct dentry *pslot;
  136.     retval = register_filesystem(&au_fs_type);

  137.     if (!retval) {
  138.         aufs_mount = kern_mount(&au_fs_type);
  139.         if (IS_ERR(aufs_mount)) {
  140.             printk(KERN_ERR "aufs: could not mount!\n");
  141.             unregister_filesystem(&au_fs_type);
  142.             return retval;
  143.         }
  144.     }

  145.     pslot = aufs_create_dir("woman star",NULL);
  146.     aufs_create_file("lbb", S_IFREG | S_IRUGO, pslot, NULL, NULL);
  147.     aufs_create_file("fbb", S_IFREG | S_IRUGO, pslot, NULL, NULL);
  148.     aufs_create_file("ljl", S_IFREG | S_IRUGO, pslot, NULL, NULL);
  149.     pslot = aufs_create_dir("man star",NULL);
  150.     aufs_create_file("ldh", S_IFREG | S_IRUGO, pslot, NULL, NULL);
  151.     aufs_create_file("lcw", S_IFREG | S_IRUGO, pslot, NULL, NULL);
  152.     aufs_create_file("jw", S_IFREG | S_IRUGO, pslot, NULL, NULL);
  153.     return retval;
  154. }

  155. static void __exit aufs_exit(void)
  156. {
  157.     simple_release_fs(&aufs_mount, &aufs_mount_count);
  158.     unregister_filesystem(&au_fs_type);
  159. }

  160. module_init(aufs_init);
  161. module_exit(aufs_exit);
  162. MODULE_LICENSE("GPL");
  163. MODULE_DESCRIPTION("This is a simple module");
  164. MODULE_VERSION("Ver 0.1");

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