devname.c revision 4271e23942bdc60e1fa6c0b26bc666a94a8b3e1d
1/*
2 * devname.c - get a dev by its device inode name
3 *
4 * Copyright (C) Andries Brouwer
5 * Copyright (C) 1999, 2000, 2001, 2002, 2003 Theodore Ts'o
6 * Copyright (C) 2001 Andreas Dilger
7 *
8 * %Begin-Header%
9 * This file may be redistributed under the terms of the
10 * GNU Lesser General Public License.
11 * %End-Header%
12 */
13
14#define _GNU_SOURCE 1
15
16#include <stdio.h>
17#include <string.h>
18#include <limits.h>
19#if HAVE_UNISTD_H
20#include <unistd.h>
21#endif
22#include <stdlib.h>
23#include <string.h>
24#include <ctype.h>
25#if HAVE_SYS_TYPES_H
26#include <sys/types.h>
27#endif
28#include <dirent.h>
29#if HAVE_SYS_STAT_H
30#include <sys/stat.h>
31#endif
32#if HAVE_ERRNO_H
33#include <errno.h>
34#endif
35#if HAVE_SYS_MKDEV_H
36#include <sys/mkdev.h>
37#endif
38#include <time.h>
39
40#include "blkidP.h"
41
42/*
43 * Find a dev struct in the cache by device name, if available.
44 *
45 * If there is no entry with the specified device name, and the create
46 * flag is set, then create an empty device entry.
47 */
48blkid_dev blkid_get_dev(blkid_cache cache, const char *devname, int flags)
49{
50	blkid_dev dev = NULL, tmp;
51	struct list_head *p, *pnext;
52
53	if (!cache || !devname)
54		return NULL;
55
56	list_for_each(p, &cache->bic_devs) {
57		tmp = list_entry(p, struct blkid_struct_dev, bid_devs);
58		if (strcmp(tmp->bid_name, devname))
59			continue;
60
61		DBG(DEBUG_DEVNAME,
62		    printf("found devname %s in cache\n", tmp->bid_name));
63		dev = tmp;
64		break;
65	}
66
67	if (!dev && (flags & BLKID_DEV_CREATE)) {
68		if (access(devname, F_OK) < 0)
69			return NULL;
70		dev = blkid_new_dev();
71		if (!dev)
72			return NULL;
73		dev->bid_time = INT_MIN;
74		dev->bid_name = blkid_strdup(devname);
75		dev->bid_cache = cache;
76		list_add_tail(&dev->bid_devs, &cache->bic_devs);
77		cache->bic_flags |= BLKID_BIC_FL_CHANGED;
78	}
79
80	if (flags & BLKID_DEV_VERIFY) {
81		dev = blkid_verify(cache, dev);
82		if (!dev || !(dev->bid_flags & BLKID_BID_FL_VERIFIED))
83			return dev;
84		/*
85		 * If the device is verified, then search the blkid
86		 * cache for any entries that match on the type, uuid,
87		 * and label, and verify them; if a cache entry can
88		 * not be verified, then it's stale and so we remove
89		 * it.
90		 */
91		list_for_each_safe(p, pnext, &cache->bic_devs) {
92			blkid_dev dev2;
93			if (!p)
94				break;
95			dev2 = list_entry(p, struct blkid_struct_dev, bid_devs);
96			if (dev2->bid_flags & BLKID_BID_FL_VERIFIED)
97				continue;
98			if (!dev->bid_type || !dev2->bid_type ||
99			    strcmp(dev->bid_type, dev2->bid_type))
100				continue;
101			if (dev->bid_label && dev2->bid_label &&
102			    strcmp(dev->bid_label, dev2->bid_label))
103				continue;
104			if (dev->bid_uuid && dev2->bid_uuid &&
105			    strcmp(dev->bid_uuid, dev2->bid_uuid))
106				continue;
107			if ((dev->bid_label && !dev2->bid_label) ||
108			    (!dev->bid_label && dev2->bid_label) ||
109			    (dev->bid_uuid && !dev2->bid_uuid) ||
110			    (!dev->bid_uuid && dev2->bid_uuid))
111				continue;
112			dev2 = blkid_verify(cache, dev2);
113			if (dev2 && !(dev2->bid_flags & BLKID_BID_FL_VERIFIED))
114				blkid_free_dev(dev2);
115		}
116	}
117	return dev;
118}
119
120/* Directories where we will try to search for device names */
121static const char *dirlist[] = { "/dev", "/devfs", "/devices", NULL };
122
123static int is_dm_leaf(const char *devname)
124{
125	struct dirent	*de, *d_de;
126	DIR		*dir, *d_dir;
127	char		path[256];
128	int		ret = 1;
129
130	if ((dir = opendir("/sys/block")) == NULL)
131		return 0;
132	while ((de = readdir(dir)) != NULL) {
133		if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..") ||
134		    !strcmp(de->d_name, devname) ||
135		    strncmp(de->d_name, "dm-", 3) ||
136		    strlen(de->d_name) > sizeof(path)-32)
137			continue;
138		sprintf(path, "/sys/block/%s/slaves", de->d_name);
139		if ((d_dir = opendir(path)) == NULL)
140			continue;
141		while ((d_de = readdir(d_dir)) != NULL) {
142			if (!strcmp(d_de->d_name, devname)) {
143				ret = 0;
144				break;
145			}
146		}
147		closedir(d_dir);
148		if (!ret)
149			break;
150	}
151	closedir(dir);
152	return ret;
153}
154
155/*
156 * Probe a single block device to add to the device cache.
157 */
158static void probe_one(blkid_cache cache, const char *ptname,
159		      dev_t devno, int pri, int only_if_new)
160{
161	blkid_dev dev = NULL;
162	struct list_head *p, *pnext;
163	const char **dir;
164	char *devname = NULL;
165
166	/* See if we already have this device number in the cache. */
167	list_for_each_safe(p, pnext, &cache->bic_devs) {
168		blkid_dev tmp = list_entry(p, struct blkid_struct_dev,
169					   bid_devs);
170		if (tmp->bid_devno == devno) {
171			if (only_if_new && !access(tmp->bid_name, F_OK))
172				return;
173			dev = blkid_verify(cache, tmp);
174			if (dev && (dev->bid_flags & BLKID_BID_FL_VERIFIED))
175				break;
176			dev = 0;
177		}
178	}
179	if (dev && dev->bid_devno == devno)
180		goto set_pri;
181
182	/* Try to translate private device-mapper dm-<N> names
183	 * to standard /dev/mapper/<name>.
184	 */
185	if (!strncmp(ptname, "dm-", 3) && isdigit(ptname[3])) {
186		blkid__scan_dir("/dev/mapper", devno, 0, &devname);
187		if (devname)
188			goto get_dev;
189	}
190
191	/*
192	 * Take a quick look at /dev/ptname for the device number.  We check
193	 * all of the likely device directories.  If we don't find it, or if
194	 * the stat information doesn't check out, use blkid_devno_to_devname()
195	 * to find it via an exhaustive search for the device major/minor.
196	 */
197	for (dir = dirlist; *dir; dir++) {
198		struct stat st;
199		char device[256];
200
201		sprintf(device, "%s/%s", *dir, ptname);
202		if ((dev = blkid_get_dev(cache, device, BLKID_DEV_FIND)) &&
203		    dev->bid_devno == devno)
204			goto set_pri;
205
206		if (stat(device, &st) == 0 && S_ISBLK(st.st_mode) &&
207		    st.st_rdev == devno) {
208			devname = blkid_strdup(device);
209			goto get_dev;
210		}
211	}
212	/* Do a short-cut scan of /dev/mapper first */
213	if (!devname)
214		blkid__scan_dir("/dev/mapper", devno, 0, &devname);
215	if (!devname) {
216		devname = blkid_devno_to_devname(devno);
217		if (!devname)
218			return;
219	}
220get_dev:
221	dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
222	free(devname);
223set_pri:
224	if (dev) {
225		if (pri)
226			dev->bid_pri = pri;
227		else if (!strncmp(dev->bid_name, "/dev/mapper/", 11)) {
228			dev->bid_pri = BLKID_PRI_DM;
229			if (is_dm_leaf(ptname))
230				dev->bid_pri += 5;
231		} else if (!strncmp(ptname, "md", 2))
232			dev->bid_pri = BLKID_PRI_MD;
233 	}
234	return;
235}
236
237#define PROC_PARTITIONS "/proc/partitions"
238#define VG_DIR		"/proc/lvm/VGs"
239
240/*
241 * This function initializes the UUID cache with devices from the LVM
242 * proc hierarchy.  We currently depend on the names of the LVM
243 * hierarchy giving us the device structure in /dev.  (XXX is this a
244 * safe thing to do?)
245 */
246#ifdef VG_DIR
247static dev_t lvm_get_devno(const char *lvm_device)
248{
249	FILE *lvf;
250	char buf[1024];
251	int ma, mi;
252	dev_t ret = 0;
253
254	DBG(DEBUG_DEVNAME, printf("opening %s\n", lvm_device));
255	if ((lvf = fopen(lvm_device, "r")) == NULL) {
256		DBG(DEBUG_DEVNAME, printf("%s: (%d) %s\n", lvm_device, errno,
257					  strerror(errno)));
258		return 0;
259	}
260
261	while (fgets(buf, sizeof(buf), lvf)) {
262		if (sscanf(buf, "device: %d:%d", &ma, &mi) == 2) {
263			ret = makedev(ma, mi);
264			break;
265		}
266	}
267	fclose(lvf);
268
269	return ret;
270}
271
272static void lvm_probe_all(blkid_cache cache, int only_if_new)
273{
274	DIR		*vg_list;
275	struct dirent	*vg_iter;
276	int		vg_len = strlen(VG_DIR);
277	dev_t		dev;
278
279	if ((vg_list = opendir(VG_DIR)) == NULL)
280		return;
281
282	DBG(DEBUG_DEVNAME, printf("probing LVM devices under %s\n", VG_DIR));
283
284	while ((vg_iter = readdir(vg_list)) != NULL) {
285		DIR		*lv_list;
286		char		*vdirname;
287		char		*vg_name;
288		struct dirent	*lv_iter;
289
290		vg_name = vg_iter->d_name;
291		if (!strcmp(vg_name, ".") || !strcmp(vg_name, ".."))
292			continue;
293		vdirname = malloc(vg_len + strlen(vg_name) + 8);
294		if (!vdirname)
295			goto exit;
296		sprintf(vdirname, "%s/%s/LVs", VG_DIR, vg_name);
297
298		lv_list = opendir(vdirname);
299		free(vdirname);
300		if (lv_list == NULL)
301			continue;
302
303		while ((lv_iter = readdir(lv_list)) != NULL) {
304			char		*lv_name, *lvm_device;
305
306			lv_name = lv_iter->d_name;
307			if (!strcmp(lv_name, ".") || !strcmp(lv_name, ".."))
308				continue;
309
310			lvm_device = malloc(vg_len + strlen(vg_name) +
311					    strlen(lv_name) + 8);
312			if (!lvm_device) {
313				closedir(lv_list);
314				goto exit;
315			}
316			sprintf(lvm_device, "%s/%s/LVs/%s", VG_DIR, vg_name,
317				lv_name);
318			dev = lvm_get_devno(lvm_device);
319			sprintf(lvm_device, "%s/%s", vg_name, lv_name);
320			DBG(DEBUG_DEVNAME, printf("LVM dev %s: devno 0x%04X\n",
321						  lvm_device,
322						  (unsigned int) dev));
323			probe_one(cache, lvm_device, dev, BLKID_PRI_LVM,
324				  only_if_new);
325			free(lvm_device);
326		}
327		closedir(lv_list);
328	}
329exit:
330	closedir(vg_list);
331}
332#endif
333
334#define PROC_EVMS_VOLUMES "/proc/evms/volumes"
335
336static int
337evms_probe_all(blkid_cache cache, int only_if_new)
338{
339	char line[100];
340	int ma, mi, sz, num = 0;
341	FILE *procpt;
342	char device[110];
343
344	procpt = fopen(PROC_EVMS_VOLUMES, "r");
345	if (!procpt)
346		return 0;
347	while (fgets(line, sizeof(line), procpt)) {
348		if (sscanf (line, " %d %d %d %*s %*s %[^\n ]",
349			    &ma, &mi, &sz, device) != 4)
350			continue;
351
352		DBG(DEBUG_DEVNAME, printf("Checking partition %s (%d, %d)\n",
353					  device, ma, mi));
354
355		probe_one(cache, device, makedev(ma, mi), BLKID_PRI_EVMS,
356			  only_if_new);
357		num++;
358	}
359	fclose(procpt);
360	return num;
361}
362
363/*
364 * Read the device data for all available block devices in the system.
365 */
366static int probe_all(blkid_cache cache, int only_if_new)
367{
368	FILE *proc;
369	char line[1024];
370	char ptname0[128], ptname1[128], *ptname = 0;
371	char *ptnames[2];
372	dev_t devs[2];
373	int ma, mi;
374	unsigned long long sz;
375	int lens[2] = { 0, 0 };
376	int which = 0, last = 0;
377	struct list_head *p, *pnext;
378
379	ptnames[0] = ptname0;
380	ptnames[1] = ptname1;
381
382	if (!cache)
383		return -BLKID_ERR_PARAM;
384
385	if (cache->bic_flags & BLKID_BIC_FL_PROBED &&
386	    time(0) - cache->bic_time < BLKID_PROBE_INTERVAL)
387		return 0;
388
389	blkid_read_cache(cache);
390	evms_probe_all(cache, only_if_new);
391#ifdef VG_DIR
392	lvm_probe_all(cache, only_if_new);
393#endif
394
395	proc = fopen(PROC_PARTITIONS, "r");
396	if (!proc)
397		return -BLKID_ERR_PROC;
398
399	while (fgets(line, sizeof(line), proc)) {
400		last = which;
401		which ^= 1;
402		ptname = ptnames[which];
403
404		if (sscanf(line, " %d %d %llu %128[^\n ]",
405			   &ma, &mi, &sz, ptname) != 4)
406			continue;
407		devs[which] = makedev(ma, mi);
408
409		DBG(DEBUG_DEVNAME, printf("read partition name %s\n", ptname));
410
411		/* Skip whole disk devs unless they have no partitions.
412		 * If base name of device has changed, also
413		 * check previous dev to see if it didn't have a partn.
414		 * heuristic: partition name ends in a digit, & partition
415		 * names contain whole device name as substring.
416		 *
417		 * Skip extended partitions.
418		 * heuristic: size is 1
419		 *
420		 * FIXME: skip /dev/{ida,cciss,rd} whole-disk devs
421		 */
422
423		lens[which] = strlen(ptname);
424
425		/* ends in a digit, clearly a partition, so check */
426		if (isdigit(ptname[lens[which] - 1])) {
427			DBG(DEBUG_DEVNAME,
428			    printf("partition dev %s, devno 0x%04X\n",
429				   ptname, (unsigned int) devs[which]));
430
431			if (sz > 1)
432				probe_one(cache, ptname, devs[which], 0,
433					  only_if_new);
434			lens[which] = 0;	/* mark as checked */
435		}
436
437		/*
438		 * If last was a whole disk and we just found a partition
439		 * on it, remove the whole-disk dev from the cache if
440		 * it exists.
441		 */
442		if (lens[last] && !strncmp(ptnames[last], ptname, lens[last])) {
443			list_for_each_safe(p, pnext, &cache->bic_devs) {
444				blkid_dev tmp;
445
446				/* find blkid dev for the whole-disk devno */
447				tmp = list_entry(p, struct blkid_struct_dev,
448						 bid_devs);
449				if (tmp->bid_devno == devs[last]) {
450					DBG(DEBUG_DEVNAME,
451						printf("freeing %s\n",
452						       tmp->bid_name));
453					blkid_free_dev(tmp);
454					cache->bic_flags |= BLKID_BIC_FL_CHANGED;
455					break;
456				}
457			}
458			lens[last] = 0;
459		}
460		/*
461		 * If last was not checked because it looked like a whole-disk
462		 * dev, and the device's base name has changed,
463		 * check last as well.
464		 */
465		if (lens[last] && strncmp(ptnames[last], ptname, lens[last])) {
466			DBG(DEBUG_DEVNAME,
467			    printf("whole dev %s, devno 0x%04X\n",
468				   ptnames[last], (unsigned int) devs[last]));
469			probe_one(cache, ptnames[last], devs[last], 0,
470				  only_if_new);
471			lens[last] = 0;
472		}
473	}
474
475	/* Handle the last device if it wasn't partitioned */
476	if (lens[which])
477		probe_one(cache, ptname, devs[which], 0, only_if_new);
478
479	fclose(proc);
480	blkid_flush_cache(cache);
481	return 0;
482}
483
484int blkid_probe_all(blkid_cache cache)
485{
486	int ret;
487
488	DBG(DEBUG_PROBE, printf("Begin blkid_probe_all()\n"));
489	ret = probe_all(cache, 0);
490	cache->bic_time = time(0);
491	cache->bic_flags |= BLKID_BIC_FL_PROBED;
492	DBG(DEBUG_PROBE, printf("End blkid_probe_all()\n"));
493	return ret;
494}
495
496int blkid_probe_all_new(blkid_cache cache)
497{
498	int ret;
499
500	DBG(DEBUG_PROBE, printf("Begin blkid_probe_all_new()\n"));
501	ret = probe_all(cache, 1);
502	DBG(DEBUG_PROBE, printf("End blkid_probe_all_new()\n"));
503	return ret;
504}
505
506
507#ifdef TEST_PROGRAM
508int main(int argc, char **argv)
509{
510	blkid_cache cache = NULL;
511	int ret;
512
513	blkid_debug_mask = DEBUG_ALL;
514	if (argc != 1) {
515		fprintf(stderr, "Usage: %s\n"
516			"Probe all devices and exit\n", argv[0]);
517		exit(1);
518	}
519	if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
520		fprintf(stderr, "%s: error creating cache (%d)\n",
521			argv[0], ret);
522		exit(1);
523	}
524	if (blkid_probe_all(cache) < 0)
525		printf("%s: error probing devices\n", argv[0]);
526
527	blkid_put_cache(cache);
528	return (0);
529}
530#endif
531