@@ -12702,6 +12702,7 @@ S: Supported
T: git git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core.git
F: fs/kernfs/
F: include/linux/kernfs.h
+F: samples/kernfs/
KEXEC
M: Eric Biederman <ebiederm@xmission.com>
@@ -291,6 +291,12 @@ config SAMPLE_CGROUP
help
Build samples that demonstrate the usage of the cgroup API.
+config SAMPLE_KERNFS
+ bool "Build sample_kernfs pseudo-filesystem."
+ help
+ Build a sample pseudo-filesystem that demonstrates the use of the
+ kernfs API. The filesystem name is sample_kernfs.
+
source "samples/rust/Kconfig"
endif # SAMPLES
@@ -9,6 +9,7 @@ obj-$(CONFIG_SAMPLE_CONNECTOR) += connector/
obj-$(CONFIG_SAMPLE_FANOTIFY_ERROR) += fanotify/
subdir-$(CONFIG_SAMPLE_HIDRAW) += hidraw
obj-$(CONFIG_SAMPLE_HW_BREAKPOINT) += hw_breakpoint/
+obj-$(CONFIG_SAMPLE_KERNFS) += kernfs/
obj-$(CONFIG_SAMPLE_KDB) += kdb/
obj-$(CONFIG_SAMPLE_KFIFO) += kfifo/
obj-$(CONFIG_SAMPLE_KOBJECT) += kobject/
new file mode 100644
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+obj-$(CONFIG_SAMPLE_KERNFS) += sample_kernfs.o
new file mode 100644
@@ -0,0 +1,55 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+===================================================
+Sample pseudo-filesystem built on top of ``kernfs``
+===================================================
+
+This directory contains a kernel module that implements a pseudo-filesystem
+built on top of ``kernfs`` and it demonstrates the basic of how to use ``kernfs``.
+
+Usage
+=====
+
+Compile your kernel with ``CONFIG_SAMPLE_KERNFS=y`` and create a
+``sample_kernfs`` mount with::
+
+ # mkdir /sample_kernfs
+ # mount -t sample_kernfs none /sample_kernfs
+
+Filesystem layout
+=================
+
+The filesystem contains a tree of counters. Here is an example, where
+``sample_kernfs`` is mounted at ``/sample_kernfs``::
+
+ /sample_kernfs
+ ├── counter
+ ├── inc
+ ├── sub1/
+ │ ├── counter
+ │ └── inc
+ └── sub2/
+ ├── counter
+ ├── inc
+ ├── sub3/
+ │ ├── counter
+ │ └── inc
+ └── sub4/
+ ├── counter
+ └── inc
+
+When a directory is created, it is automatically populated with two files:
+``counter`` and ``inc``. ``counter`` reports the current count for that node,
+and every time it is read it increments by the value in ``inc``. ``counter`` can
+be reset to a given value by writing that value to the ``counter`` file::
+
+ $ cat counter
+ 1
+ $ cat counter
+ 2
+ $ echo 4 > counter
+ $ cat counter
+ 5
+ $ echo 3 > inc
+ $ cat counter
+ 8
new file mode 100644
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * A sample kernel module showing how to build a pseudo-filesystem on top of
+ * kernfs.
+ */
+
+#define pr_fmt(fmt) "%s: " fmt, __func__
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+
+static int __init sample_kernfs_init(void)
+{
+ pr_info("Loaded sample_kernfs module.\n");
+ return 0;
+}
+
+module_init(sample_kernfs_init)
+MODULE_DESCRIPTION("Sample kernel module showing how to use kernfs");
+MODULE_LICENSE("GPL");
Adds the necessary Kconfig/Makefile boilerplate to get sample_kernfs compiled into the kernel. Also adds a README.rst file to describe how the filesystem works from a user's perspective. Signed-off-by: David Reaver <me@davidreaver.com> --- MAINTAINERS | 1 + samples/Kconfig | 6 ++++ samples/Makefile | 1 + samples/kernfs/Makefile | 3 ++ samples/kernfs/README.rst | 55 ++++++++++++++++++++++++++++++++++ samples/kernfs/sample_kernfs.c | 20 +++++++++++++ 6 files changed, 86 insertions(+) create mode 100644 samples/kernfs/Makefile create mode 100644 samples/kernfs/README.rst create mode 100644 samples/kernfs/sample_kernfs.c