[PATCH 06/18] driver: add new cdev_is_partition helper

Marco Felsch m.felsch at pengutronix.de
Wed May 31 09:36:15 PDT 2023


On 23-05-31, Ahmad Fatoum wrote:
> Partitions will have cdev->master != NULL, so often code will just do
> if (cdev->master) to check if a cdev is a partition. This is suboptimal
> as it may be misinterpreted by readers as meaning that the cdev is the
> master device, while it's the other way round.
> 
> Let's define cdev_is_partition instead and use it everywhere, where
> cdev->master is only checked, but not dereferenced.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>

Reviewed-by: Marco Felsch <m.felsch at pengutronix.de>

> ---
>  drivers/base/driver.c |  2 +-
>  fs/devfs-core.c       | 10 +++++-----
>  include/driver.h      |  4 ++++
>  3 files changed, 10 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/base/driver.c b/drivers/base/driver.c
> index f00be99cdcbf..10d765e1a213 100644
> --- a/drivers/base/driver.c
> +++ b/drivers/base/driver.c
> @@ -277,7 +277,7 @@ int unregister_device(struct device *old_dev)
>  	}
>  
>  	list_for_each_entry_safe(cdev, ct, &old_dev->cdevs, devices_list) {
> -		if (cdev->master) {
> +		if (cdev_is_partition(cdev)) {
>  			dev_dbg(old_dev, "unregister part %s\n", cdev->name);
>  			devfs_del_partition(cdev->name);
>  		}
> diff --git a/fs/devfs-core.c b/fs/devfs-core.c
> index fbcf68e81597..bb66993b90f9 100644
> --- a/fs/devfs-core.c
> +++ b/fs/devfs-core.c
> @@ -38,7 +38,7 @@ int devfs_partition_complete(struct string_list *sl, char *instr)
>  	len = strlen(instr);
>  
>  	for_each_cdev(cdev) {
> -		if (cdev->master &&
> +		if (cdev_is_partition(cdev) &&
>  		    !strncmp(instr, cdev->name, len)) {
>  			string_list_add_asprintf(sl, "%s ", cdev->name);
>  		}
> @@ -101,7 +101,7 @@ struct cdev *cdev_by_partuuid(const char *partuuid)
>  		return NULL;
>  
>  	for_each_cdev(cdev) {
> -		if (cdev->master && !strcasecmp(cdev->uuid, partuuid))
> +		if (cdev_is_partition(cdev) && !strcasecmp(cdev->uuid, partuuid))
>  			return cdev;
>  	}
>  	return NULL;
> @@ -115,7 +115,7 @@ struct cdev *cdev_by_diskuuid(const char *diskuuid)
>  		return NULL;
>  
>  	for_each_cdev(cdev) {
> -		if (!cdev->master && !strcasecmp(cdev->uuid, diskuuid))
> +		if (!cdev_is_partition(cdev) && !strcasecmp(cdev->uuid, diskuuid))
>  			return cdev;
>  	}
>  	return NULL;
> @@ -393,7 +393,7 @@ int devfs_remove(struct cdev *cdev)
>  	list_for_each_entry_safe(c, tmp, &cdev->links, link_entry)
>  		devfs_remove(c);
>  
> -	if (cdev->master)
> +	if (cdev_is_partition(cdev))
>  		list_del(&cdev->partition_entry);
>  
>  	if (cdev->link)
> @@ -549,7 +549,7 @@ int devfs_del_partition(const char *name)
>  		return ret;
>  	}
>  
> -	if (!cdev->master)
> +	if (!cdev_is_partition(cdev))
>  		return -EINVAL;
>  	if (cdev->flags & DEVFS_PARTITION_FIXED)
>  		return -EPERM;
> diff --git a/include/driver.h b/include/driver.h
> index e1ee3dc2dd7c..00b4a0e4af75 100644
> --- a/include/driver.h
> +++ b/include/driver.h
> @@ -564,6 +564,10 @@ 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_unallocated_space(struct cdev *cdev);
> +static inline bool cdev_is_partition(const struct cdev *cdev)
> +{
> +	return cdev->master != NULL;
> +}
>  
>  extern struct list_head cdev_list;
>  #define for_each_cdev(cdev) \
> -- 
> 2.39.2
> 
> 
> 



More information about the barebox mailing list