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))
137 #define PROC_PARTITIONS "/proc/partitions"
138 #define VG_DIR "/proc/lvm/VGs"
141 * This function initializes the UUID cache with devices from the LVM
142 * proc hierarchy. We currently depend on the names of the LVM
143 * hierarchy giving us the device structure in /dev. (XXX is this a
148 static dev_t lvm_get_devno(const char *lvm_device)
155 DBG(DEBUG_DEVNAME, printf("opening %s\n", lvm_device));
156 if ((lvf = fopen(lvm_device, "r")) == NULL) {
157 DBG(DEBUG_DEVNAME, printf("%s: (%d) %s\n", lvm_device, errno,
162 while (fgets(buf, sizeof(buf), lvf)) {
163 if (sscanf(buf, "device: %d:%d", &ma, &mi) == 2) {
164 ret = makedev(ma, mi);
173 static void lvm_probe_all(blkid_cache cache)
176 struct dirent *vg_iter;
177 int vg_len = strlen(VG_DIR);
180 if ((vg_list = opendir(VG_DIR)) == NULL)
183 DBG(DEBUG_DEVNAME, printf("probing LVM devices under %s\n", VG_DIR));
185 while ((vg_iter = readdir(vg_list)) != NULL) {
189 struct dirent *lv_iter;
191 vg_name = vg_iter->d_name;
192 if (LONE_CHAR(vg_name, '.') || !strcmp(vg_name, ".."))
194 vdirname = xmalloc(vg_len + strlen(vg_name) + 8);
195 sprintf(vdirname, "%s/%s/LVs", VG_DIR, vg_name);
197 lv_list = opendir(vdirname);
202 while ((lv_iter = readdir(lv_list)) != NULL) {
203 char *lv_name, *lvm_device;
205 lv_name = lv_iter->d_name;
206 if (LONE_CHAR(lv_name, '.') || !strcmp(lv_name, ".."))
209 lvm_device = xmalloc(vg_len + strlen(vg_name) +
210 strlen(lv_name) + 8);
211 sprintf(lvm_device, "%s/%s/LVs/%s", VG_DIR, vg_name,
213 dev = lvm_get_devno(lvm_device);
214 sprintf(lvm_device, "%s/%s", vg_name, lv_name);
215 DBG(DEBUG_DEVNAME, printf("LVM dev %s: devno 0x%04X\n",
217 (unsigned int) dev));
218 probe_one(cache, lvm_device, dev, BLKID_PRI_LVM);
227 #define PROC_EVMS_VOLUMES "/proc/evms/volumes"
230 evms_probe_all(blkid_cache cache)
233 int ma, mi, sz, num = 0;
237 procpt = fopen(PROC_EVMS_VOLUMES, "r");
240 while (fgets(line, sizeof(line), procpt)) {
241 if (sscanf (line, " %d %d %d %*s %*s %[^\n ]",
242 &ma, &mi, &sz, device) != 4)
245 DBG(DEBUG_DEVNAME, printf("Checking partition %s (%d, %d)\n",
248 probe_one(cache, device, makedev(ma, mi), BLKID_PRI_EVMS);
256 * Read the device data for all available block devices in the system.
258 int blkid_probe_all(blkid_cache cache)
262 char ptname0[128], ptname1[128], *ptname = 0;
266 unsigned long long sz;
267 int lens[2] = { 0, 0 };
268 int which = 0, last = 0;
270 ptnames[0] = ptname0;
271 ptnames[1] = ptname1;
274 return -BLKID_ERR_PARAM;
276 if (cache->bic_flags & BLKID_BIC_FL_PROBED &&
277 time(0) - cache->bic_time < BLKID_PROBE_INTERVAL)
280 blkid_read_cache(cache);
281 evms_probe_all(cache);
283 lvm_probe_all(cache);
286 proc = fopen(PROC_PARTITIONS, "r");
288 return -BLKID_ERR_PROC;
290 while (fgets(line, sizeof(line), proc)) {
293 ptname = ptnames[which];
295 if (sscanf(line, " %d %d %llu %128[^\n ]",
296 &ma, &mi, &sz, ptname) != 4)
298 devs[which] = makedev(ma, mi);
300 DBG(DEBUG_DEVNAME, printf("read partition name %s\n", ptname));
302 /* Skip whole disk devs unless they have no partitions
303 * If we don't have a partition on this dev, also
304 * check previous dev to see if it didn't have a partn.
305 * heuristic: partition name ends in a digit.
307 * Skip extended partitions.
308 * heuristic: size is 1
310 * FIXME: skip /dev/{ida,cciss,rd} whole-disk devs
313 lens[which] = strlen(ptname);
314 if (isdigit(ptname[lens[which] - 1])) {
316 printf("partition dev %s, devno 0x%04X\n",
317 ptname, (unsigned int) devs[which]));
320 probe_one(cache, ptname, devs[which], 0);
323 } else if (lens[last] && strncmp(ptnames[last], ptname,
326 printf("whole dev %s, devno 0x%04X\n",
327 ptnames[last], (unsigned int) devs[last]));
328 probe_one(cache, ptnames[last], devs[last], 0);
333 /* Handle the last device if it wasn't partitioned */
335 probe_one(cache, ptname, devs[which], 0);
339 cache->bic_time = time(0);
340 cache->bic_flags |= BLKID_BIC_FL_PROBED;
341 blkid_flush_cache(cache);
346 int main(int argc, char **argv)
348 blkid_cache cache = NULL;
351 blkid_debug_mask = DEBUG_ALL;
353 fprintf(stderr, "Usage: %s\n"
354 "Probe all devices and exit\n", argv[0]);
357 if ((ret = blkid_get_cache(&cache, bb_dev_null)) != 0) {
358 fprintf(stderr, "%s: error creating cache (%d)\n",
362 if (blkid_probe_all(cache) < 0)
363 printf("%s: error probing devices\n", argv[0]);
365 blkid_put_cache(cache);