mbox series

[0/5] samples/kernfs: Add a pseudo-filesystem to demonstrate kernfs usage

Message ID 20250121153646.37895-1-me@davidreaver.com (mailing list archive)
Headers show
Series samples/kernfs: Add a pseudo-filesystem to demonstrate kernfs usage | expand

Message

David Reaver Jan. 21, 2025, 3:36 p.m. UTC
This patch series creates a toy pseudo-filesystem built on top of kernfs in
samples/kernfs/.

kernfs underpins the sysfs and cgroup filesystems. Many kernel developers have
considered kernfs for other pseudo-filesystems [1][2] and a draft patch was
proposed to investigate moving tracefs to kernfs [3]. One reason kernfs isn't
used more is it is almost entirely undocumented; I certainly had to read almost
all of the kernfs code to implement this toy filesystem. This sample aims to
improve kernfs documentation by way of an example.

The README.rst file in the first patch describes how sample_kernfs works from a
user's perspective. Summary: the filesystem automatically populates directories
with counter files that increment every time they are read. Users can adjust the
increment via inc files. Counter files can be reset by writing a new value to
them.

Subsequent patches build the rest of the filesystem. The commits are structured
to guide readers in learning kernfs components and adapting them to build their
own filesystems. If reviewers would prefer this all to be in one commit, I'm
happy to do that too. Initially, I included a more complex example where you
could read the sum of all child directory counters in a parent directory, but I
didn't want to complicate the sample too much and distract from kernfs. I’m
happy to remove the inc file if reviewers feel it's unnecessary. It is funny how
even a toy can suffer from feature creep :)

This is my first substantial kernel patch, so I welcome feedback on any trivial
errors. I tested this filesystem with all of the CONFIG_DEBUG_* and similar
options I could find and I ensured none of them report any issues. They were
particularly useful when debugging a deadlock that required replacing
kernfs_remove() with kernfs_remove_self(), and discovering a memory leak fixed
with kernfs_put().

In the future, I hope to contribute further by writing documentation for kernfs
and exploring the possibility of porting debugfs and/or tracefs to kernfs (like
completing the draft in [3]). I'm curious if the reviewers feel any of those
ideas are worth doing right now.

Link: https://lwn.net/Articles/960088/ [1]
Link: https://lwn.net/Articles/981155/ [2]
Link: https://lore.kernel.org/all/20240131-tracefs-kernfs-v1-0-f20e2e9a8d61@kernel.org/ [3]

David Reaver (5):
  samples/kernfs: Adds boilerplate/README for sample_kernfs
  samples/kernfs: Make filesystem mountable
  samples/kernfs: Add counter file to each directory
  samples/kernfs: Allow creating and removing directories
  samples/kernfs: Add inc file to allow changing counter increment

 MAINTAINERS                    |   1 +
 samples/Kconfig                |   6 +
 samples/Makefile               |   1 +
 samples/kernfs/Makefile        |   3 +
 samples/kernfs/README.rst      |  55 ++++++
 samples/kernfs/sample_kernfs.c | 321 +++++++++++++++++++++++++++++++++
 6 files changed, 387 insertions(+)
 create mode 100644 samples/kernfs/Makefile
 create mode 100644 samples/kernfs/README.rst
 create mode 100644 samples/kernfs/sample_kernfs.c


base-commit: fda5e3f284002ea55dac1c98c1498d6dd684046e