Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3547276
  • 博文数量: 1805
  • 博客积分: 135
  • 博客等级: 入伍新兵
  • 技术积分: 3345
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-19 20:01
文章分类

全部博文(1805)

文章存档

2017年(19)

2016年(80)

2015年(341)

2014年(438)

2013年(349)

2012年(332)

2011年(248)

分类: LINUX

2015-09-08 23:04:24


  1. 一、devtmpfs概述
  2. 1.devtmpfs 的功用是在 Linux 核心 启动早期建立一个初步的 /dev,令一般启动程序不用等待 udev,缩短 GNU/Linux 的开机时间。

  3. 2.重要解释
  4. Devtmpfs lets the kernel create a tmpfs very early at kernel initialization, before any driver core device is registered. Every device with a major/minor will have a device node created in this tmpfs instance. After the rootfs is mounted by the kernel, the populated tmpfs is mounted at /dev. In initramfs, it can be moved to the manually mounted root filesystem before /sbin/init is executed.

  5. 3.menuconfig 中加入devtmpfs支持
  6. make menuconfig-->Device Drivers-->Generic Driver Options
  7. Maintain a devtmpfs filesystem to mount at /dev
  8. Automount devtmpfs at /dev, after the kernel mounted the rootfs

  9. 4.df -T显示devtmpfs
  10. 文件系统 类型 1K-块 已用 可用 已用% 挂载点
  11. /dev/sda1 ext4 31621016 14985712 15029008 50% /
  12. none devtmpfs 399552 276 399276 1% /dev
  13. none tmpfs 403804 24 403780 1% /dev/shm
  14. none tmpfs 403804 108 403696 1% /var/run
  15. none tmpfs 403804 0 403804 0% /var/lock
  16. none tmpfs 403804 0 403804 0% /lib/init/rw
  17. .host:/ vmhgfs 67151668 54038400 13113268 81% /mnt/hgfs
  18. /dev/loop0 ext2 16119 8528 6772 56% /mnt/loop

  19. 二、devtmpfs文件系统初始化
  20. void __init driver_init(void)
  21. {
  22.     /* These are the core pieces */
  23.     devtmpfs_init();//devtmpfs文件系统初始化
  24.     devices_init();
  25.     buses_init();
  26.     classes_init();
  27.     firmware_init();
  28.     hypervisor_init();
  29.     platform_bus_init();
  30.     system_bus_init();
  31.     cpu_dev_init();
  32.     memory_dev_init();
  33. }

  34. static struct file_system_type dev_fs_type = {
  35.     .name = "devtmpfs",
  36.     .mount = dev_mount,
  37.     .kill_sb = kill_litter_super,
  38. };
  39.    
  40. int __init devtmpfs_init(void)
  41. {
  42.     int err = register_filesystem(&dev_fs_type);//注册dev_fs_type文件系统,即将dev_fs_type添加到内核全局总链表中file_systems
  43.     if (err) {
  44.         printk(KERN_ERR "devtmpfs: unable to register devtmpfs ""type %i\n", err);
  45.         return err;
  46.     }
  47.     
  48.     thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");//创建并启动一个内核线程devtmpfsd
  49.     if (!IS_ERR(thread)) {
  50.         wait_for_completion(&setup_done);//进行一个不可打断的等待,允许一个线程告诉另一个线程工作已经完成
  51.     } else {
  52.         err = PTR_ERR(thread);
  53.         thread = NULL;
  54.     }
  55.     
  56.     if (err) {
  57.         printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
  58.         unregister_filesystem(&dev_fs_type);
  59.         return err;
  60.     }
  61.     
  62.     printk(KERN_INFO "devtmpfs: initialized\n");
  63.     return 0;
  64. }

  65. //请求创建设备节点的请求队列req结构
  66. static struct req {
  67.     struct req *next;
  68.     struct completion done;
  69.     int err;
  70.     const char *name;
  71.     umode_t mode;//0代表删除
  72.     struct device *dev;
  73. } *requests;

  74. //内核线程devtmpfsd
  75. static int devtmpfsd(void *p)
  76. {
  77.     char options[] = "mode=0755";
  78.     int *err = p;
  79.     
  80.     *err = sys_unshare(CLONE_NEWNS);
  81.     if (*err)
  82.         goto out;
  83.         
  84.     //挂载devtmpfs文件系统
  85.     //devtmpfs是待安装设备的路径名,“/”是安装点路径名,”devtmpfs“表示文件系统类型,MS_SILENT=32768,即0x8000
  86.     *err = sys_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, options);
  87.     if (*err)
  88.         goto out;
  89.     sys_chdir("/.."); //将进程的当前工作目录(pwd)设定为devtmpfs文件系统的根目录/* will traverse into overmounted root */
  90.     sys_chroot(".");
  91.     complete(&setup_done);//允许一个线程告诉另一个线程工作已经完成
  92.     while (1) {
  93.         spin_lock(&req_lock);
  94.         while (requests) {//扫描请求链表,每当要创建一个设备节点时,都需要向requests链表中添加请求
  95.             struct req *req = requests;//赋值给临时req
  96.             requests = NULL;//清空
  97.             spin_unlock(&req_lock);
  98.             while (req) {//遍历刚才requests的请求链表
  99.                 struct req *next = req->next;
  100.                 req->err = handle(req->name, req->mode, req->dev);//对链表中的每一个请求调用handle函数
  101.                 complete(&req->done);
  102.                 req = next;
  103.             }
  104.             spin_lock(&req_lock);
  105.         }
  106.         __set_current_state(TASK_INTERRUPTIBLE);//设置为睡眠状态
  107.         spin_unlock(&req_lock);
  108.         schedule();//系统切换
  109.     }
  110.     return 0;
  111. out:
  112.     complete(&setup_done);
  113.     return *err;
  114. }

  115. static int handle(const char *name, umode_t mode, struct device *dev)
  116. {
  117.     if (mode)
  118.         return handle_create(name, mode, dev);
  119.     else
  120.         return handle_remove(name, dev);
  121. }

  122. static int handle_create(const char *nodename, umode_t mode, struct device *dev)
  123. {
  124.     struct dentry *dentry;
  125.     struct path path;
  126.     int err;
  127.     
  128.     //查找节点名称的路径以及返回节点对应的父目录dentry结构,即在此目录下创建一个设备节点,即是/dev目录对应的dentry结构
  129.     dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
  130.     if (dentry == ERR_PTR(-ENOENT)) {
  131.         create_path(nodename);
  132.         dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
  133.     }
  134.     if (IS_ERR(dentry))
  135.         return PTR_ERR(dentry);
  136.     
  137.     //创建设备节点
  138.     err = vfs_mknod(path.dentry->d_inode,dentry, mode, dev->devt);
  139.     if (!err) {
  140.         struct iattr newattrs;
  141.         newattrs.ia_mode = mode;/* fixup possibly umasked mode */
  142.         newattrs.ia_valid = ATTR_MODE;
  143.         mutex_lock(&dentry->d_inode->i_mutex);
  144.         notify_change(dentry, &newattrs);
  145.         mutex_unlock(&dentry->d_inode->i_mutex);
  146.         dentry->d_inode->i_private = &thread;/* mark as kernel-created inode */
  147.     }
  148.     done_path_create(&path, dentry);//与前边kern_path_create对应,减少path和dentry的计数等
  149.     return err;
  150. }

  151. int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  152. {
  153.     int error = may_create(dir, dentry);//检查是否可以创建设备文件节点
  154.     
  155.     if (error)
  156.         return error;
  157.     
  158.     //必须是字符设备或者块设备,且具有创建节点的权限
  159.     if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  160.         return -EPERM;
  161.     
  162.     if (!dir->i_op->mknod)
  163.         return -EPERM;
  164.     
  165.     error = devcgroup_inode_mknod(mode, dev);
  166.     if (error)
  167.         return error;
  168.     
  169.     error = security_inode_mknod(dir, dentry, mode, dev);
  170.     if (error)
  171.         return error;
  172.     
  173.     //调用具体文件系统的mknod()函数
  174.     //mount时调用shmem_fill_super()-->shmem_get_inode()分配inode节点时做出的初始化
  175.     /*那么在shmem_get_inode中
  176.         caseS_IFDIR:
  177.         inc_nlink(inode);
  178.         inode->i_size= 2 * BOGO_DIRENT_SIZE;
  179.         inode->i_op= &shmem_dir_inode_operations;
  180.         inode->i_fop= &simple_dir_operations;
  181.         由于mountpoint是dev这个目录,所以dev对应的inode的i_op就是shmem_dir_inode_operations。
  182.         staticconst struct inode_operations shmem_dir_inode_operations = {
  183.             #ifdefCONFIG_TMPFS
  184.             .create =shmem_create,
  185.             .lookup =simple_lookup,
  186.             .link =shmem_link,
  187.             .unlink =shmem_unlink,
  188.             .symlink =shmem_symlink,
  189.             .mkdir =shmem_mkdir,
  190.             .rmdir =shmem_rmdir,
  191.             .mknod =shmem_mknod,
  192.             .rename =shmem_rename,
  193.             #endif
  194.             #ifdefCONFIG_TMPFS_POSIX_ACL
  195.             .setattr =shmem_notify_change,
  196.             .setxattr =generic_setxattr,
  197.             .getxattr =generic_getxattr,
  198.             .listxattr =generic_listxattr,
  199.             .removexattr =generic_removexattr,
  200.             .check_acl =generic_check_acl,
  201.             #endif
  202.             };
  203.         */
  204.     error = dir->i_op->mknod(dir, dentry, mode, dev);//所以这里调用的就是shmem_mknod
  205.     if (!error)
  206.         fsnotify_create(dir, dentry);
  207.     return error;
  208. }

  209. shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  210. {
  211.     struct inode *inode;
  212.     int error = -ENOSPC;
  213.     
  214.     inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);//获得一个要创建的设备节点的inode,并初始化
  215.     if (inode) {
  216.         error = security_inode_init_security(inode, dir,&dentry->d_name,shmem_initxattrs, NULL);
  217.         if (error) {
  218.             if (error != -EOPNOTSUPP) {
  219.                 iput(inode);
  220.                 return error;
  221.             }
  222.         }
  223. #ifdef CONFIG_TMPFS_POSIX_ACL
  224.         error = generic_acl_init(inode, dir);
  225.         if (error) {
  226.             iput(inode);
  227.             return error;
  228.         }
  229. #else
  230.         error = 0;
  231. #endif
  232.         dir->i_size += BOGO_DIRENT_SIZE;
  233.         dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  234.         d_instantiate(dentry, inode);//与dentry建立关,此时就可以在/dev下看到这个字符设备节点了
  235.         dget(dentry); //递减dentry的计数
  236.     }
  237.     return error;
  238. }

  239. 三、文件系统的mount
  240. 内核主要是通过kernel_init调用prepare_namespace()函数执行安装实际根文件系统的操作:
  241. void __init prepare_namespace(void)
  242. {
  243.     int is_floppy;
  244.   
  245.     if (root_delay) {
  246.         printk(KERN_INFO "Waiting %dsec before mounting root device...\n",
  247.                root_delay);
  248.         ssleep(root_delay);
  249.     }
  250.     wait_for_device_probe();
  251.   
  252.     md_run_setup();
  253.     /* 把root_device_name变量置为从启动参数“root”中获取的设备文件名。
  254.   * 同样,把ROOT_DEV变量置为同一设备文件的主设备号和次设备号。*/
  255.     if (saved_root_name[0]) {
  256.         root_device_name = saved_root_name;
  257.         if (!strncmp(root_device_name, "mtd", 3) ||
  258.             !strncmp(root_device_name, "ubi", 3)) {
  259.             mount_block_root(root_device_name, root_mountflags);
  260.             goto out;
  261.         }
  262.         ROOT_DEV = name_to_dev_t(root_device_name);//转换为设备号/dev/mtdblock2.
  263.         if (strncmp(root_device_name, "/dev/", 5) == 0)
  264.             root_device_name += 5;
  265.     }
  266.   
  267.     if (initrd_load())
  268.         goto out;
  269.   
  270.     /* wait for any asynchronous scanning to complete */
  271.     if ((ROOT_DEV == 0) && root_wait) {
  272.         printk(KERN_INFO "Waiting for root device %s...\n",
  273.             saved_root_name);
  274.         while (driver_probe_done() != 0 ||
  275.             (ROOT_DEV = name_to_dev_t(saved_root_name)) == 0)
  276.             msleep(100);
  277.         async_synchronize_full();
  278.     }
  279.   
  280.     is_floppy = MAJOR(ROOT_DEV) == FLOPPY_MAJOR;
  281.   
  282.     if (is_floppy && rd_doload && rd_load_disk(0))
  283.         ROOT_DEV = Root_RAM0;
  284.   
  285.     mount_root();
  286. out:
  287.     devtmpfs_mount("dev");//挂载devtmpfs文件系统
  288.     sys_mount(".", "/", NULL, MS_MOVE, NULL); /* 移动rootfs文件系统根目录上的已安装文件系统的安装点。 */
  289.     sys_chroot(".");
  290. }

  291. int devtmpfs_mount(const char *mntdir)
  292. {
  293.     int err;
  294.     
  295.     if (!mount_dev)
  296.         return 0;
  297.     
  298.     if (!thread)
  299.         return 0;
  300.     //将devtmpfs文件系统挂载到/dev目录下
  301.     err = sys_mount("devtmpfs", (char *)mntdir, "devtmpfs", MS_SILENT, NULL);
  302.     if (err)
  303.         printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
  304.     else
  305.         printk(KERN_INFO "devtmpfs: mounted\n");
  306.     return err;
  307. }


  308. 四、devtmpfs创建节点
  309. 系统在启动过程中,扫描到的设备会通过devtmpfs_create_node()函数来添加设备节点。
  310. int devtmpfs_create_node(struct device *dev)
  311. {
  312.     const char *tmp = NULL;
  313.     struct req req;
  314.     
  315.     if (!thread)
  316.     return 0;
  317.     
  318.     req.mode = 0;
  319.     req.name = device_get_devnode(dev, &req.mode, &tmp);//获得设备名
  320.     if (!req.name)
  321.         return -ENOMEM;
  322.     
  323.     if (req.mode == 0)
  324.         req.mode = 0600;
  325.     if (is_blockdev(dev))
  326.         req.mode |= S_IFBLK;//块设备
  327.     else
  328.         req.mode |= S_IFCHR;//字符设备
  329.     
  330.     req.dev = dev;
  331.     
  332.     init_completion(&req.done);
  333.     
  334.     spin_lock(&req_lock);
  335.     req.next = requests;//请求添加到requests链表
  336.     requests = &req;
  337.     spin_unlock(&req_lock);
  338.     
  339.     wake_up_process(thread);//唤醒内核线程devtmpfsd添加设备节点
  340.     wait_for_completion(&req.done);
  341.     
  342.     kfree(tmp);
  343.     
  344.     return req.err;
  345. }

  346. const char *device_get_devnode(struct device *dev,umode_t *mode, const char **tmp)
  347. {
  348.     char *s;
  349.     
  350.     *tmp = NULL;
  351.     
  352.     /* the device type may provide a specific name */
  353.     if (dev->type && dev->type->devnode)
  354.         *tmp = dev->type->devnode(dev, mode);
  355.     if (*tmp)
  356.         return *tmp;
  357.     
  358.     /* the class may provide a specific name */
  359.     if (dev->class && dev->class->devnode)
  360.         *tmp = dev->class->devnode(dev, mode);
  361.     if (*tmp)
  362.         return *tmp;
  363.     
  364.     /* return name without allocation, tmp == NULL */
  365.     if (strchr(dev_name(dev), '!') == NULL)
  366.         return dev_name(dev);
  367.     
  368.     /* replace '!' in the name with '/' */
  369.     *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
  370.     if (!*tmp)
  371.         return NULL;
  372.     while ((s = strchr(*tmp, '!')))
  373.         s[0] = '/';
  374.     return *tmp;
  375. }

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