Skip to content

Commit c0aa3e0

Browse files
Ren Mingxinmstsirkin
authored andcommitted
virtio_blk: helper function to format disk names
The current virtio block's naming algorithm just supports 18278 (26^3 + 26^2 + 26) disks. If there are more virtio blocks, there will be disks with the same name. Based on commit 3e1a7ff, add a function "virtblk_name_format()" for virtio block to support mass of disks naming. Notes: - Our naming scheme is ugly. We are stuck with it for virtio but don't use it for any new driver: new drivers should name their devices PREFIX%d where the sequence number can be allocated by ida - sd_format_disk_name has exactly the same logic. Moving it to a central place was deferred over worries that this will make people keep using the legacy naming in new drivers. We kept code idential in case someone wants to deduplicate later. Signed-off-by: Ren Mingxin <[email protected]> Acked-by: Asias He <[email protected]> Signed-off-by: Michael S. Tsirkin <[email protected]>
1 parent 5e7045b commit c0aa3e0

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

drivers/block/virtio_blk.c

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,34 @@ static int init_vq(struct virtio_blk *vblk)
374374
return err;
375375
}
376376

377+
/*
378+
* Legacy naming scheme used for virtio devices. We are stuck with it for
379+
* virtio blk but don't ever use it for any new driver.
380+
*/
381+
static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
382+
{
383+
const int base = 'z' - 'a' + 1;
384+
char *begin = buf + strlen(prefix);
385+
char *end = buf + buflen;
386+
char *p;
387+
int unit;
388+
389+
p = end - 1;
390+
*p = '\0';
391+
unit = base;
392+
do {
393+
if (p == begin)
394+
return -EINVAL;
395+
*--p = 'a' + (index % unit);
396+
index = (index / unit) - 1;
397+
} while (index >= 0);
398+
399+
memmove(begin, p, end - p);
400+
memcpy(buf, prefix, strlen(prefix));
401+
402+
return 0;
403+
}
404+
377405
static int __devinit virtblk_probe(struct virtio_device *vdev)
378406
{
379407
struct virtio_blk *vblk;
@@ -442,18 +470,7 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
442470

443471
q->queuedata = vblk;
444472

445-
if (index < 26) {
446-
sprintf(vblk->disk->disk_name, "vd%c", 'a' + index % 26);
447-
} else if (index < (26 + 1) * 26) {
448-
sprintf(vblk->disk->disk_name, "vd%c%c",
449-
'a' + index / 26 - 1, 'a' + index % 26);
450-
} else {
451-
const unsigned int m1 = (index / 26 - 1) / 26 - 1;
452-
const unsigned int m2 = (index / 26 - 1) % 26;
453-
const unsigned int m3 = index % 26;
454-
sprintf(vblk->disk->disk_name, "vd%c%c%c",
455-
'a' + m1, 'a' + m2, 'a' + m3);
456-
}
473+
virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
457474

458475
vblk->disk->major = major;
459476
vblk->disk->first_minor = index_to_minor(index);

0 commit comments

Comments
 (0)