Linux tmpfs source code analysis (1)

Source: Internet
Author: User

Tmpfs is a file system in Linux based on memory/swap partitions. Unlike ramdisk, ramdisk is a block device and ext-based file system, therefore, the page cache memory replication cannot be bypassed. For details, refer to the ramdisk mentioned earlier. For tmpfs, the memory is directly operated as a file system, rather than Block devices.

How to bypass page cache is actually very simple, as long as you set your own file's const struct file_operations directly in the setup file system, let's see how tmpfs is implemented.

In Linux 2.6.18, the source code of tmpfs is mainly in the shmem. c file.

1. Define the tmpfs File System

static struct file_system_type tmpfs_fs_type = {.owner= THIS_MODULE,.name= "tmpfs",.get_sb= shmem_get_sb,.kill_sb= kill_litter_super,};

 

In the init_tmpfs function, use register_filesystem to register tmpfs to the file system.

 

2. Change the file_operations of the file struct.

In the shmem_file_setup function, the file-> f_op = & shmem_file_operations is changed. The following describes the specific struct.

static struct file_operations shmem_file_operations = {.mmap= shmem_mmap,#ifdef CONFIG_TMPFS.llseek= generic_file_llseek,.read= shmem_file_read,.write= shmem_file_write,.fsync= simple_sync_file,.sendfile= shmem_file_sendfile,#endif};

 

That is to say, when operating the tmpfs file, the function do_sync_read (read_write.c) in the common ext file system is not used, but the function shmem_file_read encapsulated by tmpfs is called, of course, the shmem_file_read does not operate on the page cache, although it still uses structures and algorithms such as maping, file, and inode in the page cache.

 

3. The shmem_file_read function is mainly used to call the do_shmem_file_read function. In the do_shmem_file_read function, shmem_getpage is the core function. You can quickly find the page through indexes and inode.

 

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.