Skip to content
/ bnull Public

Simple /dev/bnull replacement implemented as a device driver

License

Notifications You must be signed in to change notification settings

dmcbeing/bnull

Repository files navigation

bnull

=====

Simple /dev/null replacement implemented as a device driver

This project aims to:

  • Create a simple block device driver, useful as learning material
  • Starting point for more advanced device drivers.

In the following sections:

  • Describe what it does.
  • Tell how it can be used.
  • (Briefly) Explain the code.
  • Compare its performance against /dev/null and /dev/zero.

What it does:

It exports a large volatile(non-persistent) block device (/dev/bnull) similar in functionality to /den/null. Unlike /dev/null it is bi directional,that is you can write(as /dev/null) and read(similar to /dev/zero). Unlike /dev/zero it does not return zeroed pages,instead it only completes all I/O requests it receives.

How is that useful:

The primary purpose of this driver is to present the absolute minimum code necessary for a Linux block device driver. Additionally it can be used for:

  • Performance evaluation
  • Fault injection
  • As a starting point for more advanced device drivers.

(Brief) Explanation of the Code

Before explaining the code , i would recommend the Linux Device Drivers book, as my driver was influenced by their sbull driver. In most part i will link to relevant chapters of the book.

Kernel Module Hello World

Before explaining the completed bnull module we will write a hello world module. As is customary we will begin with the simplest possible kernel module that simply prints a message.

Unlike user applications kernel modules do not have the standard file input/output streams (stdin,stdout,stderr). The Linux kernel provides the kernel log,this log can be viewed with the dmesg program. Similar to the userspace pritnf function Linux provides us with the printk function. Just like printk printk has the format:

printk(format_string,...)

Where "..." denotes a variable number of arguments.

Most character specifiers are supported:

  • u,d,x,lu,ld,lx,llu,lld,llx for integers.
  • sub-specifiers such as width.

Floating point specifiers (such as "%f") are not supported, as the Linux kernel does not allow floating point operations in the first place. The reason for this seems to be portability,performance and overall code simplicity (see [here]).

The Code:

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>

static int bnull_init( void );
static void bnull_exit( void );

static int bnull_init()
{
  printk("Hello World!");
  return 0;
}

static void bnull_exit()
{
}

module_init( bnull_init );
module_exit( bnull_exit );

The function bnull_init as the name suggests is the initializing function of our module(similar to a Java/C++ constructor function for a class). Since every module(and programmer) follows different naming conventions we explicitly state the name of the init function with the module_init function. Similarly the module_exit function allows the programmer to specify the exit function (destructor function in object oriented lingo).

About

Simple /dev/bnull replacement implemented as a device driver

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published