From: Sascha Hauer <s.hauer@pengutronix.de>
To: BAREBOX <barebox@lists.infradead.org>
Subject: [PATCH 01/10] fs: implement mknod
Date: Thu, 27 Nov 2025 10:19:23 +0100 [thread overview]
Message-ID: <20251127-devfs-v1-1-4aff12818757@pengutronix.de> (raw)
In-Reply-To: <20251127-devfs-v1-0-4aff12818757@pengutronix.de>
This implements mknod for the barebox VFS. Use the cdevname for
connecting a device special node with a cdev. In Linux this is done with
major/minor numbers which shouldn't be necessary for barebox as we do
not intend to mount filesystems which contain device special nodes, but
instead create them in ramfs. If a mounted filesystem contains special
nodes then these won't work with barebox.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
fs/devfs.c | 13 +++++++++++++
fs/fs.c | 35 +++++++++++++++++++++++++++++++++++
include/fcntl.h | 6 ++++++
include/linux/fs.h | 13 +++++++++++++
4 files changed, 67 insertions(+)
diff --git a/fs/devfs.c b/fs/devfs.c
index 15c7a63d3949a5fa7c5ec15f58bc9f4c53b7852b..be3272be49e66eb843823b3ff664a1565f326790 100644
--- a/fs/devfs.c
+++ b/fs/devfs.c
@@ -103,6 +103,12 @@ static int devfs_open(struct inode *inode, struct file *f)
struct devfs_inode *node = container_of(inode, struct devfs_inode, inode);
struct cdev *cdev = node->cdev;
+ if (inode->cdevname) {
+ cdev = cdev_by_name(inode->cdevname);
+ if (!cdev)
+ return -ENOENT;
+ }
+
f->f_size = cdev->flags & DEVFS_IS_CHARACTER_DEV ?
FILE_SIZE_STREAM : cdev->size;
f->private_data = cdev;
@@ -188,6 +194,13 @@ static const struct file_operations devfs_file_operations = {
.memmap = devfs_memmap,
};
+void init_special_inode(struct inode *inode, umode_t mode, const char *cdevname)
+{
+ inode->i_mode = mode;
+ inode->i_fop = &devfs_file_operations;
+ inode->cdevname = strdup(cdevname);
+}
+
static int devfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
{
struct devfs_inode *dinode;
diff --git a/fs/fs.c b/fs/fs.c
index 528299e039d2d73b76c363b7ece32b6255d925f1..bfcfadff7fa76de17c68a0dca4843aa02cb62b6d 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -826,6 +826,8 @@ static int dentry_delete_subtree(struct super_block *sb, struct dentry *parent)
static void destroy_inode(struct inode *inode)
{
+ free(inode->cdevname);
+
if (inode->i_sb->s_op->destroy_inode)
inode->i_sb->s_op->destroy_inode(inode);
else
@@ -2745,6 +2747,39 @@ int openat(int dirfd, const char *pathname, int flags)
}
EXPORT_SYMBOL(openat);
+static int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
+ const char *cdevname)
+{
+ int error;
+
+ if (!dir->i_op->mknod)
+ return -EPERM;
+
+ error = dir->i_op->mknod(dir, dentry, mode, cdevname);
+
+ return error;
+}
+
+int mknodat(int dirfd, const char *pathname, mode_t mode, const char *devname)
+{
+ struct dentry *dentry;
+ struct path path;
+ int error;
+
+ dentry = filename_create(dirfd, getname(pathname), &path, 0);
+ if (IS_ERR(dentry)) {
+ error = PTR_ERR(dentry);
+ goto out;
+ }
+
+ error = vfs_mknod(path.dentry->d_inode, dentry, mode, devname);
+
+ dput(dentry);
+ path_put(&path);
+out:
+ return errno_set(error);
+}
+
int unlinkat(int dirfd, const char *pathname, int flags)
{
int ret;
diff --git a/include/fcntl.h b/include/fcntl.h
index db7926ee25fbe14607063d420697964801cd8321..57c01002cc9290bfae53f3b5be5615eda874c720 100644
--- a/include/fcntl.h
+++ b/include/fcntl.h
@@ -46,6 +46,8 @@ static inline int openat(int dirfd, const char *pathname, int flags, ...)
}
#endif
+int mknodat(int dirfd, const char *pathname, mode_t mode, const char *devname);
+
static inline int open(const char *pathname, int flags, ...)
{
return openat(AT_FDCWD, pathname, flags);
@@ -56,4 +58,8 @@ static inline int creat(const char *pathname, mode_t mode)
return open(pathname, O_CREAT | O_WRONLY | O_TRUNC);
}
+static inline int mknod(const char *pathname, mode_t mode, const char *devname)
+{
+ return mknodat(AT_FDCWD, pathname, mode, devname);
+}
#endif /* __FCNTL_H */
diff --git a/include/linux/fs.h b/include/linux/fs.h
index ed4332c79d496b18b37b120b6008e1e474316e76..f6b8f6a8e08b7e56716005453d6f9c4488ba853f 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -149,6 +149,8 @@ struct inode {
char *i_link;
+ char *cdevname;
+
void *i_private; /* fs or device private pointer */
};
@@ -429,6 +431,16 @@ void ihold(struct inode *inode);
void inc_nlink(struct inode *inode);
void clear_nlink(struct inode *inode);
void set_nlink(struct inode *inode, unsigned int nlink);
+#ifdef CONFIG_FS_DEVFS
+void init_special_inode(struct inode *inode, umode_t mode, const char *cdevname);
+#else
+static inline void init_special_inode(struct inode *inode, umode_t mode,
+ const char *cdevname)
+{
+}
+#endif
+
+struct cdev;
struct inode_operations {
struct dentry * (*lookup) (struct inode *,struct dentry *, unsigned int);
@@ -436,6 +448,7 @@ struct inode_operations {
const char *(*get_link) (struct dentry *dentry, struct inode *inode);
int (*create) (struct inode *,struct dentry *, umode_t);
+ int (*mknod) (struct inode *,struct dentry *, umode_t, const char *name);
int (*link) (struct dentry *,struct inode *,struct dentry *);
int (*unlink) (struct inode *,struct dentry *);
int (*symlink) (struct inode *,struct dentry *,const char *);
--
2.47.3
next prev parent reply other threads:[~2025-11-27 9:20 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-27 9:19 [PATCH 00/10] fs: Use device special nodes for devfs Sascha Hauer
2025-11-27 9:19 ` Sascha Hauer [this message]
2025-11-27 9:19 ` [PATCH 02/10] commands: add mknod command Sascha Hauer
2025-11-27 9:19 ` [PATCH 03/10] fs: ramfs: add device file support Sascha Hauer
2025-11-27 9:19 ` [PATCH 04/10] cdev: add cdev_size() helper Sascha Hauer
2025-11-27 9:19 ` [PATCH 05/10] fs: fix st_size for device files Sascha Hauer
2025-11-27 9:19 ` [PATCH 06/10] fs: retire devfs as filesystem Sascha Hauer
2025-11-27 9:19 ` [PATCH 07/10] fs: include cdevname in struct stat Sascha Hauer
2025-11-27 9:19 ` [PATCH 08/10] fs: stat_print: get cdevname from stat Sascha Hauer
2025-11-27 9:19 ` [PATCH 09/10] fs: replace cdev links with aliases Sascha Hauer
2025-11-27 9:19 ` [PATCH 10/10] ls: use ~0 for FILE_SIZE_STREAM Sascha Hauer
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=20251127-devfs-v1-1-4aff12818757@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
/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