devname.c revision 4e60e06847c781efffc8c43342b7756bbe5cff45
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	/*
183	 * Take a quick look at /dev/ptname for the device number.  We check
184	 * all of the likely device directories.  If we don't find it, or if
185	 * the stat information doesn't check out, use blkid_devno_to_devname()
186	 * to find it via an exhaustive search for the device major/minor.
187	 */
188	for (dir = dirlist; *dir; dir++) {
189		struct stat st;
190		char device[256];
191
192		sprintf(device, "%s/%s", *dir, ptname);
193		if ((dev = blkid_get_dev(cache, device, BLKID_DEV_FIND)) &&
194		    dev->bid_devno == devno)
195			goto set_pri;
196
197		if (stat(device, &st) == 0 && S_ISBLK(st.st_mode) &&
198		    st.st_rdev == devno) {
199			devname = blkid_strdup(device);
200			break;
201		}
202	}
203	/* Do a short-cut scan of /dev/mapper first */
204	if (!devname)
205		blkid__scan_dir("/dev/mapper", devno, 0, &devname);
206	if (!devname) {
207		devname = blkid_devno_to_devname(devno);
208		if (!devname)
209			return;
210	}
211	dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
212	free(devname);
213
214set_pri:
215	if (dev) {
216		if (pri)
217			dev->bid_pri = pri;
218		else if (!strncmp(dev->bid_name, "/dev/mapper/", 11)) {
219			dev->bid_pri = BLKID_PRI_DM;
220			if (is_dm_leaf(ptname))
221				dev->bid_pri += 5;
222		} else if (!strncmp(ptname, "md", 2))
223			dev->bid_pri = BLKID_PRI_MD;
224 	}
225	return;
226}
227
228#define PROC_PARTITIONS "/proc/partitions"
229#define VG_DIR		"/proc/lvm/VGs"
230
231/*
232 * This function initializes the UUID cache with devices from the LVM
233 * proc hierarchy.  We currently depend on the names of the LVM
234 * hierarchy giving us the device structure in /dev.  (XXX is this a
235 * safe thing to do?)
236 */
237#ifdef VG_DIR
238static dev_t lvm_get_devno(const char *lvm_device)
239{
240	FILE *lvf;
241	char buf[1024];
242	int ma, mi;
243	dev_t ret = 0;
244
245	DBG(DEBUG_DEVNAME, printf("opening %s\n", lvm_device));
246	if ((lvf = fopen(lvm_device, "r")) == NULL) {
247		DBG(DEBUG_DEVNAME, printf("%s: (%d) %s\n", lvm_device, errno,
248					  strerror(errno)));
249		return 0;
250	}
251
252	while (fgets(buf, sizeof(buf), lvf)) {
253		if (sscanf(buf, "device: %d:%d", &ma, &mi) == 2) {
254			ret = makedev(ma, mi);
255			break;
256		}
257	}
258	fclose(lvf);
259
260	return ret;
261}
262
263static void lvm_probe_all(blkid_cache cache, int only_if_new)
264{
265	DIR		*vg_list;
266	struct dirent	*vg_iter;
267	int		vg_len = strlen(VG_DIR);
268	dev_t		dev;
269
270	if ((vg_list = opendir(VG_DIR)) == NULL)
271		return;
272
273	DBG(DEBUG_DEVNAME, printf("probing LVM devices under %s\n", VG_DIR));
274
275	while ((vg_iter = readdir(vg_list)) != NULL) {
276		DIR		*lv_list;
277		char		*vdirname;
278		char		*vg_name;
279		struct dirent	*lv_iter;
280
281		vg_name = vg_iter->d_name;
282		if (!strcmp(vg_name, ".") || !strcmp(vg_name, ".."))
283			continue;
284		vdirname = malloc(vg_len + strlen(vg_name) + 8);
285		if (!vdirname)
286			goto exit;
287		sprintf(vdirname, "%s/%s/LVs", VG_DIR, vg_name);
288
289		lv_list = opendir(vdirname);
290		free(vdirname);
291		if (lv_list == NULL)
292			continue;
293
294		while ((lv_iter = readdir(lv_list)) != NULL) {
295			char		*lv_name, *lvm_device;
296
297			lv_name = lv_iter->d_name;
298			if (!strcmp(lv_name, ".") || !strcmp(lv_name, ".."))
299				continue;
300
301			lvm_device = malloc(vg_len + strlen(vg_name) +
302					    strlen(lv_name) + 8);
303			if (!lvm_device) {
304				closedir(lv_list);
305				goto exit;
306			}
307			sprintf(lvm_device, "%s/%s/LVs/%s", VG_DIR, vg_name,
308				lv_name);
309			dev = lvm_get_devno(lvm_device);
310			sprintf(lvm_device, "%s/%s", vg_name, lv_name);
311			DBG(DEBUG_DEVNAME, printf("LVM dev %s: devno 0x%04X\n",
312						  lvm_device,
313						  (unsigned int) dev));
314			probe_one(cache, lvm_device, dev, BLKID_PRI_LVM,
315				  only_if_new);
316			free(lvm_device);
317		}
318		closedir(lv_list);
319	}
320exit:
321	closedir(vg_list);
322}
323#endif
324
325#define PROC_EVMS_VOLUMES "/proc/evms/volumes"
326
327static int
328evms_probe_all(blkid_cache cache, int only_if_new)
329{
330	char line[100];
331	int ma, mi, sz, num = 0;
332	FILE *procpt;
333	char device[110];
334
335	procpt = fopen(PROC_EVMS_VOLUMES, "r");
336	if (!procpt)
337		return 0;
338	while (fgets(line, sizeof(line), procpt)) {
339		if (sscanf (line, " %d %d %d %*s %*s %[^\n ]",
340			    &ma, &mi, &sz, device) != 4)
341			continue;
342
343		DBG(DEBUG_DEVNAME, printf("Checking partition %s (%d, %d)\n",
344					  device, ma, mi));
345
346		probe_one(cache, device, makedev(ma, mi), BLKID_PRI_EVMS,
347			  only_if_new);
348		num++;
349	}
350	fclose(procpt);
351	return num;
352}
353
354/*
355 * Read the device data for all available block devices in the system.
356 */
357static int probe_all(blkid_cache cache, int only_if_new)
358{
359	FILE *proc;
360	char line[1024];
361	char ptname0[128], ptname1[128], *ptname = 0;
362	char *ptnames[2];
363	dev_t devs[2];
364	int ma, mi;
365	unsigned long long sz;
366	int lens[2] = { 0, 0 };
367	int which = 0, last = 0;
368	struct list_head *p, *pnext;
369
370	ptnames[0] = ptname0;
371	ptnames[1] = ptname1;
372
373	if (!cache)
374		return -BLKID_ERR_PARAM;
375
376	if (cache->bic_flags & BLKID_BIC_FL_PROBED &&
377	    time(0) - cache->bic_time < BLKID_PROBE_INTERVAL)
378		return 0;
379
380	blkid_read_cache(cache);
381	evms_probe_all(cache, only_if_new);
382#ifdef VG_DIR
383	lvm_probe_all(cache, only_if_new);
384#endif
385
386	proc = fopen(PROC_PARTITIONS, "r");
387	if (!proc)
388		return -BLKID_ERR_PROC;
389
390	while (fgets(line, sizeof(line), proc)) {
391		last = which;
392		which ^= 1;
393		ptname = ptnames[which];
394
395		if (sscanf(line, " %d %d %llu %128[^\n ]",
396			   &ma, &mi, &sz, ptname) != 4)
397			continue;
398		devs[which] = makedev(ma, mi);
399
400		DBG(DEBUG_DEVNAME, printf("read partition name %s\n", ptname));
401
402		/* Skip whole disk devs unless they have no partitions.
403		 * If base name of device has changed, also
404		 * check previous dev to see if it didn't have a partn.
405		 * heuristic: partition name ends in a digit, & partition
406		 * names contain whole device name as substring.
407		 *
408		 * Skip extended partitions.
409		 * heuristic: size is 1
410		 *
411		 * FIXME: skip /dev/{ida,cciss,rd} whole-disk devs
412		 */
413
414		lens[which] = strlen(ptname);
415
416		/* ends in a digit, clearly a partition, so check */
417		if (isdigit(ptname[lens[which] - 1])) {
418			DBG(DEBUG_DEVNAME,
419			    printf("partition dev %s, devno 0x%04X\n",
420				   ptname, (unsigned int) devs[which]));
421
422			if (sz > 1)
423				probe_one(cache, ptname, devs[which], 0,
424					  only_if_new);
425			lens[which] = 0;	/* mark as checked */
426		}
427
428		/*
429		 * If last was a whole disk and we just found a partition
430		 * on it, remove the whole-disk dev from the cache if
431		 * it exists.
432		 */
433		if (lens[last] && !strncmp(ptnames[last], ptname, lens[last])) {
434			list_for_each_safe(p, pnext, &cache->bic_devs) {
435				blkid_dev tmp;
436
437				/* find blkid dev for the whole-disk devno */
438				tmp = list_entry(p, struct blkid_struct_dev,
439						 bid_devs);
440				if (tmp->bid_devno == devs[last]) {
441					DBG(DEBUG_DEVNAME,
442						printf("freeing %s\n",
443						       tmp->bid_name));
444					blkid_free_dev(tmp);
445					cache->bic_flags |= BLKID_BIC_FL_CHANGED;
446					break;
447				}
448			}
449			lens[last] = 0;
450		}
451		/*
452		 * If last was not checked because it looked like a whole-disk
453		 * dev, and the device's base name has changed,
454		 * check last as well.
455		 */
456		if (lens[last] && strncmp(ptnames[last], ptname, lens[last])) {
457			DBG(DEBUG_DEVNAME,
458			    printf("whole dev %s, devno 0x%04X\n",
459				   ptnames[last], (unsigned int) devs[last]));
460			probe_one(cache, ptnames[last], devs[last], 0,
461				  only_if_new);
462			lens[last] = 0;
463		}
464	}
465
466	/* Handle the last device if it wasn't partitioned */
467	if (lens[which])
468		probe_one(cache, ptname, devs[which], 0, only_if_new);
469
470	fclose(proc);
471	blkid_flush_cache(cache);
472	return 0;
473}
474
475int blkid_probe_all(blkid_cache cache)
476{
477	int ret;
478
479	DBG(DEBUG_PROBE, printf("Begin blkid_probe_all()\n"));
480	ret = probe_all(cache, 0);
481	cache->bic_time = time(0);
482	cache->bic_flags |= BLKID_BIC_FL_PROBED;
483	DBG(DEBUG_PROBE, printf("End blkid_probe_all()\n"));
484	return ret;
485}
486
487int blkid_probe_all_new(blkid_cache cache)
488{
489	int ret;
490
491	DBG(DEBUG_PROBE, printf("Begin blkid_probe_all_new()\n"));
492	ret = probe_all(cache, 1);
493	DBG(DEBUG_PROBE, printf("End blkid_probe_all_new()\n"));
494	return ret;
495}
496
497
498#ifdef TEST_PROGRAM
499int main(int argc, char **argv)
500{
501	blkid_cache cache = NULL;
502	int ret;
503
504	blkid_debug_mask = DEBUG_ALL;
505	if (argc != 1) {
506		fprintf(stderr, "Usage: %s\n"
507			"Probe all devices and exit\n", argv[0]);
508		exit(1);
509	}
510	if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
511		fprintf(stderr, "%s: error creating cache (%d)\n",
512			argv[0], ret);
513		exit(1);
514	}
515	if (blkid_probe_all(cache) < 0)
516		printf("%s: error probing devices\n", argv[0]);
517
518	blkid_put_cache(cache);
519	return (0);
520}
521#endif
522