Proc File system

Source: Internet
Author: User
Tags echo 7

Proc File system

The proc file system is a non-stored file system that is dynamically generated when a file is read, and the write function associated with the file is called when the file is written. Each proc file is associated with a byte-specific read-write function, so it provides another mechanism for communicating with the kernel: The kernel component can provide the user space with the interface to provide query information and modify the software behavior, so it is a more important special file system.

1. Contents of the proc file system

Because the proc file system provides access to user space in the form of files, these interfaces can be used to obtain information about the relevant part at run time or to modify the behavior of the part, making it a very convenient interface. The file system is heavily used in the kernel. The proc file system is a file system that can be mounted anywhere in the directory tree, but is usually mounted under/proc, which roughly contains the following information:

    • Memory management
    • Related information for each process
    • File system
    • Device drivers
    • System bus
    • Power Management
    • Terminal
    • System control Parameters
    • Internet

This information covers almost all parts of the kernel, so changing the file system is an important part of understanding system information. Because the contents of a file in the proc file system depend on the kernel part that implements the file, the implementation of each file can vary greatly.
Since the proc file system is in the form of a file, we can access the file directly via commands such as Cat,echo (with the permission of file access, of course).
For example, we can view the system's startup commands through "Cat/proc/cmdline"
Boot_image=/vmlinuz-3.5.0-40-generic root=uuid=80730516-33e7-420e-ae95-7d82bdfadb25 ro Quiet Splash vt.handoff=7
You can view IO information for a specific process through "Cat/proc/2629/io"
rchar:6906149866
wchar:14016909787
syscr:5354112
syscw:10764514
read_bytes:7827566592
write_bytes:14870413312
cancelled_write_bytes:4096
You can modify the log level of the kernel via "Echo 7>/PROC/SYS/KERNEL/PRINTK"
It is important to note that although it is convenient to use/proc, it is not recommended in Linux kernel development because its content is so complex that it is difficult to manage and understand.

2. Initialization of the proc file system

The proc file system must be initialized and mounted to the system before it is used. The initialization of the proc file system is mainly accomplished by:

    1. Call Proc_init_inodecache to create a private buffer used by the proc file system
    2. Call Register_filesystem to register the proc file system, which provides the proc filesystem's own File_system_type, which includes a function pointer for mount. This information is used when mount is executed, and the Mount function is eventually found to mount the operation
    3. Call Proc_mkdir to create some proc file directories
    4. Information about registering the proc file system under the SYS file system

The proc_fill_super is called in the Mount function of proc, which gives the information needed to proc the file system's Super block (such as the file system's Super block operation function Pointer, the super block size, etc.), and creates the root directory of the proc file system. The corresponding inode_operations and file_operations are also specified when the root directory is created, and with this information, VFS can perform various operations on the file system (create, delete, find files).

3. proc File system data structure

Proc files and directories are represented in the kernel by Proc_dir_entry. It contains all the information for the proc file inside the proc file system. Its data structure is as follows:

  1. struct Proc_dir_entry {
  2. unsigned int low_ino;
  3. umode_t mode;
  4. nlink_t Nlink;
  5. kuid_t uid;
  6. kgid_t GID;
  7. loff_t size;
  8. const struct inode_operations *proc_iops; Inode Operations
  9. const struct file_operations *proc_fops; File Operations
  10. struct Proc_dir_entry *next, *parent, *subdir;
  11. void *data;
  12. read_proc_t *read_proc;
  13. write_proc_t *write_proc;
  14. atomic_t count;
  15. int pde_users;
  16. struct completion *pde_unload_completion;
  17. struct List_head pde_openers;
  18. spinlock_t Pde_unload_lock;
  19. U8 Namelen;
  20. Char name[];
  21. };

The kernel also provides a data structure Proc_inode used to correlate proc-specific data with the inode corresponding to the file, defined as follows:

  1. struct Proc_inode {
  2. struct PID *pid;
  3. int fd;
  4. Union proc_op op;
  5. struct Proc_dir_entry *pde;
  6. struct Ctl_table_header *sysctl;
  7. struct ctl_table *sysctl_entry;
  8. void *ns;
  9. const struct proc_ns_operations *ns_ops;
  10. struct Inode vfs_inode;
  11. };

With this data structure, the kernel can easily convert between the inode and the proc data associated with that inode.

4. proc File System API

The kernel provides a set of APIs for creating proc files, with the following APIs:

struct Proc_dir_entry *proc_mkdir (const char *name, struct proc_dir_entry *parent);

