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