blob: b27b6612c6e30f2e8fa03b7753a269c981cec9a4 [file] [log] [blame]
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001/*
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#ifdef HAVE_UNISTD_H
20#include <unistd.h>
21#endif
22#include <stdlib.h>
23#include <ctype.h>
24#include <fcntl.h>
25#ifdef HAVE_SYS_TYPES_H
26#include <sys/types.h>
27#endif
28#include <dirent.h>
29#ifdef HAVE_SYS_STAT_H
30#include <sys/stat.h>
31#endif
32#ifdef HAVE_ERRNO_H
33#include <errno.h>
34#endif
35#include <time.h>
36
37#include "blkidP.h"
38
39#include "canonicalize.h" /* $(top_srcdir)/include */
40#include "pathnames.h"
41#include "sysfs.h"
42#include "at.h"
43
44/*
45 * Find a dev struct in the cache by device name, if available.
46 *
47 * If there is no entry with the specified device name, and the create
48 * flag is set, then create an empty device entry.
49 */
50blkid_dev blkid_get_dev(blkid_cache cache, const char *devname, int flags)
51{
52 blkid_dev dev = NULL, tmp;
53 struct list_head *p, *pnext;
54
55 if (!cache || !devname)
56 return NULL;
57
58 list_for_each(p, &cache->bic_devs) {
59 tmp = list_entry(p, struct blkid_struct_dev, bid_devs);
60 if (strcmp(tmp->bid_name, devname))
61 continue;
62
63 DBG(DEBUG_DEVNAME,
64 printf("found devname %s in cache\n", tmp->bid_name));
65 dev = tmp;
66 break;
67 }
68
69 if (!dev && (flags & BLKID_DEV_CREATE)) {
70 if (access(devname, F_OK) < 0)
71 return NULL;
72 dev = blkid_new_dev();
73 if (!dev)
74 return NULL;
75 dev->bid_time = INT_MIN;
76 dev->bid_name = strdup(devname);
77 dev->bid_cache = cache;
78 list_add_tail(&dev->bid_devs, &cache->bic_devs);
79 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
80 }
81
82 if (flags & BLKID_DEV_VERIFY) {
83 dev = blkid_verify(cache, dev);
84 if (!dev || !(dev->bid_flags & BLKID_BID_FL_VERIFIED))
85 return dev;
86 /*
87 * If the device is verified, then search the blkid
88 * cache for any entries that match on the type, uuid,
89 * and label, and verify them; if a cache entry can
90 * not be verified, then it's stale and so we remove
91 * it.
92 */
93 list_for_each_safe(p, pnext, &cache->bic_devs) {
94 blkid_dev dev2 = list_entry(p, struct blkid_struct_dev, bid_devs);
95 if (dev2->bid_flags & BLKID_BID_FL_VERIFIED)
96 continue;
97 if (!dev->bid_type || !dev2->bid_type ||
98 strcmp(dev->bid_type, dev2->bid_type))
99 continue;
100 if (dev->bid_label && dev2->bid_label &&
101 strcmp(dev->bid_label, dev2->bid_label))
102 continue;
103 if (dev->bid_uuid && dev2->bid_uuid &&
104 strcmp(dev->bid_uuid, dev2->bid_uuid))
105 continue;
106 if ((dev->bid_label && !dev2->bid_label) ||
107 (!dev->bid_label && dev2->bid_label) ||
108 (dev->bid_uuid && !dev2->bid_uuid) ||
109 (!dev->bid_uuid && dev2->bid_uuid))
110 continue;
111 dev2 = blkid_verify(cache, dev2);
112 if (dev2 && !(dev2->bid_flags & BLKID_BID_FL_VERIFIED))
113 blkid_free_dev(dev2);
114 }
115 }
116 return dev;
117}
118
119/* Directories where we will try to search for device names */
120static const char *dirlist[] = { "/dev", "/devfs", "/devices", NULL };
121
122static int is_dm_leaf(const char *devname)
123{
124 struct dirent *de, *d_de;
125 DIR *dir, *d_dir;
126 char path[256];
127 int ret = 1;
128
129 if ((dir = opendir("/sys/block")) == NULL)
130 return 0;
131 while ((de = readdir(dir)) != NULL) {
132 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..") ||
133 !strcmp(de->d_name, devname) ||
134 strncmp(de->d_name, "dm-", 3) ||
135 strlen(de->d_name) > sizeof(path)-32)
136 continue;
137 sprintf(path, "/sys/block/%s/slaves", de->d_name);
138 if ((d_dir = opendir(path)) == NULL)
139 continue;
140 while ((d_de = readdir(d_dir)) != NULL) {
141 if (!strcmp(d_de->d_name, devname)) {
142 ret = 0;
143 break;
144 }
145 }
146 closedir(d_dir);
147 if (!ret)
148 break;
149 }
150 closedir(dir);
151 return ret;
152}
153
154/*
155 * Probe a single block device to add to the device cache.
156 */
157static void probe_one(blkid_cache cache, const char *ptname,
158 dev_t devno, int pri, int only_if_new, int removable)
159{
160 blkid_dev dev = NULL;
161 struct list_head *p, *pnext;
162 const char **dir;
163 char *devname = NULL;
164
165 /* See if we already have this device number in the cache. */
166 list_for_each_safe(p, pnext, &cache->bic_devs) {
167 blkid_dev tmp = list_entry(p, struct blkid_struct_dev,
168 bid_devs);
169 if (tmp->bid_devno == devno) {
170 if (only_if_new && !access(tmp->bid_name, F_OK))
171 return;
172 dev = blkid_verify(cache, tmp);
173 if (dev && (dev->bid_flags & BLKID_BID_FL_VERIFIED))
174 break;
175 dev = 0;
176 }
177 }
178 if (dev && dev->bid_devno == devno)
179 goto set_pri;
180
181 /* Try to translate private device-mapper dm-<N> names
182 * to standard /dev/mapper/<name>.
183 */
184 if (!strncmp(ptname, "dm-", 3) && isdigit(ptname[3])) {
185 devname = canonicalize_dm_name(ptname);
186 if (!devname)
187 blkid__scan_dir("/dev/mapper", devno, 0, &devname);
188 if (devname)
189 goto get_dev;
190 }
191
192 /*
193 * Take a quick look at /dev/ptname for the device number. We check
194 * all of the likely device directories. If we don't find it, or if
195 * the stat information doesn't check out, use blkid_devno_to_devname()
196 * to find it via an exhaustive search for the device major/minor.
197 */
198 for (dir = dirlist; *dir; dir++) {
199 struct stat st;
200 char device[256];
201
202 snprintf(device, sizeof(device), "%s/%s", *dir, ptname);
203 if ((dev = blkid_get_dev(cache, device, BLKID_DEV_FIND)) &&
204 dev->bid_devno == devno)
205 goto set_pri;
206
207 if (stat(device, &st) == 0 &&
208 (S_ISBLK(st.st_mode) ||
209 (S_ISCHR(st.st_mode) && !strncmp(ptname, "ubi", 3))) &&
210 st.st_rdev == devno) {
211 devname = strdup(device);
212 goto get_dev;
213 }
214 }
215 /* Do a short-cut scan of /dev/mapper first */
216 if (!devname)
217 blkid__scan_dir("/dev/mapper", devno, 0, &devname);
218 if (!devname) {
219 devname = blkid_devno_to_devname(devno);
220 if (!devname)
221 return;
222 }
223
224get_dev:
225 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
226 free(devname);
227
228set_pri:
229 if (dev) {
230 if (pri)
231 dev->bid_pri = pri;
232 else if (!strncmp(dev->bid_name, "/dev/mapper/", 11)) {
233 dev->bid_pri = BLKID_PRI_DM;
234 if (is_dm_leaf(ptname))
235 dev->bid_pri += 5;
236 } else if (!strncmp(ptname, "md", 2))
237 dev->bid_pri = BLKID_PRI_MD;
238 if (removable)
239 dev->bid_flags |= BLKID_BID_FL_REMOVABLE;
240 }
241 return;
242}
243
244#define PROC_PARTITIONS "/proc/partitions"
245#define VG_DIR "/proc/lvm/VGs"
246
247/*
248 * This function initializes the UUID cache with devices from the LVM
249 * proc hierarchy. We currently depend on the names of the LVM
250 * hierarchy giving us the device structure in /dev. (XXX is this a
251 * safe thing to do?)
252 */
253#ifdef VG_DIR
254static dev_t lvm_get_devno(const char *lvm_device)
255{
256 FILE *lvf;
257 char buf[1024];
258 int ma, mi;
259 dev_t ret = 0;
260
261 DBG(DEBUG_DEVNAME, printf("opening %s\n", lvm_device));
262 if ((lvf = fopen(lvm_device, "r")) == NULL) {
263 DBG(DEBUG_DEVNAME, printf("%s: (%d) %m\n", lvm_device, errno));
264 return 0;
265 }
266
267 while (fgets(buf, sizeof(buf), lvf)) {
268 if (sscanf(buf, "device: %d:%d", &ma, &mi) == 2) {
269 ret = makedev(ma, mi);
270 break;
271 }
272 }
273 fclose(lvf);
274
275 return ret;
276}
277
278static void lvm_probe_all(blkid_cache cache, int only_if_new)
279{
280 DIR *vg_list;
281 struct dirent *vg_iter;
282 int vg_len = strlen(VG_DIR);
283 dev_t dev;
284
285 if ((vg_list = opendir(VG_DIR)) == NULL)
286 return;
287
288 DBG(DEBUG_DEVNAME, printf("probing LVM devices under %s\n", VG_DIR));
289
290 while ((vg_iter = readdir(vg_list)) != NULL) {
291 DIR *lv_list;
292 char *vdirname;
293 char *vg_name;
294 struct dirent *lv_iter;
295
296 vg_name = vg_iter->d_name;
297 if (!strcmp(vg_name, ".") || !strcmp(vg_name, ".."))
298 continue;
299 vdirname = malloc(vg_len + strlen(vg_name) + 8);
300 if (!vdirname)
301 goto exit;
302 sprintf(vdirname, "%s/%s/LVs", VG_DIR, vg_name);
303
304 lv_list = opendir(vdirname);
305 free(vdirname);
306 if (lv_list == NULL)
307 continue;
308
309 while ((lv_iter = readdir(lv_list)) != NULL) {
310 char *lv_name, *lvm_device;
311
312 lv_name = lv_iter->d_name;
313 if (!strcmp(lv_name, ".") || !strcmp(lv_name, ".."))
314 continue;
315
316 lvm_device = malloc(vg_len + strlen(vg_name) +
317 strlen(lv_name) + 8);
318 if (!lvm_device) {
319 closedir(lv_list);
320 goto exit;
321 }
322 sprintf(lvm_device, "%s/%s/LVs/%s", VG_DIR, vg_name,
323 lv_name);
324 dev = lvm_get_devno(lvm_device);
325 sprintf(lvm_device, "%s/%s", vg_name, lv_name);
326 DBG(DEBUG_DEVNAME, printf("LVM dev %s: devno 0x%04X\n",
327 lvm_device,
328 (unsigned int) dev));
329 probe_one(cache, lvm_device, dev, BLKID_PRI_LVM,
330 only_if_new, 0);
331 free(lvm_device);
332 }
333 closedir(lv_list);
334 }
335exit:
336 closedir(vg_list);
337}
338#endif
339
340#define PROC_EVMS_VOLUMES "/proc/evms/volumes"
341
342static int
343evms_probe_all(blkid_cache cache, int only_if_new)
344{
345 char line[100];
346 int ma, mi, sz, num = 0;
347 FILE *procpt;
348 char device[110];
349
350 procpt = fopen(PROC_EVMS_VOLUMES, "r");
351 if (!procpt)
352 return 0;
353 while (fgets(line, sizeof(line), procpt)) {
354 if (sscanf (line, " %d %d %d %*s %*s %[^\n ]",
355 &ma, &mi, &sz, device) != 4)
356 continue;
357
358 DBG(DEBUG_DEVNAME, printf("Checking partition %s (%d, %d)\n",
359 device, ma, mi));
360
361 probe_one(cache, device, makedev(ma, mi), BLKID_PRI_EVMS,
362 only_if_new, 0);
363 num++;
364 }
365 fclose(procpt);
366 return num;
367}
368
369static void
370ubi_probe_all(blkid_cache cache, int only_if_new)
371{
372 const char **dirname;
373
374 for (dirname = dirlist; *dirname; dirname++) {
375 DBG(DEBUG_DEVNAME, printf("probing UBI volumes under %s\n",
376 *dirname));
377
378 DIR *dir;
379 struct dirent *iter;
380
381 dir = opendir(*dirname);
382 if (dir == NULL)
383 continue ;
384
385 while ((iter = readdir(dir)) != NULL) {
386 char *name;
387 struct stat st;
388 dev_t dev;
389
390 name = iter->d_name;
391#ifdef _DIRENT_HAVE_D_TYPE
392 if (iter->d_type != DT_UNKNOWN &&
393 iter->d_type != DT_CHR && iter->d_type != DT_LNK)
394 continue;
395#endif
396 if (!strcmp(name, ".") || !strcmp(name, "..") ||
397 !strstr(name, "ubi"))
398 continue;
399 if (!strcmp(name, "ubi_ctrl"))
400 continue;
401 if (fstat_at(dirfd(dir), *dirname, name, &st, 0))
402 continue;
403
404 dev = st.st_rdev;
405
406 if (!S_ISCHR(st.st_mode) || !minor(dev))
407 continue;
408 DBG(DEBUG_DEVNAME, printf("UBI vol %s/%s: devno 0x%04X\n",
409 *dirname, name, (int) dev));
410 probe_one(cache, name, dev, BLKID_PRI_UBI, only_if_new, 0);
411 }
412 closedir(dir);
413 }
414}
415
416/*
417 * Read the device data for all available block devices in the system.
418 */
419static int probe_all(blkid_cache cache, int only_if_new)
420{
421 FILE *proc;
422 char line[1024];
423 char ptname0[128 + 1], ptname1[128 + 1], *ptname = 0;
424 char *ptnames[2];
425 dev_t devs[2];
426 int ma, mi;
427 unsigned long long sz;
428 int lens[2] = { 0, 0 };
429 int which = 0, last = 0;
430 struct list_head *p, *pnext;
431
432 ptnames[0] = ptname0;
433 ptnames[1] = ptname1;
434
435 if (!cache)
436 return -BLKID_ERR_PARAM;
437
438 if (cache->bic_flags & BLKID_BIC_FL_PROBED &&
439 time(0) - cache->bic_time < BLKID_PROBE_INTERVAL)
440 return 0;
441
442 blkid_read_cache(cache);
443 evms_probe_all(cache, only_if_new);
444#ifdef VG_DIR
445 lvm_probe_all(cache, only_if_new);
446#endif
447 ubi_probe_all(cache, only_if_new);
448
449 proc = fopen(PROC_PARTITIONS, "r");
450 if (!proc)
451 return -BLKID_ERR_PROC;
452
453 while (fgets(line, sizeof(line), proc)) {
454 last = which;
455 which ^= 1;
456 ptname = ptnames[which];
457
458 if (sscanf(line, " %d %d %llu %128[^\n ]",
459 &ma, &mi, &sz, ptname) != 4)
460 continue;
461 devs[which] = makedev(ma, mi);
462
463 DBG(DEBUG_DEVNAME, printf("read partition name %s\n", ptname));
464
465 /* Skip whole disk devs unless they have no partitions.
466 * If base name of device has changed, also
467 * check previous dev to see if it didn't have a partn.
468 * heuristic: partition name ends in a digit, & partition
469 * names contain whole device name as substring.
470 *
471 * Skip extended partitions.
472 * heuristic: size is 1
473 *
474 * FIXME: skip /dev/{ida,cciss,rd} whole-disk devs
475 */
476
477 lens[which] = strlen(ptname);
478
479 /* ends in a digit, clearly a partition, so check */
480 if (isdigit(ptname[lens[which] - 1])) {
481 DBG(DEBUG_DEVNAME,
482 printf("partition dev %s, devno 0x%04X\n",
483 ptname, (unsigned int) devs[which]));
484
485 if (sz > 1)
486 probe_one(cache, ptname, devs[which], 0,
487 only_if_new, 0);
488 lens[which] = 0; /* mark as checked */
489 }
490
491 /*
492 * If last was a whole disk and we just found a partition
493 * on it, remove the whole-disk dev from the cache if
494 * it exists.
495 */
496 if (lens[last] && !strncmp(ptnames[last], ptname, lens[last])) {
497 list_for_each_safe(p, pnext, &cache->bic_devs) {
498 blkid_dev tmp;
499
500 /* find blkid dev for the whole-disk devno */
501 tmp = list_entry(p, struct blkid_struct_dev,
502 bid_devs);
503 if (tmp->bid_devno == devs[last]) {
504 DBG(DEBUG_DEVNAME,
505 printf("freeing %s\n",
506 tmp->bid_name));
507 blkid_free_dev(tmp);
508 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
509 break;
510 }
511 }
512 lens[last] = 0;
513 }
514 /*
515 * If last was not checked because it looked like a whole-disk
516 * dev, and the device's base name has changed,
517 * check last as well.
518 */
519 if (lens[last] && strncmp(ptnames[last], ptname, lens[last])) {
520 DBG(DEBUG_DEVNAME,
521 printf("whole dev %s, devno 0x%04X\n",
522 ptnames[last], (unsigned int) devs[last]));
523 probe_one(cache, ptnames[last], devs[last], 0,
524 only_if_new, 0);
525 lens[last] = 0;
526 }
527 }
528
529 /* Handle the last device if it wasn't partitioned */
530 if (lens[which])
531 probe_one(cache, ptname, devs[which], 0, only_if_new, 0);
532
533 fclose(proc);
534 blkid_flush_cache(cache);
535 return 0;
536}
537
538/* Don't use it by default -- it's pretty slow (because cdroms, floppy, ...)
539 */
540static int probe_all_removable(blkid_cache cache)
541{
542 DIR *dir;
543 struct dirent *d;
544
545 if (!cache)
546 return -BLKID_ERR_PARAM;
547
548 dir = opendir(_PATH_SYS_BLOCK);
549 if (!dir)
550 return -BLKID_ERR_PROC;
551
552 while((d = readdir(dir))) {
553 struct sysfs_cxt sysfs = UL_SYSFSCXT_EMPTY;
554 int removable = 0;
555 dev_t devno;
556
557#ifdef _DIRENT_HAVE_D_TYPE
558 if (d->d_type != DT_UNKNOWN && d->d_type != DT_LNK)
559 continue;
560#endif
561 if (d->d_name[0] == '.' &&
562 ((d->d_name[1] == 0) ||
563 ((d->d_name[1] == '.') && (d->d_name[2] == 0))))
564 continue;
565
566 devno = sysfs_devname_to_devno(d->d_name, NULL);
567 if (!devno)
568 continue;
569
570 if (sysfs_init(&sysfs, devno, NULL) == 0) {
571 sysfs_read_int(&sysfs, "removable", &removable);
572 sysfs_deinit(&sysfs);
573 }
574
575 if (removable)
576 probe_one(cache, d->d_name, devno, 0, 0, 1);
577 }
578
579 closedir(dir);
580 return 0;
581}
582
583
584/**
585 * blkid_probe_all:
586 * @cache: cache handler
587 *
588 * Probes all block devices.
589 *
590 * Returns: 0 on success, or number less than zero in case of error.
591 */
592int blkid_probe_all(blkid_cache cache)
593{
594 int ret;
595
596 DBG(DEBUG_PROBE, printf("Begin blkid_probe_all()\n"));
597 ret = probe_all(cache, 0);
598 if (ret == 0) {
599 cache->bic_time = time(0);
600 cache->bic_flags |= BLKID_BIC_FL_PROBED;
601 }
602 DBG(DEBUG_PROBE, printf("End blkid_probe_all() [rc=%d]\n", ret));
603 return ret;
604}
605
606/**
607 * blkid_probe_all_new:
608 * @cache: cache handler
609 *
610 * Probes all new block devices.
611 *
612 * Returns: 0 on success, or number less than zero in case of error.
613 */
614int blkid_probe_all_new(blkid_cache cache)
615{
616 int ret;
617
618 DBG(DEBUG_PROBE, printf("Begin blkid_probe_all_new()\n"));
619 ret = probe_all(cache, 1);
620 DBG(DEBUG_PROBE, printf("End blkid_probe_all_new() [rc=%d]\n", ret));
621 return ret;
622}
623
624/**
625 * blkid_probe_all_removable:
626 * @cache: cache handler
627 *
628 * The libblkid probing is based on devices from /proc/partitions by default.
629 * This file usually does not contain removable devices (e.g. CDROMs) and this kind
630 * of devices are invisible for libblkid.
631 *
632 * This function adds removable block devices to @cache (probing is based on
633 * information from the /sys directory). Don't forget that removable devices
634 * (floppies, CDROMs, ...) could be pretty slow. It's very bad idea to call
635 * this function by default.
636 *
637 * Note that devices which were detected by this function won't be written to
638 * blkid.tab cache file.
639 *
640 * Returns: 0 on success, or number less than zero in case of error.
641 */
642int blkid_probe_all_removable(blkid_cache cache)
643{
644 int ret;
645
646 DBG(DEBUG_PROBE, printf("Begin blkid_probe_all_removable()\n"));
647 ret = probe_all_removable(cache);
648 DBG(DEBUG_PROBE, printf("End blkid_probe_all_removable() [rc=%d]\n", ret));
649 return ret;
650}
651
652#ifdef TEST_PROGRAM
653int main(int argc, char **argv)
654{
655 blkid_cache cache = NULL;
656 int ret;
657
658 blkid_init_debug(DEBUG_ALL);
659 if (argc != 1) {
660 fprintf(stderr, "Usage: %s\n"
661 "Probe all devices and exit\n", argv[0]);
662 exit(1);
663 }
664 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
665 fprintf(stderr, "%s: error creating cache (%d)\n",
666 argv[0], ret);
667 exit(1);
668 }
669 if (blkid_probe_all(cache) < 0)
670 printf("%s: error probing devices\n", argv[0]);
671
672 if (blkid_probe_all_removable(cache) < 0)
673 printf("%s: error probing removable devices\n", argv[0]);
674
675 blkid_put_cache(cache);
676 return (0);
677}
678#endif