歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux虛擬文件系統--open()

Linux虛擬文件系統--open()

日期:2017/2/28 14:56:30   编辑:Linux教程

open()系統調用用來打開一個文件,本文就VFS層,對open系統調用的過程進行一個簡單的分析。

  1. SYSCALL_DEFINE3(open, constchar __user *, filename, int, flags, int, mode)
  2. {
  3. long ret;
  4. if (force_o_largefile())
  5. flags |= O_LARGEFILE;
  6. ret = do_sys_open(AT_FDCWD, filename, flags, mode);
  7. /* avoid REGPARM breakage on x86: */
  8. asmlinkage_protect(3, ret, filename, flags, mode);
  9. return ret;
  10. }

force_o_largefile()用來判斷系統是否為32位的,如果不是32位,也就是說為64位,則將O_LARGEFILE置位,主體工作由do_sys_open()來做

  1. long do_sys_open(int dfd, constchar __user *filename, int flags, int mode)
  2. {
  3. char *tmp = getname(filename);//拷貝文件名字符串到內核空間
  4. int fd = PTR_ERR(tmp);
  5. if (!IS_ERR(tmp)) {
  6. fd = get_unused_fd_flags(flags);//為文件分配一個文件描述符
  7. if (fd >= 0) {
  8. //實際的OPEN操作處理
  9. struct file *f = do_filp_open(dfd, tmp, flags, mode, 0);
  10. if (IS_ERR(f)) {
  11. put_unused_fd(fd);
  12. fd = PTR_ERR(f);
  13. } else {
  14. fsnotify_open(f->f_path.dentry);
  15. fd_install(fd, f);
  16. }
  17. }
  18. putname(tmp);
  19. }
  20. return fd;
  21. }

open操作是特定於某個進程進行的,因此涉及到了VFS中特定於進程的結構,這裡簡單的介紹下

struct files_struct {
/*
* read mostly part
*/
atomic_t count;
struct fdtable *fdt;
struct fdtable fdtab;
/*
* written part on a separate cache line in SMP
*/
spinlock_t file_lock ____cacheline_aligned_in_smp;
int next_fd;
struct embedded_fd_set close_on_exec_init;
struct embedded_fd_set open_fds_init;
struct file * fd_array[NR_OPEN_DEFAULT];
};

Copyright © Linux教程網 All Rights Reserved