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 <[email protected]> --- 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 d884726187dd526213f8b51a68a2e0db1bb50e58..45bf89653c7c2e4e9546f1d41f79a6389e0b796d 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 @@ -2747,6 +2749,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
