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