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