Skip to content

Commit 8c13ae4

Browse files
Hannes Reineckekawasaki
authored andcommitted
ublk: persistent device links
Implement a 'uuid' parameter to export a device UUID in a sysfs attribute 'uuid'. The ublk server is required to set the uuid parameter when creating the device. This allows udev to create persistent device links for ublk devices eg with this rule: KERNEL=="ublk*", ATTRS{uuid}=="?*", SYMLINK+="disk/by-id/ublk-$attr{uuid}" Signed-off-by: Hannes Reinecke <[email protected]> Reviewed-by: Ming Lei <[email protected]>
1 parent 6b51c57 commit 8c13ae4

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

drivers/block/ublk_drv.c

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
(UBLK_PARAM_TYPE_BASIC | UBLK_PARAM_TYPE_DISCARD | \
9393
UBLK_PARAM_TYPE_DEVT | UBLK_PARAM_TYPE_ZONED | \
9494
UBLK_PARAM_TYPE_DMA_ALIGN | UBLK_PARAM_TYPE_SEGMENT | \
95-
UBLK_PARAM_TYPE_INTEGRITY)
95+
UBLK_PARAM_TYPE_INTEGRITY | UBLK_PARAM_TYPE_UUID)
9696

9797
#define UBLK_BATCH_F_ALL \
9898
(UBLK_BATCH_F_HAS_ZONE_LBA | \
@@ -938,6 +938,14 @@ static int ublk_validate_params(const struct ublk_device *ub)
938938
return -EINVAL;
939939
}
940940

941+
if (ub->params.types & UBLK_PARAM_TYPE_UUID) {
942+
const struct ublk_param_uuid *p = &ub->params.uuid;
943+
uuid_t uuid;
944+
945+
import_uuid(&uuid, p->uuid);
946+
if (uuid_is_null(&uuid))
947+
return -EINVAL;
948+
}
941949
return 0;
942950
}
943951

@@ -4240,6 +4248,49 @@ static int ublk_add_chdev(struct ublk_device *ub)
42404248
return ret;
42414249
}
42424250

4251+
static ssize_t uuid_show(struct device *dev,
4252+
struct device_attribute *attr, char *buf)
4253+
{
4254+
struct gendisk *disk = dev_to_disk(dev);
4255+
struct ublk_device *ub = disk->private_data;
4256+
const struct ublk_param_uuid *p = &ub->params.uuid;
4257+
uuid_t uuid;
4258+
4259+
import_uuid(&uuid, p->uuid);
4260+
return sprintf(buf, "%pU\n", &uuid);
4261+
}
4262+
4263+
static DEVICE_ATTR_RO(uuid);
4264+
4265+
static struct attribute *ublk_attrs[] = {
4266+
&dev_attr_uuid.attr,
4267+
NULL,
4268+
};
4269+
4270+
static umode_t ublk_attrs_are_visible(struct kobject *kobj,
4271+
struct attribute *a, int n)
4272+
{
4273+
struct device *dev = kobj_to_dev(kobj);
4274+
struct gendisk *disk = dev_to_disk(dev);
4275+
struct ublk_device *ub = disk->private_data;
4276+
4277+
if (a == &dev_attr_uuid.attr &&
4278+
(ub->params.types & UBLK_PARAM_TYPE_UUID))
4279+
return S_IRUGO;
4280+
4281+
return a->mode;
4282+
}
4283+
4284+
static const struct attribute_group ublk_attr_group = {
4285+
.attrs = ublk_attrs,
4286+
.is_visible = ublk_attrs_are_visible,
4287+
};
4288+
4289+
static const struct attribute_group *ublk_attr_groups[] = {
4290+
&ublk_attr_group,
4291+
NULL,
4292+
};
4293+
42434294
/* align max io buffer size with PAGE_SIZE */
42444295
static void ublk_align_max_io_size(struct ublk_device *ub)
42454296
{
@@ -4435,7 +4486,7 @@ static int ublk_ctrl_start_dev(struct ublk_device *ub,
44354486
goto out_put_cdev;
44364487
}
44374488

4438-
ret = add_disk(disk);
4489+
ret = device_add_disk(NULL, disk, ublk_attr_groups);
44394490
if (ret)
44404491
goto out_put_cdev;
44414492

include/uapi/linux/ublk_cmd.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,10 @@ struct ublk_param_integrity {
717717
__u8 pad[5];
718718
};
719719

720+
struct ublk_param_uuid {
721+
__u8 uuid[16];
722+
};
723+
720724
struct ublk_params {
721725
/*
722726
* Total length of parameters, userspace has to set 'len' for both
@@ -732,6 +736,7 @@ struct ublk_params {
732736
#define UBLK_PARAM_TYPE_DMA_ALIGN (1 << 4)
733737
#define UBLK_PARAM_TYPE_SEGMENT (1 << 5)
734738
#define UBLK_PARAM_TYPE_INTEGRITY (1 << 6) /* requires UBLK_F_INTEGRITY */
739+
#define UBLK_PARAM_TYPE_UUID (1 << 7)
735740
__u32 types; /* types of parameter included */
736741

737742
struct ublk_param_basic basic;
@@ -741,6 +746,7 @@ struct ublk_params {
741746
struct ublk_param_dma_align dma;
742747
struct ublk_param_segment seg;
743748
struct ublk_param_integrity integrity;
749+
struct ublk_param_uuid uuid;
744750
};
745751

746752
#endif

0 commit comments

Comments
 (0)