blob: b151354da9f27c61c4f166652b02eb8cf744a5be [file] [log] [blame]
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001/*
2 * devname.c - get a dev by its device inode name
3 *
4 * Copyright (C) Andries Brouwer
Theodore Ts'o0f3ac4802003-01-25 18:44:33 -05005 * Copyright (C) 1999, 2000, 2001, 2002, 2003 Theodore Ts'o
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05006 * Copyright (C) 2001 Andreas Dilger
7 *
8 * %Begin-Header%
9 * This file may be redistributed under the terms of the
10 * GNU Lesser General Public License.
11 * %End-Header%
12 */
13
Karel Zak4db2f592006-03-08 14:17:28 -050014#define _GNU_SOURCE 1
15
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050016#include <stdio.h>
17#include <string.h>
Theodore Ts'oe324b252006-03-12 23:25:15 -050018#include <limits.h>
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050019#if HAVE_UNISTD_H
20#include <unistd.h>
21#endif
22#include <stdlib.h>
23#include <string.h>
24#include <ctype.h>
25#if HAVE_SYS_TYPES_H
26#include <sys/types.h>
27#endif
Theodore Ts'oe6baebd2008-08-27 02:24:51 -040028#include <dirent.h>
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050029#if HAVE_SYS_STAT_H
30#include <sys/stat.h>
31#endif
32#if HAVE_ERRNO_H
33#include <errno.h>
34#endif
35#if HAVE_SYS_MKDEV_H
36#include <sys/mkdev.h>
37#endif
38#include <time.h>
39
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050040#include "blkidP.h"
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050041
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050042/*
43 * Find a dev struct in the cache by device name, if available.
Theodore Ts'o50b380b2003-02-12 23:51:21 -050044 *
45 * If there is no entry with the specified device name, and the create
46 * flag is set, then create an empty device entry.
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050047 */
Theodore Ts'o98999c32003-02-16 00:47:07 -050048blkid_dev blkid_get_dev(blkid_cache cache, const char *devname, int flags)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050049{
Theodore Ts'o50b380b2003-02-12 23:51:21 -050050 blkid_dev dev = NULL, tmp;
Theodore Ts'obf58e3d2008-06-28 22:09:43 -040051 struct list_head *p, *pnext;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050052
53 if (!cache || !devname)
54 return NULL;
55
56 list_for_each(p, &cache->bic_devs) {
Theodore Ts'o50b380b2003-02-12 23:51:21 -050057 tmp = list_entry(p, struct blkid_struct_dev, bid_devs);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050058 if (strcmp(tmp->bid_name, devname))
59 continue;
60
Theodore Ts'oefc6f622008-08-27 23:07:54 -040061 DBG(DEBUG_DEVNAME,
Theodore Ts'of0a22d02003-02-22 13:19:53 -050062 printf("found devname %s in cache\n", tmp->bid_name));
Theodore Ts'o50b380b2003-02-12 23:51:21 -050063 dev = tmp;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050064 break;
65 }
66
Theodore Ts'o50b380b2003-02-12 23:51:21 -050067 if (!dev && (flags & BLKID_DEV_CREATE)) {
Theodore Ts'ofe144e12008-10-18 16:47:46 -040068 if (access(devname, F_OK) < 0)
69 return NULL;
Theodore Ts'o50b380b2003-02-12 23:51:21 -050070 dev = blkid_new_dev();
71 if (!dev)
72 return NULL;
Theodore Ts'oe324b252006-03-12 23:25:15 -050073 dev->bid_time = INT_MIN;
Theodore Ts'o50b380b2003-02-12 23:51:21 -050074 dev->bid_name = blkid_strdup(devname);
75 dev->bid_cache = cache;
Theodore Ts'o50b380b2003-02-12 23:51:21 -050076 list_add_tail(&dev->bid_devs, &cache->bic_devs);
77 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
78 }
79
Theodore Ts'obf58e3d2008-06-28 22:09:43 -040080 if (flags & BLKID_DEV_VERIFY) {
Theodore Ts'o18d12962005-01-27 19:51:47 -050081 dev = blkid_verify(cache, dev);
Theodore Ts'obf58e3d2008-06-28 22:09:43 -040082 if (!dev || !(dev->bid_flags & BLKID_BID_FL_VERIFIED))
83 return dev;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040084 /*
Theodore Ts'obf58e3d2008-06-28 22:09:43 -040085 * If the device is verified, then search the blkid
86 * cache for any entries that match on the type, uuid,
87 * and label, and verify them; if a cache entry can
88 * not be verified, then it's stale and so we remove
89 * it.
90 */
91 list_for_each_safe(p, pnext, &cache->bic_devs) {
92 blkid_dev dev2;
93 if (!p)
94 break;
95 dev2 = list_entry(p, struct blkid_struct_dev, bid_devs);
96 if (dev2->bid_flags & BLKID_BID_FL_VERIFIED)
97 continue;
Theodore Ts'obb47c2a2008-07-13 19:10:10 -040098 if (!dev->bid_type || !dev2->bid_type ||
99 strcmp(dev->bid_type, dev2->bid_type))
Theodore Ts'obf58e3d2008-06-28 22:09:43 -0400100 continue;
101 if (dev->bid_label && dev2->bid_label &&
102 strcmp(dev->bid_label, dev2->bid_label))
103 continue;
104 if (dev->bid_uuid && dev2->bid_uuid &&
105 strcmp(dev->bid_uuid, dev2->bid_uuid))
106 continue;
107 if ((dev->bid_label && !dev2->bid_label) ||
108 (!dev->bid_label && dev2->bid_label) ||
109 (dev->bid_uuid && !dev2->bid_uuid) ||
110 (!dev->bid_uuid && dev2->bid_uuid))
111 continue;
112 dev2 = blkid_verify(cache, dev2);
113 if (dev2 && !(dev2->bid_flags & BLKID_BID_FL_VERIFIED))
114 blkid_free_dev(dev2);
115 }
116 }
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500117 return dev;
118}
119
Theodore Ts'of4e89bc2008-08-26 08:13:56 -0400120/* Directories where we will try to search for device names */
121static const char *dirlist[] = { "/dev", "/devfs", "/devices", NULL };
122
Theodore Ts'oe6baebd2008-08-27 02:24:51 -0400123static int is_dm_leaf(const char *devname)
124{
125 struct dirent *de, *d_de;
126 DIR *dir, *d_dir;
127 char path[256];
128 int ret = 1;
129
130 if ((dir = opendir("/sys/block")) == NULL)
131 return 0;
132 while ((de = readdir(dir)) != NULL) {
133 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..") ||
134 !strcmp(de->d_name, devname) ||
135 strncmp(de->d_name, "dm-", 3) ||
136 strlen(de->d_name) > sizeof(path)-32)
137 continue;
138 sprintf(path, "/sys/block/%s/slaves", de->d_name);
139 if ((d_dir = opendir(path)) == NULL)
140 continue;
141 while ((d_de = readdir(d_dir)) != NULL) {
142 if (!strcmp(d_de->d_name, devname)) {
143 ret = 0;
144 break;
145 }
146 }
147 closedir(d_dir);
148 if (!ret)
149 break;
150 }
151 closedir(dir);
152 return ret;
153}
154
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500155/*
Karel Zak46f3eec2009-04-27 15:00:58 +0200156 * Since 2.6.29 (patch 784aae735d9b0bba3f8b9faef4c8b30df3bf0128) kernel sysfs
157 * provides the real DM device names in /sys/block/<ptname>/dm/name
158 */
159static char *get_dm_name(const char *ptname)
160{
161 FILE *f;
162 size_t sz;
163 char path[256], name[256], *res = NULL;
164
165 snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptname);
166 if ((f = fopen(path, "r")) == NULL)
167 return NULL;
168
169 /* read "<name>\n" from sysfs */
170 if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) {
171 name[sz - 1] = '\0';
172 snprintf(path, sizeof(path), "/dev/mapper/%s", name);
173 res = blkid_strdup(path);
174 }
175 fclose(f);
176 return res;
177}
178
179/*
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500180 * Probe a single block device to add to the device cache.
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500181 */
Theodore Ts'oce72b862003-02-14 01:31:45 -0500182static void probe_one(blkid_cache cache, const char *ptname,
Theodore Ts'oed6acfa2005-05-07 17:06:27 -0400183 dev_t devno, int pri, int only_if_new)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500184{
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500185 blkid_dev dev = NULL;
Theodore Ts'o5b7adf02008-08-22 12:43:14 -0400186 struct list_head *p, *pnext;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500187 const char **dir;
188 char *devname = NULL;
189
190 /* See if we already have this device number in the cache. */
Theodore Ts'o5b7adf02008-08-22 12:43:14 -0400191 list_for_each_safe(p, pnext, &cache->bic_devs) {
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500192 blkid_dev tmp = list_entry(p, struct blkid_struct_dev,
193 bid_devs);
194 if (tmp->bid_devno == devno) {
Theodore Ts'o57926c82008-08-09 22:37:58 -0400195 if (only_if_new && !access(tmp->bid_name, F_OK))
Theodore Ts'oed6acfa2005-05-07 17:06:27 -0400196 return;
Theodore Ts'o18d12962005-01-27 19:51:47 -0500197 dev = blkid_verify(cache, tmp);
Theodore Ts'o57926c82008-08-09 22:37:58 -0400198 if (dev && (dev->bid_flags & BLKID_BID_FL_VERIFIED))
199 break;
200 dev = 0;
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500201 }
202 }
203 if (dev && dev->bid_devno == devno)
Theodore Ts'oce72b862003-02-14 01:31:45 -0500204 goto set_pri;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500205
Karel Zak4271e232009-04-27 15:00:57 +0200206 /* Try to translate private device-mapper dm-<N> names
207 * to standard /dev/mapper/<name>.
208 */
209 if (!strncmp(ptname, "dm-", 3) && isdigit(ptname[3])) {
Karel Zak46f3eec2009-04-27 15:00:58 +0200210 devname = get_dm_name(ptname);
211 if (!devname)
212 blkid__scan_dir("/dev/mapper", devno, 0, &devname);
Karel Zak4271e232009-04-27 15:00:57 +0200213 if (devname)
214 goto get_dev;
215 }
216
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500217 /*
218 * Take a quick look at /dev/ptname for the device number. We check
219 * all of the likely device directories. If we don't find it, or if
220 * the stat information doesn't check out, use blkid_devno_to_devname()
221 * to find it via an exhaustive search for the device major/minor.
222 */
Theodore Ts'of4e89bc2008-08-26 08:13:56 -0400223 for (dir = dirlist; *dir; dir++) {
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500224 struct stat st;
225 char device[256];
226
227 sprintf(device, "%s/%s", *dir, ptname);
Theodore Ts'o98999c32003-02-16 00:47:07 -0500228 if ((dev = blkid_get_dev(cache, device, BLKID_DEV_FIND)) &&
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500229 dev->bid_devno == devno)
Theodore Ts'oce72b862003-02-14 01:31:45 -0500230 goto set_pri;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500231
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400232 if (stat(device, &st) == 0 && S_ISBLK(st.st_mode) &&
Theodore Ts'o2e6a9fe2005-01-05 17:45:32 -0500233 st.st_rdev == devno) {
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500234 devname = blkid_strdup(device);
Karel Zak4271e232009-04-27 15:00:57 +0200235 goto get_dev;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500236 }
237 }
Theodore Ts'of4e89bc2008-08-26 08:13:56 -0400238 /* Do a short-cut scan of /dev/mapper first */
239 if (!devname)
Karel Zak46f3eec2009-04-27 15:00:58 +0200240 devname = get_dm_name(ptname);
241 if (!devname)
Theodore Ts'of4e89bc2008-08-26 08:13:56 -0400242 blkid__scan_dir("/dev/mapper", devno, 0, &devname);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500243 if (!devname) {
244 devname = blkid_devno_to_devname(devno);
245 if (!devname)
Theodore Ts'oce72b862003-02-14 01:31:45 -0500246 return;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500247 }
Karel Zak4271e232009-04-27 15:00:57 +0200248get_dev:
Theodore Ts'o98999c32003-02-16 00:47:07 -0500249 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500250 free(devname);
Theodore Ts'oce72b862003-02-14 01:31:45 -0500251set_pri:
Theodore Ts'of4e89bc2008-08-26 08:13:56 -0400252 if (dev) {
253 if (pri)
254 dev->bid_pri = pri;
Theodore Ts'oe6baebd2008-08-27 02:24:51 -0400255 else if (!strncmp(dev->bid_name, "/dev/mapper/", 11)) {
Theodore Ts'of4e89bc2008-08-26 08:13:56 -0400256 dev->bid_pri = BLKID_PRI_DM;
Theodore Ts'oe6baebd2008-08-27 02:24:51 -0400257 if (is_dm_leaf(ptname))
258 dev->bid_pri += 5;
259 } else if (!strncmp(ptname, "md", 2))
Theodore Ts'of4e89bc2008-08-26 08:13:56 -0400260 dev->bid_pri = BLKID_PRI_MD;
261 }
Theodore Ts'oce72b862003-02-14 01:31:45 -0500262 return;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500263}
264
265#define PROC_PARTITIONS "/proc/partitions"
266#define VG_DIR "/proc/lvm/VGs"
267
268/*
269 * This function initializes the UUID cache with devices from the LVM
270 * proc hierarchy. We currently depend on the names of the LVM
271 * hierarchy giving us the device structure in /dev. (XXX is this a
272 * safe thing to do?)
273 */
274#ifdef VG_DIR
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500275static dev_t lvm_get_devno(const char *lvm_device)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500276{
277 FILE *lvf;
278 char buf[1024];
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500279 int ma, mi;
280 dev_t ret = 0;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500281
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500282 DBG(DEBUG_DEVNAME, printf("opening %s\n", lvm_device));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500283 if ((lvf = fopen(lvm_device, "r")) == NULL) {
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500284 DBG(DEBUG_DEVNAME, printf("%s: (%d) %s\n", lvm_device, errno,
285 strerror(errno)));
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500286 return 0;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500287 }
288
289 while (fgets(buf, sizeof(buf), lvf)) {
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500290 if (sscanf(buf, "device: %d:%d", &ma, &mi) == 2) {
291 ret = makedev(ma, mi);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500292 break;
293 }
294 }
295 fclose(lvf);
296
297 return ret;
298}
299
Theodore Ts'oed6acfa2005-05-07 17:06:27 -0400300static void lvm_probe_all(blkid_cache cache, int only_if_new)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500301{
302 DIR *vg_list;
303 struct dirent *vg_iter;
304 int vg_len = strlen(VG_DIR);
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500305 dev_t dev;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500306
307 if ((vg_list = opendir(VG_DIR)) == NULL)
308 return;
309
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500310 DBG(DEBUG_DEVNAME, printf("probing LVM devices under %s\n", VG_DIR));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500311
312 while ((vg_iter = readdir(vg_list)) != NULL) {
313 DIR *lv_list;
314 char *vdirname;
315 char *vg_name;
316 struct dirent *lv_iter;
317
318 vg_name = vg_iter->d_name;
319 if (!strcmp(vg_name, ".") || !strcmp(vg_name, ".."))
320 continue;
321 vdirname = malloc(vg_len + strlen(vg_name) + 8);
322 if (!vdirname)
323 goto exit;
324 sprintf(vdirname, "%s/%s/LVs", VG_DIR, vg_name);
325
326 lv_list = opendir(vdirname);
327 free(vdirname);
328 if (lv_list == NULL)
329 continue;
330
331 while ((lv_iter = readdir(lv_list)) != NULL) {
332 char *lv_name, *lvm_device;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500333
334 lv_name = lv_iter->d_name;
335 if (!strcmp(lv_name, ".") || !strcmp(lv_name, ".."))
336 continue;
337
338 lvm_device = malloc(vg_len + strlen(vg_name) +
339 strlen(lv_name) + 8);
340 if (!lvm_device) {
341 closedir(lv_list);
342 goto exit;
343 }
344 sprintf(lvm_device, "%s/%s/LVs/%s", VG_DIR, vg_name,
345 lv_name);
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500346 dev = lvm_get_devno(lvm_device);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500347 sprintf(lvm_device, "%s/%s", vg_name, lv_name);
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500348 DBG(DEBUG_DEVNAME, printf("LVM dev %s: devno 0x%04X\n",
349 lvm_device,
350 (unsigned int) dev));
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400351 probe_one(cache, lvm_device, dev, BLKID_PRI_LVM,
Theodore Ts'oed6acfa2005-05-07 17:06:27 -0400352 only_if_new);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500353 free(lvm_device);
354 }
355 closedir(lv_list);
356 }
357exit:
358 closedir(vg_list);
359}
360#endif
361
Theodore Ts'o0f3ac4802003-01-25 18:44:33 -0500362#define PROC_EVMS_VOLUMES "/proc/evms/volumes"
363
364static int
Theodore Ts'oed6acfa2005-05-07 17:06:27 -0400365evms_probe_all(blkid_cache cache, int only_if_new)
Theodore Ts'o0f3ac4802003-01-25 18:44:33 -0500366{
367 char line[100];
368 int ma, mi, sz, num = 0;
369 FILE *procpt;
370 char device[110];
371
372 procpt = fopen(PROC_EVMS_VOLUMES, "r");
373 if (!procpt)
374 return 0;
375 while (fgets(line, sizeof(line), procpt)) {
376 if (sscanf (line, " %d %d %d %*s %*s %[^\n ]",
377 &ma, &mi, &sz, device) != 4)
378 continue;
379
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500380 DBG(DEBUG_DEVNAME, printf("Checking partition %s (%d, %d)\n",
381 device, ma, mi));
Theodore Ts'o0f3ac4802003-01-25 18:44:33 -0500382
Theodore Ts'oed6acfa2005-05-07 17:06:27 -0400383 probe_one(cache, device, makedev(ma, mi), BLKID_PRI_EVMS,
384 only_if_new);
Theodore Ts'o0f3ac4802003-01-25 18:44:33 -0500385 num++;
386 }
387 fclose(procpt);
388 return num;
389}
390
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500391/*
392 * Read the device data for all available block devices in the system.
393 */
Theodore Ts'oed6acfa2005-05-07 17:06:27 -0400394static int probe_all(blkid_cache cache, int only_if_new)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500395{
396 FILE *proc;
Theodore Ts'oce72b862003-02-14 01:31:45 -0500397 char line[1024];
398 char ptname0[128], ptname1[128], *ptname = 0;
Theodore Ts'oed78c022003-03-06 11:09:18 -0500399 char *ptnames[2];
Theodore Ts'oce72b862003-02-14 01:31:45 -0500400 dev_t devs[2];
401 int ma, mi;
402 unsigned long long sz;
403 int lens[2] = { 0, 0 };
404 int which = 0, last = 0;
Eric Sandeen4e60e062009-04-22 22:51:51 -0500405 struct list_head *p, *pnext;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500406
Theodore Ts'oed78c022003-03-06 11:09:18 -0500407 ptnames[0] = ptname0;
408 ptnames[1] = ptname1;
409
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500410 if (!cache)
411 return -BLKID_ERR_PARAM;
412
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500413 if (cache->bic_flags & BLKID_BIC_FL_PROBED &&
414 time(0) - cache->bic_time < BLKID_PROBE_INTERVAL)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500415 return 0;
416
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500417 blkid_read_cache(cache);
Theodore Ts'oed6acfa2005-05-07 17:06:27 -0400418 evms_probe_all(cache, only_if_new);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500419#ifdef VG_DIR
Theodore Ts'oed6acfa2005-05-07 17:06:27 -0400420 lvm_probe_all(cache, only_if_new);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500421#endif
422
423 proc = fopen(PROC_PARTITIONS, "r");
424 if (!proc)
425 return -BLKID_ERR_PROC;
426
Theodore Ts'oce72b862003-02-14 01:31:45 -0500427 while (fgets(line, sizeof(line), proc)) {
428 last = which;
429 which ^= 1;
430 ptname = ptnames[which];
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500431
Matthias Andreed3458822005-01-12 22:58:33 +0100432 if (sscanf(line, " %d %d %llu %128[^\n ]",
Theodore Ts'oce72b862003-02-14 01:31:45 -0500433 &ma, &mi, &sz, ptname) != 4)
434 continue;
435 devs[which] = makedev(ma, mi);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500436
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500437 DBG(DEBUG_DEVNAME, printf("read partition name %s\n", ptname));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500438
Eric Sandeen38361bb2007-11-29 12:57:35 -0600439 /* Skip whole disk devs unless they have no partitions.
440 * If base name of device has changed, also
Theodore Ts'oce72b862003-02-14 01:31:45 -0500441 * check previous dev to see if it didn't have a partn.
Eric Sandeen38361bb2007-11-29 12:57:35 -0600442 * heuristic: partition name ends in a digit, & partition
443 * names contain whole device name as substring.
Theodore Ts'oce72b862003-02-14 01:31:45 -0500444 *
445 * Skip extended partitions.
446 * heuristic: size is 1
447 *
448 * FIXME: skip /dev/{ida,cciss,rd} whole-disk devs
449 */
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500450
Theodore Ts'oce72b862003-02-14 01:31:45 -0500451 lens[which] = strlen(ptname);
Eric Sandeen38361bb2007-11-29 12:57:35 -0600452
453 /* ends in a digit, clearly a partition, so check */
Theodore Ts'oce72b862003-02-14 01:31:45 -0500454 if (isdigit(ptname[lens[which] - 1])) {
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500455 DBG(DEBUG_DEVNAME,
456 printf("partition dev %s, devno 0x%04X\n",
457 ptname, (unsigned int) devs[which]));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500458
Theodore Ts'oce72b862003-02-14 01:31:45 -0500459 if (sz > 1)
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400460 probe_one(cache, ptname, devs[which], 0,
Theodore Ts'oed6acfa2005-05-07 17:06:27 -0400461 only_if_new);
Eric Sandeen38361bb2007-11-29 12:57:35 -0600462 lens[which] = 0; /* mark as checked */
463 }
464
465 /*
Eric Sandeen4e60e062009-04-22 22:51:51 -0500466 * If last was a whole disk and we just found a partition
467 * on it, remove the whole-disk dev from the cache if
468 * it exists.
469 */
470 if (lens[last] && !strncmp(ptnames[last], ptname, lens[last])) {
471 list_for_each_safe(p, pnext, &cache->bic_devs) {
472 blkid_dev tmp;
473
474 /* find blkid dev for the whole-disk devno */
475 tmp = list_entry(p, struct blkid_struct_dev,
476 bid_devs);
477 if (tmp->bid_devno == devs[last]) {
478 DBG(DEBUG_DEVNAME,
479 printf("freeing %s\n",
480 tmp->bid_name));
481 blkid_free_dev(tmp);
482 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
483 break;
484 }
485 }
486 lens[last] = 0;
487 }
488 /*
Eric Sandeen38361bb2007-11-29 12:57:35 -0600489 * If last was not checked because it looked like a whole-disk
490 * dev, and the device's base name has changed,
491 * check last as well.
492 */
493 if (lens[last] && strncmp(ptnames[last], ptname, lens[last])) {
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500494 DBG(DEBUG_DEVNAME,
495 printf("whole dev %s, devno 0x%04X\n",
496 ptnames[last], (unsigned int) devs[last]));
Theodore Ts'oed6acfa2005-05-07 17:06:27 -0400497 probe_one(cache, ptnames[last], devs[last], 0,
498 only_if_new);
Theodore Ts'oce72b862003-02-14 01:31:45 -0500499 lens[last] = 0;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500500 }
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500501 }
Theodore Ts'oce72b862003-02-14 01:31:45 -0500502
503 /* Handle the last device if it wasn't partitioned */
504 if (lens[which])
Theodore Ts'oed6acfa2005-05-07 17:06:27 -0400505 probe_one(cache, ptname, devs[which], 0, only_if_new);
Theodore Ts'oce72b862003-02-14 01:31:45 -0500506
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500507 fclose(proc);
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500508 blkid_flush_cache(cache);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500509 return 0;
510}
511
Theodore Ts'oed6acfa2005-05-07 17:06:27 -0400512int blkid_probe_all(blkid_cache cache)
513{
514 int ret;
515
516 DBG(DEBUG_PROBE, printf("Begin blkid_probe_all()\n"));
517 ret = probe_all(cache, 0);
518 cache->bic_time = time(0);
519 cache->bic_flags |= BLKID_BIC_FL_PROBED;
520 DBG(DEBUG_PROBE, printf("End blkid_probe_all()\n"));
521 return ret;
522}
523
524int blkid_probe_all_new(blkid_cache cache)
525{
526 int ret;
527
528 DBG(DEBUG_PROBE, printf("Begin blkid_probe_all_new()\n"));
529 ret = probe_all(cache, 1);
530 DBG(DEBUG_PROBE, printf("End blkid_probe_all_new()\n"));
531 return ret;
532}
533
534
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500535#ifdef TEST_PROGRAM
536int main(int argc, char **argv)
537{
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500538 blkid_cache cache = NULL;
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500539 int ret;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500540
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500541 blkid_debug_mask = DEBUG_ALL;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500542 if (argc != 1) {
543 fprintf(stderr, "Usage: %s\n"
544 "Probe all devices and exit\n", argv[0]);
545 exit(1);
546 }
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500547 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
548 fprintf(stderr, "%s: error creating cache (%d)\n",
549 argv[0], ret);
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500550 exit(1);
551 }
552 if (blkid_probe_all(cache) < 0)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500553 printf("%s: error probing devices\n", argv[0]);
554
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500555 blkid_put_cache(cache);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500556 return (0);
557}
558#endif