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.
24 #include <sys/types.h>
33 #include <sys/mkdev.h>
40 * Find a dev struct in the cache by device name, if available.
42 * If there is no entry with the specified device name, and the create
43 * flag is set, then create an empty device entry.
45 blkid_dev blkid_get_dev(blkid_cache cache, const char *devname, int flags)
47 blkid_dev dev = NULL, tmp;
50 if (!cache || !devname)
53 list_for_each(p, &cache->bic_devs) {
54 tmp = list_entry(p, struct blkid_struct_dev, bid_devs);
55 if (strcmp(tmp->bid_name, devname))
59 printf("found devname %s in cache\n", tmp->bid_name));
64 if (!dev && (flags & BLKID_DEV_CREATE)) {
65 dev = blkid_new_dev();
68 dev->bid_name = blkid_strdup(devname);
69 dev->bid_cache = cache;
70 list_add_tail(&dev->bid_devs, &cache->bic_devs);
71 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
74 if (flags & BLKID_DEV_VERIFY)
75 dev = blkid_verify(cache, dev);
80 * Probe a single block device to add to the device cache.
82 static void probe_one(blkid_cache cache, const char *ptname,
90 /* See if we already have this device number in the cache. */
91 list_for_each(p, &cache->bic_devs) {
92 blkid_dev tmp = list_entry(p, struct blkid_struct_dev,
94 if (tmp->bid_devno == devno) {
95 dev = blkid_verify(cache, tmp);
99 if (dev && dev->bid_devno == devno)
103 * Take a quick look at /dev/ptname for the device number. We check
104 * all of the likely device directories. If we don't find it, or if
105 * the stat information doesn't check out, use blkid_devno_to_devname()
106 * to find it via an exhaustive search for the device major/minor.
108 for (dir = blkid_devdirs; *dir; dir++) {
112 sprintf(device, "%s/%s", *dir, ptname);
113 if ((dev = blkid_get_dev(cache, device, BLKID_DEV_FIND)) &&
114 dev->bid_devno == devno)
117 if (stat(device, &st) == 0 && S_ISBLK(st.st_mode) &&
118 st.st_rdev == devno) {
119 devname = blkid_strdup(device);
124 devname = blkid_devno_to_devname(devno);
128 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
132 if (!pri && !strncmp(ptname, "md", 2))
139 #define PROC_PARTITIONS "/proc/partitions"
140 #define VG_DIR "/proc/lvm/VGs"
143 * This function initializes the UUID cache with devices from the LVM
144 * proc hierarchy. We currently depend on the names of the LVM
145 * hierarchy giving us the device structure in /dev. (XXX is this a
150 static dev_t lvm_get_devno(const char *lvm_device)
157 DBG(DEBUG_DEVNAME, printf("opening %s\n", lvm_device));
158 if ((lvf = fopen(lvm_device, "r")) == NULL) {
159 DBG(DEBUG_DEVNAME, printf("%s: (%d) %s\n", lvm_device, errno,
164 while (fgets(buf, sizeof(buf), lvf)) {
165 if (sscanf(buf, "device: %d:%d", &ma, &mi) == 2) {
166 ret = makedev(ma, mi);
175 static void lvm_probe_all(blkid_cache cache)
178 struct dirent *vg_iter;
179 int vg_len = strlen(VG_DIR);
182 if ((vg_list = opendir(VG_DIR)) == NULL)
185 DBG(DEBUG_DEVNAME, printf("probing LVM devices under %s\n", VG_DIR));
187 while ((vg_iter = readdir(vg_list)) != NULL) {
191 struct dirent *lv_iter;
193 vg_name = vg_iter->d_name;
194 if (!strcmp(vg_name, ".") || !strcmp(vg_name, ".."))
196 vdirname = xmalloc(vg_len + strlen(vg_name) + 8);
197 sprintf(vdirname, "%s/%s/LVs", VG_DIR, vg_name);
199 lv_list = opendir(vdirname);
204 while ((lv_iter = readdir(lv_list)) != NULL) {
205 char *lv_name, *lvm_device;
207 lv_name = lv_iter->d_name;
208 if (!strcmp(lv_name, ".") || !strcmp(lv_name, ".."))
211 lvm_device = xmalloc(vg_len + strlen(vg_name) +
212 strlen(lv_name) + 8);
213 sprintf(lvm_device, "%s/%s/LVs/%s", VG_DIR, vg_name,
215 dev = lvm_get_devno(lvm_device);
216 sprintf(lvm_device, "%s/%s", vg_name, lv_name);
217 DBG(DEBUG_DEVNAME, printf("LVM dev %s: devno 0x%04X\n",
219 (unsigned int) dev));
220 probe_one(cache, lvm_device, dev, BLKID_PRI_LVM);
229 #define PROC_EVMS_VOLUMES "/proc/evms/volumes"
232 evms_probe_all(blkid_cache cache)
235 int ma, mi, sz, num = 0;
239 procpt = fopen(PROC_EVMS_VOLUMES, "r");
242 while (fgets(line, sizeof(line), procpt)) {
243 if (sscanf (line, " %d %d %d %*s %*s %[^\n ]",
244 &ma, &mi, &sz, device) != 4)
247 DBG(DEBUG_DEVNAME, printf("Checking partition %s (%d, %d)\n",
250 probe_one(cache, device, makedev(ma, mi), BLKID_PRI_EVMS);
258 * Read the device data for all available block devices in the system.
260 int blkid_probe_all(blkid_cache cache)
264 char ptname0[128], ptname1[128], *ptname = 0;
268 unsigned long long sz;
269 int lens[2] = { 0, 0 };
270 int which = 0, last = 0;
272 ptnames[0] = ptname0;
273 ptnames[1] = ptname1;
276 return -BLKID_ERR_PARAM;
278 if (cache->bic_flags & BLKID_BIC_FL_PROBED &&
279 time(0) - cache->bic_time < BLKID_PROBE_INTERVAL)
282 blkid_read_cache(cache);
283 evms_probe_all(cache);
285 lvm_probe_all(cache);
288 proc = fopen(PROC_PARTITIONS, "r");
290 return -BLKID_ERR_PROC;
292 while (fgets(line, sizeof(line), proc)) {
295 ptname = ptnames[which];
297 if (sscanf(line, " %d %d %llu %128[^\n ]",
298 &ma, &mi, &sz, ptname) != 4)
300 devs[which] = makedev(ma, mi);
302 DBG(DEBUG_DEVNAME, printf("read partition name %s\n", ptname));
304 /* Skip whole disk devs unless they have no partitions
305 * If we don't have a partition on this dev, also
306 * check previous dev to see if it didn't have a partn.
307 * heuristic: partition name ends in a digit.
309 * Skip extended partitions.
310 * heuristic: size is 1
312 * FIXME: skip /dev/{ida,cciss,rd} whole-disk devs
315 lens[which] = strlen(ptname);
316 if (isdigit(ptname[lens[which] - 1])) {
318 printf("partition dev %s, devno 0x%04X\n",
319 ptname, (unsigned int) devs[which]));
322 probe_one(cache, ptname, devs[which], 0);
325 } else if (lens[last] && strncmp(ptnames[last], ptname,
328 printf("whole dev %s, devno 0x%04X\n",
329 ptnames[last], (unsigned int) devs[last]));
330 probe_one(cache, ptnames[last], devs[last], 0);
335 /* Handle the last device if it wasn't partitioned */
337 probe_one(cache, ptname, devs[which], 0);
341 cache->bic_time = time(0);
342 cache->bic_flags |= BLKID_BIC_FL_PROBED;
343 blkid_flush_cache(cache);
348 int main(int argc, char **argv)
350 blkid_cache cache = NULL;
353 blkid_debug_mask = DEBUG_ALL;
355 fprintf(stderr, "Usage: %s\n"
356 "Probe all devices and exit\n", argv[0]);
359 if ((ret = blkid_get_cache(&cache, bb_dev_null)) != 0) {
360 fprintf(stderr, "%s: error creating cache (%d)\n",
364 if (blkid_probe_all(cache) < 0)
365 printf("%s: error probing devices\n", argv[0]);
367 blkid_put_cache(cache);