blob: e99c15b02dc37a46dbcbed75816751d7c956c09a [file] [log] [blame]
Denis Vlasenkode7684a2008-02-18 21:08:49 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Support functions for mounting devices by label/uuid
4 *
5 * Copyright (C) 2006 by Jason Schoon <floydpink@gmail.com>
6 * Some portions cribbed from e2fsprogs, util-linux, dosfstools
7 *
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9 */
10
11#include "volume_id_internal.h"
12
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000013//#define BLKGETSIZE64 _IOR(0x12,114,size_t)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000014
15static struct uuidCache_s {
16 struct uuidCache_s *next;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000017// int major, minor;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000018 char *device;
19 char *label;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000020 char *uc_uuid; /* prefix makes it easier to grep for */
Denis Vlasenkode7684a2008-02-18 21:08:49 +000021} *uuidCache;
22
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000023/* Returns !0 on error.
24 * Otherwise, returns malloc'ed strings for label and uuid
Denis Vlasenko582dff02008-10-19 19:36:30 +000025 * (and they can't be NULL, although they can be "").
26 * NB: closes fd. */
Denis Vlasenkode7684a2008-02-18 21:08:49 +000027static int
Denis Vlasenko582dff02008-10-19 19:36:30 +000028get_label_uuid(int fd, char **label, char **uuid)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000029{
30 int rv = 1;
31 uint64_t size;
32 struct volume_id *vid;
33
Denis Vlasenko582dff02008-10-19 19:36:30 +000034 /* fd is owned by vid now */
35 vid = volume_id_open_node(fd);
Denis Vlasenkode7684a2008-02-18 21:08:49 +000036
Denis Vlasenko582dff02008-10-19 19:36:30 +000037 if (ioctl(/*vid->*/fd, BLKGETSIZE64, &size) != 0)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000038 size = 0;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000039
Denis Vlasenko582dff02008-10-19 19:36:30 +000040 if (volume_id_probe_all(vid, 0, size) != 0)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000041 goto ret;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000042
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000043 if (vid->label[0] != '\0' || vid->uuid[0] != '\0') {
Denis Vlasenkode7684a2008-02-18 21:08:49 +000044 *label = xstrndup(vid->label, sizeof(vid->label));
45 *uuid = xstrndup(vid->uuid, sizeof(vid->uuid));
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000046 dbg("found label '%s', uuid '%s' on %s", *label, *uuid, device);
Denis Vlasenkode7684a2008-02-18 21:08:49 +000047 rv = 0;
48 }
49 ret:
Denis Vlasenko582dff02008-10-19 19:36:30 +000050 free_volume_id(vid); /* also closes fd */
Denis Vlasenkode7684a2008-02-18 21:08:49 +000051 return rv;
52}
53
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000054/* NB: we take ownership of (malloc'ed) label and uuid */
Denis Vlasenkode7684a2008-02-18 21:08:49 +000055static void
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000056uuidcache_addentry(char *device, /*int major, int minor,*/ char *label, char *uuid)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000057{
58 struct uuidCache_s *last;
Denis Vlasenko42cc3042008-03-24 02:05:58 +000059
Denis Vlasenkode7684a2008-02-18 21:08:49 +000060 if (!uuidCache) {
61 last = uuidCache = xzalloc(sizeof(*uuidCache));
62 } else {
63 for (last = uuidCache; last->next; last = last->next)
64 continue;
65 last->next = xzalloc(sizeof(*uuidCache));
66 last = last->next;
67 }
68 /*last->next = NULL; - xzalloc did it*/
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000069// last->major = major;
70// last->minor = minor;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000071 last->device = device;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000072 last->label = label;
73 last->uc_uuid = uuid;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000074}
75
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000076/* If get_label_uuid() on device_name returns success,
77 * add a cache entry for this device.
78 * If device node does not exist, it will be temporarily created. */
Denis Vlasenko582dff02008-10-19 19:36:30 +000079static int FAST_FUNC
80uuidcache_check_device(const char *device,
81 struct stat *statbuf,
82 void *userData UNUSED_PARAM,
83 int depth UNUSED_PARAM)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000084{
Denis Vlasenko582dff02008-10-19 19:36:30 +000085 char *uuid = uuid; /* for compiler */
86 char *label = label;
87 int fd;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000088
Denis Vlasenko582dff02008-10-19 19:36:30 +000089 if (!S_ISBLK(statbuf->st_mode))
90 return TRUE;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000091
Denis Vlasenko582dff02008-10-19 19:36:30 +000092 fd = open(device, O_RDONLY);
93 if (fd < 0)
94 return TRUE;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000095
Denis Vlasenko582dff02008-10-19 19:36:30 +000096 /* get_label_uuid() closes fd in all cases (success & failure) */
97 if (get_label_uuid(fd, &label, &uuid) == 0) {
98 /* uuidcache_addentry() takes ownership of all three params */
99 uuidcache_addentry(xstrdup(device), /*ma, mi,*/ label, uuid);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000100 }
Denis Vlasenko582dff02008-10-19 19:36:30 +0000101 return TRUE;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000102}
103
104static void
105uuidcache_init(void)
106{
107 if (uuidCache)
108 return;
109
Denis Vlasenko582dff02008-10-19 19:36:30 +0000110 recursive_action("/dev", ACTION_RECURSE,
111 uuidcache_check_device, /* file_action */
112 NULL, /* dir_action */
113 NULL, /* userData */
114 0 /* depth */);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000115}
116
117#define UUID 1
118#define VOL 2
119
120#ifdef UNUSED
121static char *
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000122get_spec_by_x(int n, const char *t, int *majorPtr, int *minorPtr)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000123{
124 struct uuidCache_s *uc;
125
126 uuidcache_init();
127 uc = uuidCache;
128
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000129 while (uc) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000130 switch (n) {
131 case UUID:
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000132 if (strcmp(t, uc->uc_uuid) == 0) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000133 *majorPtr = uc->major;
134 *minorPtr = uc->minor;
135 return uc->device;
136 }
137 break;
138 case VOL:
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000139 if (strcmp(t, uc->label) == 0) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000140 *majorPtr = uc->major;
141 *minorPtr = uc->minor;
142 return uc->device;
143 }
144 break;
145 }
146 uc = uc->next;
147 }
148 return NULL;
149}
150
151static unsigned char
152fromhex(char c)
153{
154 if (isdigit(c))
155 return (c - '0');
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000156 return ((c|0x20) - 'a' + 10);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000157}
158
159static char *
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000160get_spec_by_uuid(const char *s, int *major, int *minor)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000161{
162 unsigned char uuid[16];
163 int i;
164
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000165 if (strlen(s) != 36 || s[8] != '-' || s[13] != '-'
166 || s[18] != '-' || s[23] != '-'
167 ) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000168 goto bad_uuid;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000169 }
170 for (i = 0; i < 16; i++) {
171 if (*s == '-')
172 s++;
173 if (!isxdigit(s[0]) || !isxdigit(s[1]))
174 goto bad_uuid;
175 uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
176 s += 2;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000177 }
178 return get_spec_by_x(UUID, (char *)uuid, major, minor);
179
180 bad_uuid:
181 fprintf(stderr, _("mount: bad UUID"));
182 return 0;
183}
184
185static char *
186get_spec_by_volume_label(const char *s, int *major, int *minor)
187{
188 return get_spec_by_x(VOL, s, major, minor);
189}
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000190#endif // UNUSED
191
Denis Vlasenko1e19afd2008-10-12 11:20:08 +0000192/* Used by blkid */
193void display_uuid_cache(void)
194{
195 struct uuidCache_s *u;
196
197 uuidcache_init();
198 u = uuidCache;
199 while (u) {
200 printf("%s:", u->device);
201 if (u->label[0])
202 printf(" LABEL=\"%s\"", u->label);
203 if (u->uc_uuid[0])
204 printf(" UUID=\"%s\"", u->uc_uuid);
205 bb_putchar('\n');
206 u = u->next;
207 }
208}
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000209
210/* Used by mount and findfs */
211
212char *get_devname_from_label(const char *spec)
213{
214 struct uuidCache_s *uc;
215 int spec_len = strlen(spec);
216
217 uuidcache_init();
218 uc = uuidCache;
219 while (uc) {
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000220// FIXME: empty label ("LABEL=") matches anything??!
221 if (uc->label[0] && strncmp(spec, uc->label, spec_len) == 0) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000222 return xstrdup(uc->device);
223 }
224 uc = uc->next;
225 }
226 return NULL;
227}
228
229char *get_devname_from_uuid(const char *spec)
230{
231 struct uuidCache_s *uc;
232
233 uuidcache_init();
234 uc = uuidCache;
235 while (uc) {
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000236 /* case of hex numbers doesn't matter */
237 if (strcasecmp(spec, uc->uc_uuid) == 0) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000238 return xstrdup(uc->device);
239 }
240 uc = uc->next;
241 }
242 return NULL;
243}