This function is used to create a directory entry in the proc file system, most of the time, when we want to implement our own proc file, we have to create our own directory, and then create our own files in that directory, of course, we can also directly in the existing proc file system directory to create their own files.
Each parameter of the function has the following meanings:

    • Name: The names of the directories
    • Parent: The name of the directory's parents Directory

void Proc_remove (struct proc_dir_entry *de);
This function is used to delete a directory.
struct Proc_dir_entry *proc_create (const char *name, umode_t mode, struct proc_dir_entry *parent, const struct FILE_OPERAT Ions *proc_fops);
This function is used to create a proc file in the proc file system with the following parameter meanings:

    • The name of the Name:proc file, expressed in the proc file system, is the name of the file.
    • The access mode of the Mode:proc file, which is represented in the proc file system, is the access mode of the file.
    • Parent: The directory where the proc file resides
    • Proc_fops: Pointer to the file operation that is used to manipulate the file

These parameters provide the key information required for a file, including file name, access mode, directory entry (to specify the location of the file in the file system), and a document manipulation pointer.
When you want to delete a file, you need to use the API:
void Remove_proc_entry (const char *name, struct proc_dir_entry *parent);
The meaning of the parameter is obvious, and the function is used to delete the specified proc file from the specified directory of the proc file system. There are other APIs that are defined in include/linux/prof_fs.h.
When a file or directory is created in the proc file system, it is eventually transferred to Proc_register, which assigns the correct file_operations and inode_operations to the newly created file or directory, which will be used when accessing the file.

5. File operation process

The process of file operation is basically similar, first the VFS will find the file corresponding inode, and then call the relevant functions on the inode to complete the corresponding operation, such as when reading and writing a file, the steps to be performed:

    1. The first thing to open the file, for the proc file system, it is to do: Find the file, after the file data structure found in the file_operations will be initialized to the file Inode file_operations (Reference path DO_FILP_ Open->path_openat->do_last->nameidata_to_filp->__dentry_open->fops_get). You can then invoke the open operation of the file.
    2. Write the file, this step is also very simple, the file structure of the write operation can be.

This process is also well understood, because for any file system, they are present inode,inode is used to represent a file, it contains a lot of useful information, it is used to maintain the file itself without involving the contents of the file. For the file system stored on the memory, some of the inode information is stored on the memory, but still a part of it is dynamically generated, and for non-memory file systems, such as the proc file system, the Inode is dynamically generated. When a file is used, its inode is loaded into memory for use and a new one is created if the associated inode does not already exist. The super_operations of each filesystem's Super block contains function pointers for creating and deleting Inode nodes for this file system, and function pointers for manipulating inode nodes in the Inode's operation function set, including functions to find inode nodes. When you open a file, if the inode buffer does not yet have an inode for the file, the Lookup_real will eventually be called after the VFS process, which invokes the lookup function in the Inode action set to find the inode of the file being opened If the inode for the file already exists, it will get the inode for that file from the cache, which is done by Do_lookup.

Simply put, the inode information is found in the first step of the file operation (if it is not created and initialized), and then the file structure of the files is initialized with the inode information. Then use the file structure to manipulate the files.

6./proc/sys Catalogue Example

For the/proc/sys directory, this directory is created in Proc_sys_init:

  1. int __init proc_sys_init (void)
  2. {
  3. struct Proc_dir_entry *proc_sys_root;
  4. Proc_sys_root = Proc_mkdir ("sys", NULL);
  5. Proc_sys_root->proc_iops = &proc_sys_dir_operations;
  6. Proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
  7. Proc_sys_root->nlink = 0;
  8. return 0;
  9. }

Then, when using a file in this directory, you need to find the inode for the file, using the lookup function in Inode_operations, which is proc_sys_lookup:

    1. Static struct dentry *proc_sys_lookup (struct inode *dir, struct dentry *dentry,
    2. struct Nameidata *nd)
    3. {
    4. struct Ctl_table_header *head = Grab_header (dir);
 
    1. static struct  ctl_table_header *grab_header (struct inode *inode)   
    2. {  
    3.          struct ctl_table_header *head = proc_i (inode)->sysctl;   
    4.         if  (! Head)   
    5.                  head = &sysctl_table_root.default_set.dir.header;   
    6.         return sysctl_head_ Grab (head);   
    7. }  

And Sysctl_table_root is the/proc/sys corresponding ctl_table_root structure, it holds the/proc/sys content of the entry information, Register_sysctl_paths used to add content to/proc/sys,/ Proc/sys/net, and so on, is added to the/proc/sys by this call, and then the lookup function finds the corresponding content (such as Ctl_table_header).

Proc File system

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.