Linux 文件系统注册register_filesystem与注销unregister_filesystem源码详解

来源:互联网 发布:html css js项目实战 编辑:程序博客网 时间:2024/05/21 00:56
  1. 参考:  http://blog.csdn.net/gxfan/article/details/3079766
  2.              http://bbs.chinaunix.net/thread-4103398-1-1.html  
  3. 在linux内核中,每一种注册了的文件系统都由一个类型为file_system_type的结构体来代表,该结构体中含有一个类型为file_system_type*的域next,linux正是通过这个next域把所有注册了的文件系统连接起来的,同时,linux内核还定义了一个指向链表中第一个元素的全局指针file_systems和一个用来用来防止并发访问该链表的读/写自旋锁file_systems_lock。
  4. //查找file_systems中指定name的文件系统类型是否存在,若在,则返回指针,否则返回file_systems末尾的next指针
  5. static struct file_system_type **find_filesystem(const char *name, unsigned len)
  6. {
  7.         struct file_system_type **p;
  8.         for (p=&file_systems; *p; p=&(*p)->next)
  9.                 if (strlen((*p)->name) == len &&
  10.                     strncmp((*p)->name, name, len) == 0)
  11.                         break;
  12.         return p;
  13. }

  14. /**
  15. *        register_filesystem - register a new filesystem
  16. *        @fs: the file system structure
  17. *
  18. *        Adds the file system passed to the list of file systems the kernel
  19. *        is aware of for mount and other syscalls. Returns 0 on success,
  20. *        or a negative errno code on an error.
  21. *
  22. *        The &struct file_system_type that is passed is linked into the kernel
  23. *        structures and must not be freed until the file system has been
  24. *        unregistered.
  25. */

  26. int register_filesystem(struct file_system_type * fs)  
  27. {
  28.         int res = 0;
  29.         struct file_system_type ** p;

  30.         BUG_ON(strchr(fs->name, '.')); //这里没添加
  31.         if (fs->next)
  32.                 return -EBUSY;
  33.         INIT_LIST_HEAD(&fs->fs_supers); //只是做了初始化
  34.         write_lock(&file_systems_lock);      //加锁
  35.         p = find_filesystem(fs->name, strlen(fs->name)); //这里只是查找是否存在
  36.         if (*p)
  37.                 res = -EBUSY;                     //出错返回
  38.         else
  39.                 *p = fs;                              //将filesystem静态变量指向fs
  40.         write_unlock(&file_systems_lock); //解锁
  41.         return res;
  42. }

  43. //------------------------------------------------------
  44. static inline void INIT_LIST_HEAD(struct list_head *list)
  45. {
  46.         list->next = list;
  47.         list->prev = list;
  48. }
  49. //------------------------------------------------------

  50. EXPORT_SYMBOL(register_filesystem);

  51. /**
  52. *        unregister_filesystem - unregister a file system
  53. *        @fs: filesystem to unregister
  54. *
  55. *        Remove a file system that was previously successfully registered
  56. *        with the kernel. An error is returned if the file system is not found.
  57. *        Zero is returned on a success.
  58. *
  59. *        Once this function has returned the &struct file_system_type structure
  60. *        may be freed or reused.
  61. */

  62. int unregister_filesystem(struct file_system_type * fs)
  63. {
  64.         struct file_system_type ** tmp;

  65.         write_lock(&file_systems_lock);
  66.         tmp = &file_systems;
  67.         while (*tmp) {                                              //对比上面的代码,这里将注册了的文件系统类型从list中移除
  68.                 if (fs == *tmp) {
  69.                         *tmp = fs->next;
  70.                         fs->next = NULL;
  71.                         write_unlock(&file_systems_lock);
  72.                         return 0;
  73.                 }
  74.                 tmp = &(*tmp)->next;
  75.         }
  76.         write_unlock(&file_systems_lock);

  77.         synchronize_rcu();

  78.         return -EINVAL;
  79. }

  80. EXPORT_SYMBOL(unregister_filesystem)
  81.     整个算法的关键部分就在于:申请的临时指针变量不是直接指向单链表中的结构体的,而是指向的“指向结构体的指针”,这些“指向结构体的指针”包括:指向单链表第一个元素的指针以及每一个结构体中的next变量。这里我们可以把指向单链表第一个元素的指针file_systems看成一个特殊的next元素来理解。
原创粉丝点击