fs: Use device special nodes for devfs
Subsystems (2)
cover fs: Use device special nodes for devfs
Until now we have a devfs which is a filesystem of its own. This comes with a few limitations:
- we cannot create directories under /dev/
- Links are implemented in the devfs filesystem. They do not show up as regular links
This series overcomes these limitations by implementing device special nodes. These nodes are limited to ramfs, so we cannot mount arbitrary filesystems and use the device special files found on them. Nevertheless we can create and use device special files on ramfs which is enough to drop the devfs filesystem implementation and just use ramfs on /dev/.
This will in future allow us to create /dev/disk/by-* structures under /dev/.
---
Sascha Hauer (10):
fs: implement mknod
commands: add mknod command
fs: ramfs: add device file support
cdev: add cdev_size() helper
fs: fix st_size for device files
fs: retire devfs as filesystem
fs: include cdevname in struct stat
fs: stat_print: get cdevname from stat
fs: replace cdev links with aliases
ls: use ~0 for FILE_SIZE_STREAM
commands/Kconfig | 4 + commands/Makefile | 1 + commands/devinfo.c | 7 +- commands/ls.c | 8 +- commands/mknod.c | 43 +++++++++++ common/partitions.c | 2 +- common/startup.c | 2 +- drivers/block/dm/dm-core.c | 1 - fs/devfs-core.c | 179 ++++++++++++++++++++++++-------------------- fs/devfs.c | 180 +++------------------------------------------ fs/fs.c | 75 +++++++++++++------ fs/ramfs.c | 18 +++++ include/block.h | 2 - include/driver.h | 10 +-- include/fcntl.h | 6 ++ include/linux/fs.h | 13 ++++ include/linux/stat.h | 1 + 17 files changed, 266 insertions(+), 286 deletions(-)
---
base-commit: f018a6e4606ef31c6975af6b960aa7824f79df73
change-id: 20251127-devfs-c138cff5482a
Best regards,
Signature · 1 line
Sascha Hauer <[email protected]>
PATCH 1/10 fs: implement mknod
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.
--- fs/devfs.c | 13 +++++++++++++ fs/fs.c | 35 +++++++++++++++++++++++++++++++++++ include/fcntl.h | 6 ++++++ include/linux/fs.h | 13 +++++++++++++ 4 files changed, 67 insertions(+)
fs/devfs.c +13 -0
@@ -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;
fs/fs.c +35 -0
@@ -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;
include/fcntl.h +6 -0
@@ -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 */
include/linux/fs.h +13 -0
@@ -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 *);
Signature · 1 line
2.47.3
PATCH 2/10 commands: add mknod command
In reply to Sascha HauerAdd a mknod command. This is intended for testing/debugging purposes as normally device special nodes are created automatically in devfs.
--- commands/Kconfig | 4 ++++ commands/Makefile | 1 + commands/mknod.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+)
commands/Kconfig +4 -0
@@ -1144,6 +1144,10 @@ config CMD_MKDIR Options: -p make parent directories as needed +config CMD_MKNOD + tristate + prompt "mknod" + config CMD_PWD tristate default y
commands/Makefile +1 -0
@@ -34,6 +34,7 @@ obj-$(CONFIG_CMD_CD) += cd.o obj-$(CONFIG_CMD_PWD) += pwd.o obj-$(CONFIG_CMD_MKDIR) += mkdir.o obj-$(CONFIG_CMD_RMDIR) += rmdir.o +obj-$(CONFIG_CMD_MKNOD) += mknod.o obj-$(CONFIG_CMD_CP) += cp.o obj-$(CONFIG_CMD_RM) += rm.o obj-$(CONFIG_CMD_CAT) += cat.o
commands/mknod.c new file +43 -0
43 lines not on this page · raw message
Signature · 1 line
2.47.3
PATCH 3/10 fs: ramfs: add device file support
In reply to Sascha HauerImplement the mknod hook in ramfs for creating device special files.
--- fs/ramfs.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
fs/ramfs.c +18 -0
@@ -89,6 +89,9 @@ static struct inode *ramfs_get_inode(struct super_block *sb, const struct inodecase S_IFLNK : inode->i_op = &ramfs_symlink_inode_operations;break ; +case S_IFBLK : +case S_IFCHR : +break ; }return inode;
@@ -149,6 +152,20 @@ static int ramfs_create(struct inode *dir, struct dentry *dentry, umode_t mode)return ramfs_mknod (dir, dentry, mode |S_IFREG ); } +static int __ramfs_mknod (struct inode *dir,struct dentry *dentry,umode_t mode, +const char *cdevname) +{ +int ret; + + ret =ramfs_mknod (dir, dentry, mode); +if (ret) +return ret; + +init_special_inode (dentry->d_inode, mode, cdevname); + +return 0 ; +} +static int ramfs_symlink (struct inode *dir,struct dentry *dentry,const char *symname) {
@@ -196,6 +213,7 @@ static const struct inode_operations ramfs_dir_inode_operations = .unlink = simple_unlink, .create = ramfs_create, .tmpfile = ramfs_tmpfile, + .mknod = __ramfs_mknod, }; static struct ramfs_chunk *ramfs_find_chunk(struct ramfs_inode *node,
Signature · 1 line
2.47.3
PATCH 4/10 cdev: add cdev_size() helper
In reply to Sascha HauerWe'll need the size of a cdev later, add a helper for retrieving the size.
--- fs/devfs-core.c | 6 ++++++ fs/devfs.c | 3 +-- include/driver.h | 1 + 3 files changed, 8 insertions(+), 2 deletions(-)
fs/devfs-core.c +6 -0
@@ -435,6 +435,12 @@ int cdev_truncate(struct cdev *cdev, size_t size)return -EPERM ; } +loff_t cdev_size (struct cdev *cdev) +{ +return cdev->flags &DEVFS_IS_CHARACTER_DEV ? +FILE_SIZE_STREAM : cdev->size; +} +static struct cdev *cdev_alloc (const char *name) {struct cdev *new;
fs/devfs.c +1 -2
@@ -109,8 +109,7 @@ static int devfs_open(struct inode *inode, struct file *f)return -ENOENT ; } - f->f_size = cdev->flags &DEVFS_IS_CHARACTER_DEV ? -FILE_SIZE_STREAM : cdev->size; + f->f_size =cdev_size (cdev); f->private_data = cdev;return cdev_open (cdev, f->f_flags);
include/driver.h +1 -0
@@ -524,6 +524,7 @@ int cdev_protect(struct cdev*, size_t count, loff_t offset, int prot);int cdev_discard_range (struct cdev *,loff_t count,loff_t offset);int cdev_memmap (struct cdev *,void **map,int flags);int cdev_truncate (struct cdev *,size_t size); +loff_t cdev_size (struct cdev *cdev);loff_t cdev_unallocated_space (struct cdev *cdev);static inline bool cdev_is_partition (const struct cdev *cdev) {
Signature · 1 line
2.47.3
PATCH 5/10 fs: fix st_size for device files
In reply to Sascha HauerIn Linux device special files have zero size, but in barebox the device files traditionally have the size of the underlying device. Initialize struct stat::s_size with the cdev size.
--- fs/fs.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
fs/fs.c +13 -1
@@ -1100,11 +1100,23 @@ EXPORT_SYMBOL(readdir);static void stat_inode (struct inode *inode,struct stat *s) { +if (inode->cdevname) { +struct cdev *cdev; + + cdev =cdev_by_name (inode->cdevname); +if (cdev) { + s->st_size =cdev_size (cdev); + }else { + s->st_size =0 ; + } + }else { + s->st_size = inode->i_size; + } + s->st_ino = inode->i_ino; s->st_mode = inode->i_mode; s->st_uid = inode->i_uid; s->st_gid = inode->i_gid; - s->st_size = inode->i_size; }int fstat (int fd,struct stat *s)
Signature · 1 line
2.47.3
PATCH 6/10 fs: retire devfs as filesystem
In reply to Sascha HauerSwitch over to device special files for implementing devfs. With this we can drop the current devfs implementation as a filesystem driver.
--- common/startup.c | 2 +- fs/devfs-core.c | 35 +++++++++++ fs/devfs.c | 174 +------------------------------------------------------ include/driver.h | 1 + 4 files changed, 39 insertions(+), 173 deletions(-)
common/startup.c +1 -1
@@ -60,7 +60,7 @@ static int mount_root(void)mkdir ("/dev" ,0 );mkdir ("/tmp" ,0 );mkdir ("/mnt" ,0 ); -mount ("none" ,"devfs" ,"/dev" ,NULL ); +devfs_init ();if (IS_ENABLED (CONFIG_FS_EFIVARFS ) &&efi_is_payload ()) {mkdir ("/efivars" ,0 );
fs/devfs-core.c +35 -0
@@ -458,6 +458,39 @@ static void cdev_free(struct cdev *cdev)free (cdev); } +static bool devfs_initialized; + +static void devfs_mknod (struct cdev *cdev) +{ +char *path; +int ret; + +if (!devfs_initialized) +return ; + + path =xasprintf ("/dev/%s" , cdev->name); + +if (cdev->link) + ret =symlink (cdev->link->name, path); +else + ret =mknod (path,S_IFCHR |0600 , cdev->name); + +free (path); + +if (ret) +pr_err ("Failed to create /dev/%s: %pe\n" , cdev->name,ERR_PTR (ret)); +} + +void devfs_init (void ) +{ +struct cdev *cdev; + + devfs_initialized = true; + +for_each_cdev (cdev) +devfs_mknod (cdev); +} +int devfs_create (struct cdev *new) {struct cdev *cdev;
@@ -479,6 +512,8 @@ int devfs_create(struct cdev *new)if (new->link)list_add_tail (&new->link_entry, &new->link->links); +devfs_mknod (new); +return 0 ; }
fs/devfs.c +2 -172
190 lines not on this page · raw message
include/driver.h +1 -0
@@ -459,6 +459,7 @@ static inline const char *cdev_name(struct cdev *cdev)return cdev ? cdev->name :NULL ; } +void devfs_init (void );int devfs_create (struct cdev *);int devfs_create_link (struct cdev *,const char *name);int devfs_remove (struct cdev *);
Signature · 1 line
2.47.3
PATCH 7/10 fs: include cdevname in struct stat
In reply to Sascha HauerIn Linux struct stat contains the major/minor numbers of the underlying device, consequently we put the cdev name into struct stat.
--- fs/fs.c | 1 + include/linux/stat.h | 1 + 2 files changed, 2 insertions(+)
fs/fs.c +1 -0
@@ -1117,6 +1117,7 @@ static void stat_inode(struct inode *inode, struct stat *s) s->st_mode = inode->i_mode; s->st_uid = inode->i_uid; s->st_gid = inode->i_gid; + s->st_cdevname = inode->cdevname; }int fstat (int fd,struct stat *s)
include/linux/stat.h +1 -0
@@ -55,6 +55,7 @@ struct stat {unsigned short st_mode;unsigned short st_uid;unsigned short st_gid; +const char *st_cdevname;/* barebox specific */ loff_t st_size; };
Signature · 1 line
2.47.3
PATCH 8/10 fs: stat_print: get cdevname from stat
In reply to Sascha HauerIn stat_print() we have to take the filename component of the input path and retrieve the cdev from that name. Now that we have the cdevname in struct stat we can simplify this.
--- fs/fs.c | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-)
fs/fs.c +4 -21
@@ -137,7 +137,6 @@ void stat_print(int dirfd, const char *filename, const struct stat *st)struct fs_device *fdev;struct cdev *cdev =NULL ;const char *type =NULL , *typeprefix ="" ; -bool is_cdev_link = false;char modestr[11 ];mkmodestr (st->st_mode, modestr);
@@ -152,23 +151,10 @@ void stat_print(int dirfd, const char *filename, const struct stat *st)case S_IFREG : type ="regular file" ;break ; } -if (st->st_mode &S_IFCHR ) { -char *path; - - path =canonicalize_path (dirfd, filename); -if (path) { -const char *devicefile =devpath_to_name (path); -struct cdev *lcdev; - - lcdev =lcdev_by_name (devicefile); - cdev =cdev_readlink (lcdev); -if (cdev != lcdev) - is_cdev_link = true; -if (cdev) - bdev =cdev_get_block_device (cdev); - -free (path); - } +if ((st->st_mode &S_IFCHR ) && st->st_cdevname) { + cdev =cdev_by_name (st->st_cdevname); +if (cdev) + bdev =cdev_get_block_device (cdev); }printf (" File: %s" , filename);
@@ -182,9 +168,6 @@ void stat_print(int dirfd, const char *filename, const struct stat *st)printf (" -> <readlink error %pe>" ,ERR_PTR (ret)); else printf(" -> %s", realname); - }else if (is_cdev_link) { -printf (" ~> %s" , cdev->name); - typeprefix ="cdev link to " ; }printf ("\nSize: " );
Signature · 1 line
2.47.3
PATCH 9/10 fs: replace cdev links with aliases
In reply to Sascha Hauer--- commands/devinfo.c | 7 ++- common/partitions.c | 2 +- drivers/block/dm/dm-core.c | 1 - fs/devfs-core.c | 148 +++++++++++++++++++-------------------------- include/block.h | 2 - include/driver.h | 8 +-- 6 files changed, 71 insertions(+), 97 deletions(-)
commands/devinfo.c +4 -3
@@ -9,7 +9,8 @@static int do_devinfo_subtree (struct device *dev,int depth) {struct device *child; -struct cdev *cdev, *cdevl; +struct cdev *cdev; +struct string_list *sl;int i;for (i =0 ; i < depth; i++)
@@ -26,8 +27,8 @@ static int do_devinfo_subtree(struct device *dev, int depth) cdev->offset + cdev->size -1 ,size_human_readable (cdev->size), cdev->name); -list_for_each_entry (cdevl, &cdev->links, link_entry) -printf (", %s" , cdevl->name); +string_list_for_each_entry (sl, &cdev->aliases) +printf (", %s" , sl->str);printf ("\n" ); } }else {
common/partitions.c +1 -1
@@ -63,7 +63,7 @@ static int register_one_partition(struct block_device *blk, struct partition *pareturn 0 ; partition_name =xasprintf ("%s.%s" , blk->cdev.name, part->name); - ret =devfs_create_link (cdev, partition_name); + ret =devfs_add_alias (cdev, partition_name);if (ret)dev_warn (blk->dev,"Failed to create link from %s to %s\n" , partition_name, cdev->name);
drivers/block/dm/dm-core.c +0 -1
@@ -77,7 +77,6 @@ int dm_cdev_open(struct dm_cdev *dmcdev, const char *path, ulong flags,return -ENODEV ; } - dmcdev->cdev =cdev_readlink (dmcdev->cdev);break ;default : *errmsg =xstrdup ("Only regular files and device specials are supported" );
fs/devfs-core.c +63 -85
255 lines not on this page · raw message
include/block.h +0 -2
@@ -104,13 +104,11 @@ static inline bool cdev_is_block_device(const struct cdev *cdev)static inline bool cdev_is_block_partition (const struct cdev *cdev) { - cdev =cdev_readlink (cdev);return cdev_is_block_device (cdev) &&cdev_is_partition (cdev); }static inline bool cdev_is_block_disk (const struct cdev *cdev) { - cdev =cdev_readlink (cdev);return cdev_is_block_device (cdev) && !cdev_is_partition (cdev); }
include/driver.h +3 -5
@@ -17,6 +17,7 @@#include <init.h> #include <errno.h> #include <filetype.h> +#include <stringlist.h> #define FORMAT_DRIVER_NAME_ID "%s%d"
@@ -432,8 +433,7 @@ struct cdev {u16 typeflags;/* GPT type-specific attributes */ int open;struct mtd_info *mtd; -struct cdev *link; -struct list_head link_entry, links; +struct string_list aliases;struct list_head partition_entry, partitions;struct cdev *master;enum filetype filetype;
@@ -461,13 +461,11 @@ static inline const char *cdev_name(struct cdev *cdev)void devfs_init (void );int devfs_create (struct cdev *); -int devfs_create_link (struct cdev *,const char *name); +int devfs_add_alias (struct cdev *,const char *name);int devfs_remove (struct cdev *);int cdev_find_free_index (const char *);struct cdev *cdev_find_partition (struct cdev *cdevm,const char *name);struct cdev *device_find_partition (struct device *dev,const char *name); -struct cdev *lcdev_by_name (const char *filename); -struct cdev *cdev_readlink (const struct cdev *cdev);struct cdev *cdev_by_device_node (struct device_node *node);struct cdev *cdev_by_partuuid (const char *partuuid);struct cdev *cdev_by_diskuuid (const char *partuuid);
Signature · 1 line
2.47.3
PATCH 10/10 ls: use ~0 for FILE_SIZE_STREAM
In reply to Sascha HauerFor stream size files we used to show ((loff_t) -1) as file size which is quite unreadable. Use ~0 in this case.
--- commands/ls.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
commands/ls.c +6 -2
@@ -33,8 +33,12 @@ static void ls_one(const char *path, const char* fullname)return ;mkmodestr (s.st_mode, modestr); -printf ("%s %*llu %*.*s" , modestr,SIZELEN , s.st_size, namelen, - namelen, path); +if (s.st_size ==FILE_SIZE_STREAM ) +printf ("%s %*s %*.*s" , modestr,SIZELEN ,"~0" , namelen, + namelen, path); +else +printf ("%s %*llu %*.*s" , modestr,SIZELEN , s.st_size, namelen, + namelen, path);if (S_ISLNK (s.st_mode)) {char realname[PATH_MAX ];
Signature · 1 line
2.47.3