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.
23 #include <sys/types.h>
32 #include <sys/mkdev.h>
39 * Find a dev struct in the cache by device name, if available.
41 * If there is no entry with the specified device name, and the create
42 * flag is set, then create an empty device entry.
44 blkid_dev blkid_get_dev(blkid_cache cache, const char *devname, int flags)
46 blkid_dev dev = NULL, tmp;
49 if (!cache || !devname)
52 list_for_each(p, &cache->bic_devs) {
53 tmp = list_entry(p, struct blkid_struct_dev, bid_devs);
54 if (strcmp(tmp->bid_name, devname))
58 printf("found devname %s in cache\n", tmp->bid_name));
63 if (!dev && (flags & BLKID_DEV_CREATE)) {
64 dev = blkid_new_dev();
67 dev->bid_name = blkid_strdup(devname);
68 dev->bid_cache = cache;
69 list_add_tail(&dev->bid_devs, &cache->bic_devs);
70 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
73 if (flags & BLKID_DEV_VERIFY)
74 dev = blkid_verify(cache, dev);
79 * Probe a single block device to add to the device cache.
81 static void probe_one(blkid_cache cache, const char *ptname,
89 /* See if we already have this device number in the cache. */
90 list_for_each(p, &cache->bic_devs) {
91 blkid_dev tmp = list_entry(p, struct blkid_struct_dev,
93 if (tmp->bid_devno == devno) {
94 dev = blkid_verify(cache, tmp);
98 if (dev && dev->bid_devno == devno)
102 * Take a quick look at /dev/ptname for the device number. We check
103 * all of the likely device directories. If we don't find it, or if
104 * the stat information doesn't check out, use blkid_devno_to_devname()
105 * to find it via an exhaustive search for the device major/minor.
107 for (dir = blkid_devdirs; *dir; dir++) {
111 sprintf(device, "%s/%s", *dir, ptname);
112 if ((dev = blkid_get_dev(cache, device, BLKID_DEV_FIND)) &&
113 dev->bid_devno == devno)
116 if (stat(device, &st) == 0 && S_ISBLK(st.st_mode) &&
117 st.st_rdev == devno) {
118 devname = blkid_strdup(device);
123 devname = blkid_devno_to_devname(devno);
127 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
131 if (!pri && !strncmp(ptname, "md", 2))
138 #define PROC_PARTITIONS "/proc/partitions"
139 #define VG_DIR "/proc/lvm/VGs"
142 * This function initializes the UUID cache with devices from the LVM
143 * proc hierarchy. We currently depend on the names of the LVM
144 * hierarchy giving us the device structure in /dev. (XXX is this a
149 static dev_t lvm_get_devno(const char *lvm_device)
156 DBG(DEBUG_DEVNAME, printf("opening %s\n", lvm_device));
157 if ((lvf = fopen(lvm_device, "r")) == NULL) {
158 DBG(DEBUG_DEVNAME, printf("%s: (%d) %s\n", lvm_device, errno,
163 while (fgets(buf, sizeof(buf), lvf)) {
164 if (sscanf(buf, "device: %d:%d", &ma, &mi) == 2) {
165 ret = makedev(ma, mi);
174 static void lvm_probe_all(blkid_cache cache)
177 struct dirent *vg_iter;
178 int vg_len = strlen(VG_DIR);
181 if ((vg_list = opendir(VG_DIR)) == NULL)
184 DBG(DEBUG_DEVNAME, printf("probing LVM devices under %s\n", VG_DIR));
186 while ((vg_iter = readdir(vg_list)) != NULL) {
190 struct dirent *lv_iter;
192 vg_name = vg_iter->d_name;
193 if (!strcmp(vg_name, ".") || !strcmp(vg_name, ".."))
195 vdirname = xmalloc(vg_len + strlen(vg_name) + 8);
196 sprintf(vdirname, "%s/%s/LVs", VG_DIR, vg_name);
198 lv_list = opendir(vdirname);
203 while ((lv_iter = readdir(lv_list)) != NULL) {
204 char *lv_name, *lvm_device;
206 lv_name = lv_iter->d_name;
207 if (!strcmp(lv_name, ".") || !strcmp(lv_name, ".."))
210 lvm_device = xmalloc(vg_len + strlen(vg_name) +
211 strlen(lv_name) + 8);
212 sprintf(lvm_device, "%s/%s/LVs/%s", VG_DIR, vg_name,
214 dev = lvm_get_devno(lvm_device);
215 sprintf(lvm_device, "%s/%s", vg_name, lv_name);
216 DBG(DEBUG_DEVNAME, printf("LVM dev %s: devno 0x%04X\n",
218 (unsigned int) dev));
219 probe_one(cache, lvm_device, dev, BLKID_PRI_LVM);
228 #define PROC_EVMS_VOLUMES "/proc/evms/volumes"
231 evms_probe_all(blkid_cache cache)
234 int ma, mi, sz, num = 0;
238 procpt = fopen(PROC_EVMS_VOLUMES, "r");
241 while (fgets(line, sizeof(line), procpt)) {
242 if (sscanf (line, " %d %d %d %*s %*s %[^\n ]",
243 &ma, &mi, &sz, device) != 4)
246 DBG(DEBUG_DEVNAME, printf("Checking partition %s (%d, %d)\n",
249 probe_one(cache, device, makedev(ma, mi), BLKID_PRI_EVMS);
257 * Read the device data for all available block devices in the system.
259 int blkid_probe_all(blkid_cache cache)
263 char ptname0[128], ptname1[128], *ptname = 0;
267 unsigned long long sz;
268 int lens[2] = { 0, 0 };
269 int which = 0, last = 0;
271 ptnames[0] = ptname0;
272 ptnames[1] = ptname1;
275 return -BLKID_ERR_PARAM;
277 if (cache->bic_flags & BLKID_BIC_FL_PROBED &&
278 time(0) - cache->bic_time < BLKID_PROBE_INTERVAL)
281 blkid_read_cache(cache);
282 evms_probe_all(cache);
284 lvm_probe_all(cache);
287 proc = fopen(PROC_PARTITIONS, "r");
289 return -BLKID_ERR_PROC;
291 while (fgets(line, sizeof(line), proc)) {
294 ptname = ptnames[which];
296 if (sscanf(line, " %d %d %llu %128[^\n ]",
297 &ma, &mi, &sz, ptname) != 4)
299 devs[which] = makedev(ma, mi);
301 DBG(DEBUG_DEVNAME, printf("read partition name %s\n", ptname));
303 /* Skip whole disk devs unless they have no partitions
304 * If we don't have a partition on this dev, also
305 * check previous dev to see if it didn't have a partn.
306 * heuristic: partition name ends in a digit.
308 * Skip extended partitions.
309 * heuristic: size is 1
311 * FIXME: skip /dev/{ida,cciss,rd} whole-disk devs
314 lens[which] = strlen(ptname);
315 if (isdigit(ptname[lens[which] - 1])) {
317 printf("partition dev %s, devno 0x%04X\n",
318 ptname, (unsigned int) devs[which]));
321 probe_one(cache, ptname, devs[which], 0);
324 } else if (lens[last] && strncmp(ptnames[last], ptname,
327 printf("whole dev %s, devno 0x%04X\n",
328 ptnames[last], (unsigned int) devs[last]));
329 probe_one(cache, ptnames[last], devs[last], 0);
334 /* Handle the last device if it wasn't partitioned */
336 probe_one(cache, ptname, devs[which], 0);
340 cache->bic_time = time(0);
341 cache->bic_flags |= BLKID_BIC_FL_PROBED;
342 blkid_flush_cache(cache);
347 int main(int argc, char **argv)
349 blkid_cache cache = NULL;
352 blkid_debug_mask = DEBUG_ALL;
354 fprintf(stderr, "Usage: %s\n"
355 "Probe all devices and exit\n", argv[0]);
358 if ((ret = blkid_get_cache(&cache, bb_dev_null)) != 0) {
359 fprintf(stderr, "%s: error creating cache (%d)\n",
363 if (blkid_probe_all(cache) < 0)
364 printf("%s: error probing devices\n", argv[0]);
366 blkid_put_cache(cache);