I. task_struct and file system-related information
[CPP]View plaincopy
- <Sched. h>
- Struct task_struct {
- ...
- /* File system info */
- Int link_count, total_link_count;
- ...
- /* Filesystem information */
- Struct fs_struct * FS;
- /* Open File Information */
- Struct files_struct * files;
- /* Namespaces */
- Struct nsproxy * nsproxy;
- ...
- }
Data related to the file system of the process is stored in FS, which contains the current working directory
[CPP]View plaincopy
- Fs_struct is mainly used to manage information about a specific process,
- Struct fs_struct {
- Atomic_t count;
- Int umask; // indicates the standard mask, used to set permissions for new files
- 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
- Struct vfsmount * rootmnt, * pwdmnt, * altrootmnt; // rootmnt indicates the file system in which the root directory is located,
- };
[CPP]View plaincopy
- Struct files_struct {
- Atomic_t count; // number of processes that share the file
- Struct fdtable * FDT;
- Struct fdtable fdtab;
- Int next_fd; // The file descriptor ID used by the process to open a new file next time
- Struct embedded_fd_set close_on_exec_init;
- Struct embedded_fd_set open_fds_init;
- Struct file * fd_array [nr_open_default]; each member is a pointer pointing to each open file struct file instance
- };
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
- Struct fdtable {
- Unsigned int max_fds; // The maximum number of file objects and file descriptors that the process can process currently.
- Struct file ** FD;/* Current FD array */
- Fd_set * close_on_exec;
- Fd_set * open_fds;
- Struct rcu_head RCU;
- Struct files_struct * free_files;
- Struct fdtable * next;
- };
Struct file points to the real file information,
[CPP]View plaincopy
- Struct file {
- Struct list_head fu_list;
- Struct path f_path; // specifies the association between the file name and inode. Information about the file system where the file is located
- # Define f_dentry f_path.dentry
- # Define f_vfsmnt f_path.mnt
- Const struct file_operations * f_op; // various functions called by file operations
- Atomic_t f_count;
- Unsigned int f_flags;
- Mode_t f_mode;
- Loff_t f_pos; // indicates the location where the process operates on files.
- Struct fown_struct f_owner; // process information about the file
- Unsigned int f_uid, f_gid; // your uid GID
- Struct file_ra_state f_ra; // pre-read feature that specifies whether to pre-read data before actually reading data
- Unsigned long f_version;
- ...
- Struct address_space * f_mapping; // address space ing pointing to the inode instance related to the file
- ...
- };
- Struct path {
- Struct vfsmount * MNT;
- Struct dentry * dentry;
- };
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