Linux Kernel Schema Resolution - Linux file associations with processes

The relationship between a process and its open file
If the file management system is the manager and provider of the file, then the process is the user of the file in the file system. That is, the relationship between the file management system and the process is the relationship between the service and the customer.

File object

When a process opens a file through a system call, open(), the system call finds the file and encapsulates it into an instance of the file structure that is provided to the process, called a file object.

struct file {
	union {
		struct list_head	fu_list;        //List of all open files
		struct rcu_head 	fu_rcuhead;
	} f_u;
	
	struct dentry     *f_dentry;            //dentry of file
	const struct file_operations	*f_op;    //Point to File Action Function Set
	...
};

Notably, the pointer f_in the structure OP points to structure file_operations, which encapsulates operation functions on files. The structure is in the file include/linux/fs.h is defined as follows:

struct file_operations {
	struct module *owner;
	loff_t (*llseek) (struct file *, loff_t, int);
	ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
	ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
	ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
	ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
	int (*readdir) (struct file *, void *, filldir_t);
	unsigned int (*poll) (struct file *, struct poll_table_struct *);
	int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
	long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
	long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
	int (*mmap) (struct file *, struct vm_area_struct *);
	int (*open) (struct inode *, struct file *);
	int (*flush) (struct file *, fl_owner_t id);
	int (*release) (struct inode *, struct file *);
	...
};
struct files_struct {
	atomic_t count;            //Reference Count
	
	struct file * fd_array[NR_OPEN_DEFAULT];        //File Descriptor Table Pointer
};

Overall Series Diagram

Task_ The struct structure holds all the data structures of a process
Where the file pointer points to files_ The struct structure, which combines this file descriptor array with some dynamic information in the system, forms a new data structure, the process opens the file table.
A process opens a file in the file table that has been opened by all processes and encapsulates all his information within the struct file structure.
There are two main parts: 1 file operation, 2 file location

The relationship between dentry and inode nodes

  • dentry records file names, directories at higher levels, subdirectories, and so on, and the tree structure we see (directory tree, file tree)
  • inode records the location and distribution of files on physical storage media
  • An inode represents a physical file and an array is derived from the inode that records the location of the file contents. If the array is (4,5,9), the corresponding data is located in 4,5,9 blocks on the hard disk.
  • Dentry->d_ Inode points to the corresponding inode structure

The location where a process resides on the file system is called the relationship between the process and the file system. Structure fs_for Linux Strct describes the location of a process in the file system. Structure fs_struct in file include/linux.fs_struct.h is defined as follows:

struct fs_struct {
	int users;
	rwlock_t lock;
	int umask;
	int in_exec; 
	struct vfsmount *rootmnt, *pwdmnt, *altrootmnt;        //Installation-related pointers
	struct dentry *root, *pwd, *altroot;           //Pointer relative to process location
};


pwd points to the current directory of the process; Root points to the root directory of the process;


Reference Blog: https://blog.csdn.net/qq_38410730/article/details/81415419

Keywords: C Linux kernel

Added by vikela on Sun, 19 Dec 2021 20:04:39 +0200