Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 10: Filesystems without Persistent Storage


10.2.4 Pseudo Filesystems


Recall from Section 8.4.1 that the kernel supports pseudo-filesystems that collect related inodes, but
cannot be mounted and are thus not visible in userland. Libfs also provides an auxiliary function to
implement this specialized type of filesystem.

The kernel employs a pseudo-filesystem to keep track of all inodes that represent block devices:

fs/block_dev.c
static int bd_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data, struct vfsmount *mnt)
{
return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576, mnt);
}

static struct file_system_type bd_type = {
.name = "bdev",
.get_sb = bd_get_sb,
.kill_sb = kill_anon_super,
};

The code looks as for any regular filesystem, but libfs provides the methodget_sb_pseudowhich
ensures that the filesystem cannot be mounted from userspace. This is simple: It just needs to set the
flagMS_NOUSERas discussed in Chapter 8. Besides, an instance ofstruct super_blockis filled in, and the
root inode for the pseudo-filesystem is allocated.

To use a pseudo-filesystem, the kernel needs to mount it usingkern_mountorkern_mount_data.Itcan
be used to collect inodes without the hassle of writing a specialized data structure to do so. Forbdev,all
inodes that represent block devices are grouped together. The collection, however, will only be visible to
the kernel and not to userspace.

10.3 Sysfs


Sysfs is a filesystem for exporting kernel objects to userspace, providing the ability to not only observe
properties of kernel-internal data structures, but also to modify them. Especially important is the highly
hierarchical organization of the filesystem layout: The entries of sysfs originate from kernel objects
(kobjects) as introduced in Chapter 1, and the hierarchical order of these is directly reflected in the
directory layout of sysfs.^5 Since all devices and buses of the system are organized viakobjects, sysfs
provides a representation of the system’s hardware topology.

In many cases, short, human readable text strings are used to export object properties, but passing binary
data to and from the kernel via sysfs is also frequently employed. Sysfs has become an alternative to the
more old-fashioned IOCTL mechanism. Instead of sending cryptic ioctls into the kernel, which usually
requires a C program, it is much simpler to read from or write a value to a sysfs file. A simple shell
command is sufficient. Another advantage is that a simple directory listing provides a quick overview on
what options can be set.

As for many virtual filesystems, sysfs was initiallybased on ramfs; thus, the implementation uses many
techniques known from other places in the kernel. Note that sysfs is always compiled into the kernel

(^5) The large number of extensively interconnected data structures known from the kobject mechanism is thus also directly transferred
to sysfs, at least when akobjectis exported to the filesystem.

Free download pdf