From mboxrd@z Thu Jan 1 00:00:00 1970 From: Kay Sievers Date: Sat, 08 May 2004 11:29:32 +0000 Subject: Re: Re: [PATCH] udev callout for reading filesystem labels Message-Id: <20040508112932.GA5172@vrfy.org> MIME-Version: 1 Content-Type: multipart/mixed; boundary="OXfL5xGRrasGEqWY" List-Id: References: <20040429210446.GA9836@vrfy.org> In-Reply-To: <20040429210446.GA9836@vrfy.org> To: linux-hotplug@vger.kernel.org --OXfL5xGRrasGEqWY Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Sat, May 08, 2004 at 12:54:01AM +0200, Arnd Bergmann wrote: > Kay Sievers schrieb am 07.05.2004, 23:52:56: > > > Hmm, doesn't it break UTF-8 characters, which are common in udf labels? > Oops, that's probably right. > > > Should we better do it only for dasd and keep the set_label_string() private? > > I had first done something special for dasd until I noticed that some > function like that already exists. Actually for dasd labels, it should > be completely sufficient to do the trailing space elimination because > the tools for setting the label do most of the sanitizing, but treating > dasd labels different from fs labels during output would be pointless. > > What I wanted to avoid is trouble from invalid labels, indepent of > disk vs. fs labels. Note that udev does not really behave well if > there are spaces within the label and though that is invalid for dasd, > it might not be for some other label type. > > Aside from whitespace, which is interpreted by udev, we at least need > to filter out '/' characters as well as "." and "..", which are special > to the file system. Ignoring all non-alphanumerical characters was just > my safe guess. Arnd, here is a new patch on top of the volume_id of the current bk tree. Your fixes are integrated, the leading whitespace of the volume strings is removed, spaces replaced by underscore, and slashes are skipped. More special character handling can easily be added in the switch(). The rules are placed in it's own file in the volume_id directory, as we support multiple config files, the user just need to drop it in /etc/udev/ruled.d/. thanks, Kay --OXfL5xGRrasGEqWY Content-Type: text/plain; charset=utf-8 Content-Disposition: inline; filename="01-dasd-volume_id.patch" Content-Transfer-Encoding: quoted-printable diff -Nru a/extras/volume_id/Makefile b/extras/volume_id/Makefile --- a/extras/volume_id/Makefile Sat May 8 13:10:47 2004 +++ b/extras/volume_id/Makefile Sat May 8 13:10:47 2004 @@ -1,6 +1,6 @@ # Makefile for udev_volume_id # -# Copyright (C) 2004 Kay Sievers +# Copyright (C) 2004 Kay Sievers # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -29,8 +29,19 @@ INSTALL_SCRIPT =3D ${INSTALL_PROGRAM} =20 override CFLAGS+=3D-Wall -fno-builtin -OBJS =3D volume_id.o udev_volume_id.o -HEADERS =3D volume_id.h + +SYSFS =3D ../../libsysfs/sysfs_bus.o \ + ../../libsysfs/sysfs_class.o \ + ../../libsysfs/sysfs_device.o \ + ../../libsysfs/sysfs_dir.o \ + ../../libsysfs/sysfs_driver.o \ + ../../libsysfs/sysfs_utils.o \ + ../../libsysfs/dlist.o + + + +OBJS =3D volume_id.o udev_volume_id.o dasdlabel.o $(SYSFS) +HEADERS =3D volume_id.h dasdlabel.h =20 $(OBJS): $(HEADERS) =20 diff -Nru a/extras/volume_id/dasdlabel.c b/extras/volume_id/dasdlabel.c --- /dev/null Wed Dec 31 16:00:00 1969 +++ b/extras/volume_id/dasdlabel.c Sat May 8 13:10:47 2004 @@ -0,0 +1,178 @@ +/* + * dasdlabel - read label from s390 block device + * + * Copyright (C) 2004 Arnd Bergmann + * + * This program is free software; you can redistribute it and/or modify = it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation version 2 of the License. + *=20 + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + *=20 + * You should have received a copy of the GNU General Public License alo= ng + * with this program; if not, write to the Free Software Foundation, Inc= ., + * 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + + +#include +#include +#include +#include +#include +#include + +#include "dasdlabel.h" + +static unsigned char EBCtoASC[256] =3D +{ +/* 0x00 NUL SOH STX ETX *SEL HT *RNL DEL */ + 0x00, 0x01, 0x02, 0x03, 0x07, 0x09, 0x07, 0x7F, +/* 0x08 -GE -SPS -RPT VT FF CR SO SI */ + 0x07, 0x07, 0x07, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, +/* 0x10 DLE DC1 DC2 DC3 -RES -NL BS -POC + -ENP ->LF */ + 0x10, 0x11, 0x12, 0x13, 0x07, 0x0A, 0x08, 0x07, +/* 0x18 CAN EM -UBS -CU1 -IFS -IGS -IRS -ITB + -IUS */ + 0x18, 0x19, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, +/* 0x20 -DS -SOS FS -WUS -BYP LF ETB ESC + -INP */ + 0x07, 0x07, 0x1C, 0x07, 0x07, 0x0A, 0x17, 0x1B, +/* 0x28 -SA -SFE -SM -CSP -MFA ENQ ACK BEL + -SW */=20 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x05, 0x06, 0x07, +/* 0x30 ---- ---- SYN -IR -PP -TRN -NBS EOT */ + 0x07, 0x07, 0x16, 0x07, 0x07, 0x07, 0x07, 0x04, +/* 0x38 -SBS -IT -RFF -CU3 DC4 NAK ---- SUB */ + 0x07, 0x07, 0x07, 0x07, 0x14, 0x15, 0x07, 0x1A, +/* 0x40 SP RSP =C3=A4 ---- */ + 0x20, 0xFF, 0x83, 0x84, 0x85, 0xA0, 0x07, 0x86, +/* 0x48 . < ( + | */ + 0x87, 0xA4, 0x9B, 0x2E, 0x3C, 0x28, 0x2B, 0x7C, +/* 0x50 & ---- */ + 0x26, 0x82, 0x88, 0x89, 0x8A, 0xA1, 0x8C, 0x07, +/* 0x58 =C3=9F ! $ * ) ; */ + 0x8D, 0xE1, 0x21, 0x24, 0x2A, 0x29, 0x3B, 0xAA, +/* 0x60 - / ---- =C3=84 ---- ---- ---- */ + 0x2D, 0x2F, 0x07, 0x8E, 0x07, 0x07, 0x07, 0x8F, +/* 0x68 ---- , % _ > ? */=20 + 0x80, 0xA5, 0x07, 0x2C, 0x25, 0x5F, 0x3E, 0x3F, +/* 0x70 --- ---- ---- ---- ---- ---- ---- */ + 0x07, 0x90, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, +/* 0x78 * ` : # @ ' =3D " */ + 0x70, 0x60, 0x3A, 0x23, 0x40, 0x27, 0x3D, 0x22, +/* 0x80 * a b c d e f g */ + 0x07, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, +/* 0x88 h i ---- ---- ---- */ + 0x68, 0x69, 0xAE, 0xAF, 0x07, 0x07, 0x07, 0xF1, +/* 0x90 =C2=B0 j k l m n o p */ + 0xF8, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, +/* 0x98 q r ---- ---- */ + 0x71, 0x72, 0xA6, 0xA7, 0x91, 0x07, 0x92, 0x07, +/* 0xA0 ~ s t u v w x */ + 0xE6, 0x7E, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, +/* 0xA8 y z ---- ---- ---- ---- */ + 0x79, 0x7A, 0xAD, 0xAB, 0x07, 0x07, 0x07, 0x07, +/* 0xB0 ^ ---- =C2=A7 ---- */ + 0x5E, 0x9C, 0x9D, 0xFA, 0x07, 0x07, 0x07, 0xAC, +/* 0xB8 ---- [ ] ---- ---- ---- ---- */ + 0xAB, 0x07, 0x5B, 0x5D, 0x07, 0x07, 0x07, 0x07, +/* 0xC0 { A B C D E F G */ + 0x7B, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, +/* 0xC8 H I ---- =C3=B6 ---- */ + 0x48, 0x49, 0x07, 0x93, 0x94, 0x95, 0xA2, 0x07, +/* 0xD0 } J K L M N O P */ + 0x7D, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, +/* 0xD8 Q R ---- =C3=BC */ + 0x51, 0x52, 0x07, 0x96, 0x81, 0x97, 0xA3, 0x98, +/* 0xE0 \ S T U V W X */ + 0x5C, 0xF6, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, +/* 0xE8 Y Z ---- =C3=96 ---- ---- ---- */ + 0x59, 0x5A, 0xFD, 0x07, 0x99, 0x07, 0x07, 0x07, +/* 0xF0 0 1 2 3 4 5 6 7 */ + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, +/* 0xF8 8 9 ---- ---- =C3=9C ---- ---- ---- */ + 0x38, 0x39, 0x07, 0x07, 0x9A, 0x07, 0x07, 0x07 +}; + +static void vtoc_ebcdic_dec (unsigned char *source, unsigned char *targe= t, int l)=20 +{ + int i; + + for (i =3D 0; i < l; i++)=20 + target[i]=3DEBCtoASC[(unsigned char)(source[i])]; +} + +/*=20 + * struct dasd_information_t + * represents any data about the data, which is visible to userspace + */ +typedef struct dasd_information_t { + unsigned int devno; /* S/390 devno */ + unsigned int real_devno; /* for aliases */ + unsigned int schid; /* S/390 subchannel identifier */ + unsigned int cu_type : 16; /* from SenseID */ + unsigned int cu_model : 8; /* from SenseID */ + unsigned int dev_type : 16; /* from SenseID */ + unsigned int dev_model : 8; /* from SenseID */ + unsigned int open_count; + unsigned int req_queue_len; + unsigned int chanq_len; /* length of chanq */ + char type[4]; /* from discipline.name, 'none' for unknown */ + unsigned int status; /* current device level */ + unsigned int label_block; /* where to find the VOLSER */ + unsigned int FBA_layout; /* fixed block size (like AIXVOL) */ + unsigned int characteristics_size; + unsigned int confdata_size; + char characteristics[64]; /* from read_device_characteristics */ + char configuration_data[256]; /* from read_configuration_data */ +} dasd_information_t; + +#define _IOC_NRBITS 8 +#define _IOC_TYPEBITS 8 +#define _IOC_SIZEBITS 14 +#define _IOC_DIRBITS 2 +#define _IOC_NRMASK ((1 << _IOC_NRBITS)-1) +#define _IOC_TYPEMASK ((1 << _IOC_TYPEBITS)-1) +#define _IOC_SIZEMASK ((1 << _IOC_SIZEBITS)-1) +#define _IOC_DIRMASK ((1 << _IOC_DIRBITS)-1) +#define _IOC_NRSHIFT 0 +#define _IOC_TYPESHIFT (_IOC_NRSHIFT+_IOC_NRBITS) +#define _IOC_SIZESHIFT (_IOC_TYPESHIFT+_IOC_TYPEBITS) +#define _IOC_DIRSHIFT (_IOC_SIZESHIFT+_IOC_SIZEBITS) +#define DASD_IOCTL_LETTER 'D' + +#define BIODASDINFO _IOR(DASD_IOCTL_LETTER,1,dasd_information_t) +#define BLKSSZGET _IO(0x12,104) + +int probe_ibm_partition(int fd, char *out) +{ + int blocksize; + dasd_information_t info; + char name[7] =3D {0,}; + unsigned char data[16]; + + if (ioctl(fd, BIODASDINFO, (unsigned long)&info) !=3D 0) + return -1; + + if (ioctl(fd, BLKSSZGET, (unsigned long)&blocksize)) + return -1; + + lseek(fd, info.label_block * blocksize, SEEK_SET); + if (read(fd, &data, 16) !=3D 16) + return -1; + + if ((!info.FBA_layout) && (!strcmp(info.type, "ECKD"))) + strncpy(name, data + 8, 6); + else + strncpy(name, data + 4, 6); + + vtoc_ebcdic_dec(name, out, 6); + + return 0; +} diff -Nru a/extras/volume_id/dasdlabel.h b/extras/volume_id/dasdlabel.h --- /dev/null Wed Dec 31 16:00:00 1969 +++ b/extras/volume_id/dasdlabel.h Sat May 8 13:10:47 2004 @@ -0,0 +1,26 @@ +/* + * dasdlabel - read label from s390 block device + * + * Copyright (C) 2004 Arnd Bergmann + * + * This program is free software; you can redistribute it and/or modify = it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation version 2 of the License. + *=20 + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + *=20 + * You should have received a copy of the GNU General Public License alo= ng + * with this program; if not, write to the Free Software Foundation, Inc= ., + * 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +#ifndef _DASDLABEL_H_ +#define _DASDLABEL_H_ + +extern int probe_ibm_partition(int fd, char *out); + +#endif diff -Nru a/extras/volume_id/s390-dasd.rules b/extras/volume_id/s390-dasd= .rules --- /dev/null Wed Dec 31 16:00:00 1969 +++ b/extras/volume_id/s390-dasd.rules Sat May 8 13:10:47 2004 @@ -0,0 +1,7 @@ +# s390 block devices named by disk label +KERNEL=3D"dasd*[a-z]", PROGRAM=3D"/sbin/udev_volume_id -d -l" RESULT=3D"= [0-9A-Z]*", SYMLINK=3D"dasd/%c/disc dasd/%b/disc" +KERNEL=3D"dasd*[0-9]", PROGRAM=3D"/sbin/udev_volume_id -d -l" RESULT=3D"= [0-9A-Z]*", SYMLINK=3D"dasd/%c/part%n dasd/%b/part%n" +KERNEL=3D"dasd*[a-z]", SYMLINK=3D"dasd/%b/disc" +KERNEL=3D"dasd*[0-9]", SYMLINK=3D"dasd/%b/part%n" +KERNEL=3D"dcssblk*", NAME=3D"%k", SYMLINK=3D"dcssblk/%b" + diff -Nru a/extras/volume_id/udev_volume_id.c b/extras/volume_id/udev_vol= ume_id.c --- a/extras/volume_id/udev_volume_id.c Sat May 8 13:10:47 2004 +++ b/extras/volume_id/udev_volume_id.c Sat May 8 13:10:47 2004 @@ -4,7 +4,7 @@ * Copyright (C) 2004 Kay Sievers * * sample udev rule for creation of a symlink with the filsystem uuid: - * KERNEL=3D"sd*", PROGRAM=3D"/sbin/udev_volume_id -M%M -m%m -u", SYMLIN= K=3D"%c" + * KERNEL=3D"sd*", PROGRAM=3D"/sbin/udev_volume_id -u", SYMLINK=3D"%c" * * This program is free software; you can redistribute it and/or modify = it * under the terms of the GNU General Public License as published by the @@ -24,21 +24,75 @@ #include #include #include +#include =20 +#include "../../libsysfs/sysfs/libsysfs.h" +#include "../../udev_lib.h" +#include "../../logging.h" #include "volume_id.h" +#include "dasdlabel.h" =20 -int main(int argc, char *argv[]) +#ifdef LOG +unsigned char logname[LOGNAME_SIZE]; +void log_message(int level, const char *format, ...) +{ + va_list args; + + va_start(args, format); + vsyslog(level, format, args); + va_end(args); +} +#endif + +static struct volume_id *open_classdev(struct sysfs_class_device *class_= dev) { struct volume_id *vid; - const char help[] =3D "usage: udev_volume_id -m -M [-t|-l= |-u]\n"; - int major =3D -1; - int minor =3D -1; - char *tail; - static const char short_options[] =3D "M:m:htlu"; - int option; - char print =3D '\0'; - int rc; + struct sysfs_attribute *attr; + int major, minor; + + attr =3D sysfs_get_classdev_attr(class_dev, "dev"); + + if (attr =3D=3D NULL) { + printf("error reading 'dev' attribute\n"); + return NULL; + } + + if (sscanf(attr->value, "%u:%u", &major, &minor) !=3D 2) { + printf("error getting major/minor number\n"); + return NULL; + } + + vid =3D volume_id_open_dev_t(makedev(major, minor)); + if (vid =3D=3D NULL) { + printf("error open volume\n"); + return NULL; + } =20 + return vid; +} + +int main(int argc, char *argv[]) +{ + const char help[] =3D "usage: udev_volume_id [-t|-l|-u|-d]\n" + " -t filesystem type\n" + " -l filesystem label\n" + " -u filesystem uuid\n" + " -d dasd label from parent device\n" + "\n"; + static const char short_options[] =3D "htlud"; + int option; + char sysfs_path[SYSFS_PATH_MAX]; + char dev_path[SYSFS_PATH_MAX]; + struct sysfs_class_device *class_dev =3D NULL; + struct sysfs_class_device *class_dev_parent =3D NULL; + struct volume_id *vid =3D NULL; + char *devpath; + char probe =3D 'a'; + char print =3D 'a'; + char dasd_label[7]; + static char name[VOLUME_ID_LABEL_SIZE]; + int len, i, j; + int rc =3D 1; =20 while (1) { option =3D getopt(argc, argv, short_options); @@ -46,29 +100,18 @@ break; =20 switch (option) { - case 'M': - major =3D (int) strtoul(optarg, &tail, 10); - if (tail[0] !=3D '\0') { - printf("invalid major\n"); - exit(1); - } - break; - case 'm': - minor =3D (int) strtoul(optarg, &tail, 10); - if (tail[0] !=3D '\0') { - printf("invalid minor\n"); - exit(1); - } - break; case 't': print =3D 't'; - break; + continue; case 'l': print =3D 'l'; - break; + continue; case 'u': print =3D 'u'; - break; + continue; + case 'd': + probe =3D 'd'; + continue; case 'h': case '?': default: @@ -77,44 +120,114 @@ } } =20 - if (major =3D=3D -1 || minor =3D=3D -1) { - printf(help); - exit(1); + devpath =3D getenv("DEVPATH"); + if (devpath =3D=3D NULL) { + printf("error DEVPATH empty\n"); + goto exit; } =20 - vid =3D volume_id_open_dev_t(makedev(major, minor)); - if (vid =3D=3D NULL) { - printf("error open volume\n"); - exit(1); + if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) !=3D 0) { + printf("error getting sysfs mount path\n"); + goto exit; + } + + strfieldcpy(dev_path, sysfs_path); + strfieldcat(dev_path, devpath); + + class_dev =3D sysfs_open_class_device_path(dev_path); + if (class_dev =3D=3D NULL) { + printf("error getting class device\n"); + goto exit; + } + + switch(probe) { + case 'a' : + vid =3D open_classdev(class_dev); + if (vid =3D=3D NULL) + goto exit; + if (volume_id_probe(vid, ALL) =3D=3D 0) + goto print; + break; + case 'd' : + /* if we are on a partition, close it and open main block device */ + class_dev_parent =3D sysfs_get_classdev_parent(class_dev); + if (class_dev_parent !=3D NULL) { + volume_id_close(vid); + vid =3D open_classdev(class_dev_parent); + } else { + vid =3D open_classdev(class_dev_parent); + } + if (vid =3D=3D NULL) + goto exit; + if (probe_ibm_partition(vid->fd, dasd_label) =3D=3D 0) { + vid->fs_name =3D "dasd"; + strncpy(vid->label_string, dasd_label, 6); + vid->label_string[6] =3D '\0'; + goto print; + } + break; } =20 - rc =3D volume_id_probe(vid, ALL); - if (rc !=3D 0) { - printf("error probing volume\n"); - exit(1); + printf("unknown volume type\n"); + goto exit; + + +print: + len =3D strnlen(vid->label_string, VOLUME_ID_LABEL_SIZE); + + /* remove trailing spaces */ + while (len > 0 && isspace(vid->label_string[len-1])) + len--; + name[len] =3D '\0'; + + /* substitute chars */ + i =3D 0; + j =3D 0; + while (j < len) { + switch(vid->label_string[j]) { + case '/' : + break; + case ' ' : + name[i++] =3D '_'; + break; + default : + name[i++] =3D vid->label_string[j]; + } + j++; } + name[i] =3D '\0'; =20 switch (print) { case 't': printf("%s\n", vid->fs_name); break; case 'l': - if (vid->label_string[0] =3D=3D '\0') - exit(2); - printf("%s\n", vid->label_string); + if (name[0] =3D=3D '\0') { + rc =3D 2; + goto exit; + } + printf("%s\n", name); break; case 'u': - if (vid->uuid_string[0] =3D=3D '\0') - exit(2); + if (vid->uuid_string[0] =3D=3D '\0') { + rc =3D 2; + goto exit; + } printf("%s\n", vid->uuid_string); break; - default: + case 'a': printf("T:%s\n", vid->fs_name); printf("L:%s\n", vid->label_string); + printf("N:%s\n", name); printf("U:%s\n", vid->uuid_string); } + rc =3D 0; =20 - volume_id_close(vid); +exit: + if (class_dev !=3D NULL) + sysfs_close_class_device(class_dev); + if (vid !=3D NULL) + volume_id_close(vid); =20 - exit(0); + exit(rc); } diff -Nru a/extras/volume_id/volume_id.c b/extras/volume_id/volume_id.c --- a/extras/volume_id/volume_id.c Sat May 8 13:10:47 2004 +++ b/extras/volume_id/volume_id.c Sat May 8 13:10:47 2004 @@ -82,7 +82,7 @@ memcpy(id->label_string, buf, count); =20 /* remove trailing whitespace */ - i =3D strlen(id->label_string); + i =3D strnlen(id->label_string, count); while (i--) { if (! isspace(id->label_string[i])) break; @@ -839,10 +839,11 @@ char tmp_node[VOLUME_ID_PATH_MAX]; =20 snprintf(tmp_node, VOLUME_ID_PATH_MAX, - "/tmp/volume-%u-%u", major(devt), minor(devt)); + "/tmp/volume-%u-%u-%u", getpid(), major(devt), minor(devt)); tmp_node[VOLUME_ID_PATH_MAX] =3D '\0'; =20 /* create tempory node to open the block device */ + unlink(tmp_node); if (mknod(tmp_node, (S_IFBLK | 0600), devt) !=3D 0) return NULL; =20 --OXfL5xGRrasGEqWY-- ------------------------------------------------------- This SF.Net email is sponsored by Sleepycat Software Learn developer strategies Cisco, Motorola, Ericsson & Lucent use to deliver higher performing products faster, at low TCO. http://www.sleepycat.com/telcomwpreg.php?From=osdnemail3 _______________________________________________ Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net Linux-hotplug-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel