1 /* vi: set sw=4 ts=4: */
3 * Support functions for mounting devices by label/uuid
5 * Copyright (C) 2006 by Jason Schoon <floydpink@gmail.com>
6 * Some portions cribbed from e2fsprogs, util-linux, dosfstools
8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
11 //kbuild:lib-$(CONFIG_BLKID) += get_devname.o
12 //kbuild:lib-$(CONFIG_FINDFS) += get_devname.o
13 //kbuild:lib-$(CONFIG_FEATURE_MOUNT_LABEL) += get_devname.o
15 #include <sys/mount.h> /* BLKGETSIZE64 */
16 #if !defined(BLKGETSIZE64)
17 # define BLKGETSIZE64 _IOR(0x12,114,size_t)
19 #include "volume_id_internal.h"
21 static struct uuidCache_s {
22 struct uuidCache_s *next;
26 char *uc_uuid; /* prefix makes it easier to grep for */
27 IF_FEATURE_BLKID_TYPE(const char *type;)
30 #if !ENABLE_FEATURE_BLKID_TYPE
31 #define get_label_uuid(fd, label, uuid, type) \
32 get_label_uuid(fd, label, uuid)
33 #define uuidcache_addentry(device, label, uuid, type) \
34 uuidcache_addentry(device, label, uuid)
37 /* Returns !0 on error.
38 * Otherwise, returns malloc'ed strings for label and uuid
39 * (and they can't be NULL, although they can be "").
42 get_label_uuid(int fd, char **label, char **uuid, const char **type)
46 struct volume_id *vid;
48 /* fd is owned by vid now */
49 vid = volume_id_open_node(fd);
51 if (ioctl(/*vid->*/fd, BLKGETSIZE64, &size) != 0)
54 if (volume_id_probe_all(vid, /*0,*/ size) != 0)
57 if (vid->label[0] != '\0' || vid->uuid[0] != '\0'
58 #if ENABLE_FEATURE_BLKID_TYPE
62 *label = xstrndup(vid->label, sizeof(vid->label));
63 *uuid = xstrndup(vid->uuid, sizeof(vid->uuid));
64 #if ENABLE_FEATURE_BLKID_TYPE
66 dbg("found label '%s', uuid '%s', type '%s'", *label, *uuid, *type);
68 dbg("found label '%s', uuid '%s'", *label, *uuid);
73 free_volume_id(vid); /* also closes fd */
77 /* NB: we take ownership of (malloc'ed) label and uuid */
79 uuidcache_addentry(char *device, /*int major, int minor,*/ char *label, char *uuid, const char *type)
81 struct uuidCache_s *last;
84 last = uuidCache = xzalloc(sizeof(*uuidCache));
86 for (last = uuidCache; last->next; last = last->next)
88 last->next = xzalloc(sizeof(*uuidCache));
91 /*last->next = NULL; - xzalloc did it*/
92 // last->major = major;
93 // last->minor = minor;
94 last->device = device;
97 IF_FEATURE_BLKID_TYPE(last->type = type;)
100 /* If get_label_uuid() on device_name returns success,
101 * add a cache entry for this device.
102 * If device node does not exist, it will be temporarily created. */
104 uuidcache_check_device(const char *device,
105 struct stat *statbuf,
106 void *userData UNUSED_PARAM,
107 int depth UNUSED_PARAM)
109 /* note: this check rejects links to devices, among other nodes */
110 if (!S_ISBLK(statbuf->st_mode))
113 /* Users report that mucking with floppies (especially non-present
114 * ones) is significant PITA. This is a horribly dirty hack,
115 * but it is very useful in real world.
116 * If this will ever need to be enabled, consider using O_NONBLOCK.
118 if (major(statbuf->st_rdev) == 2)
121 add_to_uuid_cache(device);
126 static struct uuidCache_s*
127 uuidcache_init(int scan_devices)
129 dbg("DBG: uuidCache=%x, uuidCache");
133 /* We were scanning /proc/partitions
134 * and /proc/sys/dev/cdrom/info here.
135 * Missed volume managers. I see that "standard" blkid uses these:
136 * /dev/mapper/control
140 * This is unacceptably complex. Let's just scan /dev.
141 * (Maybe add scanning of /sys/block/XXX/dev for devices
142 * somehow not having their /dev/XXX entries created?) */
144 recursive_action("/dev", ACTION_RECURSE,
145 uuidcache_check_device, /* file_action */
146 NULL, /* dir_action */
158 get_spec_by_x(int n, const char *t, int *majorPtr, int *minorPtr)
160 struct uuidCache_s *uc;
162 uc = uuidcache_init(/*scan_devices:*/ 1);
166 if (strcmp(t, uc->uc_uuid) == 0) {
167 *majorPtr = uc->major;
168 *minorPtr = uc->minor;
173 if (strcmp(t, uc->label) == 0) {
174 *majorPtr = uc->major;
175 *minorPtr = uc->minor;
190 return ((c|0x20) - 'a' + 10);
194 get_spec_by_uuid(const char *s, int *major, int *minor)
196 unsigned char uuid[16];
199 if (strlen(s) != 36 || s[8] != '-' || s[13] != '-'
200 || s[18] != '-' || s[23] != '-'
204 for (i = 0; i < 16; i++) {
207 if (!isxdigit(s[0]) || !isxdigit(s[1]))
209 uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
212 return get_spec_by_x(UUID, (char *)uuid, major, minor);
215 fprintf(stderr, _("mount: bad UUID"));
220 get_spec_by_volume_label(const char *s, int *major, int *minor)
222 return get_spec_by_x(VOL, s, major, minor);
227 void display_uuid_cache(int scan_devices)
229 struct uuidCache_s *uc;
231 uc = uuidcache_init(scan_devices);
233 printf("%s:", uc->device);
235 printf(" LABEL=\"%s\"", uc->label);
237 printf(" UUID=\"%s\"", uc->uc_uuid);
238 #if ENABLE_FEATURE_BLKID_TYPE
240 printf(" TYPE=\"%s\"", uc->type);
247 int add_to_uuid_cache(const char *device)
249 char *uuid = uuid; /* for compiler */
251 #if ENABLE_FEATURE_BLKID_TYPE
252 const char *type = type;
256 fd = open(device, O_RDONLY);
260 /* get_label_uuid() closes fd in all cases (success & failure) */
261 if (get_label_uuid(fd, &label, &uuid, &type) == 0) {
262 /* uuidcache_addentry() takes ownership of all four params */
263 uuidcache_addentry(xstrdup(device), /*ma, mi,*/ label, uuid, type);
270 /* Used by mount and findfs */
272 char *get_devname_from_label(const char *spec)
274 struct uuidCache_s *uc;
276 uc = uuidcache_init(/*scan_devices:*/ 1);
278 if (uc->label[0] && strcmp(spec, uc->label) == 0) {
279 return xstrdup(uc->device);
286 char *get_devname_from_uuid(const char *spec)
288 struct uuidCache_s *uc;
290 uc = uuidcache_init(/*scan_devices:*/ 1);
292 /* case of hex numbers doesn't matter */
293 if (strcasecmp(spec, uc->uc_uuid) == 0) {
294 return xstrdup(uc->device);
301 int resolve_mount_spec(char **fsname)
305 if (strncmp(*fsname, "UUID=", 5) == 0)
306 tmp = get_devname_from_uuid(*fsname + 5);
307 else if (strncmp(*fsname, "LABEL=", 6) == 0)
308 tmp = get_devname_from_label(*fsname + 6);
311 return 0; /* no UUID= or LABEL= prefix found */