devname.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. return;
  121. }
  122. #define PROC_PARTITIONS "/proc/partitions"
  123. #define VG_DIR "/proc/lvm/VGs"
  124. /*
  125. * This function initializes the UUID cache with devices from the LVM
  126. * proc hierarchy. We currently depend on the names of the LVM
  127. * hierarchy giving us the device structure in /dev. (XXX is this a
  128. * safe thing to do?)
  129. */
  130. #ifdef VG_DIR
  131. #include <dirent.h>
  132. static dev_t lvm_get_devno(const char *lvm_device)
  133. {
  134. FILE *lvf;
  135. char buf[1024];
  136. int ma, mi;
  137. dev_t ret = 0;
  138. DBG(DEBUG_DEVNAME, printf("opening %s\n", lvm_device));
  139. if ((lvf = fopen(lvm_device, "r")) == NULL) {
  140. DBG(DEBUG_DEVNAME, printf("%s: (%d) %s\n", lvm_device, errno,
  141. strerror(errno)));
  142. return 0;
  143. }
  144. while (fgets(buf, sizeof(buf), lvf)) {
  145. if (sscanf(buf, "device: %d:%d", &ma, &mi) == 2) {
  146. ret = makedev(ma, mi);
  147. break;
  148. }
  149. }
  150. fclose(lvf);
  151. return ret;
  152. }
  153. static void lvm_probe_all(blkid_cache cache)
  154. {
  155. DIR *vg_list;
  156. struct dirent *vg_iter;
  157. int vg_len = strlen(VG_DIR);
  158. dev_t dev;
  159. if ((vg_list = opendir(VG_DIR)) == NULL)
  160. return;
  161. DBG(DEBUG_DEVNAME, printf("probing LVM devices under %s\n", VG_DIR));
  162. while ((vg_iter = readdir(vg_list)) != NULL) {
  163. DIR *lv_list;
  164. char *vdirname;
  165. char *vg_name;
  166. struct dirent *lv_iter;
  167. vg_name = vg_iter->d_name;
  168. if (!strcmp(vg_name, ".") || !strcmp(vg_name, ".."))
  169. continue;
  170. vdirname = xmalloc(vg_len + strlen(vg_name) + 8);
  171. sprintf(vdirname, "%s/%s/LVs", VG_DIR, vg_name);
  172. lv_list = opendir(vdirname);
  173. free(vdirname);
  174. if (lv_list == NULL)
  175. continue;
  176. while ((lv_iter = readdir(lv_list)) != NULL) {
  177. char *lv_name, *lvm_device;
  178. lv_name = lv_iter->d_name;
  179. if (!strcmp(lv_name, ".") || !strcmp(lv_name, ".."))
  180. continue;
  181. lvm_device = xmalloc(vg_len + strlen(vg_name) +
  182. strlen(lv_name) + 8);
  183. sprintf(lvm_device, "%s/%s/LVs/%s", VG_DIR, vg_name,
  184. lv_name);
  185. dev = lvm_get_devno(lvm_device);
  186. sprintf(lvm_device, "%s/%s", vg_name, lv_name);
  187. DBG(DEBUG_DEVNAME, printf("LVM dev %s: devno 0x%04X\n",
  188. lvm_device,
  189. (unsigned int) dev));
  190. probe_one(cache, lvm_device, dev, BLKID_PRI_LVM);
  191. free(lvm_device);
  192. }
  193. closedir(lv_list);
  194. }
  195. closedir(vg_list);
  196. }
  197. #endif
  198. #define PROC_EVMS_VOLUMES "/proc/evms/volumes"
  199. static int
  200. evms_probe_all(blkid_cache cache)
  201. {
  202. char line[100];
  203. int ma, mi, sz, num = 0;
  204. FILE *procpt;
  205. char device[110];
  206. procpt = fopen(PROC_EVMS_VOLUMES, "r");
  207. if (!procpt)
  208. return 0;
  209. while (fgets(line, sizeof(line), procpt)) {
  210. if (sscanf (line, " %d %d %d %*s %*s %[^\n ]",
  211. &ma, &mi, &sz, device) != 4)
  212. continue;
  213. DBG(DEBUG_DEVNAME, printf("Checking partition %s (%d, %d)\n",
  214. device, ma, mi));
  215. probe_one(cache, device, makedev(ma, mi), BLKID_PRI_EVMS);
  216. num++;
  217. }
  218. fclose(procpt);
  219. return num;
  220. }
  221. /*
  222. * Read the device data for all available block devices in the system.
  223. */
  224. int blkid_probe_all(blkid_cache cache)
  225. {
  226. FILE *proc;
  227. char line[1024];
  228. char ptname0[128], ptname1[128], *ptname = 0;
  229. char *ptnames[2];
  230. dev_t devs[2];
  231. int ma, mi;
  232. unsigned long long sz;
  233. int lens[2] = { 0, 0 };
  234. int which = 0, last = 0;
  235. ptnames[0] = ptname0;
  236. ptnames[1] = ptname1;
  237. if (!cache)
  238. return -BLKID_ERR_PARAM;
  239. if (cache->bic_flags & BLKID_BIC_FL_PROBED &&
  240. time(0) - cache->bic_time < BLKID_PROBE_INTERVAL)
  241. return 0;
  242. blkid_read_cache(cache);
  243. evms_probe_all(cache);
  244. #ifdef VG_DIR
  245. lvm_probe_all(cache);
  246. #endif
  247. proc = fopen(PROC_PARTITIONS, "r");
  248. if (!proc)
  249. return -BLKID_ERR_PROC;
  250. while (fgets(line, sizeof(line), proc)) {
  251. last = which;
  252. which ^= 1;
  253. ptname = ptnames[which];
  254. if (sscanf(line, " %d %d %llu %128[^\n ]",
  255. &ma, &mi, &sz, ptname) != 4)
  256. continue;
  257. devs[which] = makedev(ma, mi);
  258. DBG(DEBUG_DEVNAME, printf("read partition name %s\n", ptname));
  259. /* Skip whole disk devs unless they have no partitions
  260. * If we don't have a partition on this dev, also
  261. * check previous dev to see if it didn't have a partn.
  262. * heuristic: partition name ends in a digit.
  263. *
  264. * Skip extended partitions.
  265. * heuristic: size is 1
  266. *
  267. * FIXME: skip /dev/{ida,cciss,rd} whole-disk devs
  268. */
  269. lens[which] = strlen(ptname);
  270. if (isdigit(ptname[lens[which] - 1])) {
  271. DBG(DEBUG_DEVNAME,
  272. printf("partition dev %s, devno 0x%04X\n",
  273. ptname, (unsigned int) devs[which]));
  274. if (sz > 1)
  275. probe_one(cache, ptname, devs[which], 0);
  276. lens[which] = 0;
  277. lens[last] = 0;
  278. } else if (lens[last] && strncmp(ptnames[last], ptname,
  279. lens[last])) {
  280. DBG(DEBUG_DEVNAME,
  281. printf("whole dev %s, devno 0x%04X\n",
  282. ptnames[last], (unsigned int) devs[last]));
  283. probe_one(cache, ptnames[last], devs[last], 0);
  284. lens[last] = 0;
  285. }
  286. }
  287. /* Handle the last device if it wasn't partitioned */
  288. if (lens[which])
  289. probe_one(cache, ptname, devs[which], 0);
  290. fclose(proc);
  291. cache->bic_time = time(0);
  292. cache->bic_flags |= BLKID_BIC_FL_PROBED;
  293. blkid_flush_cache(cache);
  294. return 0;
  295. }
  296. #ifdef TEST_PROGRAM
  297. int main(int argc, char **argv)
  298. {
  299. blkid_cache cache = NULL;
  300. int ret;
  301. blkid_debug_mask = DEBUG_ALL;
  302. if (argc != 1) {
  303. fprintf(stderr, "Usage: %s\n"
  304. "Probe all devices and exit\n", argv[0]);
  305. exit(1);
  306. }
  307. if ((ret = blkid_get_cache(&cache, bb_dev_null)) != 0) {
  308. fprintf(stderr, "%s: error creating cache (%d)\n",
  309. argv[0], ret);
  310. exit(1);
  311. }
  312. if (blkid_probe_all(cache) < 0)
  313. printf("%s: error probing devices\n", argv[0]);
  314. blkid_put_cache(cache);
  315. return 0;
  316. }
  317. #endif