免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4004 | 回复: 5
打印 上一主题 下一主题

[内核入门] register_filesystem函数的代码不理解 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-10-28 16:56 |只看该作者 |倒序浏览


  1. static struct file_system_type **find_filesystem(const char *name, unsigned len)
  2. {
  3.         struct file_system_type **p;
  4.         for (p=&file_systems; *p; p=&(*p)->next)
  5.                 if (strlen((*p)->name) == len &&
  6.                     strncmp((*p)->name, name, len) == 0)
  7.                         break;
  8.         return p;
  9. }

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

  22. int register_filesystem(struct file_system_type * fs)  //这个函数没看到将文件系统类型加到list里????
  23. {
  24.         int res = 0;
  25.         struct file_system_type ** p;

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

  39. //------------------------------------------------------
  40. static inline void INIT_LIST_HEAD(struct list_head *list)
  41. {
  42.         list->next = list;
  43.         list->prev = list;
  44. }
  45. //------------------------------------------------------

  46. EXPORT_SYMBOL(register_filesystem);

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

  58. int unregister_filesystem(struct file_system_type * fs)
  59. {
  60.         struct file_system_type ** tmp;

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

  73.         synchronize_rcu();

  74.         return -EINVAL;
  75. }

  76. EXPORT_SYMBOL(unregister_filesystem)
复制代码

论坛徽章:
17
水瓶座
日期:2013-08-29 12:09:27白羊座
日期:2014-08-07 12:36:42丑牛
日期:2014-07-24 12:44:41寅虎
日期:2014-04-16 16:15:33寅虎
日期:2014-03-12 09:28:43摩羯座
日期:2014-03-06 13:22:04技术图书徽章
日期:2014-03-06 11:34:50天蝎座
日期:2014-01-09 11:31:44寅虎
日期:2013-12-27 17:01:44双子座
日期:2013-12-27 12:32:29双子座
日期:2013-12-25 09:03:33丑牛
日期:2013-12-24 16:18:44
2 [报告]
发表于 2013-10-28 17:34 |只看该作者
回复 1# 李营长

p = find_filesystem(fs->name, strlen(fs->name)); //这里只是查找是否存在

如果未找到,则 p是 list中最后一个 &file_system_type->next,后面那句赋值自然将 fs加入到 list了啊


   

论坛徽章:
0
3 [报告]
发表于 2013-10-28 17:46 |只看该作者
回复 2# asuka2001


    fs只是做了一次初始化fs->next = fs; fs->prev = fs; 它只是指向自身。如果要加入list,至少也要fs->next指向全局的list

论坛徽章:
17
水瓶座
日期:2013-08-29 12:09:27白羊座
日期:2014-08-07 12:36:42丑牛
日期:2014-07-24 12:44:41寅虎
日期:2014-04-16 16:15:33寅虎
日期:2014-03-12 09:28:43摩羯座
日期:2014-03-06 13:22:04技术图书徽章
日期:2014-03-06 11:34:50天蝎座
日期:2014-01-09 11:31:44寅虎
日期:2013-12-27 17:01:44双子座
日期:2013-12-27 12:32:29双子座
日期:2013-12-25 09:03:33丑牛
日期:2013-12-24 16:18:44
4 [报告]
发表于 2013-10-28 17:52 |只看该作者
本帖最后由 asuka2001 于 2013-10-28 17:57 编辑

回复 3# 李营长

你需要仔细看看 find_filesystem()的返回值和 *p = fs的含义!


INIT_LIST_HEAD(&fs->fs_supers);

请注意,file_system_type->next这个成员的类型定义

         struct file_system_type * next;


   

论坛徽章:
0
5 [报告]
发表于 2013-10-29 09:06 |只看该作者
本帖最后由 李营长 于 2013-10-29 09:08 编辑

回复 4# asuka2001


    *p = fs 明白了

论坛徽章:
0
6 [报告]
发表于 2015-06-15 14:04 |只看该作者
楼主能讲解一下吗,我还是没明白
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP