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