]>
Commit | Line | Data |
---|---|---|
a0948ffe KZ |
1 | /* |
2 | * devname.c - get a dev by its device inode name | |
3 | * | |
4 | * Copyright (C) Andries Brouwer | |
5 | * Copyright (C) 1999, 2000, 2001, 2002, 2003 Theodore Ts'o | |
6 | * 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 | ||
14 | #define _GNU_SOURCE 1 | |
15 | ||
16 | #include <stdio.h> | |
17 | #include <string.h> | |
18 | #include <limits.h> | |
fbc333fe | 19 | #ifdef HAVE_UNISTD_H |
a0948ffe KZ |
20 | #include <unistd.h> |
21 | #endif | |
22 | #include <stdlib.h> | |
a0948ffe | 23 | #include <ctype.h> |
49361dc4 | 24 | #include <fcntl.h> |
fbc333fe | 25 | #ifdef HAVE_SYS_TYPES_H |
a0948ffe KZ |
26 | #include <sys/types.h> |
27 | #endif | |
fd5a4d7f | 28 | #include <dirent.h> |
fbc333fe | 29 | #ifdef HAVE_SYS_STAT_H |
a0948ffe KZ |
30 | #include <sys/stat.h> |
31 | #endif | |
fbc333fe | 32 | #ifdef HAVE_ERRNO_H |
a0948ffe KZ |
33 | #include <errno.h> |
34 | #endif | |
a0948ffe KZ |
35 | #include <time.h> |
36 | ||
37 | #include "blkidP.h" | |
49361dc4 | 38 | |
a992137b | 39 | #include "canonicalize.h" /* $(top_srcdir)/include */ |
49361dc4 | 40 | #include "pathnames.h" |
d8a84552 | 41 | #include "sysfs.h" |
a0948ffe | 42 | |
a0948ffe KZ |
43 | /* |
44 | * Find a dev struct in the cache by device name, if available. | |
45 | * | |
46 | * If there is no entry with the specified device name, and the create | |
47 | * flag is set, then create an empty device entry. | |
48 | */ | |
49 | blkid_dev blkid_get_dev(blkid_cache cache, const char *devname, int flags) | |
50 | { | |
51 | blkid_dev dev = NULL, tmp; | |
52 | struct list_head *p, *pnext; | |
924c93d9 | 53 | char *cn = NULL; |
a0948ffe KZ |
54 | |
55 | if (!cache || !devname) | |
56 | return NULL; | |
57 | ||
924c93d9 | 58 | /* search by name */ |
a0948ffe KZ |
59 | list_for_each(p, &cache->bic_devs) { |
60 | tmp = list_entry(p, struct blkid_struct_dev, bid_devs); | |
61 | if (strcmp(tmp->bid_name, devname)) | |
62 | continue; | |
a0948ffe KZ |
63 | dev = tmp; |
64 | break; | |
65 | } | |
66 | ||
924c93d9 KZ |
67 | /* try canonicalize the name */ |
68 | if (!dev && (cn = canonicalize_path(devname))) { | |
69 | if (strcmp(cn, devname) != 0) { | |
9e930041 | 70 | DBG(DEVNAME, ul_debug("search canonical %s", cn)); |
924c93d9 KZ |
71 | list_for_each(p, &cache->bic_devs) { |
72 | tmp = list_entry(p, struct blkid_struct_dev, bid_devs); | |
73 | if (strcmp(tmp->bid_name, cn)) | |
74 | continue; | |
75 | dev = tmp; | |
76 | ||
77 | /* update name returned by blkid_dev_devname() */ | |
78 | free(dev->bid_xname); | |
79 | dev->bid_xname = strdup(devname); | |
80 | break; | |
81 | } | |
82 | } else { | |
83 | free(cn); | |
84 | cn = NULL; | |
85 | } | |
86 | } | |
87 | ||
a0948ffe | 88 | if (!dev && (flags & BLKID_DEV_CREATE)) { |
1de91705 | 89 | if (access(devname, F_OK) < 0) |
924c93d9 | 90 | goto done; |
a0948ffe KZ |
91 | dev = blkid_new_dev(); |
92 | if (!dev) | |
924c93d9 | 93 | goto done; |
a0948ffe | 94 | dev->bid_time = INT_MIN; |
924c93d9 KZ |
95 | if (cn) { |
96 | dev->bid_name = cn; | |
97 | dev->bid_xname = strdup(devname); | |
98 | cn = NULL; /* see free() below */ | |
99 | } else | |
100 | dev->bid_name = strdup(devname); | |
101 | ||
a0948ffe KZ |
102 | dev->bid_cache = cache; |
103 | list_add_tail(&dev->bid_devs, &cache->bic_devs); | |
104 | cache->bic_flags |= BLKID_BIC_FL_CHANGED; | |
105 | } | |
106 | ||
107 | if (flags & BLKID_DEV_VERIFY) { | |
108 | dev = blkid_verify(cache, dev); | |
109 | if (!dev || !(dev->bid_flags & BLKID_BID_FL_VERIFIED)) | |
924c93d9 | 110 | goto done; |
a0948ffe KZ |
111 | /* |
112 | * If the device is verified, then search the blkid | |
113 | * cache for any entries that match on the type, uuid, | |
114 | * and label, and verify them; if a cache entry can | |
115 | * not be verified, then it's stale and so we remove | |
116 | * it. | |
117 | */ | |
118 | list_for_each_safe(p, pnext, &cache->bic_devs) { | |
b29a6412 | 119 | blkid_dev dev2 = list_entry(p, struct blkid_struct_dev, bid_devs); |
a0948ffe KZ |
120 | if (dev2->bid_flags & BLKID_BID_FL_VERIFIED) |
121 | continue; | |
122 | if (!dev->bid_type || !dev2->bid_type || | |
123 | strcmp(dev->bid_type, dev2->bid_type)) | |
124 | continue; | |
125 | if (dev->bid_label && dev2->bid_label && | |
126 | strcmp(dev->bid_label, dev2->bid_label)) | |
127 | continue; | |
128 | if (dev->bid_uuid && dev2->bid_uuid && | |
129 | strcmp(dev->bid_uuid, dev2->bid_uuid)) | |
130 | continue; | |
131 | if ((dev->bid_label && !dev2->bid_label) || | |
132 | (!dev->bid_label && dev2->bid_label) || | |
133 | (dev->bid_uuid && !dev2->bid_uuid) || | |
134 | (!dev->bid_uuid && dev2->bid_uuid)) | |
135 | continue; | |
136 | dev2 = blkid_verify(cache, dev2); | |
137 | if (dev2 && !(dev2->bid_flags & BLKID_BID_FL_VERIFIED)) | |
138 | blkid_free_dev(dev2); | |
139 | } | |
140 | } | |
924c93d9 KZ |
141 | done: |
142 | if (dev) | |
143 | DBG(DEVNAME, ul_debug("%s requested, found %s in cache", devname, dev->bid_name)); | |
144 | free(cn); | |
a0948ffe KZ |
145 | return dev; |
146 | } | |
147 | ||
241b6cf3 TT |
148 | /* Directories where we will try to search for device names */ |
149 | static const char *dirlist[] = { "/dev", "/devfs", "/devices", NULL }; | |
150 | ||
fd5a4d7f TT |
151 | static int is_dm_leaf(const char *devname) |
152 | { | |
153 | struct dirent *de, *d_de; | |
154 | DIR *dir, *d_dir; | |
f64ddc95 | 155 | char path[NAME_MAX + 18 + 1]; |
fd5a4d7f TT |
156 | int ret = 1; |
157 | ||
158 | if ((dir = opendir("/sys/block")) == NULL) | |
159 | return 0; | |
160 | while ((de = readdir(dir)) != NULL) { | |
161 | if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..") || | |
162 | !strcmp(de->d_name, devname) || | |
163 | strncmp(de->d_name, "dm-", 3) || | |
164 | strlen(de->d_name) > sizeof(path)-32) | |
165 | continue; | |
166 | sprintf(path, "/sys/block/%s/slaves", de->d_name); | |
167 | if ((d_dir = opendir(path)) == NULL) | |
168 | continue; | |
169 | while ((d_de = readdir(d_dir)) != NULL) { | |
170 | if (!strcmp(d_de->d_name, devname)) { | |
171 | ret = 0; | |
172 | break; | |
173 | } | |
174 | } | |
175 | closedir(d_dir); | |
176 | if (!ret) | |
177 | break; | |
178 | } | |
179 | closedir(dir); | |
180 | return ret; | |
181 | } | |
182 | ||
a0948ffe KZ |
183 | /* |
184 | * Probe a single block device to add to the device cache. | |
185 | */ | |
186 | static void probe_one(blkid_cache cache, const char *ptname, | |
49361dc4 | 187 | dev_t devno, int pri, int only_if_new, int removable) |
a0948ffe KZ |
188 | { |
189 | blkid_dev dev = NULL; | |
190 | struct list_head *p, *pnext; | |
191 | const char **dir; | |
192 | char *devname = NULL; | |
193 | ||
194 | /* See if we already have this device number in the cache. */ | |
195 | list_for_each_safe(p, pnext, &cache->bic_devs) { | |
196 | blkid_dev tmp = list_entry(p, struct blkid_struct_dev, | |
197 | bid_devs); | |
a0948ffe KZ |
198 | if (tmp->bid_devno == devno) { |
199 | if (only_if_new && !access(tmp->bid_name, F_OK)) | |
200 | return; | |
201 | dev = blkid_verify(cache, tmp); | |
202 | if (dev && (dev->bid_flags & BLKID_BID_FL_VERIFIED)) | |
203 | break; | |
87918040 | 204 | dev = NULL; |
a0948ffe KZ |
205 | } |
206 | } | |
207 | if (dev && dev->bid_devno == devno) | |
208 | goto set_pri; | |
209 | ||
0e1ae8ab KZ |
210 | /* Try to translate private device-mapper dm-<N> names |
211 | * to standard /dev/mapper/<name>. | |
212 | */ | |
213 | if (!strncmp(ptname, "dm-", 3) && isdigit(ptname[3])) { | |
a992137b | 214 | devname = canonicalize_dm_name(ptname); |
6f52b6e9 | 215 | if (!devname) |
87918040 | 216 | blkid__scan_dir("/dev/mapper", devno, NULL, &devname); |
0e1ae8ab KZ |
217 | if (devname) |
218 | goto get_dev; | |
219 | } | |
220 | ||
a0948ffe KZ |
221 | /* |
222 | * Take a quick look at /dev/ptname for the device number. We check | |
223 | * all of the likely device directories. If we don't find it, or if | |
224 | * the stat information doesn't check out, use blkid_devno_to_devname() | |
225 | * to find it via an exhaustive search for the device major/minor. | |
226 | */ | |
241b6cf3 | 227 | for (dir = dirlist; *dir; dir++) { |
a0948ffe KZ |
228 | struct stat st; |
229 | char device[256]; | |
230 | ||
e3436956 | 231 | snprintf(device, sizeof(device), "%s/%s", *dir, ptname); |
a0948ffe KZ |
232 | if ((dev = blkid_get_dev(cache, device, BLKID_DEV_FIND)) && |
233 | dev->bid_devno == devno) | |
234 | goto set_pri; | |
235 | ||
c1ba7962 CC |
236 | if (stat(device, &st) == 0 && |
237 | (S_ISBLK(st.st_mode) || | |
238 | (S_ISCHR(st.st_mode) && !strncmp(ptname, "ubi", 3))) && | |
a0948ffe | 239 | st.st_rdev == devno) { |
e0a9b8cf | 240 | devname = strdup(device); |
0e1ae8ab | 241 | goto get_dev; |
a0948ffe KZ |
242 | } |
243 | } | |
241b6cf3 TT |
244 | /* Do a short-cut scan of /dev/mapper first */ |
245 | if (!devname) | |
87918040 | 246 | blkid__scan_dir("/dev/mapper", devno, NULL, &devname); |
a0948ffe KZ |
247 | if (!devname) { |
248 | devname = blkid_devno_to_devname(devno); | |
249 | if (!devname) | |
250 | return; | |
251 | } | |
0e1ae8ab KZ |
252 | |
253 | get_dev: | |
a0948ffe KZ |
254 | dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL); |
255 | free(devname); | |
256 | ||
257 | set_pri: | |
241b6cf3 TT |
258 | if (dev) { |
259 | if (pri) | |
260 | dev->bid_pri = pri; | |
fd5a4d7f | 261 | else if (!strncmp(dev->bid_name, "/dev/mapper/", 11)) { |
241b6cf3 | 262 | dev->bid_pri = BLKID_PRI_DM; |
fd5a4d7f TT |
263 | if (is_dm_leaf(ptname)) |
264 | dev->bid_pri += 5; | |
265 | } else if (!strncmp(ptname, "md", 2)) | |
241b6cf3 | 266 | dev->bid_pri = BLKID_PRI_MD; |
49361dc4 KZ |
267 | if (removable) |
268 | dev->bid_flags |= BLKID_BID_FL_REMOVABLE; | |
241b6cf3 | 269 | } |
a0948ffe KZ |
270 | return; |
271 | } | |
272 | ||
a0948ffe KZ |
273 | #define PROC_PARTITIONS "/proc/partitions" |
274 | #define VG_DIR "/proc/lvm/VGs" | |
275 | ||
276 | /* | |
277 | * This function initializes the UUID cache with devices from the LVM | |
278 | * proc hierarchy. We currently depend on the names of the LVM | |
279 | * hierarchy giving us the device structure in /dev. (XXX is this a | |
280 | * safe thing to do?) | |
281 | */ | |
282 | #ifdef VG_DIR | |
a0948ffe KZ |
283 | static dev_t lvm_get_devno(const char *lvm_device) |
284 | { | |
285 | FILE *lvf; | |
286 | char buf[1024]; | |
287 | int ma, mi; | |
288 | dev_t ret = 0; | |
289 | ||
c62a6311 | 290 | DBG(DEVNAME, ul_debug("opening %s", lvm_device)); |
4000fc12 | 291 | if ((lvf = fopen(lvm_device, "r" UL_CLOEXECSTR)) == NULL) { |
c62a6311 | 292 | DBG(DEVNAME, ul_debug("%s: (%d) %m", lvm_device, errno)); |
a0948ffe KZ |
293 | return 0; |
294 | } | |
295 | ||
296 | while (fgets(buf, sizeof(buf), lvf)) { | |
297 | if (sscanf(buf, "device: %d:%d", &ma, &mi) == 2) { | |
298 | ret = makedev(ma, mi); | |
299 | break; | |
300 | } | |
301 | } | |
302 | fclose(lvf); | |
303 | ||
304 | return ret; | |
305 | } | |
306 | ||
307 | static void lvm_probe_all(blkid_cache cache, int only_if_new) | |
308 | { | |
309 | DIR *vg_list; | |
310 | struct dirent *vg_iter; | |
311 | int vg_len = strlen(VG_DIR); | |
312 | dev_t dev; | |
313 | ||
314 | if ((vg_list = opendir(VG_DIR)) == NULL) | |
315 | return; | |
316 | ||
c62a6311 | 317 | DBG(DEVNAME, ul_debug("probing LVM devices under %s", VG_DIR)); |
a0948ffe KZ |
318 | |
319 | while ((vg_iter = readdir(vg_list)) != NULL) { | |
320 | DIR *lv_list; | |
321 | char *vdirname; | |
322 | char *vg_name; | |
323 | struct dirent *lv_iter; | |
324 | ||
325 | vg_name = vg_iter->d_name; | |
326 | if (!strcmp(vg_name, ".") || !strcmp(vg_name, "..")) | |
327 | continue; | |
328 | vdirname = malloc(vg_len + strlen(vg_name) + 8); | |
329 | if (!vdirname) | |
330 | goto exit; | |
331 | sprintf(vdirname, "%s/%s/LVs", VG_DIR, vg_name); | |
332 | ||
333 | lv_list = opendir(vdirname); | |
334 | free(vdirname); | |
335 | if (lv_list == NULL) | |
336 | continue; | |
337 | ||
338 | while ((lv_iter = readdir(lv_list)) != NULL) { | |
339 | char *lv_name, *lvm_device; | |
340 | ||
341 | lv_name = lv_iter->d_name; | |
342 | if (!strcmp(lv_name, ".") || !strcmp(lv_name, "..")) | |
343 | continue; | |
344 | ||
345 | lvm_device = malloc(vg_len + strlen(vg_name) + | |
346 | strlen(lv_name) + 8); | |
347 | if (!lvm_device) { | |
348 | closedir(lv_list); | |
349 | goto exit; | |
350 | } | |
351 | sprintf(lvm_device, "%s/%s/LVs/%s", VG_DIR, vg_name, | |
352 | lv_name); | |
353 | dev = lvm_get_devno(lvm_device); | |
354 | sprintf(lvm_device, "%s/%s", vg_name, lv_name); | |
c62a6311 | 355 | DBG(DEVNAME, ul_debug("LVM dev %s: devno 0x%04X", |
a0948ffe KZ |
356 | lvm_device, |
357 | (unsigned int) dev)); | |
358 | probe_one(cache, lvm_device, dev, BLKID_PRI_LVM, | |
49361dc4 | 359 | only_if_new, 0); |
a0948ffe KZ |
360 | free(lvm_device); |
361 | } | |
362 | closedir(lv_list); | |
363 | } | |
364 | exit: | |
365 | closedir(vg_list); | |
366 | } | |
367 | #endif | |
368 | ||
369 | #define PROC_EVMS_VOLUMES "/proc/evms/volumes" | |
370 | ||
371 | static int | |
372 | evms_probe_all(blkid_cache cache, int only_if_new) | |
373 | { | |
374 | char line[100]; | |
375 | int ma, mi, sz, num = 0; | |
376 | FILE *procpt; | |
377 | char device[110]; | |
378 | ||
4000fc12 | 379 | procpt = fopen(PROC_EVMS_VOLUMES, "r" UL_CLOEXECSTR); |
a0948ffe KZ |
380 | if (!procpt) |
381 | return 0; | |
382 | while (fgets(line, sizeof(line), procpt)) { | |
383 | if (sscanf (line, " %d %d %d %*s %*s %[^\n ]", | |
384 | &ma, &mi, &sz, device) != 4) | |
385 | continue; | |
386 | ||
c62a6311 | 387 | DBG(DEVNAME, ul_debug("Checking partition %s (%d, %d)", |
a0948ffe KZ |
388 | device, ma, mi)); |
389 | ||
390 | probe_one(cache, device, makedev(ma, mi), BLKID_PRI_EVMS, | |
49361dc4 | 391 | only_if_new, 0); |
a0948ffe KZ |
392 | num++; |
393 | } | |
394 | fclose(procpt); | |
395 | return num; | |
396 | } | |
397 | ||
c1ba7962 CC |
398 | static void |
399 | ubi_probe_all(blkid_cache cache, int only_if_new) | |
400 | { | |
401 | const char **dirname; | |
402 | ||
403 | for (dirname = dirlist; *dirname; dirname++) { | |
c1ba7962 CC |
404 | DIR *dir; |
405 | struct dirent *iter; | |
406 | ||
c62a6311 | 407 | DBG(DEVNAME, ul_debug("probing UBI volumes under %s", |
3acc206d SK |
408 | *dirname)); |
409 | ||
c1ba7962 CC |
410 | dir = opendir(*dirname); |
411 | if (dir == NULL) | |
412 | continue ; | |
413 | ||
414 | while ((iter = readdir(dir)) != NULL) { | |
f38fd19d | 415 | char *name; |
c1ba7962 CC |
416 | struct stat st; |
417 | dev_t dev; | |
418 | ||
419 | name = iter->d_name; | |
f38fd19d KZ |
420 | #ifdef _DIRENT_HAVE_D_TYPE |
421 | if (iter->d_type != DT_UNKNOWN && | |
422 | iter->d_type != DT_CHR && iter->d_type != DT_LNK) | |
423 | continue; | |
424 | #endif | |
c1ba7962 CC |
425 | if (!strcmp(name, ".") || !strcmp(name, "..") || |
426 | !strstr(name, "ubi")) | |
427 | continue; | |
428 | if (!strcmp(name, "ubi_ctrl")) | |
429 | continue; | |
2208b3cc | 430 | if (fstatat(dirfd(dir), name, &st, 0)) |
f38fd19d KZ |
431 | continue; |
432 | ||
c1ba7962 | 433 | dev = st.st_rdev; |
f38fd19d KZ |
434 | |
435 | if (!S_ISCHR(st.st_mode) || !minor(dev)) | |
436 | continue; | |
c62a6311 | 437 | DBG(DEVNAME, ul_debug("UBI vol %s/%s: devno 0x%04X", |
f38fd19d | 438 | *dirname, name, (int) dev)); |
49361dc4 | 439 | probe_one(cache, name, dev, BLKID_PRI_UBI, only_if_new, 0); |
c1ba7962 CC |
440 | } |
441 | closedir(dir); | |
442 | } | |
443 | } | |
444 | ||
a0948ffe KZ |
445 | /* |
446 | * Read the device data for all available block devices in the system. | |
447 | */ | |
448 | static int probe_all(blkid_cache cache, int only_if_new) | |
449 | { | |
450 | FILE *proc; | |
451 | char line[1024]; | |
87918040 | 452 | char ptname0[128 + 1], ptname1[128 + 1], *ptname = NULL; |
a0948ffe | 453 | char *ptnames[2]; |
709beed9 | 454 | dev_t devs[2] = { 0, 0 }; |
969e8cbe | 455 | int iswhole[2] = { 0, 0 }; |
a0948ffe KZ |
456 | int ma, mi; |
457 | unsigned long long sz; | |
458 | int lens[2] = { 0, 0 }; | |
459 | int which = 0, last = 0; | |
0295be03 | 460 | struct list_head *p, *pnext; |
a0948ffe KZ |
461 | |
462 | ptnames[0] = ptname0; | |
463 | ptnames[1] = ptname1; | |
464 | ||
465 | if (!cache) | |
466 | return -BLKID_ERR_PARAM; | |
467 | ||
468 | if (cache->bic_flags & BLKID_BIC_FL_PROBED && | |
87918040 | 469 | time(NULL) - cache->bic_time < BLKID_PROBE_INTERVAL) |
a0948ffe KZ |
470 | return 0; |
471 | ||
472 | blkid_read_cache(cache); | |
a0948ffe KZ |
473 | evms_probe_all(cache, only_if_new); |
474 | #ifdef VG_DIR | |
475 | lvm_probe_all(cache, only_if_new); | |
476 | #endif | |
c1ba7962 | 477 | ubi_probe_all(cache, only_if_new); |
a0948ffe | 478 | |
4000fc12 | 479 | proc = fopen(PROC_PARTITIONS, "r" UL_CLOEXECSTR); |
a0948ffe KZ |
480 | if (!proc) |
481 | return -BLKID_ERR_PROC; | |
482 | ||
483 | while (fgets(line, sizeof(line), proc)) { | |
484 | last = which; | |
485 | which ^= 1; | |
486 | ptname = ptnames[which]; | |
487 | ||
488 | if (sscanf(line, " %d %d %llu %128[^\n ]", | |
489 | &ma, &mi, &sz, ptname) != 4) | |
490 | continue; | |
491 | devs[which] = makedev(ma, mi); | |
492 | ||
969e8cbe | 493 | DBG(DEVNAME, ul_debug("read device name %s", ptname)); |
a0948ffe KZ |
494 | |
495 | /* Skip whole disk devs unless they have no partitions. | |
496 | * If base name of device has changed, also | |
497 | * check previous dev to see if it didn't have a partn. | |
498 | * heuristic: partition name ends in a digit, & partition | |
499 | * names contain whole device name as substring. | |
500 | * | |
501 | * Skip extended partitions. | |
502 | * heuristic: size is 1 | |
a0948ffe KZ |
503 | */ |
504 | ||
505 | lens[which] = strlen(ptname); | |
969e8cbe | 506 | iswhole[which] = sysfs_devno_is_wholedisk(devs[which]); |
a0948ffe | 507 | |
969e8cbe KZ |
508 | /* probably partition, so check */ |
509 | if (!iswhole[which]) { | |
510 | DBG(DEVNAME, ul_debug(" partition dev %s, devno 0x%04X", | |
a0948ffe KZ |
511 | ptname, (unsigned int) devs[which])); |
512 | ||
513 | if (sz > 1) | |
514 | probe_one(cache, ptname, devs[which], 0, | |
49361dc4 | 515 | only_if_new, 0); |
a0948ffe KZ |
516 | lens[which] = 0; /* mark as checked */ |
517 | } | |
518 | ||
0295be03 ES |
519 | /* |
520 | * If last was a whole disk and we just found a partition | |
521 | * on it, remove the whole-disk dev from the cache if | |
522 | * it exists. | |
523 | */ | |
969e8cbe KZ |
524 | if (lens[last] && iswhole[last] |
525 | && !strncmp(ptnames[last], ptname, lens[last])) { | |
526 | ||
0295be03 ES |
527 | list_for_each_safe(p, pnext, &cache->bic_devs) { |
528 | blkid_dev tmp; | |
529 | ||
530 | /* find blkid dev for the whole-disk devno */ | |
531 | tmp = list_entry(p, struct blkid_struct_dev, | |
532 | bid_devs); | |
533 | if (tmp->bid_devno == devs[last]) { | |
969e8cbe | 534 | DBG(DEVNAME, ul_debug(" freeing %s", |
0295be03 ES |
535 | tmp->bid_name)); |
536 | blkid_free_dev(tmp); | |
537 | cache->bic_flags |= BLKID_BIC_FL_CHANGED; | |
538 | break; | |
539 | } | |
540 | } | |
969e8cbe | 541 | lens[last] = 0; /* mark as checked */ |
0295be03 | 542 | } |
a0948ffe KZ |
543 | /* |
544 | * If last was not checked because it looked like a whole-disk | |
545 | * dev, and the device's base name has changed, | |
546 | * check last as well. | |
547 | */ | |
548 | if (lens[last] && strncmp(ptnames[last], ptname, lens[last])) { | |
969e8cbe | 549 | DBG(DEVNAME, ul_debug(" whole dev %s, devno 0x%04X", |
a0948ffe KZ |
550 | ptnames[last], (unsigned int) devs[last])); |
551 | probe_one(cache, ptnames[last], devs[last], 0, | |
49361dc4 | 552 | only_if_new, 0); |
969e8cbe KZ |
553 | |
554 | lens[last] = 0; /* mark as checked */ | |
a0948ffe KZ |
555 | } |
556 | } | |
557 | ||
558 | /* Handle the last device if it wasn't partitioned */ | |
559 | if (lens[which]) | |
49361dc4 | 560 | probe_one(cache, ptname, devs[which], 0, only_if_new, 0); |
a0948ffe KZ |
561 | |
562 | fclose(proc); | |
563 | blkid_flush_cache(cache); | |
564 | return 0; | |
565 | } | |
566 | ||
49361dc4 KZ |
567 | /* Don't use it by default -- it's pretty slow (because cdroms, floppy, ...) |
568 | */ | |
569 | static int probe_all_removable(blkid_cache cache) | |
570 | { | |
708a58b6 | 571 | struct path_cxt *pc; |
49361dc4 KZ |
572 | DIR *dir; |
573 | struct dirent *d; | |
49361dc4 KZ |
574 | |
575 | if (!cache) | |
576 | return -BLKID_ERR_PARAM; | |
577 | ||
578 | dir = opendir(_PATH_SYS_BLOCK); | |
579 | if (!dir) | |
580 | return -BLKID_ERR_PROC; | |
581 | ||
708a58b6 KZ |
582 | pc = ul_new_path(NULL); |
583 | ||
49361dc4 | 584 | while((d = readdir(dir))) { |
90e9fcda | 585 | int removable = 0; |
d8a84552 | 586 | dev_t devno; |
49361dc4 KZ |
587 | |
588 | #ifdef _DIRENT_HAVE_D_TYPE | |
589 | if (d->d_type != DT_UNKNOWN && d->d_type != DT_LNK) | |
590 | continue; | |
591 | #endif | |
592 | if (d->d_name[0] == '.' && | |
593 | ((d->d_name[1] == 0) || | |
594 | ((d->d_name[1] == '.') && (d->d_name[2] == 0)))) | |
595 | continue; | |
596 | ||
708a58b6 | 597 | devno = sysfs_devname_to_devno(d->d_name); |
d8a84552 | 598 | if (!devno) |
49361dc4 KZ |
599 | continue; |
600 | ||
708a58b6 KZ |
601 | if (sysfs_blkdev_init_path(pc, devno, NULL) == 0 |
602 | && ul_path_read_s32(pc, &removable, "removable") != 0) | |
eb542dff | 603 | removable = 0; |
49361dc4 | 604 | |
d8a84552 KZ |
605 | if (removable) |
606 | probe_one(cache, d->d_name, devno, 0, 0, 1); | |
49361dc4 KZ |
607 | } |
608 | ||
708a58b6 | 609 | ul_unref_path(pc); |
49361dc4 KZ |
610 | closedir(dir); |
611 | return 0; | |
612 | } | |
613 | ||
614 | ||
488e52be KZ |
615 | /** |
616 | * blkid_probe_all: | |
617 | * @cache: cache handler | |
618 | * | |
619 | * Probes all block devices. | |
620 | * | |
621 | * Returns: 0 on success, or number less than zero in case of error. | |
622 | */ | |
a0948ffe KZ |
623 | int blkid_probe_all(blkid_cache cache) |
624 | { | |
625 | int ret; | |
626 | ||
c62a6311 | 627 | DBG(PROBE, ul_debug("Begin blkid_probe_all()")); |
a0948ffe | 628 | ret = probe_all(cache, 0); |
e3436956 | 629 | if (ret == 0) { |
87918040 | 630 | cache->bic_time = time(NULL); |
e3436956 KZ |
631 | cache->bic_flags |= BLKID_BIC_FL_PROBED; |
632 | } | |
c62a6311 | 633 | DBG(PROBE, ul_debug("End blkid_probe_all() [rc=%d]", ret)); |
a0948ffe KZ |
634 | return ret; |
635 | } | |
636 | ||
488e52be KZ |
637 | /** |
638 | * blkid_probe_all_new: | |
639 | * @cache: cache handler | |
640 | * | |
641 | * Probes all new block devices. | |
642 | * | |
643 | * Returns: 0 on success, or number less than zero in case of error. | |
644 | */ | |
a0948ffe KZ |
645 | int blkid_probe_all_new(blkid_cache cache) |
646 | { | |
647 | int ret; | |
648 | ||
c62a6311 | 649 | DBG(PROBE, ul_debug("Begin blkid_probe_all_new()")); |
a0948ffe | 650 | ret = probe_all(cache, 1); |
c62a6311 | 651 | DBG(PROBE, ul_debug("End blkid_probe_all_new() [rc=%d]", ret)); |
a0948ffe KZ |
652 | return ret; |
653 | } | |
654 | ||
49361dc4 KZ |
655 | /** |
656 | * blkid_probe_all_removable: | |
657 | * @cache: cache handler | |
658 | * | |
659 | * The libblkid probing is based on devices from /proc/partitions by default. | |
660 | * This file usually does not contain removable devices (e.g. CDROMs) and this kind | |
661 | * of devices are invisible for libblkid. | |
662 | * | |
663 | * This function adds removable block devices to @cache (probing is based on | |
664 | * information from the /sys directory). Don't forget that removable devices | |
665 | * (floppies, CDROMs, ...) could be pretty slow. It's very bad idea to call | |
666 | * this function by default. | |
667 | * | |
668 | * Note that devices which were detected by this function won't be written to | |
669 | * blkid.tab cache file. | |
670 | * | |
671 | * Returns: 0 on success, or number less than zero in case of error. | |
672 | */ | |
673 | int blkid_probe_all_removable(blkid_cache cache) | |
674 | { | |
675 | int ret; | |
676 | ||
c62a6311 | 677 | DBG(PROBE, ul_debug("Begin blkid_probe_all_removable()")); |
49361dc4 | 678 | ret = probe_all_removable(cache); |
c62a6311 | 679 | DBG(PROBE, ul_debug("End blkid_probe_all_removable() [rc=%d]", ret)); |
49361dc4 KZ |
680 | return ret; |
681 | } | |
a0948ffe KZ |
682 | |
683 | #ifdef TEST_PROGRAM | |
684 | int main(int argc, char **argv) | |
685 | { | |
686 | blkid_cache cache = NULL; | |
687 | int ret; | |
688 | ||
0540ea54 | 689 | blkid_init_debug(BLKID_DEBUG_ALL); |
a0948ffe KZ |
690 | if (argc != 1) { |
691 | fprintf(stderr, "Usage: %s\n" | |
692 | "Probe all devices and exit\n", argv[0]); | |
693 | exit(1); | |
694 | } | |
695 | if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) { | |
696 | fprintf(stderr, "%s: error creating cache (%d)\n", | |
697 | argv[0], ret); | |
698 | exit(1); | |
699 | } | |
700 | if (blkid_probe_all(cache) < 0) | |
701 | printf("%s: error probing devices\n", argv[0]); | |
702 | ||
49361dc4 KZ |
703 | if (blkid_probe_all_removable(cache) < 0) |
704 | printf("%s: error probing removable devices\n", argv[0]); | |
705 | ||
a0948ffe KZ |
706 | blkid_put_cache(cache); |
707 | return (0); | |
708 | } | |
709 | #endif |