五、devtmpfs檔案系統 建立裝置節點
阿新 • • 發佈:2019-02-09
- 轉載來自:http://blog.chinaunix.net/uid-27717694-id-3574368.html
- 一、devtmpfs概述
- 1.devtmpfs 的功用是在 Linux 核心 啟動早期建立一個初步的 /dev,令一般啟動程式不用等待 udev,縮短 GNU/Linux 的開機時間。
- 2.重要解釋
- 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
- 3.menuconfig 中加入devtmpfs支援
- make menuconfig-->Device Drivers-->Generic Driver Options
- Maintain a devtmpfs filesystem to
- Automount devtmpfs at /dev, after the kernel mounted the rootfs
- 4.df -T顯示devtmpfs
- 檔案系統 型別 1K-塊 已用 可用 已用% 掛載點
- /dev/sda1 ext4 31621016 14985712 15029008 50% /
- none devtmpfs 399552 276 399276 1% /dev
- none tmpfs 403804 24 403780 1% /dev/shm
- none tmpfs 403804 108 403696 1% /var/run
- none tmpfs 403804 0 403804 0% /var/lock
- none tmpfs 403804 0 403804 0%
- .host:/ vmhgfs 67151668 54038400 13113268 81% /mnt/hgfs
- /dev/loop0 ext2 16119 8528 6772 56% /mnt/loop
- 二、devtmpfs檔案系統初始化
- void __init driver_init(void)
- {
- /* These are the core pieces */
- devtmpfs_init();//devtmpfs檔案系統初始化
- devices_init();
- buses_init();
- classes_init();
- firmware_init();
- hypervisor_init();
- platform_bus_init();
- system_bus_init();
- cpu_dev_init();
- memory_dev_init();
- }
- static struct file_system_type dev_fs_type = {
- .name = "devtmpfs",
- .mount = dev_mount,
- .kill_sb = kill_litter_super,
- };
- int __init devtmpfs_init(void)
- {
- int err = register_filesystem(&dev_fs_type);//註冊dev_fs_type檔案系統,即將dev_fs_type新增到核心全域性總連結串列中file_systems
- if (err) {
- printk(KERN_ERR "devtmpfs: unable to register devtmpfs ""type %i\n", err);
- return err;
- }
- thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");//建立並啟動一個核心執行緒devtmpfsd
- if (!IS_ERR(thread)) {
- wait_for_completion(&setup_done);//進行一個不可打斷的等待,允許一個執行緒告訴另一個執行緒工作已經完成
- } else {
- err = PTR_ERR(thread);
- thread = NULL;
- }
- if (err) {
- printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
- unregister_filesystem(&dev_fs_type);
- return err;
- }
- printk(KERN_INFO "devtmpfs: initialized\n");
- return 0;
- }
- //請求建立裝置節點的請求佇列req結構
- static struct req {
- struct req *next;
- struct completion done;
- int err;
- const char *name;
- umode_t mode;//0代表刪除
- struct device *dev;
- } *requests;
- //核心執行緒devtmpfsd
- static int devtmpfsd(void *p)
- {
- char options[] = "mode=0755";
- int *err = p;
- *err = sys_unshare(CLONE_NEWNS);
- if (*err)
- goto out;
- //掛載devtmpfs檔案系統
- //devtmpfs是待安裝裝置的路徑名,“/”是安裝點路徑名,”devtmpfs“表示檔案系統型別,MS_SILENT=32768,即0x8000
- *err = sys_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, options);
- if (*err)
- goto out;
- sys_chdir("/.."); //將程序的當前工作目錄(pwd)設定為devtmpfs檔案系統的根目錄/* will
traverse into overmounted root */
- sys_chroot(".");
- complete(&setup_done);//允許一個執行緒告訴另一個執行緒工作已經完成
- while (1) {
- spin_lock(&req_lock);
- while (requests) {//掃描請求連結串列,每當要建立一個裝置節點時,都需要向requests連結串列中新增請求
- struct req *req = requests;//賦值給臨時req
- requests = NULL;//清空
- spin_unlock(&req_lock);
- while (req) {//遍歷剛才requests的請求連結串列
- struct req *next = req->next;
- req->err = handle(req->name, req->mode, req->dev);//對連結串列中的每一個請求呼叫handle函式
- complete(&req->done);
- req = next;
- }
- spin_lock(&req_lock);
- }
- __set_current_state(TASK_INTERRUPTIBLE);//設定為睡眠狀態
- spin_unlock(&req_lock);
- schedule();//系統切換
- }
- return 0;
- out:
- complete(&setup_done);
- return *err;
- }
- static int handle(const char *name, umode_t mode, struct device *dev)
- {
- if (mode)
- return handle_create(name, mode, dev);
- else
- return handle_remove(name, dev);
- }
- static int handle_create(const char *nodename, umode_t mode, struct
device *dev)
- {
- struct dentry *dentry;
- struct path path;
- int err;
- //查詢節點名稱的路徑以及返回節點對應的父目錄dentry結構,即在此目錄下建立一個裝置節點,即是/dev目錄對應的dentry結構
- dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
- if (dentry == ERR_PTR(-ENOENT)) {
- create_path(nodename);
- dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
- }
- if (IS_ERR(dentry))
- return PTR_ERR(dentry);
- //建立裝置節點
- err = vfs_mknod(path.dentry->d_inode,dentry, mode, dev->devt);
- if (!err) {
- struct iattr newattrs;
- newattrs.ia_mode = mode;/* fixup possibly umasked mode */
- newattrs.ia_valid = ATTR_MODE;
- mutex_lock(&dentry->d_inode->i_mutex);
- notify_change(dentry, &newattrs);
- mutex_unlock(&dentry->d_inode->i_mutex);
- dentry->d_inode->i_private = &thread;/* mark
as kernel-created inode */
- }
- done_path_create(&path, dentry);//與前邊kern_path_create對應,減少path和dentry的計數等
- return err;
- }
- int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
- {
- int error = may_create(dir, dentry);//檢查是否可以建立裝置檔案節點
- if (error)
- return error;
- //必須是字元裝置或者塊裝置,且具有建立節點的許可權
- if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
- return -EPERM;
- if (!dir->i_op->mknod)
- return -EPERM;
- error = devcgroup_inode_mknod(mode, dev);
- if (error)
- return error;
- error = security_inode_mknod(dir, dentry, mode, dev);
- if (error)
- return error;
- //呼叫具體檔案系統的mknod()函式
- //mount時呼叫shmem_fill_super()-->shmem_get_inode()分配inode節點時做出的初始化
- /*那麼在shmem_get_inode中
- caseS_IFDIR:
- inc_nlink(inode);
- inode->i_size= 2 * BOGO_DIRENT_SIZE;
- inode->i_op= &shmem_dir_inode_operations;
- inode->i_fop= &simple_dir_operations;
- 由於mountpoint是dev這個目錄,所以dev對應的inode的i_op就是shmem_dir_inode_operations。
- staticconst struct inode_operations shmem_dir_inode_operations = {
- #ifdefCONFIG_TMPFS
- .create =shmem_create,
- .lookup =simple_lookup,
- .link =shmem_link,
- .unlink =shmem_unlink,
- .symlink =shmem_symlink,
- .mkdir =shmem_mkdir,
- .rmdir =shmem_rmdir,
- .mknod =shmem_mknod,
- .rename =shmem_rename,
- #endif
- #ifdefCONFIG_TMPFS_POSIX_ACL
- .setattr =shmem_notify_change,
- .setxattr =generic_setxattr,
- .getxattr =generic_getxattr,
- .listxattr =generic_listxattr,
- .removexattr =generic_removexattr,
- .check_acl =generic_check_acl,
- #endif
- };
- */
- error = dir->i_op->mknod(dir, dentry, mode, dev);//所以這裡呼叫的就是shmem_mknod
- if (!error)
- fsnotify_create(dir, dentry);
- return error;
- }
- shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
- {
- struct inode *inode;
- int error = -ENOSPC;
- inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);//獲得一個要建立的裝置節點的inode,並初始化
- if (inode) {
- error = security_inode_init_security(inode, dir,&dentry->d_name,shmem_initxattrs, NULL);
- if (error) {
- if (error != -EOPNOTSUPP) {
- iput(inode);
- return error;
- }
- }
- #ifdef CONFIG_TMPFS_POSIX_ACL
- error = generic_acl_init(inode, dir);
- if (error) {
- iput(inode);
- return error;
- }
- #else
- error = 0;
- #endif
- dir->i_size += BOGO_DIRENT_SIZE;
- dir->i_ctime = dir->i_mtime = CURRENT_TIME;
- d_instantiate(dentry, inode);//與dentry建立關,此時就可以在/dev下看到這個字元裝置節點了
- dget(dentry); //遞減dentry的計數
- }
- return error;
- }
- 三、檔案系統的mount
- 核心主要是通過kernel_init呼叫prepare_namespace()函式執行安裝實際根檔案系統的操作:
- void __init prepare_namespace(void)
- {
- int is_floppy;
- if (root_delay) {
- printk(KERN_INFO "Waiting %dsec before mounting root device...\n",
- root_delay);
- ssleep(root_delay);
- }
- wait_for_device_probe();
- md_run_setup();
- /* 把root_device_name變數置為從啟動引數“root”中獲取的裝置檔名。
- * 同樣,把ROOT_DEV變數置為同一裝置檔案的主裝置號和次裝置號。*/
- if (saved_root_name[0]) {
- root_device_name = saved_root_name;
- if (!strncmp(root_device_name, "mtd", 3) ||
- !strncmp(root_device_name, "ubi", 3)) {
- mount_block_root(root_device_name, root_mountflags);
- goto out;
- }
- ROOT_DEV = name_to_dev_t(root_device_name);//轉換為裝置號/dev/mtdblock2.
- if (strncmp(root_device_name, "/dev/", 5) == 0)
- root_device_name += 5;
- }
- if (initrd_load())
- goto out;
- /* wait for any asynchronous scanning to complete */
- if ((ROOT_DEV == 0) && root_wait) {
- printk(KERN_INFO "Waiting for root device %s...\n",
- saved_root_name);
- while (driver_probe_done() != 0 ||
- (ROOT_DEV = name_to_dev_t(saved_root_name)) == 0)
- msleep(100);
- async_synchronize_full();
- }
- is_floppy = MAJOR(ROOT_DEV) == FLOPPY_MAJOR;
- if (is_floppy && rd_doload && rd_load_disk(0))
- ROOT_DEV = Root_RAM0;
- mount_root();
- out:
- devtmpfs_mount("dev");//掛載devtmpfs檔案系統
- sys_mount(".", "/", NULL, MS_MOVE, NULL); /* 移動rootfs檔案系統根目錄上的已安裝檔案系統的安裝點。 */
- sys_chroot(".");
- }
- int devtmpfs_mount(const char *mntdir)
- {
- int err;
- if (!mount_dev)
- return 0;
- if (!thread)
- return 0;
- //將devtmpfs檔案系統掛載到/dev目錄下
- err = sys_mount("devtmpfs", (char *)mntdir, "devtmpfs", MS_SILENT, NULL);
- if (err)
- printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
- else
- printk(KERN_INFO "devtmpfs: mounted\n");
- return err;
- }
- 四、devtmpfs建立節點
- 系統在啟動過程中,掃描到的裝置會通過devtmpfs_create_node()函式來新增裝置節點。
- int devtmpfs_create_node(struct device *dev)
- {
- const char *tmp = NULL;
- struct req req;
- if (!thread)
- return 0;
- req.mode = 0;
- req.name = device_get_devnode(dev, &req.mode, &tmp);//獲得裝置名
- if (!req.name)
- return -ENOMEM;
- if (req.mode == 0)
- req.mode = 0600;
- if (is_blockdev(dev))
- req.mode |= S_IFBLK;//塊裝置
- else
- req.mode |= S_IFCHR;//字元裝置
- req.dev = dev;
- init_completion(&req.done);
- spin_lock(&req_lock);
- req.next = requests;//請求新增到requests連結串列
- requests = &req;
- spin_unlock(&req_lock);
- wake_up_process(thread);//喚醒核心執行緒devtmpfsd新增裝置節點
- wait_for_completion(&req.done);
- kfree(tmp);
- return req.err;
- }
- const char *device_get_devnode(struct device *dev,umode_t *mode, const char **tmp)
- {
- char *s;
- *tmp = NULL;
- /* the device type may provide a specific name */
- if (dev->type && dev->type->devnode)
- *tmp = dev->type->devnode(dev, mode);
- if (*tmp)
- return *tmp;
- /* the class may provide a specific name */
- if (dev->class && dev->class->devnode)
- *tmp = dev->class->devnode(dev, mode);
- if (*tmp)
- return *tmp;
- /* return name without allocation, tmp == NULL */
- if (strchr(dev_name(dev), '!') == NULL)
- return dev_name(dev);
- /* replace '!' in the name with '/' */
- *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
- if (!*tmp)
- return NULL;
- while ((s = strchr(*tmp, '!')))
- s[0] = '/';
- return *tmp;