kernel module: fix fops... and move its documentation into README

Sometimes I wonder if anyone has ever run this tutorial, otherwise how
can such basic bugs persist for so long?

test_all.sh: crete
This commit is contained in:
Ciro Santilli
2018-06-29 08:40:15 +01:00
parent 7f3671894f
commit 0cd1a2b602
7 changed files with 98 additions and 64 deletions

View File

@@ -21,7 +21,6 @@
. Pseudo filesystems
.. link:anonymous_inode.c[]
.. link:debugfs.c[]
.. link:fops.c[]
.. link:ioctl.c[]
.. link:mmap.c[]
.. link:poll.c[]

View File

@@ -59,7 +59,8 @@ static int myinit(void)
}
/* Created on the toplevel of the debugfs mount,
* and with explicit fops instead of a fixed integer value. */
* and with explicit fops instead of a fixed integer value.
*/
toplevel_file = debugfs_create_file(
"lkmc_debugfs_file", S_IWUSR, NULL, NULL, &fops);
if (!toplevel_file) {

View File

@@ -1,22 +1,3 @@
/*
Basic fops example, with a fixed size static data buffer.
Usage:
/fops.sh
The buffer can be written and read from. If data overflows, data is thrown away.
No, there ain't no official docs:
http://stackoverflow.com/questions/15213932/what-are-the-struct-file-operations-arguments
fops define what the kernel will do on filesystem system calls on all of
/dev, /proc, /sys, and consistute the main method of userland communication
in drivers (syscalls being the other one).
Here we use debugfs.
*/
#include <linux/debugfs.h>
#include <linux/errno.h> /* EFAULT */
#include <linux/fs.h> /* file_operations */
@@ -39,7 +20,7 @@ static int open(struct inode *inode, struct file *filp)
* We must increment this by the ammount of bytes read.
* Then when userland reads the same file descriptor again,
* we start from that point instead.
* */
*/
static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
{
ssize_t ret;
@@ -65,7 +46,8 @@ static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off
/* Similar to read, but with one notable difference:
* we must return ENOSPC if the user tries to write more
* than the size of our buffer. Otherwise, Bash > just
* keeps trying to write to it infinitely. */
* keeps trying to write to it infinitely.
*/
static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
{
ssize_t ret;
@@ -92,10 +74,9 @@ static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff
return ret;
}
/*
Called on the last close:
http://stackoverflow.com/questions/11393674/why-is-the-close-function-is-called-release-in-struct-file-operations-in-the-l
*/
/* Called on the last close:
* http://stackoverflow.com/questions/11393674/why-is-the-close-function-is-called-release-in-struct-file-operations-in-the-l
*/
static int release(struct inode *inode, struct file *filp)
{
pr_info("release\n");