[File System] File System Study Notes (2) --- task_struct

Source: Internet
Author: User

I. task_struct and file system-related information

 

[CPP]View plaincopy
  1. <Sched. h>
  2. Struct task_struct {
  3. ...
  4. /* File system info */
  5. Int link_count, total_link_count;
  6. ...
  7. /* Filesystem information */
  8. Struct fs_struct * FS;
  9. /* Open File Information */
  10. Struct files_struct * files;
  11. /* Namespaces */
  12. Struct nsproxy * nsproxy;
  13. ...
  14. }

 

Data related to the file system of the process is stored in FS, which contains the current working directory

 

[CPP]View plaincopy
  1. Fs_struct is mainly used to manage information about a specific process,
  2. Struct fs_struct {
  3. Atomic_t count;
  4. Int umask; // indicates the standard mask, used to set permissions for new files
  5. Struct dentry * root, * Pwd, * altroot; // root indicates the root directory of the Process, PWD indicates the directory of the current process, and the shell CD command will change when used
  6. Struct vfsmount * rootmnt, * pwdmnt, * altrootmnt; // rootmnt indicates the file system in which the root directory is located,
  7. };

 

 

[CPP]View plaincopy
  1. Struct files_struct {
  2. Atomic_t count; // number of processes that share the file
  3. Struct fdtable * FDT;
  4. Struct fdtable fdtab;
  5. Int next_fd; // The file descriptor ID used by the process to open a new file next time
  6. Struct embedded_fd_set close_on_exec_init;
  7. Struct embedded_fd_set open_fds_init;
  8. Struct file * fd_array [nr_open_default]; each member is a pointer pointing to each open file struct file instance
  9. };


By default, each process in the kernel runs to open the nr_open_default file. The default value is bits_per_long. The value is 32 on 32-bit systems.

[CPP]View plaincopy
  1. Struct fdtable {
  2. Unsigned int max_fds; // The maximum number of file objects and file descriptors that the process can process currently.
  3. Struct file ** FD;/* Current FD array */
  4. Fd_set * close_on_exec;
  5. Fd_set * open_fds;
  6. Struct rcu_head RCU;
  7. Struct files_struct * free_files;
  8. Struct fdtable * next;
  9. };


Struct file points to the real file information,

[CPP]View plaincopy
  1. Struct file {
  2. Struct list_head fu_list;
  3. Struct path f_path; // specifies the association between the file name and inode. Information about the file system where the file is located
  4. # Define f_dentry f_path.dentry
  5. # Define f_vfsmnt f_path.mnt
  6. Const struct file_operations * f_op; // various functions called by file operations
  7. Atomic_t f_count;
  8. Unsigned int f_flags;
  9. Mode_t f_mode;
  10. Loff_t f_pos; // indicates the location where the process operates on files.
  11. Struct fown_struct f_owner; // process information about the file
  12. Unsigned int f_uid, f_gid; // your uid GID
  13. Struct file_ra_state f_ra; // pre-read feature that specifies whether to pre-read data before actually reading data
  14. Unsigned long f_version;
  15. ...
  16. Struct address_space * f_mapping; // address space ing pointing to the inode instance related to the file
  17. ...
  18. };
  19. Struct path {
  20. Struct vfsmount * MNT;
  21. Struct dentry * dentry;
  22. };

There is no file structure on the hard disk. When a process opens a file, the kernel dynamically creates a file object. The same file has different file objects in different processes.

Appendix:

Process to open a file:
1. the user-layer open () function finally calls the sys_open () function (FS/open. c) The sys_open () function mainly uses the do_flip_open () function to find the inode of the file. The do_flip_open () function first calls the open_namei () function, and the open_namei () function calls path_lookup () the function looks for the inode of the file and performs several additional checks. If a new file system item is created, the function also needs to be applied to the umask (current-> FS-> umask) process) by default. The do_flip_open () function then calls the nameidata_to_flip () function to initialize the pre-read structure, place the newly created file instance on the super fast s_files chain, and call open () in the file_operations structure of the underlying file system () function.
The final control is transferred back to the user process. before returning the file descriptor, fd_install must place the file instance in the files-> fd array of the task_struct process.

[File System] File System Study Notes (2) --- task_struct

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.