I understand that mmap() allows an application to map a file into memory, so that there's a one-on-one correspondence between a memory address and a word in the file.
But my question is what if the file is a non-regular file created by a device driver? As I know, some non-regular files are mmap-able, some are not. What does that mean from programming's perspective? What should I do if I want my non-regular file to be mmap-able?
I have worked on a Linux-kernel-module in which I implemented mmap function pointer(struct file_operations). This module would create a device entry in /dev/ directory. Now my user-space application would open this entry using "open" and would make a mmap system call. Eventually inside the Linux-Kernel-module I insmoded the mmap function will be called and will do the implemented processing and will return back to the user-space.
This was just an example to represent the service requested by the user-space to the OS(Kernel).
When ever user wants to access hardware or wants to request service from the kernel(like mapping physical memory to user-virtual-address-space), it can do it using the entry created by the driver in the /dev/ or /sys/ or /proc/ etc. These files can be termed as "virtual interface" to the kernel.
Related
Is there an API to read the Open-Firmware device tree from userspace?
For example, reading /sys/firmware/fdt (flattened device tree)?
If not, is the file format published so a userspace application can read entries from the fdt?
This is easier than I first thought. On devices that support Open-Firmware, the linux kernel mounts it as a virtual filesystem at /proc/device-tree/.
In my case, on the systems I happened to be checking, this was a symbolic link to /sys/firmware/devicetree/base. But either way, walking through the files and directories in /proc/device-tree/ is relatively easy to do, regardless of the language.
When ever we fire a command on linux terminal.The process thus created traverses to the VFS layer,where it decides which file system function to be called like ext4 ,ext3 or anyother filesystem. So my question is How does the VFS differntiate the filesystems? form where the VFS gets the filesystem information,is it the fs_struct in task_struct that tells the VFS ?
As a part of the FS implementation you need to implement file, inode, superblock operations, which will register the the underlying FS ops (ex: ext3_open()) with VFS layer. Depending the the path to the file provided to the open(), VFS will invoke the appropriate file system specific implementation of the syscall.
Lets say you have already mounted a file system, when you are mounting a file system you register your FS for specific operations with VFS layer during the module initialization. During this step, two handlers get_sb() and kill_sb(). get_sb() is called at the time of mounting the file system. kill_sb() is called at the time of unmounting the file system.
For more information refer to RKFS and look into the how the file operations are implemented along with the data flow diagrams.
I need to know how to write a system call that blocks(lock) and unblocks(unlock) an archive(inode) or a partition(super_block) for read and write functions.
Example: these function are in fs.h
lock_super(struct super_block *);
unlock_super(struct super_block *);
How to obtain the super_block (/dev/sda1 for example)?
The lock_super and unlock_super calls are not meant to be controlled directly by the user level processes. It is only meant to be called by the VFS layer, when a operation(operation on inode) on the filesystem is called by the user process. If you still wish to do that, you have to write your own device driver and expose the desired functionality(locking unlocking of the inode) to the user level.
There are no current system calls that would allow you to lock, unlock inodes. There are many reasons why it is not wise to implement new system call, without due consideration. But if you wish to do that, you would need to write the handler of your own system call in the kernel. It seems you want fine-grain control over the file-system, perhaps you are implementing user-level file-system.
For the answer on how to get the super_block, every file-system module registers itself with the VFS(Virtual File System). VFS acts as a intermediate layer between user and the actual file-system. So, it is the VFS that knows about the function pointers to the lock_super and unlock_super methods. The VFS Superblock contains the "device info" and "set of pointers to the file-system superblock". You can get those pointers from here and call them. But remember, because the actual file-system is managed by the VFS, you would be potentially corrupting the data.
What are the different types of Linux files that can be created entirely in memory?
For example, a pipe file may be created, but does the location of where a file is created (or the filesystem type of the file's path) make a difference to whether a disk access is involved? If I create a pipe file in an ext3 file system, could a physical disk access result?
Off the top of my head, and without looking at any books :D, I think it breaks down like this:
mmap-able:
files (of course)
soft-links (final target if it's a file, block device or kernel device)
hard-links (final target if it's a file, block device or kernel device)
block devices (/dev/ram1, /dev/sda1, etc..)
character devices (You can mmap character devices, but in some cases it won't make sense (or work right). For instance an easy way to develop a driver in userland is to have a kernel module handle a basic mmap to your hardware and then expose the hardware via a mmapable character device so that a non-privileged user can access it. (USB, audio, flash cards) use this. A lot of embedded stuff does too.
unix domain sockets? Does zerocopy/sendfile count?
mmap-able but not a file?
shared memory
un-memmappable?
directories
fifos (one reader, one writer) ?
I am writing a program consisting of user program and a kernel module. The kernel module needs to gather data that it will then "send" to the user program. This has to be done via a /proc file. Now, I create the file, everything is fine, and spent ages reading the internet for answer and still cannot find one. How do you read/write a /proc file from the kernel space ? The write_proc and read_proc supplied to the procfile are used to read and write data from USER space, whereas I need the module to be able to write the /proc file itself.
That's not how it works. When a userspace program opens the files, they are generated on the fly on a case-by-case basis. Most of them are readonly and generated by a common mechanism:
Register an entry with create_proc_read_entry
Supply a callback function (called read_proc by convention) which is called when the file is read
This callback function should populate a supplied buffer and (typically) call proc_calc_metrics to update the file pointer etc supplied to userspace.
You (from the kernel) do not "write" to procfs files, you supply the results dynamically when userspace requests them.
One of the approaches to get data across to the user space would be seq_files. In order to configure (write) kernel parameters you may want to consider sys-fs nodes.
Thanks,
Vijay