zoukankan      html  css  js  c++  java
  • devtmpfs文件系统创建设备节点

    转载来自:wangbaolin719的ChinaUnix博客 

    1. 一、devtmpfs概述
    1. 1.devtmpfs 的功用是在 Linux 核心 启动早期建立一个初步的 /dev,令一般启动程序不用等待 udev,缩短 GNU/Linux 的开机时间。
    2. 2.重要解释
    3. 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.
    4. 3.menuconfig 中加入devtmpfs支持 
    5. make menuconfig-->Device Drivers-->Generic Driver Options
    6. Maintain a devtmpfs filesystem to mount at /dev
    7. Automount devtmpfs at /dev, after the kernel mounted the rootfs
    8. 4.df -T显示devtmpfs
    9. 文件系统 类型 1K-块 已用 可用 已用% 挂载点
    10. /dev/sda1 ext4 31621016 14985712 15029008 50% /
    11. none devtmpfs 399552 276 399276 1% /dev
    12. none tmpfs 403804 24 403780 1% /dev/shm
    13. none tmpfs 403804 108 403696 1% /var/run
    14. none tmpfs 403804 0 403804 0% /var/lock
    15. none tmpfs 403804 0 403804 0% /lib/init/rw
    16. .host:/ vmhgfs 67151668 54038400 13113268 81% /mnt/hgfs
    17. /dev/loop0 ext2 16119 8528 6772 56% /mnt/loop
    18. 二、devtmpfs文件系统初始化
    19. void __init driver_init(void)
    20.     /* These are the core pieces */
    21.     devtmpfs_init();//devtmpfs文件系统初始化
    22.     devices_init();
    23.     buses_init();
    24.     classes_init();
    25.     firmware_init();
    26.     hypervisor_init();
    27.     platform_bus_init();
    28.     system_bus_init();
    29.     cpu_dev_init();
    30.     memory_dev_init();
    31. }
    32. static struct file_system_type dev_fs_type = {
    33.     .name = "devtmpfs",
    34.     .mount = dev_mount,
    35.     .kill_sb = kill_litter_super,
    36. };
    37. int __init devtmpfs_init(void)
    38. {
    39.     int err = register_filesystem(&dev_fs_type);//注册dev_fs_type文件系统,即将dev_fs_type添加到内核全局总链表中file_systems
    40.     if (err) {
    41.         printk(KERN_ERR "devtmpfs: unable to register devtmpfs ""type %i ", err);
    42.         return err;
    43.     }
    44.     thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");//创建并启动一个内核线程devtmpfsd
    45.     if (!IS_ERR(thread)) {
    46.         wait_for_completion(&setup_done);//进行一个不可打断的等待,允许一个线程告诉另一个线程工作已经完成
    47.     } else {
    48.         err = PTR_ERR(thread);
    49.         thread = NULL;
    50.     }
    51.     if (err) {
    52.         printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i ", err);
    53.         unregister_filesystem(&dev_fs_type);
    54.         return err;
    55.     }
    56.     printk(KERN_INFO "devtmpfs: initialized ");
    57.     return 0;
    58. }
    59. //请求创建设备节点的请求队列req结构
    60. static struct req {
    61.     struct req *next;
    62.     struct completion done;
    63.     int err;
    64.     const char *name;
    65.     umode_t mode;//0代表删除
    66.     struct device *dev;
    67. } *requests;
    68. //内核线程devtmpfsd
    69. static int devtmpfsd(void *p)
    70. {
    71.     char options[] = "mode=0755";
    72.     int *err = p;
    73.     *err = sys_unshare(CLONE_NEWNS);
    74.     if (*err)
    75.         goto out;
    76.     //挂载devtmpfs文件系统
    77.     //devtmpfs是待安装设备的路径名,“/”是安装点路径名,”devtmpfs“表示文件系统类型,MS_SILENT=32768,即0x8000
    78.     *err = sys_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, options);
    79.     if (*err)
    80.         goto out;
    81.     sys_chdir("/.."); //将进程的当前工作目录(pwd)设定为devtmpfs文件系统的根目录/* will traverse into overmounted root */
    82.     sys_chroot(".");
    83.     complete(&setup_done);//允许一个线程告诉另一个线程工作已经完成
    84.     while (1) {
    85.         spin_lock(&req_lock);
    86.         while (requests) {//扫描请求链表,每当要创建一个设备节点时,都需要向requests链表中添加请求
    87.             struct req *req = requests;//赋值给临时req
    88.             requests = NULL;//清空
    89.             spin_unlock(&req_lock);
    90.             while (req) {//遍历刚才requests的请求链表
    91.                 struct req *next = req->next;
    92.                 req->err = handle(req->name, req->mode, req->dev);//对链表中的每一个请求调用handle函数
    93.                 complete(&req->done);
    94.                 req = next;
    95.             }
    96.             spin_lock(&req_lock);
    97.         }
    98.         __set_current_state(TASK_INTERRUPTIBLE);//设置为睡眠状态
    99.         spin_unlock(&req_lock);
    100.         schedule();//系统切换
    101.     }
    102.     return 0;
    103. out:
    104.     complete(&setup_done);
    105.     return *err;
    106. }
    107. static int handle(const char *name, umode_t mode, struct device *dev)
    108. {
    109.     if (mode)
    110.         return handle_create(name, mode, dev);
    111.     else
    112.         return handle_remove(name, dev);
    113. }
    114. static int handle_create(const char *nodename, umode_t mode, struct device *dev)
    115. {
    116.     struct dentry *dentry;
    117.     struct path path;
    118.     int err;
    119.     //查找节点名称的路径以及返回节点对应的父目录dentry结构,即在此目录下创建一个设备节点,即是/dev目录对应的dentry结构
    120.     dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
    121.     if (dentry == ERR_PTR(-ENOENT)) {
    122.         create_path(nodename);
    123.         dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
    124.     }
    125.     if (IS_ERR(dentry))
    126.         return PTR_ERR(dentry);
    127.     //创建设备节点
    128.     err = vfs_mknod(path.dentry->d_inode,dentry, mode, dev->devt);
    129.     if (!err) {
    130.         struct iattr newattrs;
    131.         newattrs.ia_mode = mode;/* fixup possibly umasked mode */
    132.         newattrs.ia_valid = ATTR_MODE;
    133.         mutex_lock(&dentry->d_inode->i_mutex);
    134.         notify_change(dentry, &newattrs);
    135.         mutex_unlock(&dentry->d_inode->i_mutex);
    136.         dentry->d_inode->i_private = &thread;/* mark as kernel-created inode */
    137.     }
    138.     done_path_create(&path, dentry);//与前边kern_path_create对应,减少path和dentry的计数等
    139.     return err;
    140. }
    141. int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
    142. {
    143.     int error = may_create(dir, dentry);//检查是否可以创建设备文件节点
    144.     if (error)
    145.         return error;
    146.     //必须是字符设备或者块设备,且具有创建节点的权限
    147.     if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
    148.         return -EPERM;
    149.     if (!dir->i_op->mknod)
    150.         return -EPERM;
    151.     error = devcgroup_inode_mknod(mode, dev);
    152.     if (error)
    153.         return error;
    154.     error = security_inode_mknod(dir, dentry, mode, dev);
    155.     if (error)
    156.         return error;
    157.     //调用具体文件系统的mknod()函数
    158.     //mount时调用shmem_fill_super()-->shmem_get_inode()分配inode节点时做出的初始化
    159.     /*那么在shmem_get_inode中
    160.         caseS_IFDIR:
    161.         inc_nlink(inode);
    162.         inode->i_size= 2 * BOGO_DIRENT_SIZE;
    163.         inode->i_op= &shmem_dir_inode_operations;
    164.         inode->i_fop= &simple_dir_operations;
    165.         由于mountpoint是dev这个目录,所以dev对应的inode的i_op就是shmem_dir_inode_operations。
    166.         staticconst struct inode_operations shmem_dir_inode_operations = {
    167.             #ifdefCONFIG_TMPFS
    168.             .create =shmem_create,
    169.             .lookup =simple_lookup,
    170.             .link =shmem_link,
    171.             .unlink =shmem_unlink,
    172.             .symlink =shmem_symlink,
    173.             .mkdir =shmem_mkdir,
    174.             .rmdir =shmem_rmdir,
    175.             .mknod =shmem_mknod,
    176.             .rename =shmem_rename,
    177.             #endif
    178.             #ifdefCONFIG_TMPFS_POSIX_ACL
    179.             .setattr =shmem_notify_change,
    180.             .setxattr =generic_setxattr,
    181.             .getxattr =generic_getxattr,
    182.             .listxattr =generic_listxattr,
    183.             .removexattr =generic_removexattr,
    184.             .check_acl =generic_check_acl,
    185.             #endif
    186.             };
    187.         */
    188.     error = dir->i_op->mknod(dir, dentry, mode, dev);//所以这里调用的就是shmem_mknod
    189.     if (!error)
    190.         fsnotify_create(dir, dentry);
    191.     return error;
    192. }
    193. shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
    194. {
    195.     struct inode *inode;
    196.     int error = -ENOSPC;
    197.     inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);//获得一个要创建的设备节点的inode,并初始化
    198.     if (inode) {
    199.         error = security_inode_init_security(inode, dir,&dentry->d_name,shmem_initxattrs, NULL);
    200.         if (error) {
    201.             if (error != -EOPNOTSUPP) {
    202.                 iput(inode);
    203.                 return error;
    204.             }
    205.         }
    206. #ifdef CONFIG_TMPFS_POSIX_ACL
    207.         error = generic_acl_init(inode, dir);
    208.         if (error) {
    209.             iput(inode);
    210.             return error;
    211.         }
    212. #else
    213.         error = 0;
    214. #endif
    215.         dir->i_size += BOGO_DIRENT_SIZE;
    216.         dir->i_ctime = dir->i_mtime = CURRENT_TIME;
    217.         d_instantiate(dentry, inode);//与dentry建立关,此时就可以在/dev下看到这个字符设备节点了
    218.         dget(dentry); //递减dentry的计数
    219.     }
    220.     return error;
    221. }
    222. 三、文件系统的mount
    223. 内核主要是通过kernel_init调用prepare_namespace()函数执行安装实际根文件系统的操作:
    224. void __init prepare_namespace(void) 
    225.     int is_floppy; 
    226.     if (root_delay) { 
    227.         printk(KERN_INFO "Waiting %dsec before mounting root device... ", 
    228.                root_delay); 
    229.         ssleep(root_delay); 
    230.     } 
    231.     wait_for_device_probe(); 
    232.     md_run_setup(); 
    233.     /* 把root_device_name变量置为从启动参数“root”中获取的设备文件名。 
    234.   * 同样,把ROOT_DEV变量置为同一设备文件的主设备号和次设备号。*/ 
    235.     if (saved_root_name[0]) { 
    236.         root_device_name = saved_root_name; 
    237.         if (!strncmp(root_device_name, "mtd", 3) || 
    238.             !strncmp(root_device_name, "ubi", 3)) { 
    239.             mount_block_root(root_device_name, root_mountflags); 
    240.             goto out; 
    241.         } 
    242.         ROOT_DEV = name_to_dev_t(root_device_name);//转换为设备号/dev/mtdblock2. 
    243.         if (strncmp(root_device_name, "/dev/", 5) == 0) 
    244.             root_device_name += 5; 
    245.     } 
    246.     if (initrd_load()) 
    247.         goto out; 
    248.     /* wait for any asynchronous scanning to complete */ 
    249.     if ((ROOT_DEV == 0) && root_wait) { 
    250.         printk(KERN_INFO "Waiting for root device %s... ", 
    251.             saved_root_name); 
    252.         while (driver_probe_done() != 0 || 
    253.             (ROOT_DEV = name_to_dev_t(saved_root_name)) == 0) 
    254.             msleep(100); 
    255.         async_synchronize_full(); 
    256.     } 
    257.     is_floppy = MAJOR(ROOT_DEV) == FLOPPY_MAJOR; 
    258.     if (is_floppy && rd_doload && rd_load_disk(0)) 
    259.         ROOT_DEV = Root_RAM0; 
    260.     mount_root(); 
    261. out: 
    262.     devtmpfs_mount("dev");//挂载devtmpfs文件系统 
    263.     sys_mount(".", "/", NULL, MS_MOVE, NULL); /* 移动rootfs文件系统根目录上的已安装文件系统的安装点。 */ 
    264.     sys_chroot("."); 
    265. int devtmpfs_mount(const char *mntdir)
    266. {
    267.     int err;
    268.     if (!mount_dev)
    269.         return 0;
    270.     if (!thread)
    271.         return 0;
    272.     //将devtmpfs文件系统挂载到/dev目录下
    273.     err = sys_mount("devtmpfs", (char *)mntdir, "devtmpfs", MS_SILENT, NULL);
    274.     if (err)
    275.         printk(KERN_INFO "devtmpfs: error mounting %i ", err);
    276.     else
    277.         printk(KERN_INFO "devtmpfs: mounted ");
    278.     return err;
    279. }
    280. 四、devtmpfs创建节点
    281. 系统在启动过程中,扫描到的设备会通过devtmpfs_create_node()函数来添加设备节点。
    282. int devtmpfs_create_node(struct device *dev)
    283. {
    284.     const char *tmp = NULL;
    285.     struct req req;
    286.     if (!thread)
    287.     return 0;
    288.     req.mode = 0;
    289.     req.name = device_get_devnode(dev, &req.mode, &tmp);//获得设备名
    290.     if (!req.name)
    291.         return -ENOMEM;
    292.     if (req.mode == 0)
    293.         req.mode = 0600;
    294.     if (is_blockdev(dev))
    295.         req.mode |= S_IFBLK;//块设备
    296.     else
    297.         req.mode |= S_IFCHR;//字符设备
    298.     req.dev = dev;
    299.     init_completion(&req.done);
    300.     spin_lock(&req_lock);
    301.     req.next = requests;//请求添加到requests链表
    302.     requests = &req;
    303.     spin_unlock(&req_lock);
    304.     wake_up_process(thread);//唤醒内核线程devtmpfsd添加设备节点
    305.     wait_for_completion(&req.done);
    306.     kfree(tmp);
    307.     return req.err;
    308. }
    309. const char *device_get_devnode(struct device *dev,umode_t *mode, const char **tmp)
    310. {
    311.     char *s;
    312.     *tmp = NULL;
    313.     /* the device type may provide a specific name */
    314.     if (dev->type && dev->type->devnode)
    315.         *tmp = dev->type->devnode(dev, mode);
    316.     if (*tmp)
    317.         return *tmp;
    318.     /* the class may provide a specific name */
    319.     if (dev->class && dev->class->devnode)
    320.         *tmp = dev->class->devnode(dev, mode);
    321.     if (*tmp)
    322.         return *tmp;
    323.     /* return name without allocation, tmp == NULL */
    324.     if (strchr(dev_name(dev), '!') == NULL)
    325.         return dev_name(dev);
    326.     /* replace '!' in the name with '/' */
    327.     *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
    328.     if (!*tmp)
    329.         return NULL;
    330.     while ((s = strchr(*tmp, '!')))
    331.         s[0] = '/';
    332.     return *tmp;
    333. }
  • 相关阅读:
    SpringBoot第五篇:整合Mybatis
    SpringBoot第四篇:整合JDBCTemplate
    SpringBoot第三篇:配置文件详解二
    分享一篇去年的项目总结
    Oracle生成多表触发器sql
    Oracle 设置用户密码永不过期
    Oracle建表提示SQL 错误: ORA-00904: : 标识符无效
    MySql数据备份
    ETL全量多表同步简述
    ETL全量单表同步简述
  • 原文地址:https://www.cnblogs.com/yfz0/p/4730162.html
Copyright © 2011-2022 走看看