1 /* vi: set sw=4 ts=4: */
3 * devname.c - get a dev by its device inode name
5 * Copyright (C) Andries Brouwer
6 * Copyright (C) 1999, 2000, 2001, 2002, 2003 Theodore Ts'o
7 * Copyright (C) 2001 Andreas Dilger
10 * This file may be redistributed under the terms of the
11 * GNU Lesser General Public License.
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
30 #ifdef HAVE_SYS_MKDEV_H
31 #include <sys/mkdev.h>
38 * Find a dev struct in the cache by device name, if available.
40 * If there is no entry with the specified device name, and the create
41 * flag is set, then create an empty device entry.
43 blkid_dev blkid_get_dev(blkid_cache cache, const char *devname, int flags)
45 blkid_dev dev = NULL, tmp;
48 if (!cache || !devname)
51 list_for_each(p, &cache->bic_devs) {
52 tmp = list_entry(p, struct blkid_struct_dev, bid_devs);
53 if (strcmp(tmp->bid_name, devname))
57 printf("found devname %s in cache\n", tmp->bid_name));
62 if (!dev && (flags & BLKID_DEV_CREATE)) {
63 dev = blkid_new_dev();
66 dev->bid_name = blkid_strdup(devname);
67 dev->bid_cache = cache;
68 list_add_tail(&dev->bid_devs, &cache->bic_devs);
69 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
72 if (flags & BLKID_DEV_VERIFY)
73 dev = blkid_verify(cache, dev);
78 * Probe a single block device to add to the device cache.
80 static void probe_one(blkid_cache cache, const char *ptname,
88 /* See if we already have this device number in the cache. */
89 list_for_each(p, &cache->bic_devs) {
90 blkid_dev tmp = list_entry(p, struct blkid_struct_dev,
92 if (tmp->bid_devno == devno) {
93 dev = blkid_verify(cache, tmp);
97 if (dev && dev->bid_devno == devno)
101 * Take a quick look at /dev/ptname for the device number. We check
102 * all of the likely device directories. If we don't find it, or if
103 * the stat information doesn't check out, use blkid_devno_to_devname()
104 * to find it via an exhaustive search for the device major/minor.
106 for (dir = blkid_devdirs; *dir; dir++) {
110 sprintf(device, "%s/%s", *dir, ptname);
111 if ((dev = blkid_get_dev(cache, device, BLKID_DEV_FIND)) &&
112 dev->bid_devno == devno)
115 if (stat(device, &st) == 0 && S_ISBLK(st.st_mode) &&
116 st.st_rdev == devno) {
117 devname = blkid_strdup(device);
122 devname = blkid_devno_to_devname(devno);
126 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
130 if (!pri && !strncmp(ptname, "md", 2))
136 #define PROC_PARTITIONS "/proc/partitions"
137 #define VG_DIR "/proc/lvm/VGs"
140 * This function initializes the UUID cache with devices from the LVM
141 * proc hierarchy. We currently depend on the names of the LVM
142 * hierarchy giving us the device structure in /dev. (XXX is this a
147 static dev_t lvm_get_devno(const char *lvm_device)
154 DBG(DEBUG_DEVNAME, printf("opening %s\n", lvm_device));
155 if ((lvf = fopen(lvm_device, "r")) == NULL) {
156 DBG(DEBUG_DEVNAME, printf("%s: (%d) %s\n", lvm_device, errno,
161 while (fgets(buf, sizeof(buf), lvf)) {
162 if (sscanf(buf, "device: %d:%d", &ma, &mi) == 2) {
163 ret = makedev(ma, mi);
172 static void lvm_probe_all(blkid_cache cache)
175 struct dirent *vg_iter;
176 int vg_len = strlen(VG_DIR);
179 if ((vg_list = opendir(VG_DIR)) == NULL)
182 DBG(DEBUG_DEVNAME, printf("probing LVM devices under %s\n", VG_DIR));
184 while ((vg_iter = readdir(vg_list)) != NULL) {
188 struct dirent *lv_iter;
190 vg_name = vg_iter->d_name;
191 if (LONE_CHAR(vg_name, '.') || !strcmp(vg_name, ".."))
193 vdirname = xmalloc(vg_len + strlen(vg_name) + 8);
194 sprintf(vdirname, "%s/%s/LVs", VG_DIR, vg_name);
196 lv_list = opendir(vdirname);
201 while ((lv_iter = readdir(lv_list)) != NULL) {
202 char *lv_name, *lvm_device;
204 lv_name = lv_iter->d_name;
205 if (LONE_CHAR(lv_name, '.') || !strcmp(lv_name, ".."))
208 lvm_device = xmalloc(vg_len + strlen(vg_name) +
209 strlen(lv_name) + 8);
210 sprintf(lvm_device, "%s/%s/LVs/%s", VG_DIR, vg_name,
212 dev = lvm_get_devno(lvm_device);
213 sprintf(lvm_device, "%s/%s", vg_name, lv_name);
214 DBG(DEBUG_DEVNAME, printf("LVM dev %s: devno 0x%04X\n",
216 (unsigned int) dev));
217 probe_one(cache, lvm_device, dev, BLKID_PRI_LVM);
226 #define PROC_EVMS_VOLUMES "/proc/evms/volumes"
229 evms_probe_all(blkid_cache cache)
232 int ma, mi, sz, num = 0;
236 procpt = fopen(PROC_EVMS_VOLUMES, "r");
239 while (fgets(line, sizeof(line), procpt)) {
240 if (sscanf(line, " %d %d %d %*s %*s %[^\n ]",
241 &ma, &mi, &sz, device) != 4)
244 DBG(DEBUG_DEVNAME, printf("Checking partition %s (%d, %d)\n",
247 probe_one(cache, device, makedev(ma, mi), BLKID_PRI_EVMS);
255 * Read the device data for all available block devices in the system.
257 int blkid_probe_all(blkid_cache cache)
261 char ptname0[128], ptname1[128], *ptname = 0;
265 unsigned long long sz;
266 int lens[2] = { 0, 0 };
267 int which = 0, last = 0;
269 ptnames[0] = ptname0;
270 ptnames[1] = ptname1;
273 return -BLKID_ERR_PARAM;
275 if (cache->bic_flags & BLKID_BIC_FL_PROBED &&
276 time(0) - cache->bic_time < BLKID_PROBE_INTERVAL)
279 blkid_read_cache(cache);
280 evms_probe_all(cache);
282 lvm_probe_all(cache);
285 proc = fopen(PROC_PARTITIONS, "r");
287 return -BLKID_ERR_PROC;
289 while (fgets(line, sizeof(line), proc)) {
292 ptname = ptnames[which];
294 if (sscanf(line, " %d %d %llu %128[^\n ]",
295 &ma, &mi, &sz, ptname) != 4)
297 devs[which] = makedev(ma, mi);
299 DBG(DEBUG_DEVNAME, printf("read partition name %s\n", ptname));
301 /* Skip whole disk devs unless they have no partitions
302 * If we don't have a partition on this dev, also
303 * check previous dev to see if it didn't have a partn.
304 * heuristic: partition name ends in a digit.
306 * Skip extended partitions.
307 * heuristic: size is 1
309 * FIXME: skip /dev/{ida,cciss,rd} whole-disk devs
312 lens[which] = strlen(ptname);
313 if (isdigit(ptname[lens[which] - 1])) {
315 printf("partition dev %s, devno 0x%04X\n",
316 ptname, (unsigned int) devs[which]));
319 probe_one(cache, ptname, devs[which], 0);
322 } else if (lens[last] && strncmp(ptnames[last], ptname,
325 printf("whole dev %s, devno 0x%04X\n",
326 ptnames[last], (unsigned int) devs[last]));
327 probe_one(cache, ptnames[last], devs[last], 0);
332 /* Handle the last device if it wasn't partitioned */
334 probe_one(cache, ptname, devs[which], 0);
338 cache->bic_time = time(0);
339 cache->bic_flags |= BLKID_BIC_FL_PROBED;
340 blkid_flush_cache(cache);
345 int main(int argc, char **argv)
347 blkid_cache cache = NULL;
350 blkid_debug_mask = DEBUG_ALL;
352 fprintf(stderr, "Usage: %s\n"
353 "Probe all devices and exit\n", argv[0]);
356 if ((ret = blkid_get_cache(&cache, bb_dev_null)) != 0) {
357 fprintf(stderr, "%s: error creating cache (%d)\n",
361 if (blkid_probe_all(cache) < 0)
362 printf("%s: error probing devices\n", argv[0]);
364 blkid_put_cache(cache);