devname.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * devname.c - get a dev by its device inode name
  4. *
  5. * Copyright (C) Andries Brouwer
  6. * Copyright (C) 1999, 2000, 2001, 2002, 2003 Theodore Ts'o
  7. * Copyright (C) 2001 Andreas Dilger
  8. *
  9. * %Begin-Header%
  10. * This file may be redistributed under the terms of the
  11. * GNU Lesser General Public License.
  12. * %End-Header%
  13. */
  14. #include <stdio.h>
  15. #include <string.h>
  16. #ifdef HAVE_UNISTD_H
  17. #include <unistd.h>
  18. #endif
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22. #ifdef HAVE_SYS_TYPES_H
  23. #include <sys/types.h>
  24. #endif
  25. #include <sys/stat.h>
  26. #ifdef HAVE_ERRNO_H
  27. #include <errno.h>
  28. #endif
  29. #ifdef HAVE_SYS_MKDEV_H
  30. #include <sys/mkdev.h>
  31. #endif
  32. #include <time.h>
  33. #include "blkidP.h"
  34. /*
  35. * Find a dev struct in the cache by device name, if available.
  36. *
  37. * If there is no entry with the specified device name, and the create
  38. * flag is set, then create an empty device entry.
  39. */
  40. blkid_dev blkid_get_dev(blkid_cache cache, const char *devname, int flags)
  41. {
  42. blkid_dev dev = NULL, tmp;
  43. struct list_head *p;
  44. if (!cache || !devname)
  45. return NULL;
  46. list_for_each(p, &cache->bic_devs) {
  47. tmp = list_entry(p, struct blkid_struct_dev, bid_devs);
  48. if (strcmp(tmp->bid_name, devname))
  49. continue;
  50. DBG(DEBUG_DEVNAME,
  51. printf("found devname %s in cache\n", tmp->bid_name));
  52. dev = tmp;
  53. break;
  54. }
  55. if (!dev && (flags & BLKID_DEV_CREATE)) {
  56. dev = blkid_new_dev();
  57. if (!dev)
  58. return NULL;
  59. dev->bid_name = blkid_strdup(devname);
  60. dev->bid_cache = cache;
  61. list_add_tail(&dev->bid_devs, &cache->bic_devs);
  62. cache->bic_flags |= BLKID_BIC_FL_CHANGED;
  63. }
  64. if (flags & BLKID_DEV_VERIFY)
  65. dev = blkid_verify(cache, dev);
  66. return dev;
  67. }
  68. /*
  69. * Probe a single block device to add to the device cache.
  70. */
  71. static void probe_one(blkid_cache cache, const char *ptname,
  72. dev_t devno, int pri)
  73. {
  74. blkid_dev dev = NULL;
  75. struct list_head *p;
  76. const char **dir;
  77. char *devname = NULL;
  78. /* See if we already have this device number in the cache. */
  79. list_for_each(p, &cache->bic_devs) {
  80. blkid_dev tmp = list_entry(p, struct blkid_struct_dev,
  81. bid_devs);
  82. if (tmp->bid_devno == devno) {
  83. dev = blkid_verify(cache, tmp);
  84. break;
  85. }
  86. }
  87. if (dev && dev->bid_devno == devno)
  88. goto set_pri;
  89. /*
  90. * Take a quick look at /dev/ptname for the device number. We check
  91. * all of the likely device directories. If we don't find it, or if
  92. * the stat information doesn't check out, use blkid_devno_to_devname()
  93. * to find it via an exhaustive search for the device major/minor.
  94. */
  95. for (dir = blkid_devdirs; *dir; dir++) {
  96. struct stat st;
  97. char device[256];
  98. sprintf(device, "%s/%s", *dir, ptname);
  99. if ((dev = blkid_get_dev(cache, device, BLKID_DEV_FIND)) &&
  100. dev->bid_devno == devno)
  101. goto set_pri;
  102. if (stat(device, &st) == 0 && S_ISBLK(st.st_mode) &&
  103. st.st_rdev == devno) {
  104. devname = blkid_strdup(device);
  105. break;
  106. }
  107. }
  108. if (!devname) {
  109. devname = blkid_devno_to_devname(devno);
  110. if (!devname)
  111. return;
  112. }
  113. dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
  114. free(devname);
  115. set_pri:
  116. if (!pri && !strncmp(ptname, "md", 2))
  117. pri = BLKID_PRI_MD;
  118. if (dev)
  119. dev->bid_pri = pri;
  120. }
  121. #define PROC_PARTITIONS "/proc/partitions"
  122. #define VG_DIR "/proc/lvm/VGs"
  123. /*
  124. * This function initializes the UUID cache with devices from the LVM
  125. * proc hierarchy. We currently depend on the names of the LVM
  126. * hierarchy giving us the device structure in /dev. (XXX is this a
  127. * safe thing to do?)
  128. */
  129. #ifdef VG_DIR
  130. #include <dirent.h>
  131. static dev_t lvm_get_devno(const char *lvm_device)
  132. {
  133. FILE *lvf;
  134. char buf[1024];
  135. int ma, mi;
  136. dev_t ret = 0;
  137. DBG(DEBUG_DEVNAME, printf("opening %s\n", lvm_device));
  138. if ((lvf = fopen_for_read(lvm_device)) == NULL) {
  139. DBG(DEBUG_DEVNAME, printf("%s: (%d) %s\n", lvm_device, errno,
  140. strerror(errno)));
  141. return 0;
  142. }
  143. while (fgets(buf, sizeof(buf), lvf)) {
  144. if (sscanf(buf, "device: %d:%d", &ma, &mi) == 2) {
  145. ret = makedev(ma, mi);
  146. break;
  147. }
  148. }
  149. fclose(lvf);
  150. return ret;
  151. }
  152. static void lvm_probe_all(blkid_cache cache)
  153. {
  154. DIR *vg_list;
  155. struct dirent *vg_iter;
  156. int vg_len = strlen(VG_DIR);
  157. dev_t dev;
  158. if ((vg_list = opendir(VG_DIR)) == NULL)
  159. return;
  160. DBG(DEBUG_DEVNAME, printf("probing LVM devices under %s\n", VG_DIR));
  161. while ((vg_iter = readdir(vg_list)) != NULL) {
  162. DIR *lv_list;
  163. char *vdirname;
  164. char *vg_name;
  165. struct dirent *lv_iter;
  166. vg_name = vg_iter->d_name;
  167. if (LONE_CHAR(vg_name, '.') || !strcmp(vg_name, ".."))
  168. continue;
  169. vdirname = xmalloc(vg_len + strlen(vg_name) + 8);
  170. sprintf(vdirname, "%s/%s/LVs", VG_DIR, vg_name);
  171. lv_list = opendir(vdirname);
  172. free(vdirname);
  173. if (lv_list == NULL)
  174. continue;
  175. while ((lv_iter = readdir(lv_list)) != NULL) {
  176. char *lv_name, *lvm_device;
  177. lv_name = lv_iter->d_name;
  178. if (LONE_CHAR(lv_name, '.') || !strcmp(lv_name, ".."))
  179. continue;
  180. lvm_device = xmalloc(vg_len + strlen(vg_name) +
  181. strlen(lv_name) + 8);
  182. sprintf(lvm_device, "%s/%s/LVs/%s", VG_DIR, vg_name,
  183. lv_name);
  184. dev = lvm_get_devno(lvm_device);
  185. sprintf(lvm_device, "%s/%s", vg_name, lv_name);
  186. DBG(DEBUG_DEVNAME, printf("LVM dev %s: devno 0x%04X\n",
  187. lvm_device,
  188. (unsigned int) dev));
  189. probe_one(cache, lvm_device, dev, BLKID_PRI_LVM);
  190. free(lvm_device);
  191. }
  192. closedir(lv_list);
  193. }
  194. closedir(vg_list);
  195. }
  196. #endif
  197. #define PROC_EVMS_VOLUMES "/proc/evms/volumes"
  198. static int
  199. evms_probe_all(blkid_cache cache)
  200. {
  201. char line[100];
  202. int ma, mi, sz, num = 0;
  203. FILE *procpt;
  204. char device[110];
  205. procpt = fopen_for_read(PROC_EVMS_VOLUMES);
  206. if (!procpt)
  207. return 0;
  208. while (fgets(line, sizeof(line), procpt)) {
  209. if (sscanf(line, " %d %d %d %*s %*s %[^\n ]",
  210. &ma, &mi, &sz, device) != 4)
  211. continue;
  212. DBG(DEBUG_DEVNAME, printf("Checking partition %s (%d, %d)\n",
  213. device, ma, mi));
  214. probe_one(cache, device, makedev(ma, mi), BLKID_PRI_EVMS);
  215. num++;
  216. }
  217. fclose(procpt);
  218. return num;
  219. }
  220. /*
  221. * Read the device data for all available block devices in the system.
  222. */
  223. int blkid_probe_all(blkid_cache cache)
  224. {
  225. FILE *proc;
  226. char line[1024];
  227. char ptname0[128], ptname1[128], *ptname = NULL;
  228. char *ptnames[2];
  229. dev_t devs[2];
  230. int ma, mi;
  231. unsigned long long sz;
  232. int lens[2] = { 0, 0 };
  233. int which = 0, last = 0;
  234. ptnames[0] = ptname0;
  235. ptnames[1] = ptname1;
  236. if (!cache)
  237. return -BLKID_ERR_PARAM;
  238. if (cache->bic_flags & BLKID_BIC_FL_PROBED &&
  239. time(NULL) - cache->bic_time < BLKID_PROBE_INTERVAL)
  240. return 0;
  241. blkid_read_cache(cache);
  242. evms_probe_all(cache);
  243. #ifdef VG_DIR
  244. lvm_probe_all(cache);
  245. #endif
  246. proc = fopen_for_read(PROC_PARTITIONS);
  247. if (!proc)
  248. return -BLKID_ERR_PROC;
  249. while (fgets(line, sizeof(line), proc)) {
  250. last = which;
  251. which ^= 1;
  252. ptname = ptnames[which];
  253. if (sscanf(line, " %d %d %llu %128[^\n ]",
  254. &ma, &mi, &sz, ptname) != 4)
  255. continue;
  256. devs[which] = makedev(ma, mi);
  257. DBG(DEBUG_DEVNAME, printf("read partition name %s\n", ptname));
  258. /* Skip whole disk devs unless they have no partitions
  259. * If we don't have a partition on this dev, also
  260. * check previous dev to see if it didn't have a partn.
  261. * heuristic: partition name ends in a digit.
  262. *
  263. * Skip extended partitions.
  264. * heuristic: size is 1
  265. *
  266. * FIXME: skip /dev/{ida,cciss,rd} whole-disk devs
  267. */
  268. lens[which] = strlen(ptname);
  269. if (isdigit(ptname[lens[which] - 1])) {
  270. DBG(DEBUG_DEVNAME,
  271. printf("partition dev %s, devno 0x%04X\n",
  272. ptname, (unsigned int) devs[which]));
  273. if (sz > 1)
  274. probe_one(cache, ptname, devs[which], 0);
  275. lens[which] = 0;
  276. lens[last] = 0;
  277. } else if (lens[last] && strncmp(ptnames[last], ptname,
  278. lens[last])) {
  279. DBG(DEBUG_DEVNAME,
  280. printf("whole dev %s, devno 0x%04X\n",
  281. ptnames[last], (unsigned int) devs[last]));
  282. probe_one(cache, ptnames[last], devs[last], 0);
  283. lens[last] = 0;
  284. }
  285. }
  286. /* Handle the last device if it wasn't partitioned */
  287. if (lens[which])
  288. probe_one(cache, ptname, devs[which], 0);
  289. fclose(proc);
  290. cache->bic_time = time(NULL);
  291. cache->bic_flags |= BLKID_BIC_FL_PROBED;
  292. blkid_flush_cache(cache);
  293. return 0;
  294. }
  295. #ifdef TEST_PROGRAM
  296. int main(int argc, char **argv)
  297. {
  298. blkid_cache cache = NULL;
  299. int ret;
  300. blkid_debug_mask = DEBUG_ALL;
  301. if (argc != 1) {
  302. fprintf(stderr, "Usage: %s\n"
  303. "Probe all devices and exit\n", argv[0]);
  304. exit(1);
  305. }
  306. if ((ret = blkid_get_cache(&cache, bb_dev_null)) != 0) {
  307. fprintf(stderr, "%s: error creating cache (%d)\n",
  308. argv[0], ret);
  309. exit(1);
  310. }
  311. if (blkid_probe_all(cache) < 0)
  312. printf("%s: error probing devices\n", argv[0]);
  313. blkid_put_cache(cache);
  314. return 0;
  315. }
  316. #endif