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