diff mbox series

[1/5] samples/kernfs: Adds boilerplate/README for sample_kernfs

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

Commit Message

David Reaver Jan. 21, 2025, 3:46 p.m. UTC
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
diff mbox series

Patch

diff --git a/MAINTAINERS b/MAINTAINERS
index 0fa7c5728f1e..5791aced4b93 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -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>
diff --git a/samples/Kconfig b/samples/Kconfig
index b288d9991d27..968294ffb35d 100644
--- a/samples/Kconfig
+++ b/samples/Kconfig
@@ -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
diff --git a/samples/Makefile b/samples/Makefile
index b85fa64390c5..e024e76e396d 100644
--- a/samples/Makefile
+++ b/samples/Makefile
@@ -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/
diff --git a/samples/kernfs/Makefile b/samples/kernfs/Makefile
new file mode 100644
index 000000000000..3bd2e4773b91
--- /dev/null
+++ b/samples/kernfs/Makefile
@@ -0,0 +1,3 @@ 
+# SPDX-License-Identifier: GPL-2.0-only
+
+obj-$(CONFIG_SAMPLE_KERNFS) += sample_kernfs.o
diff --git a/samples/kernfs/README.rst b/samples/kernfs/README.rst
new file mode 100644
index 000000000000..e0e747514df1
--- /dev/null
+++ b/samples/kernfs/README.rst
@@ -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
diff --git a/samples/kernfs/sample_kernfs.c b/samples/kernfs/sample_kernfs.c
new file mode 100644
index 000000000000..82d4b73a4534
--- /dev/null
+++ b/samples/kernfs/sample_kernfs.c
@@ -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");