Kernel state file Operation __ work information (kernel)

Source: Internet
Author: User
Sometimes it is necessary to read and write file data in Linux kernel--most of the drivers that need to be debugged. There are no standard libraries available for manipulating files in kernel, and some of the functions of kernel need to be exploited: Filp_open () Filp_close (), Vfs_read () Vfs_write (), Set_fs (), GET_FS (), etc. These functions are declared in the Linux/fs.h and Asm/uaccess.h header files. The main steps are described below
1. Open the file

Filp_open () can open the file in the kernel, and its prototype is as follows:

Strcut file* filp_open (const char* filename, int open_mode, int mode);

The function returns the Strcut file* structure pointer, which is used by the subsequent function operation to verify the validity of the return value using Is_err ().

Parameter description

FileName: Indicates the name (including the path portion) to open or create the file. Open files in the kernel need to be aware of the timing of the open, it is easy to open the file driver is very early load and open the file, but need to open the file where the device is not mounted to the file system, and caused the open failed.

Open_mode: The file opens the way, its value and the standard library's open corresponding parameter is similar, may take o_creat,o_rdwr,o_rdonly and so on.

Mode: Use when creating files, setting read and Write permissions to create files, and other situations can be set to 0

2. Read and Write files

The read-write operation of files in kernel can use Vfs_read () and vfs_write, and you need to explain the functions of get_fs () and Set_fs () before using these two functions.

The Vfs_read () Vfs_write () Two functions are as follows:

ssize_t vfs_read (struct file* filp, char __user* buffer, size_t len, loff_t* POS);

ssize_t vfs_write (struct file* filp, const char __user* buffer, size_t len, loff_t* POS);

Note that the second parameter of the two functions, buffer, preceded by the __user modifier, requires that the two buffer pointers should point to the memory of the user space, and if the parameter is passed a pointer to the kernel space, both functions will return the failed-efault. However, in kernel, we generally do not easily generate user space pointers, or the convenience of independent use of user space memory. To make these two read and write functions work correctly using the buffer pointer of the kernel space, you need to use the SET_FS () function or macro (SET_FS () may be a macro definition), and if it is a function, its prototype is as follows:

void Set_fs (mm_segment_t FS);

The function is to change the KERNEL of memory address check processing mode, in fact, the function of the parameters of FS only two values: User_ds,kernel_ds, respectively, representing user space and kernel space, by default, KERNEL value is User_ds, That is, check the user's space address and make a transform. To use the kernel space address in this function that checks the memory address, you need to use SET_FS (Kernel_ds) to set it. Get_fs () may also be a macro definition, which is used to get the current settings, and the general usage of these two functions is:

mm_segment_t Old_fs;

Old_fs = Get_fs ();

Set_fs (Kernel_ds);

...//memory-related operations

Set_fs (OLD_FS);

There are other kernel functions that also use __user-modified parameters, which can be used in kernel where kernel space memory is needed instead.

The last thing to note with Vfs_read () and Vfs_write () is that the last parameter loff_t * Pos,pos the value to initialize, indicating where to start reading from the file.

3. Close read and write files

int Filp_close (struct FILE*FILP, fl_owner_t ID);

The use of this function is simple, and the second parameter generally passes a null value, which is also useful for current->files as the argument.

Other points of attention for using the above functions:

1. In fact, the Linux kernel Group members do not support the kernel in the independent reading and writing files (this may affect the strategy and security issues), the kernel needs of the file content, preferably by the application layer with the completion.

2. Using this method in a Loadable kernel module can cause the module to fail to load because the kernel may not be able to export all of the functions you need.

3. The analysis of the parameters of some of these functions shows that the proper operation of these functions depends on the process environment, so some functions cannot be executed in an interrupted handle or kernel code that is not part of any process, or a crash may occur, to avoid this You can create kernel threads in kernel and put them into execution in a thread environment (the way to create kernel threads is to kernel_thread the () function).

=========================================================================================

Reproduced in: http://blogold.chinaunix.net/u3/113927/showart_2495807.html

With the support of the VFS, the user-state process reads and writes two system calls with read and write, but no such system calls in the Linux kernel how we manipulate the files. We know that read and write, after entering the kernel state, actually execute sys_read and sys_write, but look at the kernel source and find that the functions of these operations files are not exported (using Export_symbol export), which means that they are not available in the kernel module. , that's how it's good.

By looking at Sys_open's source code we found that the main use of the Do_filp_open () function, the function in Fs/namei.c, and in the file, the Filp_open function is called the Do_filp_open function, and the interface and Sys_ The open function is very similar, the invocation parameter is the same as Sys_open, and is exported using Export_symbol, so we assume that the function can open the file, and the function is the same as open. Using the same lookup method, we found a set of functions that manipulate files in the kernel, as follows:

Functional function prototypes
Open files struct file *filp_open (const char *filename, int flags, int mode)
Read files ssize_t vfs_read (struct file *file, char __user *buf, size_t count, loff_t *pos)
Write files ssize_t vfs_write (struct file *file, const char __user *buf, size_t count, loff_t *pos)
Close file int filp_close (struct file *filp, fl_owner_t ID)


We notice that in the Vfs_read and Vfs_write functions, the parameter buf the memory address of the user space pointed to, and if we use the kernel-space pointer directly, we will return-efalut. So we need to use
Set_fs () and GET_FS () macros change how the kernel handles memory address checking, so the read and write process for files in kernel space is:

mm_segment_t fs = Get_fs ();
Set_fs (KERNEL_FS);
Vfs_write ();
Vfs_read ();
SET_FS (FS);
The following is an example of a file operation in the kernel:

#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
static char buf[] = "Hello";
static Char buf1[10];

int __init hello_init (void)
{
struct file *fp;
mm_segment_t FS;
loff_t Pos;
PRINTK ("Hello enter\n");
fp = Filp_open ("/home/niutao/kernel_file", O_RDWR | O_creat, 0644);
if (Is_err (FP)) {
PRINTK ("Create file error\n");
return-1;
}
FS = Get_fs ();
Set_fs (kernel_ds);
pos = 0;
Vfs_write (FP, buf, sizeof (BUF), &pos);
pos = 0;
Vfs_read (FP, buf1, sizeof (BUF), &pos);
PRINTK ("read:%s\n", buf1);
Filp_close (FP, NULL);
SET_FS (FS);
return 0;
}
void __exit hello_exit (void)
{
printk ("Hello exit\n");
}

Module_init (hello_init);
Module_exit (hello_exit);

Module_license ("GPL");



Other:

Inux kernel State does not use the C function library, so writing a file is a relatively troublesome thing.
The following code, followed by my previous article, is the refactoring of Linux semantics, which functions to get the offset of some structural elements.
The main code for writing the file is as follows

01 static void FileWrite (char* filename, char* data)
02 {
03 struct file *filp;
04 MM_SEGMENT_T FS;
05 FILP = Filp_open (filename, o_rdwr| o_append| O_creat, 0644);
06 if (Is_err (FILP))
07 {
, ], "," [+)]
[       

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.