Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1062407pxu; Mon, 23 Nov 2020 10:37:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJzixwL6khOy5syjA40h2rXPLlpZUDOWPgNk2RoyEYBP3YFpiO27QQW2gsyWOKg3x3xgh0pL X-Received: by 2002:a17:907:10d9:: with SMTP id rv25mr853391ejb.437.1606156655635; Mon, 23 Nov 2020 10:37:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606156655; cv=none; d=google.com; s=arc-20160816; b=AFHx/fVvUmh4ZdvKKOeEyEgra/6byss/XCPmc7+OP8Fp3DyC9Z0MVQIGj5oG/rWrFV fZvObAqbC/MMywTav8jCfJ1ngmJDoilkCYxKj05nyADu/8NF+AUq9qV8po/KHxbsz0gB wQf2OybZdL/5T3VkPtGas+Ic+JwO4AQqzIMOnUPv4jwrsPFZdKdSZ/3UUSi6EJ3jwCVx A9eBhaQh/SVByxAAd5yyvDSfn2Ut+8aDWjpHg0CYn6nFGfg9vB7bXxJJDpLmXtewFwbm XKXcsyEOwoe1SdwkgRBaJUwX0XqORV1PDaRg9gcryB4fCJJJXWo/3vReIWlZTJyYFV7A pxaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=dx2LuTR1stcdM+p6/zghW41o1Jwlk3oavAuC4o51JCk=; b=kbgt6R+EDsYWi3gwqcIUlXpvEY0lbDHjcyfm2RP5qSTkHnGjwvaw4X6ROeGyYDVsLG yYgqqORasxu7uW2uxJ1FuZ00LkLsP5XNWJ7jAqG3DHRM3F5zfjYFaHlK43YLV3aeJsAC EKHipt62ZKFfsUrKJ0eEApH8w7OVGf3g5dDbVdcYqpuLD7jvfIFGftFjBT/P/hG/XVjq IIxOp6qlZ5L59LVEN6gXZF6EH4+FmDjubBmOX96+v3Jk5lmO33bYS6U1Q5eVYOSIR8Ec X8hKIX6ik1RkhfY+e8wnFF1ZUQDOgWu9Z0741C8a1v33HdRyBhjGTwKZmvvzbJK1nbEL w/LA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=collabora.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m17si521445ejq.464.2020.11.23.10.37.13; Mon, 23 Nov 2020 10:37:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731922AbgKWSbt (ORCPT + 99 others); Mon, 23 Nov 2020 13:31:49 -0500 Received: from bhuna.collabora.co.uk ([46.235.227.227]:37148 "EHLO bhuna.collabora.co.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731900AbgKWSbs (ORCPT ); Mon, 23 Nov 2020 13:31:48 -0500 Received: from obbardc-laptop.home (unknown [IPv6:2a00:23c5:9900:2d00:fa7c:ed23:2f0a:448d]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: obbardc) by bhuna.collabora.co.uk (Postfix) with ESMTPSA id 3DD761F44D56; Mon, 23 Nov 2020 18:31:45 +0000 (GMT) From: Christopher Obbard To: linux-um@lists.infradead.org, linux-kernel@vger.kernel.org, Jeff Dike , Richard Weinberger , Anton Ivanov Cc: Gabriel Krisman Bertazi , Arnd Bergmann , Ben Hutchings , Sjoerd Simons , Christopher Obbard Subject: [PATCH] um: ubd: Set device serial attribute from cmdline Date: Mon, 23 Nov 2020 18:31:37 +0000 Message-Id: <20201123183137.741647-1-chris.obbard@collabora.com> X-Mailer: git-send-email 2.28.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Adds the ability to set the UBD device serial number from the commandline, disabling the serial number functionality by default. In some cases it may be useful to set a serial to the UBD device, such that downstream users (i.e. udev) can use this information to better describe the hardware to the user from the UML cmdline. In our case we use this parameter to create some entries under /dev/disk/by-ubd-id/ for each of the UBD devices passed through the UML cmdline. Signed-off-by: Christopher Obbard --- arch/um/drivers/ubd_kern.c | 78 ++++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 16 deletions(-) diff --git a/arch/um/drivers/ubd_kern.c b/arch/um/drivers/ubd_kern.c index a97b40432331..f59e23748c16 100644 --- a/arch/um/drivers/ubd_kern.c +++ b/arch/um/drivers/ubd_kern.c @@ -155,6 +155,7 @@ struct ubd { /* name (and fd, below) of the file opened for writing, either the * backing or the cow file. */ char *file; + char *serial; int count; int fd; __u64 size; @@ -180,6 +181,7 @@ struct ubd { #define DEFAULT_UBD { \ .file = NULL, \ + .serial = NULL, \ .count = 0, \ .fd = -1, \ .size = -1, \ @@ -272,7 +274,7 @@ static int ubd_setup_common(char *str, int *index_out, char **error_out) { struct ubd *ubd_dev; struct openflags flags = global_openflags; - char *backing_file; + char *file, *backing_file, *serial; int n, err = 0, i; if(index_out) *index_out = -1; @@ -368,24 +370,27 @@ static int ubd_setup_common(char *str, int *index_out, char **error_out) goto out; break_loop: - backing_file = strchr(str, ','); + file = strsep(&str, ",:"); + if (*file == '\0') + file = NULL; - if (backing_file == NULL) - backing_file = strchr(str, ':'); + backing_file = strsep(&str, ",:"); + if (*backing_file == '\0') + backing_file = NULL; - if(backing_file != NULL){ - if(ubd_dev->no_cow){ - *error_out = "Can't specify both 'd' and a cow file"; - goto out; - } - else { - *backing_file = '\0'; - backing_file++; - } + serial = strsep(&str, ",:"); + if (*serial == '\0') + serial = NULL; + + if (backing_file && ubd_dev->no_cow) { + *error_out = "Can't specify both 'd' and a cow file"; + goto out; } + err = 0; - ubd_dev->file = str; + ubd_dev->file = file; ubd_dev->cow.file = backing_file; + ubd_dev->serial = serial; ubd_dev->boot_openflags = flags; out: mutex_unlock(&ubd_lock); @@ -406,7 +411,7 @@ static int ubd_setup(char *str) __setup("ubd", ubd_setup); __uml_help(ubd_setup, -"ubd=[(:|,)]\n" +"ubd=[(:|,)][(:|,)]\n" " This is used to associate a device with a file in the underlying\n" " filesystem. When specifying two filenames, the first one is the\n" " COW name and the second is the backing file name. As separator you can\n" @@ -429,6 +434,12 @@ __uml_help(ubd_setup, " UMLs and file locking will be turned off - this is appropriate for a\n" " cluster filesystem and inappropriate at almost all other times.\n\n" " 't' will disable trim/discard support on the device (enabled by default).\n\n" +" An optional device serial number can be exposed using the serial parameter\n" +" on the cmdline which is exposed as a sysfs entry. This is particularly\n" +" useful when a unique number should be given to the device. Note when\n" +" specifying a label, the filename2 must be also presented. It can be\n" +" an empty string, in which case the backing file is not used:\n" +" ubd0=File,,Serial\n" ); static int udb_setup(char *str) @@ -868,6 +879,41 @@ static void ubd_device_release(struct device *dev) *ubd_dev = ((struct ubd) DEFAULT_UBD); } +static ssize_t serial_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct gendisk *disk = dev_to_disk(dev); + struct ubd *ubd_dev = disk->private_data; + + if (!ubd_dev) + return 0; + + return sprintf(buf, "%s", ubd_dev->serial); +} + +static DEVICE_ATTR_RO(serial); + +static struct attribute *ubd_attrs[] = { + &dev_attr_serial.attr, + NULL, +}; + +static umode_t ubd_attrs_are_visible(struct kobject *kobj, + struct attribute *a, int n) +{ + return a->mode; +} + +static const struct attribute_group ubd_attr_group = { + .attrs = ubd_attrs, + .is_visible = ubd_attrs_are_visible, +}; + +static const struct attribute_group *ubd_attr_groups[] = { + &ubd_attr_group, + NULL, +}; + static int ubd_disk_register(int major, u64 size, int unit, struct gendisk **disk_out) { @@ -899,7 +945,7 @@ static int ubd_disk_register(int major, u64 size, int unit, disk->private_data = &ubd_devs[unit]; disk->queue = ubd_devs[unit].queue; - device_add_disk(parent, disk, NULL); + device_add_disk(parent, disk, ubd_attr_groups); *disk_out = disk; return 0; -- 2.28.0