Linux Kernel File Operations

Source: Internet
Author: User

With the support of VFS, user-State processes can use read and write to read and write file systems of any type, but how can we operate files without such a system calling in the Linux kernel? We know that when read and write are in the kernel state, sys_read and sys_write are actually executed. However, when we look at the kernel source code, we find that the functions of these operation files are not exported (exported using export_symbol ), that is to say, it cannot be used in the kernel module. How can this problem be solved?

 

Through viewing the source code of sys_open, we found that it mainly uses the do_filp_open () function, which is in FS/nameI. c, and in the modified file, the filp_open function also calls the do_filp_open function, and the interface is very similar to the sys_open function. The call parameters are the same as those of sys_open, and export_symbol is used to export the function, so we guess this function can open a file, which is the same as open. Using the same search method, we found a group of functions that operate files in the kernel, as shown below:

 

Function Function prototype
Open a file StructFile * filp_open (Const Char* Filename,IntFlags,IntMode)
Read files Ssize_tVfs_read (StructFile * file,Char_ User * Buf,Size_tCount,
Loff_t * POS)
Write files Ssize_tVfs_write (StructFile * file,Const Char_ User
* Buf,Size_tCount, loff_t * POS)
Close file IntFilp_close (StructFile * filp, fl_owner_t ID)


 

We noticed that in the vfs_read and vfs_write functions, the Buf parameter points to the memory address of the user space. If we use the pointer of the kernel space directly,-efalut is returned. So we need to use
Set_fs () and get_fs () macros are used to change the kernel's processing method for memory address check. Therefore, the file read/write process in the kernel space is as follows:

  1. Mm_segment_t FS = get_fs ();
  2. Set_fs (kernel_fs );
  3. // Vfs_write ();
  4. Vfs_read ();
  5. Set_fs (FS );

The following is an example of file operations in the kernel:

# Include # includestatic 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 ");

The above content from: http://www.ej38.com/showinfo/linux-192439.html

Another question is, what method should I use to obtain the file attributes (directory files, common files, uid, and GID) in the kernel state?

The user-state method is called by the stat system, but the kernel-state system call is not exported, so it cannot be used.

I don't know if sys_stat and vfs_stat can be implemented. No, I'm trying ........

Favorites

Http://www.iteedu.com// OS /linux/linuxprgm/linuxcfunctions/index.php

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.