public inbox for linux-fsdevel@vger.kernel.org
 help / color / mirror / Atom feed
From: David Reaver <me@davidreaver.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Tejun Heo <tj@kernel.org>
Cc: David Reaver <me@davidreaver.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Christian Brauner <brauner@kernel.org>,
	Al Viro <viro@zeniv.linux.org.uk>,
	Jonathan Corbet <corbet@lwn.net>,
	James Bottomley <James.Bottomley@HansenPartnership.com>,
	Krister Johansen <kjlx@templeofstupid.com>,
	linux-fsdevel@vger.kernel.org
Subject: [PATCH 2/5] samples/kernfs: Make filesystem mountable
Date: Tue, 21 Jan 2025 07:46:40 -0800	[thread overview]
Message-ID: <20250121154641.42975-1-me@davidreaver.com> (raw)
In-Reply-To: <20250121153646.37895-1-me@davidreaver.com>

Implements the bare minimum functionality to safely mount and unmount the
sample_kernfs filesystem.

Signed-off-by: David Reaver <me@davidreaver.com>
---
 samples/kernfs/sample_kernfs.c | 69 +++++++++++++++++++++++++++++++++-
 1 file changed, 68 insertions(+), 1 deletion(-)

diff --git a/samples/kernfs/sample_kernfs.c b/samples/kernfs/sample_kernfs.c
index 82d4b73a4534..3ea8411a72ae 100644
--- a/samples/kernfs/sample_kernfs.c
+++ b/samples/kernfs/sample_kernfs.c
@@ -6,12 +6,79 @@

 #define pr_fmt(fmt) "%s: " fmt, __func__

+#include <linux/fs.h>
+#include <linux/fs_context.h>
+#include <linux/kernfs.h>
 #include <linux/kernel.h>
 #include <linux/module.h>

+#define SAMPLE_KERNFS_MAGIC 0x8d000ff0
+
+static void sample_kernfs_fs_context_free(struct fs_context *fc)
+{
+	struct kernfs_fs_context *kfc = fc->fs_private;
+
+	kernfs_free_fs_context(fc);
+	kfree(kfc);
+}
+
+static const struct fs_context_operations sample_kernfs_fs_context_ops = {
+	.get_tree	= kernfs_get_tree,
+	.free		= sample_kernfs_fs_context_free,
+};
+
+static int sample_kernfs_init_fs_context(struct fs_context *fc)
+{
+	struct kernfs_fs_context *kfc;
+	struct kernfs_root *root;
+	int err;
+
+	kfc = kzalloc(sizeof(struct kernfs_fs_context), GFP_KERNEL);
+	if (!kfc)
+		return -ENOMEM;
+
+	root = kernfs_create_root(NULL, 0, NULL);
+	if (IS_ERR(root)) {
+		err = PTR_ERR(root);
+		goto err_free_kfc;
+	}
+
+	kfc->root = root;
+	kfc->magic = SAMPLE_KERNFS_MAGIC;
+	fc->fs_private = kfc;
+	fc->ops = &sample_kernfs_fs_context_ops;
+	fc->global = true;
+
+	return 0;
+
+err_free_kfc:
+	kfree(kfc);
+	return err;
+}
+
+static void sample_kernfs_kill_sb(struct super_block *sb)
+{
+	struct kernfs_root *root = kernfs_root_from_sb(sb);
+
+	kernfs_kill_sb(sb);
+	kernfs_destroy_root(root);
+}
+
+static struct file_system_type sample_kernfs_fs_type = {
+	.name			= "sample_kernfs",
+	.init_fs_context	= sample_kernfs_init_fs_context,
+	.kill_sb		= sample_kernfs_kill_sb,
+	.fs_flags		= FS_USERNS_MOUNT,
+};
+
 static int __init sample_kernfs_init(void)
 {
-	pr_info("Loaded sample_kernfs module.\n");
+	int err;
+
+	err = register_filesystem(&sample_kernfs_fs_type);
+	if (err)
+		return err;
+
 	return 0;
 }

  parent reply	other threads:[~2025-01-21 15:46 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-21 15:36 [PATCH 0/5] samples/kernfs: Add a pseudo-filesystem to demonstrate kernfs usage David Reaver
2025-01-21 15:46 ` [PATCH 1/5] samples/kernfs: Adds boilerplate/README for sample_kernfs David Reaver
2025-01-21 15:46 ` David Reaver [this message]
2025-01-21 15:46 ` [PATCH 3/5] samples/kernfs: Add counter file to each directory David Reaver
2025-01-21 15:47 ` [PATCH 4/5] samples/kernfs: Allow creating and removing directories David Reaver
2025-01-21 15:47 ` [PATCH 5/5] samples/kernfs: Add inc file to allow changing counter increment David Reaver
2025-01-28  6:08 ` [PATCH 0/5] samples/kernfs: Add a pseudo-filesystem to demonstrate kernfs usage Christoph Hellwig
2025-01-28 15:27   ` Steven Rostedt
2025-01-28 22:05     ` Linus Torvalds
2025-01-28 22:42       ` Steven Rostedt
2025-01-28 22:51         ` Tejun Heo
2025-01-28 23:29           ` Steven Rostedt
2025-01-28 23:38             ` Tejun Heo
2025-01-29  0:02               ` Steven Rostedt
2025-02-03 15:05                 ` Greg Kroah-Hartman
2025-02-03 16:12                   ` David Reaver

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250121154641.42975-1-me@davidreaver.com \
    --to=me@davidreaver.com \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=brauner@kernel.org \
    --cc=corbet@lwn.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=kjlx@templeofstupid.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=tj@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox