fs: Use device special nodes for devfs
Subsystems (3)
v2 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/.
---
Changes in v2:
- Fix storage-by-uuid driver for devfs changes
- remove created device special files and links on devfs_remove()
- Link to v1: https://lore.barebox.org/[email protected]
---
Sascha Hauer (13):
fs: devfs-core: add devfs_create_link_node()
storage-by-alias: drop fake cdev
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
common: cdev-alias: rename struct
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/cdev-alias.c | 12 +-- common/partitions.c | 2 +- common/startup.c | 2 +- drivers/block/dm/dm-core.c | 1 - drivers/misc/storage-by-alias.c | 120 ++-------------------- fs/devfs-core.c | 214 +++++++++++++++++++++++++--------------- fs/devfs.c | 180 +++------------------------------ fs/fs.c | 75 +++++++++----- fs/ramfs.c | 18 ++++ include/block.h | 2 - include/driver.h | 20 +++- include/fcntl.h | 6 ++ include/linux/fs.h | 13 +++ include/linux/stat.h | 1 + 19 files changed, 327 insertions(+), 402 deletions(-)
---
base-commit: 500df27eb054f86ce232c5bb30c3a7ea8f771e61
change-id: 20251127-devfs-c138cff5482a
Best regards,
Signature · 1 line
Sascha Hauer <[email protected]>
PATCH v2 1/13 fs: devfs-core: add devfs_create_link_node()
Allow to create a devfs link that not only has its own name, but also its own device_node.
--- fs/devfs-core.c | 8 +++++++- include/driver.h | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-)
fs/devfs-core.c +7 -1
@@ -476,7 +476,7 @@ int devfs_create(struct cdev *new)return 0 ; } -int devfs_create_link (struct cdev *cdev,const char *name) +int devfs_create_link_node (struct cdev *cdev,const char *name,struct device_node *node) {struct cdev *new;int ret;
@@ -489,6 +489,7 @@ int devfs_create_link(struct cdev *cdev, const char *name) new =cdev_alloc (name); new->link = cdev; + new->device_node = node; ret =devfs_create (new);if (ret) {
@@ -512,6 +513,11 @@ int devfs_create_link(struct cdev *cdev, const char *name)return 0 ; } +int devfs_create_link (struct cdev *cdev,const char *name) +{ +return devfs_create_link_node (cdev, name,NULL ); +} +int devfs_remove (struct cdev *cdev) {struct cdev *c, *tmp;
include/driver.h +2 -0
@@ -461,6 +461,8 @@ static inline const char *cdev_name(struct cdev *cdev)int devfs_create (struct cdev *);int devfs_create_link (struct cdev *,const char *name); +int devfs_create_link_node (struct cdev *cdev,const char *name, +struct device_node *node);int devfs_remove (struct cdev *);int cdev_find_free_index (const char *);struct cdev *cdev_find_partition (struct cdev *cdevm,const char *name);
Signature · 1 line
2.47.3
Re: [PATCH v2 01/13] fs: devfs-core: add devfs_create_link_node()
Quoted 4 lines · Sascha Hauer
Allow to create a devfs link that not only has its own name, but also its own
device_node.Signed-off-by Sascha Hauer <[email protected]>
Quoted 52 lines
--- fs/devfs-core.c | 8 +++++++- include/driver.h | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-)fs/devfs-core.c +7
-1@@ -476,7 +476,7 @@ int devfs_create(struct cdev *new)return 0 ; } -int devfs_create_link (struct cdev *cdev,const char *name) +int devfs_create_link_node (struct cdev *cdev,const char *name,struct device_node *node) {struct cdev *new;int ret;@@ -489,6 +489,7 @@ int devfs_create_link(struct cdev *cdev, const char *name) new =cdev_alloc (name); new->link = cdev; + new->device_node = node; ret =devfs_create (new);if (ret) {@@ -512,6 +513,11 @@ int devfs_create_link(struct cdev *cdev, const char *name)return 0 ; } +int devfs_create_link (struct cdev *cdev,const char *name) +{ +return devfs_create_link_node (cdev, name,NULL ); +} +int devfs_remove (struct cdev *cdev) {struct cdev *c, *tmp;include/driver.h +2
-0@@ -461,6 +461,8 @@ static inline const char *cdev_name(struct cdev *cdev)int devfs_create (struct cdev *);int devfs_create_link (struct cdev *,const char *name); +int devfs_create_link_node (struct cdev *cdev,const char *name, +struct device_node *node);int devfs_remove (struct cdev *);int cdev_find_free_index (const char *);struct cdev *cdev_find_partition (struct cdev *cdevm,const char *name);
Signature · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
PATCH v2 2/13 storage-by-alias: drop fake cdev
In reply to Sascha Hauerstorage-by-alias creates its own cdev, but that is never used. The only purpose is to create a cdev with its own device node which otherwise links to the original cdev. This means we can drop all the cdev operations and use the newly created devfs_create_link_node() with the same effect.
--- drivers/misc/storage-by-alias.c | 120 +++------------------------------------- 1 file changed, 7 insertions(+), 113 deletions(-)
drivers/misc/storage-by-alias.c +7 -113
@@ -24,102 +24,8 @@ static LIST_HEAD(sba_list);struct sba {char *alias;struct device *dev; -struct cdev *rcdev; -struct cdev cdev;struct list_head list; -}; - -static ssize_t sba_read (struct cdev *cdev,void *buf,size_t count,loff_t offset,ulong flags) -{ -struct sba *sba = cdev->priv; - -return cdev_read (sba->rcdev, buf, count, offset, flags); -} - -static ssize_t sba_write (struct cdev *cdev,const void *buf,size_t count,loff_t offset,ulong flags) -{ -struct sba *sba = cdev->priv; - -return cdev_write (sba->rcdev, buf, count, offset, flags); -} - -static int sba_ioctl (struct cdev *cdev,unsigned int request,void *buf) -{ -struct sba *sba = cdev->priv; - -return cdev_ioctl (sba->rcdev, request, buf); -} - -static int sba_open (struct cdev *cdev,unsigned long flags) -{ -struct sba *sba = cdev->priv; - -return cdev_open (sba->rcdev, flags); -} - -static int sba_close (struct cdev *cdev) -{ -struct sba *sba = cdev->priv; - -cdev_close (sba->rcdev); - -return 0 ; -} - -static int sba_flush (struct cdev *cdev) -{ -struct sba *sba = cdev->priv; - -return cdev_flush (sba->rcdev); -} - -static int sba_erase (struct cdev *cdev,loff_t count,loff_t offset) -{ -struct sba *sba = cdev->priv; - -return cdev_erase (sba->rcdev, count, offset); -} - -static int sba_protect (struct cdev *cdev,size_t count,loff_t offset,int prot) -{ -struct sba *sba = cdev->priv; - -return cdev_protect (sba->rcdev, count, offset, prot); -} - -static int sba_discard_range (struct cdev *cdev,loff_t count,loff_t offset) -{ -struct sba *sba = cdev->priv; - -return cdev_discard_range (sba->rcdev, count, offset); -} - -static int sba_memmap (struct cdev *cdev,void **map,int flags) -{ -struct sba *sba = cdev->priv; - -return cdev_memmap (sba->rcdev, map, flags); -} - -static int sba_truncate (struct cdev *cdev,size_t size) -{ -struct sba *sba = cdev->priv; - -return cdev_truncate (sba->rcdev, size); -} - -static struct cdev_operations sba_ops = { - .read = sba_read, - .write = sba_write, - .ioctl = sba_ioctl, - .open = sba_open, - .close = sba_close, - .flush = sba_flush, - .erase = sba_erase, - .protect = sba_protect, - .discard_range = sba_discard_range, - .memmap = sba_memmap, - .truncate = sba_truncate, +bool populated; };static int sba_add_partitions (struct cdev *rcdev,void *data)
@@ -127,29 +33,17 @@ static int sba_add_partitions(struct cdev *rcdev, void *data)struct sba *sba = data;int ret; -dev_dbg (sba->dev,"Adding %s -> %s\n" , sba->alias, rcdev->name); - -if (sba->rcdev) +if (sba->populated)return 0 ; - sba->rcdev = rcdev; - sba->cdev.link = rcdev; - sba->cdev.name = sba->alias; - sba->cdev.size = rcdev->size; - sba->cdev.ops = &sba_ops; - sba->cdev.dev = sba->dev; - sba->cdev.priv = sba; +dev_info (sba->dev,"Adding %s -> %s\n" , sba->alias, rcdev->name); -if (rcdev->flags &DEVFS_WRITE_AUTOERASE ) - sba->cdev.flags |=DEVFS_WRITE_AUTOERASE ; + ret =devfs_create_link_node (rcdev, sba->alias, sba->dev->device_node); +if (ret) +return ret; - ret =devfs_create (&sba->cdev); -if (ret) { -dev_err (sba->dev,"Failed to create cdev: %pe\n" ,ERR_PTR (ret)); -return 0 ; - } + sba->populated = true; -of_parse_partitions (&sba->cdev, sba->dev->of_node);return 0 ; }
Signature · 1 line
2.47.3
Re: [PATCH v2 02/13] storage-by-alias: drop fake cdev
Quoted 7 lines · Sascha Hauer
storage-by-alias creates its own cdev, but that is never used. The only purpose is to create a cdev with its own device node which otherwise links to the original cdev. This means we can drop all the cdev operations and use the newly created
devfs_create_link_node()with the same effect.Signed-off-by Sascha Hauer <[email protected]>
Quoted 149 lines
--- drivers/misc/storage-by-alias.c | 120 +++------------------------------------- 1 file changed, 7 insertions(+), 113 deletions(-)drivers/misc/storage-by-alias.c +7
-113@@ -24,102 +24,8 @@ static LIST_HEAD(sba_list);struct sba {char *alias;struct device *dev; -struct cdev *rcdev; -struct cdev cdev;struct list_head list; -}; - -static ssize_t sba_read (struct cdev *cdev,void *buf,size_t count,loff_t offset,ulong flags) -{ -struct sba *sba = cdev->priv; - -return cdev_read (sba->rcdev, buf, count, offset, flags); -} - -static ssize_t sba_write (struct cdev *cdev,const void *buf,size_t count,loff_t offset,ulong flags) -{ -struct sba *sba = cdev->priv; - -return cdev_write (sba->rcdev, buf, count, offset, flags); -} - -static int sba_ioctl (struct cdev *cdev,unsigned int request,void *buf) -{ -struct sba *sba = cdev->priv; - -return cdev_ioctl (sba->rcdev, request, buf); -} - -static int sba_open (struct cdev *cdev,unsigned long flags) -{ -struct sba *sba = cdev->priv; - -return cdev_open (sba->rcdev, flags); -} - -static int sba_close (struct cdev *cdev) -{ -struct sba *sba = cdev->priv; - -cdev_close (sba->rcdev); - -return 0 ; -} - -static int sba_flush (struct cdev *cdev) -{ -struct sba *sba = cdev->priv; - -return cdev_flush (sba->rcdev); -} - -static int sba_erase (struct cdev *cdev,loff_t count,loff_t offset) -{ -struct sba *sba = cdev->priv; - -return cdev_erase (sba->rcdev, count, offset); -} - -static int sba_protect (struct cdev *cdev,size_t count,loff_t offset,int prot) -{ -struct sba *sba = cdev->priv; - -return cdev_protect (sba->rcdev, count, offset, prot); -} - -static int sba_discard_range (struct cdev *cdev,loff_t count,loff_t offset) -{ -struct sba *sba = cdev->priv; - -return cdev_discard_range (sba->rcdev, count, offset); -} - -static int sba_memmap (struct cdev *cdev,void **map,int flags) -{ -struct sba *sba = cdev->priv; - -return cdev_memmap (sba->rcdev, map, flags); -} - -static int sba_truncate (struct cdev *cdev,size_t size) -{ -struct sba *sba = cdev->priv; - -return cdev_truncate (sba->rcdev, size); -} - -static struct cdev_operations sba_ops = { - .read = sba_read, - .write = sba_write, - .ioctl = sba_ioctl, - .open = sba_open, - .close = sba_close, - .flush = sba_flush, - .erase = sba_erase, - .protect = sba_protect, - .discard_range = sba_discard_range, - .memmap = sba_memmap, - .truncate = sba_truncate, +bool populated; };static int sba_add_partitions (struct cdev *rcdev,void *data)@@ -127,29 +33,17 @@ static int sba_add_partitions(struct cdev *rcdev, void *data)struct sba *sba = data;int ret; -dev_dbg (sba->dev,"Adding %s -> %s\n" , sba->alias, rcdev->name); - -if (sba->rcdev) +if (sba->populated)return 0 ; - sba->rcdev = rcdev; - sba->cdev.link = rcdev; - sba->cdev.name = sba->alias; - sba->cdev.size = rcdev->size; - sba->cdev.ops = &sba_ops; - sba->cdev.dev = sba->dev; - sba->cdev.priv = sba; +dev_info (sba->dev,"Adding %s -> %s\n" , sba->alias, rcdev->name); -if (rcdev->flags &DEVFS_WRITE_AUTOERASE ) - sba->cdev.flags |=DEVFS_WRITE_AUTOERASE ; + ret =devfs_create_link_node (rcdev, sba->alias, sba->dev->device_node); +if (ret) +return ret; - ret =devfs_create (&sba->cdev); -if (ret) { -dev_err (sba->dev,"Failed to create cdev: %pe\n" ,ERR_PTR (ret)); -return 0 ; - } + sba->populated = true; -of_parse_partitions (&sba->cdev, sba->dev->of_node);return 0 ; }
Signature · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
PATCH v2 3/13 fs: implement mknod
In reply to Sascha HauerThis 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
@@ -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;
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
Re: [PATCH v2 03/13] fs: implement mknod
Quoted 8 lines · Sascha Hauer
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]>
With two minor points below addressed:
Quoted 25 lines
--- 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 +6
-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;
fs/devfs.c static const struct file_operations devfs_file_operations = { · line 201
Quoted 8 lines
fs/devfs.c +5
-0@@ -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);
xstrdup_const? or return an error?
Quoted 10 lines
fs/devfs.c +2
-0+} +static int devfs_lookup_revalidate (struct dentry *dentry,unsigned int flags) {struct devfs_inode *dinode;fs/fs.c +0
-0
fs/fs.c static int dentry_delete_subtree(struct super_block *sb, struct dentry *parent) · line 829
Quoted 4 lines
fs/fs.c +1
-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);
free_const?
Cheers,
Ahmad
Signature · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
PATCH v2 4/13 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
Re: [PATCH v2 04/13] commands: add mknod command
Hi,
Quoted 4 lines · Sascha Hauer
Add a mknod command. This is intended for testing/debugging purposes as normally device special nodes are created automatically in devfs.
Signed-off-by Sascha Hauer <[email protected]>
Single nitpick below:
commands/mknod.c line 11
Quoted 6 lines
commands/mknod.c new file +6
-0+static int do_mknod (int argc,char *argv[]) +{ +const char *filename, *cdevname; +umode_t mode; + +if (argc <4 )
argc != 4 to reduce misuse.
Quoted 33 lines
commands/mknod.c new file +32
-0+return COMMAND_ERROR_USAGE ; + + filename = argv[1 ]; +if (!strcmp (argv[2 ],"b" )) + mode =S_IFBLK ; +else if (!strcmp (argv[2 ],"c" )) + mode =S_IFCHR ; +else +return COMMAND_ERROR_USAGE ; + + cdevname = argv[3 ]; + +return mknod (filename, mode, cdevname); +} + +BAREBOX_CMD_HELP_START(mknod) +BAREBOX_CMD_HELP_TEXT("make a device special node.") +BAREBOX_CMD_HELP_TEXT("") +BAREBOX_CMD_HELP_TEXT("usage: mknod <name> c|b <cdevname>") +BAREBOX_CMD_HELP_TEXT("") +BAREBOX_CMD_HELP_TEXT("Create a device special node named <name> directing") +BAREBOX_CMD_HELP_TEXT("to cdev <cdevname>. This can either be a block (b) or") +BAREBOX_CMD_HELP_TEXT("character (c) device.") +BAREBOX_CMD_HELP_END + +BAREBOX_CMD_START (mknod) + .cmd = do_mknod, + BAREBOX_CMD_DESC("make device nodes") + BAREBOX_CMD_OPTS("NAME TYPE CDEVNAME") + BAREBOX_CMD_GROUP(CMD_GRP_FILE) + BAREBOX_CMD_HELP(cmd_mknod_help) +BAREBOX_CMD_END
Signature · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
PATCH v2 5/13 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
Re: [PATCH v2 05/13] fs: ramfs: add device file support
Hi,
Quoted 3 lines · Sascha Hauer
Implement the mknod hook in ramfs for creating device special files.
Signed-off-by Sascha Hauer <[email protected]>
Quoted 48 lines
--- 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 · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
PATCH v2 6/13 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
@@ -526,6 +526,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
Re: [PATCH v2 06/13] cdev: add cdev_size() helper
Quoted 4 lines · Sascha Hauer
We'll need the size of a cdev later, add a helper for retrieving the size.
Signed-off-by Sascha Hauer <[email protected]>
Quoted 50 lines
--- 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@@ -526,6 +526,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 · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
PATCH v2 7/13 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
Re: [PATCH v2 07/13] fs: fix st_size for device files
Quoted 5 lines · Sascha Hauer
In 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_sizewith the cdev size.Signed-off-by Sascha Hauer <[email protected]>
Single nitpick below:
Quoted 11 lines
--- fs/fs.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)fs/fs.c +0
-0@@ -1100,11 +1100,23 @@ EXPORT_SYMBOL(readdir);static void stat_inode (struct inode *inode,struct stat *s)
fs/fs.c EXPORT_SYMBOL(readdir); · line 1111
Quoted 10 lines
fs/fs.c +9
-0{ +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 ; + }
s->st_size = cdev ? cdev_size(cdev) : 0;
Quoted 13 lines
fs/fs.c +4
-1+ }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 · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
PATCH v2 8/13 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_create_link_node(struct cdev *cdev, const char *name,
Signature · 1 line
2.47.3
Re: [PATCH v2 08/13] fs: retire devfs as filesystem
Quoted 4 lines · Sascha Hauer
Switch over to device special files for implementing devfs. With this we can drop the current devfs implementation as a filesystem driver.
Signed-off-by Sascha Hauer <[email protected]>
fs/devfs-core.c static void cdev_free(struct cdev *cdev) · line 474
Quoted 12 lines
fs/devfs-core.c +12
-0+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);
\o/
Signature · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
PATCH v2 9/13 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
Re: [PATCH v2 09/13] fs: include cdevname in struct stat
Quoted 4 lines · Sascha Hauer
In Linux struct stat contains the major/minor numbers of the underlying device, consequently we put the cdev name into struct stat.
Signed-off-by Sascha Hauer <[email protected]>
Quoted 30 lines
--- 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 · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
PATCH v2 10/13 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
Re: [PATCH v2 10/13] fs: stat_print: get cdevname from stat
Quoted 5 lines · Sascha Hauer
In
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.Signed-off-by Sascha Hauer <[email protected]>
Quoted 55 lines
--- 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 · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
PATCH v2 11/13 common: cdev-alias: rename struct
In reply to Sascha HauerWe'll introduce a globally available struct cdev_alias in the next step, so rename struct cdev_alias in common/cdev-alias.c to avoid name conflicts.
--- common/cdev-alias.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
common/cdev-alias.c +6 -6
@@ -11,13 +11,13 @@#include <driver.h> #include <init.h> -struct cdev_alias { +struct cdev_alias_res {const char *name; -int (*resolve)(struct cdev_alias *,const char *arg, +int (*resolve)(struct cdev_alias_res *,const char *arg,cdev_alias_processor_t fn,void *data); }; -static int cdev_alias_resolve_bootsource (struct cdev_alias *cdev_alias, +static int cdev_alias_resolve_bootsource (struct cdev_alias_res *cdev_alias_res,const char *partname,cdev_alias_processor_t fn,void *data)
@@ -37,7 +37,7 @@ static int cdev_alias_resolve_bootsource(struct cdev_alias *cdev_alias,return fn (cdev, data); } -static int cdev_alias_resolve_diskuuid (struct cdev_alias *cdev_alias, +static int cdev_alias_resolve_diskuuid (struct cdev_alias_res *cdev_alias_res,const char *uuid,cdev_alias_processor_t fn,void *data)
@@ -67,7 +67,7 @@ static int cdev_alias_resolve_diskuuid(struct cdev_alias *cdev_alias,return 0 ; } -static struct cdev_alias cdev_alias_aliases[] = { +static struct cdev_alias_res cdev_alias_aliases[] = { {"bootsource" , cdev_alias_resolve_bootsource }, {"diskuuid" , cdev_alias_resolve_diskuuid }, {/* sentinel */ }
@@ -76,7 +76,7 @@ static struct cdev_alias cdev_alias_aliases[] = {int cdev_alias_resolve_for_each (const char *name,cdev_alias_processor_t fn,void *data) { -struct cdev_alias *alias; +struct cdev_alias_res *alias;int ret =0 ;char *buf, *arg;
Signature · 1 line
2.47.3
Re: [PATCH v2 11/13] common: cdev-alias: rename struct
Quoted 5 lines · Sascha Hauer
We'll introduce a globally available struct
cdev_aliasin the next step, so rename structcdev_aliasincommon/cdev-alias.cto avoid name conflicts.Signed-off-by Sascha Hauer <[email protected]>
Quoted 53 lines
--- common/cdev-alias.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)common/cdev-alias.c +6
-6@@ -11,13 +11,13 @@#include <driver.h> #include <init.h> -struct cdev_alias { +struct cdev_alias_res {const char *name; -int (*resolve)(struct cdev_alias *,const char *arg, +int (*resolve)(struct cdev_alias_res *,const char *arg,cdev_alias_processor_t fn,void *data); }; -static int cdev_alias_resolve_bootsource (struct cdev_alias *cdev_alias, +static int cdev_alias_resolve_bootsource (struct cdev_alias_res *cdev_alias_res,const char *partname,cdev_alias_processor_t fn,void *data)@@ -37,7 +37,7 @@ static int cdev_alias_resolve_bootsource(struct cdev_alias *cdev_alias,return fn (cdev, data); } -static int cdev_alias_resolve_diskuuid (struct cdev_alias *cdev_alias, +static int cdev_alias_resolve_diskuuid (struct cdev_alias_res *cdev_alias_res,const char *uuid,cdev_alias_processor_t fn,void *data)@@ -67,7 +67,7 @@ static int cdev_alias_resolve_diskuuid(struct cdev_alias *cdev_alias,return 0 ; } -static struct cdev_alias cdev_alias_aliases[] = { +static struct cdev_alias_res cdev_alias_aliases[] = { {"bootsource" , cdev_alias_resolve_bootsource }, {"diskuuid" , cdev_alias_resolve_diskuuid }, {/* sentinel */ }@@ -76,7 +76,7 @@ static struct cdev_alias cdev_alias_aliases[] = {int cdev_alias_resolve_for_each (const char *name,cdev_alias_processor_t fn,void *data) { -struct cdev_alias *alias; +struct cdev_alias_res *alias;int ret =0 ;char *buf, *arg;
Signature · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
PATCH v2 12/13 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 - drivers/misc/storage-by-alias.c | 2 +- fs/devfs-core.c | 191 +++++++++++++++++++++------------------- include/block.h | 2 - include/driver.h | 20 +++-- 7 files changed, 121 insertions(+), 104 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 cdev_alias *alias;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); +cdev_for_each_alias (alias, cdev) +printf (", %s" , alias->name);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" );
drivers/misc/storage-by-alias.c +1 -1
@@ -38,7 +38,7 @@ static int sba_add_partitions(struct cdev *rcdev, void *data)dev_info (sba->dev,"Adding %s -> %s\n" , sba->alias, rcdev->name); - ret =devfs_create_link_node (rcdev, sba->alias, sba->dev->device_node); + ret =devfs_add_alias_node (rcdev, sba->alias, sba->dev->device_node);if (ret)return ret;
fs/devfs-core.c +102 -89
308 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 +13 -7
@@ -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 list_head aliases;struct list_head partition_entry, partitions;struct cdev *master;enum filetype filetype;
@@ -443,6 +443,15 @@ struct cdev { }; }; +#define cdev_for_each_alias (alias, cdev) \ + list_for_each_entry(alias, &cdev->aliases, list) + +struct cdev_alias { +char *name; +struct device_node *device_node; +struct list_head list; +}; +static inline struct device_node *cdev_of_node (const struct cdev *cdev) {return IS_ENABLED (CONFIG_OFDEVICE ) ? cdev->device_node :NULL ;
@@ -461,15 +470,12 @@ 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_create_link_node (struct cdev *cdev,const char *name, -struct device_node *node); +int devfs_add_alias (struct cdev *,const char *name); +int devfs_add_alias_node (struct cdev *,const char *name,struct device_node *np);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
Re: [PATCH v2 12/13] fs: replace cdev links with aliases
Hi,
On 12/9/25 1:51 PM, Sascha Hauer wrote:
A commit message would be grand. :)
Quoted 1 line
Signed-off-by Sascha Hauer <[email protected]>
Quoted 472 lines
--- commands/devinfo.c | 7 +- common/partitions.c | 2 +- drivers/block/dm/dm-core.c | 1 - drivers/misc/storage-by-alias.c | 2 +- fs/devfs-core.c | 191 +++++++++++++++++++++------------------- include/block.h | 2 - include/driver.h | 20 +++-- 7 files changed, 121 insertions(+), 104 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 cdev_alias *alias;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); +cdev_for_each_alias (alias, cdev) +printf (", %s" , alias->name);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" );drivers/misc/storage-by-alias.c +1
-1@@ -38,7 +38,7 @@ static int sba_add_partitions(struct cdev *rcdev, void *data)dev_info (sba->dev,"Adding %s -> %s\n" , sba->alias, rcdev->name); - ret =devfs_create_link_node (rcdev, sba->alias, sba->dev->device_node); + ret =devfs_add_alias_node (rcdev, sba->alias, sba->dev->device_node);if (ret)return ret;fs/devfs-core.c +102
-89@@ -50,42 +50,21 @@ int devfs_partition_complete(struct string_list *sl, char *instr) }#endif -struct cdev *cdev_readlink (const struct cdev *cdev) -{ -if (!cdev) -return NULL ; - -if (cdev->link) - cdev = cdev->link; - -/* links to links are not allowed */ -BUG_ON (cdev->link); - -return (void *)cdev; -} - -struct cdev *lcdev_by_name (const char *filename) +struct cdev *cdev_by_name (const char *filename) {struct cdev *cdev; +struct cdev_alias *alias;for_each_cdev (cdev) {if (!strcmp (cdev->name, filename))return cdev; +cdev_for_each_alias (alias, cdev) +if (!strcmp (alias->name, filename)) +return cdev; }return NULL ; } -struct cdev *cdev_by_name (const char *filename) -{ -struct cdev *cdev; - - cdev =lcdev_by_name (filename); -if (!cdev) -return NULL ; - -return cdev_readlink (cdev); -} -struct cdev *cdev_by_device_node (struct device_node *node) {struct cdev *cdev;@@ -94,8 +73,14 @@ struct cdev *cdev_by_device_node(struct device_node *node)return NULL ;for_each_cdev (cdev) { +struct cdev_alias *alias; +if (cdev_of_node (cdev) == node) -return cdev_readlink (cdev); +return cdev; + +cdev_for_each_alias (alias, cdev) +if (alias->device_node == node) +return cdev; }return NULL ; }@@ -133,9 +118,6 @@ cdev_find_child_by_gpt_typeuuid(struct cdev *cdev, const guid_t *typeuuid) {struct cdev *partcdev; -/* Follow links to support storage-by-alias */ - cdev =cdev_readlink (cdev); -if (!cdev_is_gpt_partitioned (cdev))return ERR_PTR (-EINVAL );@@ -153,6 +135,29 @@ cdev_find_child_by_gpt_typeuuid(struct cdev *cdev, const guid_t *typeuuid)return ERR_PTR (-ENOENT ); } +static bool cdev_has_partname_alias (struct cdev *cdev,const char *partname) +{ +char *fullname; +struct cdev_alias *alias; +bool ret = false; + +if (!cdev->master) +return false; + + fullname =xasprintf ("%s.%s" , cdev->master->name, partname); + +cdev_for_each_alias (alias, cdev) { +if (streq_ptr (alias->name, fullname)) { + ret = true; +break ; + } + } + +free (fullname); + +return ret; +} +/** * cdev_find_partition - find a partition belonging to a physical device * @@ -164,15 +169,10 @@ struct cdev *cdev_find_partition(struct cdev *cdevm, const char *name)struct cdev *partcdev;for_each_cdev_partition (partcdev, cdevm) { -struct cdev *cdevl; -if (streq_ptr (partcdev->partname, name))return partcdev; - -list_for_each_entry (cdevl, &partcdev->links, link_entry) { -if (streq_ptr (cdevl->partname, name)) -return cdevl; - } +if (cdev_has_partname_alias (partcdev, name)) +return partcdev; }return NULL ;@@ -190,15 +190,11 @@ struct cdev *device_find_partition(struct device *dev, const char *name)struct device *child;list_for_each_entry (cdev, &dev->cdevs, devices_list) { -struct cdev *cdevl; -if (streq_ptr (cdev->partname, name))return cdev; -list_for_each_entry (cdevl, &cdev->links, link_entry) { -if (streq_ptr (cdevl->partname, name)) -return cdev_readlink (cdevl); - } +if (cdev_has_partname_alias (cdev, name)) +return cdev; }device_for_each_child (dev, child) {@@ -460,25 +456,54 @@ static void cdev_free(struct cdev *cdev)static bool devfs_initialized; +static int cdev_symlink (struct cdev *cdev,const char *linkname) +{ +char *path; +int ret; + +if (!devfs_initialized) +return 0 ; + + path =xasprintf ("/dev/%s" , linkname); + ret =symlink (cdev->name, path); +free (path); + +return ret; +} +static void devfs_mknod (struct cdev *cdev) {char *path;int ret; +struct cdev_alias *alias;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); +cdev_for_each_alias (alias, cdev) +cdev_symlink (cdev, alias->name); + + ret =mknod (path,S_IFCHR |0600 , cdev->name);free (path); -if (ret) +if (ret) {pr_err ("Failed to create /dev/%s: %pe\n" , cdev->name,ERR_PTR (ret)); +dump_stack (); + } +} + +static void devfs_unlink (const char *name) +{ +char *path; + + path =xasprintf ("/dev/%s" , name); + +unlink (path); + +free (path); }void devfs_init (void )@@ -499,8 +524,8 @@ int devfs_create(struct cdev *new)if (cdev)return -EEXIST ; -INIT_LIST_HEAD (&new->links);INIT_LIST_HEAD (&new->partitions); +INIT_LIST_HEAD (&new->aliases);list_add_tail (&new->list, &cdev_list);if (new->dev) {@@ -509,54 +534,44 @@ int devfs_create(struct cdev *new) new->device_node = new->dev->of_node; } -if (new->link) -list_add_tail (&new->link_entry, &new->link->links); -devfs_mknod (new);return 0 ; } -int devfs_create_link_node (struct cdev *cdev,const char *name,struct device_node *node) +int devfs_add_alias_node (struct cdev *cdev,const char *name,struct device_node *np) { -struct cdev *new; -int ret; - -/* -* Create a link to the real cdev instead of creating -* a link to a link. -*/ - cdev =cdev_readlink (cdev); - - new =cdev_alloc (name); - new->link = cdev; - new->device_node = node; - - ret =devfs_create (new); -if (ret) { -cdev_free (new); -return ret; - } - -if (cdev->partname) { -size_t partnameoff =0 ; +struct cdev *conflict; +struct cdev_alias *alias; -if (cdev->master) { -size_t masterlen =strlen (cdev->master->name); + conflict =cdev_by_name (name); +if (conflict) +return -EEXIST ; -if (!strncmp (name, cdev->master->name, masterlen)) - partnameoff += masterlen +1 ; - } + alias =xzalloc (sizeof (*alias)); + alias->name =xstrdup (name); + alias->device_node = np; +list_add_tail (&alias->list, &cdev->aliases); - new->partname =xstrdup (name + partnameoff); - } +cdev_symlink (cdev, name);return 0 ; } -int devfs_create_link (struct cdev *cdev,const char *name) +int devfs_add_alias (struct cdev *cdev,const char *name) +{ +return devfs_add_alias_node (cdev, name,NULL ); +} + +static void devfs_remove_aliases (struct cdev *cdev) { -return devfs_create_link_node (cdev, name,NULL ); +struct cdev_alias *alias, *tmp; + +list_for_each_entry_safe (alias, tmp, &cdev->aliases, list) { +devfs_unlink (alias->name); +free (alias->name); +free (alias); + } }int devfs_remove (struct cdev *cdev)@@ -571,8 +586,9 @@ int devfs_remove(struct cdev *cdev)if (cdev->dev)list_del (&cdev->devices_list); -list_for_each_entry_safe (c, tmp, &cdev->links, link_entry) -devfs_remove (c); +devfs_unlink (cdev->name); + +devfs_remove_aliases (cdev);list_for_each_entry_safe (c, tmp, &cdev->partitions, partition_entry)cdevfs_del_partition (c);@@ -580,9 +596,6 @@ int devfs_remove(struct cdev *cdev)if (cdev_is_partition (cdev))list_del (&cdev->partition_entry); -if (cdev->link) -cdev_free (cdev); -return 0 ; }@@ -687,7 +700,7 @@ static struct cdev *__devfs_add_partition(struct cdev *cdev,if (overlap) {if (!IS_ERR (overlap)) {/* only fails with -EEXIST, which is fine */ - (void )devfs_create_link (overlap, partinfo->name); + (void )devfs_add_alias (overlap, partinfo->name); }return overlap;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 +13
-7@@ -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 list_head aliases;struct list_head partition_entry, partitions;struct cdev *master;enum filetype filetype;@@ -443,6 +443,15 @@ struct cdev { }; }; +#define cdev_for_each_alias (alias, cdev) \ + list_for_each_entry(alias, &cdev->aliases, list) + +struct cdev_alias { +char *name; +struct device_node *device_node; +struct list_head list; +}; +static inline struct device_node *cdev_of_node (const struct cdev *cdev) {return IS_ENABLED (CONFIG_OFDEVICE ) ? cdev->device_node :NULL ;@@ -461,15 +470,12 @@ 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_create_link_node (struct cdev *cdev,const char *name, -struct device_node *node); +int devfs_add_alias (struct cdev *,const char *name); +int devfs_add_alias_node (struct cdev *,const char *name,struct device_node *np);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 · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
Re: [PATCH v2 12/13] fs: replace cdev links with aliases
Quoted 5 lines · Ahmad Fatoum
Hi,
On 12/9/25 1:51 PM, Sascha Hauer wrote:
A commit message would be grand. :)
Added one and also integrated the other changes you suggested, thanks Ahamd.
Sascha
Quoted 485 lines
Quoted 2 lines
Signed-off-by Sascha Hauer <[email protected]>Acked-by Ahmad Fatoum <[email protected]>Quoted 472 lines
--- commands/devinfo.c | 7 +- common/partitions.c | 2 +- drivers/block/dm/dm-core.c | 1 - drivers/misc/storage-by-alias.c | 2 +- fs/devfs-core.c | 191 +++++++++++++++++++++------------------- include/block.h | 2 - include/driver.h | 20 +++-- 7 files changed, 121 insertions(+), 104 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 cdev_alias *alias;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); +cdev_for_each_alias (alias, cdev) +printf (", %s" , alias->name);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" );drivers/misc/storage-by-alias.c +1
-1@@ -38,7 +38,7 @@ static int sba_add_partitions(struct cdev *rcdev, void *data)dev_info (sba->dev,"Adding %s -> %s\n" , sba->alias, rcdev->name); - ret =devfs_create_link_node (rcdev, sba->alias, sba->dev->device_node); + ret =devfs_add_alias_node (rcdev, sba->alias, sba->dev->device_node);if (ret)return ret;fs/devfs-core.c +102
-89@@ -50,42 +50,21 @@ int devfs_partition_complete(struct string_list *sl, char *instr) }#endif -struct cdev *cdev_readlink (const struct cdev *cdev) -{ -if (!cdev) -return NULL ; - -if (cdev->link) - cdev = cdev->link; - -/* links to links are not allowed */ -BUG_ON (cdev->link); - -return (void *)cdev; -} - -struct cdev *lcdev_by_name (const char *filename) +struct cdev *cdev_by_name (const char *filename) {struct cdev *cdev; +struct cdev_alias *alias;for_each_cdev (cdev) {if (!strcmp (cdev->name, filename))return cdev; +cdev_for_each_alias (alias, cdev) +if (!strcmp (alias->name, filename)) +return cdev; }return NULL ; } -struct cdev *cdev_by_name (const char *filename) -{ -struct cdev *cdev; - - cdev =lcdev_by_name (filename); -if (!cdev) -return NULL ; - -return cdev_readlink (cdev); -} -struct cdev *cdev_by_device_node (struct device_node *node) {struct cdev *cdev;@@ -94,8 +73,14 @@ struct cdev *cdev_by_device_node(struct device_node *node)return NULL ;for_each_cdev (cdev) { +struct cdev_alias *alias; +if (cdev_of_node (cdev) == node) -return cdev_readlink (cdev); +return cdev; + +cdev_for_each_alias (alias, cdev) +if (alias->device_node == node) +return cdev; }return NULL ; }@@ -133,9 +118,6 @@ cdev_find_child_by_gpt_typeuuid(struct cdev *cdev, const guid_t *typeuuid) {struct cdev *partcdev; -/* Follow links to support storage-by-alias */ - cdev =cdev_readlink (cdev); -if (!cdev_is_gpt_partitioned (cdev))return ERR_PTR (-EINVAL );@@ -153,6 +135,29 @@ cdev_find_child_by_gpt_typeuuid(struct cdev *cdev, const guid_t *typeuuid)return ERR_PTR (-ENOENT ); } +static bool cdev_has_partname_alias (struct cdev *cdev,const char *partname) +{ +char *fullname; +struct cdev_alias *alias; +bool ret = false; + +if (!cdev->master) +return false; + + fullname =xasprintf ("%s.%s" , cdev->master->name, partname); + +cdev_for_each_alias (alias, cdev) { +if (streq_ptr (alias->name, fullname)) { + ret = true; +break ; + } + } + +free (fullname); + +return ret; +} +/** * cdev_find_partition - find a partition belonging to a physical device * @@ -164,15 +169,10 @@ struct cdev *cdev_find_partition(struct cdev *cdevm, const char *name)struct cdev *partcdev;for_each_cdev_partition (partcdev, cdevm) { -struct cdev *cdevl; -if (streq_ptr (partcdev->partname, name))return partcdev; - -list_for_each_entry (cdevl, &partcdev->links, link_entry) { -if (streq_ptr (cdevl->partname, name)) -return cdevl; - } +if (cdev_has_partname_alias (partcdev, name)) +return partcdev; }return NULL ;@@ -190,15 +190,11 @@ struct cdev *device_find_partition(struct device *dev, const char *name)struct device *child;list_for_each_entry (cdev, &dev->cdevs, devices_list) { -struct cdev *cdevl; -if (streq_ptr (cdev->partname, name))return cdev; -list_for_each_entry (cdevl, &cdev->links, link_entry) { -if (streq_ptr (cdevl->partname, name)) -return cdev_readlink (cdevl); - } +if (cdev_has_partname_alias (cdev, name)) +return cdev; }device_for_each_child (dev, child) {@@ -460,25 +456,54 @@ static void cdev_free(struct cdev *cdev)static bool devfs_initialized; +static int cdev_symlink (struct cdev *cdev,const char *linkname) +{ +char *path; +int ret; + +if (!devfs_initialized) +return 0 ; + + path =xasprintf ("/dev/%s" , linkname); + ret =symlink (cdev->name, path); +free (path); + +return ret; +} +static void devfs_mknod (struct cdev *cdev) {char *path;int ret; +struct cdev_alias *alias;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); +cdev_for_each_alias (alias, cdev) +cdev_symlink (cdev, alias->name); + + ret =mknod (path,S_IFCHR |0600 , cdev->name);free (path); -if (ret) +if (ret) {pr_err ("Failed to create /dev/%s: %pe\n" , cdev->name,ERR_PTR (ret)); +dump_stack (); + } +} + +static void devfs_unlink (const char *name) +{ +char *path; + + path =xasprintf ("/dev/%s" , name); + +unlink (path); + +free (path); }void devfs_init (void )@@ -499,8 +524,8 @@ int devfs_create(struct cdev *new)if (cdev)return -EEXIST ; -INIT_LIST_HEAD (&new->links);INIT_LIST_HEAD (&new->partitions); +INIT_LIST_HEAD (&new->aliases);list_add_tail (&new->list, &cdev_list);if (new->dev) {@@ -509,54 +534,44 @@ int devfs_create(struct cdev *new) new->device_node = new->dev->of_node; } -if (new->link) -list_add_tail (&new->link_entry, &new->link->links); -devfs_mknod (new);return 0 ; } -int devfs_create_link_node (struct cdev *cdev,const char *name,struct device_node *node) +int devfs_add_alias_node (struct cdev *cdev,const char *name,struct device_node *np) { -struct cdev *new; -int ret; - -/* -* Create a link to the real cdev instead of creating -* a link to a link. -*/ - cdev =cdev_readlink (cdev); - - new =cdev_alloc (name); - new->link = cdev; - new->device_node = node; - - ret =devfs_create (new); -if (ret) { -cdev_free (new); -return ret; - } - -if (cdev->partname) { -size_t partnameoff =0 ; +struct cdev *conflict; +struct cdev_alias *alias; -if (cdev->master) { -size_t masterlen =strlen (cdev->master->name); + conflict =cdev_by_name (name); +if (conflict) +return -EEXIST ; -if (!strncmp (name, cdev->master->name, masterlen)) - partnameoff += masterlen +1 ; - } + alias =xzalloc (sizeof (*alias)); + alias->name =xstrdup (name); + alias->device_node = np; +list_add_tail (&alias->list, &cdev->aliases); - new->partname =xstrdup (name + partnameoff); - } +cdev_symlink (cdev, name);return 0 ; } -int devfs_create_link (struct cdev *cdev,const char *name) +int devfs_add_alias (struct cdev *cdev,const char *name) +{ +return devfs_add_alias_node (cdev, name,NULL ); +} + +static void devfs_remove_aliases (struct cdev *cdev) { -return devfs_create_link_node (cdev, name,NULL ); +struct cdev_alias *alias, *tmp; + +list_for_each_entry_safe (alias, tmp, &cdev->aliases, list) { +devfs_unlink (alias->name); +free (alias->name); +free (alias); + } }int devfs_remove (struct cdev *cdev)@@ -571,8 +586,9 @@ int devfs_remove(struct cdev *cdev)if (cdev->dev)list_del (&cdev->devices_list); -list_for_each_entry_safe (c, tmp, &cdev->links, link_entry) -devfs_remove (c); +devfs_unlink (cdev->name); + +devfs_remove_aliases (cdev);list_for_each_entry_safe (c, tmp, &cdev->partitions, partition_entry)cdevfs_del_partition (c);@@ -580,9 +596,6 @@ int devfs_remove(struct cdev *cdev)if (cdev_is_partition (cdev))list_del (&cdev->partition_entry); -if (cdev->link) -cdev_free (cdev); -return 0 ; }@@ -687,7 +700,7 @@ static struct cdev *__devfs_add_partition(struct cdev *cdev,if (overlap) {if (!IS_ERR (overlap)) {/* only fails with -EEXIST, which is fine */ - (void )devfs_create_link (overlap, partinfo->name); + (void )devfs_add_alias (overlap, partinfo->name); }return overlap;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 +13
-7@@ -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 list_head aliases;struct list_head partition_entry, partitions;struct cdev *master;enum filetype filetype;@@ -443,6 +443,15 @@ struct cdev { }; }; +#define cdev_for_each_alias (alias, cdev) \ + list_for_each_entry(alias, &cdev->aliases, list) + +struct cdev_alias { +char *name; +struct device_node *device_node; +struct list_head list; +}; +static inline struct device_node *cdev_of_node (const struct cdev *cdev) {return IS_ENABLED (CONFIG_OFDEVICE ) ? cdev->device_node :NULL ;@@ -461,15 +470,12 @@ 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_create_link_node (struct cdev *cdev,const char *name, -struct device_node *node); +int devfs_add_alias (struct cdev *,const char *name); +int devfs_add_alias_node (struct cdev *,const char *name,struct device_node *np);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 · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
Signature · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
PATCH v2 13/13 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
Re: [PATCH v2 13/13] ls: use ~0 for FILE_SIZE_STREAM
Quoted 4 lines · Sascha Hauer
For stream size files we used to show ((
loff_t) -1) as file size which is quite unreadable. Use ~0 in this case.Signed-off-by Sascha Hauer <[email protected]>
Quoted 24 lines
--- 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 · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
Sascha Hauer
AppliedQuoted 8 lines · Sascha Hauer
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[...]
Applied, thanks!
[01/13] fs: devfs-core: add devfs_create_link_node()
https://git.pengutronix.de/cgit/barebox/commit/?id=0532ad160214 (link may not be stable)
[02/13] storage-by-alias: drop fake cdev
https://git.pengutronix.de/cgit/barebox/commit/?id=50f91eaeac24 (link may not be stable)
[03/13] fs: implement mknod
https://git.pengutronix.de/cgit/barebox/commit/?id=d37d0ed18ddc (link may not be stable)
[04/13] commands: add mknod command
https://git.pengutronix.de/cgit/barebox/commit/?id=65a86ce343b3 (link may not be stable)
[05/13] fs: ramfs: add device file support
https://git.pengutronix.de/cgit/barebox/commit/?id=84efacef29bf (link may not be stable)
[06/13] cdev: add cdev_size() helper
https://git.pengutronix.de/cgit/barebox/commit/?id=1935728cfea8 (link may not be stable)
[07/13] fs: fix st_size for device files
https://git.pengutronix.de/cgit/barebox/commit/?id=37b73179a340 (link may not be stable)
[08/13] fs: retire devfs as filesystem
https://git.pengutronix.de/cgit/barebox/commit/?id=cff48c7caba4 (link may not be stable)
[09/13] fs: include cdevname in struct stat
https://git.pengutronix.de/cgit/barebox/commit/?id=f7950b1fac33 (link may not be stable)
[10/13] fs: stat_print: get cdevname from stat
https://git.pengutronix.de/cgit/barebox/commit/?id=6f646eccbf9f (link may not be stable)
[11/13] common: cdev-alias: rename struct
https://git.pengutronix.de/cgit/barebox/commit/?id=0511991f9c04 (link may not be stable)
[12/13] fs: replace cdev links with aliases
https://git.pengutronix.de/cgit/barebox/commit/?id=224b5a781f34 (link may not be stable)
[13/13] ls: use ~0 for FILE_SIZE_STREAM
https://git.pengutronix.de/cgit/barebox/commit/?id=6bcded4d13b3 (link may not be stable)
Best regards,
Signature · 1 line
Sascha Hauer <[email protected]>