]>
git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - lib/blkid/devname.c
2 * devname.c - get a dev by its device inode name
4 * Copyright (C) Andries Brouwer
5 * Copyright (C) 1999, 2000, 2001, 2002, 2003 Theodore Ts'o
6 * Copyright (C) 2001 Andreas Dilger
9 * This file may be redistributed under the terms of the
10 * GNU Lesser General Public License.
26 #include <sys/types.h>
35 #include <sys/mkdev.h>
42 #include <libdevmapper.h>
46 * Find a dev struct in the cache by device name, if available.
48 * If there is no entry with the specified device name, and the create
49 * flag is set, then create an empty device entry.
51 blkid_dev
blkid_get_dev(blkid_cache cache
, const char *devname
, int flags
)
53 blkid_dev dev
= NULL
, tmp
;
56 if (!cache
|| !devname
)
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
))
65 printf("found devname %s in cache\n", tmp
->bid_name
));
70 if (!dev
&& (flags
& BLKID_DEV_CREATE
)) {
71 dev
= blkid_new_dev();
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
;
81 if (flags
& BLKID_DEV_VERIFY
)
82 dev
= blkid_verify(cache
, dev
);
87 static int dm_device_is_leaf(const dev_t dev
);
91 * Probe a single block device to add to the device cache.
93 static void probe_one(blkid_cache cache
, const char *ptname
,
94 dev_t devno
, int pri
, int only_if_new
)
101 /* See if we already have this device number in the cache. */
102 list_for_each(p
, &cache
->bic_devs
) {
103 blkid_dev tmp
= list_entry(p
, struct blkid_struct_dev
,
105 #ifdef HAVE_DEVMAPPER
106 if (!dm_device_is_leaf(devno
))
109 if (tmp
->bid_devno
== devno
) {
112 dev
= blkid_verify(cache
, tmp
);
116 if (dev
&& dev
->bid_devno
== devno
)
120 * Take a quick look at /dev/ptname for the device number. We check
121 * all of the likely device directories. If we don't find it, or if
122 * the stat information doesn't check out, use blkid_devno_to_devname()
123 * to find it via an exhaustive search for the device major/minor.
125 for (dir
= blkid_devdirs
; *dir
; dir
++) {
129 sprintf(device
, "%s/%s", *dir
, ptname
);
130 if ((dev
= blkid_get_dev(cache
, device
, BLKID_DEV_FIND
)) &&
131 dev
->bid_devno
== devno
)
134 if (stat(device
, &st
) == 0 && S_ISBLK(st
.st_mode
) &&
135 st
.st_rdev
== devno
) {
136 devname
= blkid_strdup(device
);
141 devname
= blkid_devno_to_devname(devno
);
145 dev
= blkid_get_dev(cache
, devname
, BLKID_DEV_NORMAL
);
149 if (!pri
&& !strncmp(ptname
, "md", 2))
156 #ifdef HAVE_DEVMAPPER
157 static void dm_quiet_log(int level
, const char *file
, int line
,
164 * device-mapper support
166 static int dm_device_has_dep(const dev_t dev
, const char *name
)
168 struct dm_task
*task
;
169 struct dm_deps
*deps
;
173 task
= dm_task_create(DM_DEVICE_DEPS
);
177 dm_task_set_name(task
, name
);
179 dm_task_get_info(task
, &info
);
182 dm_task_destroy(task
);
186 deps
= dm_task_get_deps(task
);
187 if (!deps
|| deps
->count
== 0) {
188 dm_task_destroy(task
);
192 for (i
= 0; i
< deps
->count
; i
++) {
193 dev_t dep_dev
= deps
->device
[i
];
195 if (dev
== dep_dev
) {
196 dm_task_destroy(task
);
201 dm_task_destroy(task
);
205 static int dm_device_is_leaf(const dev_t dev
)
207 struct dm_task
*task
;
208 struct dm_names
*names
;
209 unsigned int next
= 0;
212 dm_log_init(dm_quiet_log
);
213 task
= dm_task_create(DM_DEVICE_LIST
);
219 names
= dm_task_get_names(task
);
220 if (!names
|| !names
->dev
) {
221 dm_task_destroy(task
);
227 names
= (void *)names
+ next
;
229 if (dm_device_has_dep(dev
, names
->name
))
235 dm_task_destroy(task
);
240 static dev_t
dm_get_devno(const char *name
)
242 struct dm_task
*task
;
246 task
= dm_task_create(DM_DEVICE_INFO
);
250 dm_task_set_name(task
, name
);
252 dm_task_get_info(task
, &info
);
255 dm_task_destroy(task
);
259 ret
= makedev(info
.major
, info
.minor
);
261 dm_task_destroy(task
);
266 static void dm_probe_all(blkid_cache cache
, int only_if_new
)
268 struct dm_task
*task
;
269 struct dm_names
*names
;
270 unsigned int next
= 0;
273 dm_log_init(dm_quiet_log
);
274 task
= dm_task_create(DM_DEVICE_LIST
);
280 names
= dm_task_get_names(task
);
281 if (!names
|| !names
->dev
) {
282 dm_task_destroy(task
);
292 names
= (void *)names
+ next
;
294 rc
= asprintf(&device
, "mapper/%s", names
->name
);
298 dev
= dm_get_devno(names
->name
);
302 if (!dm_device_is_leaf(dev
))
305 probe_one(cache
, device
, dev
, BLKID_PRI_DM
, only_if_new
);
312 dm_task_destroy(task
);
314 #endif /* HAVE_DEVMAPPER */
316 #define PROC_PARTITIONS "/proc/partitions"
317 #define VG_DIR "/proc/lvm/VGs"
320 * This function initializes the UUID cache with devices from the LVM
321 * proc hierarchy. We currently depend on the names of the LVM
322 * hierarchy giving us the device structure in /dev. (XXX is this a
327 static dev_t
lvm_get_devno(const char *lvm_device
)
334 DBG(DEBUG_DEVNAME
, printf("opening %s\n", lvm_device
));
335 if ((lvf
= fopen(lvm_device
, "r")) == NULL
) {
336 DBG(DEBUG_DEVNAME
, printf("%s: (%d) %s\n", lvm_device
, errno
,
341 while (fgets(buf
, sizeof(buf
), lvf
)) {
342 if (sscanf(buf
, "device: %d:%d", &ma
, &mi
) == 2) {
343 ret
= makedev(ma
, mi
);
352 static void lvm_probe_all(blkid_cache cache
, int only_if_new
)
355 struct dirent
*vg_iter
;
356 int vg_len
= strlen(VG_DIR
);
359 if ((vg_list
= opendir(VG_DIR
)) == NULL
)
362 DBG(DEBUG_DEVNAME
, printf("probing LVM devices under %s\n", VG_DIR
));
364 while ((vg_iter
= readdir(vg_list
)) != NULL
) {
368 struct dirent
*lv_iter
;
370 vg_name
= vg_iter
->d_name
;
371 if (!strcmp(vg_name
, ".") || !strcmp(vg_name
, ".."))
373 vdirname
= malloc(vg_len
+ strlen(vg_name
) + 8);
376 sprintf(vdirname
, "%s/%s/LVs", VG_DIR
, vg_name
);
378 lv_list
= opendir(vdirname
);
383 while ((lv_iter
= readdir(lv_list
)) != NULL
) {
384 char *lv_name
, *lvm_device
;
386 lv_name
= lv_iter
->d_name
;
387 if (!strcmp(lv_name
, ".") || !strcmp(lv_name
, ".."))
390 lvm_device
= malloc(vg_len
+ strlen(vg_name
) +
391 strlen(lv_name
) + 8);
396 sprintf(lvm_device
, "%s/%s/LVs/%s", VG_DIR
, vg_name
,
398 dev
= lvm_get_devno(lvm_device
);
399 sprintf(lvm_device
, "%s/%s", vg_name
, lv_name
);
400 DBG(DEBUG_DEVNAME
, printf("LVM dev %s: devno 0x%04X\n",
402 (unsigned int) dev
));
403 probe_one(cache
, lvm_device
, dev
, BLKID_PRI_LVM
,
414 #define PROC_EVMS_VOLUMES "/proc/evms/volumes"
417 evms_probe_all(blkid_cache cache
, int only_if_new
)
420 int ma
, mi
, sz
, num
= 0;
424 procpt
= fopen(PROC_EVMS_VOLUMES
, "r");
427 while (fgets(line
, sizeof(line
), procpt
)) {
428 if (sscanf (line
, " %d %d %d %*s %*s %[^\n ]",
429 &ma
, &mi
, &sz
, device
) != 4)
432 DBG(DEBUG_DEVNAME
, printf("Checking partition %s (%d, %d)\n",
435 probe_one(cache
, device
, makedev(ma
, mi
), BLKID_PRI_EVMS
,
444 * Read the device data for all available block devices in the system.
446 static int probe_all(blkid_cache cache
, int only_if_new
)
450 char ptname0
[128], ptname1
[128], *ptname
= 0;
454 unsigned long long sz
;
455 int lens
[2] = { 0, 0 };
456 int which
= 0, last
= 0;
458 ptnames
[0] = ptname0
;
459 ptnames
[1] = ptname1
;
462 return -BLKID_ERR_PARAM
;
464 if (cache
->bic_flags
& BLKID_BIC_FL_PROBED
&&
465 time(0) - cache
->bic_time
< BLKID_PROBE_INTERVAL
)
468 blkid_read_cache(cache
);
469 #ifdef HAVE_DEVMAPPER
470 dm_probe_all(cache
, only_if_new
);
472 evms_probe_all(cache
, only_if_new
);
474 lvm_probe_all(cache
, only_if_new
);
477 proc
= fopen(PROC_PARTITIONS
, "r");
479 return -BLKID_ERR_PROC
;
481 while (fgets(line
, sizeof(line
), proc
)) {
484 ptname
= ptnames
[which
];
486 if (sscanf(line
, " %d %d %llu %128[^\n ]",
487 &ma
, &mi
, &sz
, ptname
) != 4)
489 devs
[which
] = makedev(ma
, mi
);
491 DBG(DEBUG_DEVNAME
, printf("read partition name %s\n", ptname
));
493 /* Skip whole disk devs unless they have no partitions
494 * If we don't have a partition on this dev, also
495 * check previous dev to see if it didn't have a partn.
496 * heuristic: partition name ends in a digit.
498 * Skip extended partitions.
499 * heuristic: size is 1
501 * FIXME: skip /dev/{ida,cciss,rd} whole-disk devs
504 lens
[which
] = strlen(ptname
);
505 if (isdigit(ptname
[lens
[which
] - 1])) {
507 printf("partition dev %s, devno 0x%04X\n",
508 ptname
, (unsigned int) devs
[which
]));
511 probe_one(cache
, ptname
, devs
[which
], 0,
515 } else if (lens
[last
] && strncmp(ptnames
[last
], ptname
,
518 printf("whole dev %s, devno 0x%04X\n",
519 ptnames
[last
], (unsigned int) devs
[last
]));
520 probe_one(cache
, ptnames
[last
], devs
[last
], 0,
526 /* Handle the last device if it wasn't partitioned */
528 probe_one(cache
, ptname
, devs
[which
], 0, only_if_new
);
531 blkid_flush_cache(cache
);
535 int blkid_probe_all(blkid_cache cache
)
539 DBG(DEBUG_PROBE
, printf("Begin blkid_probe_all()\n"));
540 ret
= probe_all(cache
, 0);
541 cache
->bic_time
= time(0);
542 cache
->bic_flags
|= BLKID_BIC_FL_PROBED
;
543 DBG(DEBUG_PROBE
, printf("End blkid_probe_all()\n"));
547 int blkid_probe_all_new(blkid_cache cache
)
551 DBG(DEBUG_PROBE
, printf("Begin blkid_probe_all_new()\n"));
552 ret
= probe_all(cache
, 1);
553 DBG(DEBUG_PROBE
, printf("End blkid_probe_all_new()\n"));
559 int main(int argc
, char **argv
)
561 blkid_cache cache
= NULL
;
564 blkid_debug_mask
= DEBUG_ALL
;
566 fprintf(stderr
, "Usage: %s\n"
567 "Probe all devices and exit\n", argv
[0]);
570 if ((ret
= blkid_get_cache(&cache
, "/dev/null")) != 0) {
571 fprintf(stderr
, "%s: error creating cache (%d)\n",
575 if (blkid_probe_all(cache
) < 0)
576 printf("%s: error probing devices\n", argv
[0]);
578 blkid_put_cache(cache
);