devname.c 8.4 KB

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