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.