volume_id.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * volume_id - reads filesystem label and uuid
  3. *
  4. * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include "volume_id_internal.h"
  21. /* Some detection routines do not set label or uuid anyway,
  22. * so they are disabled. */
  23. /* Looks for partitions, we don't use it: */
  24. #define ENABLE_FEATURE_VOLUMEID_MAC 0
  25. /* #define ENABLE_FEATURE_VOLUMEID_MSDOS 0 - NB: this one
  26. * was not properly added to probe table anyway - ??! */
  27. /* None of RAIDs have label or uuid, except LinuxRAID: */
  28. #define ENABLE_FEATURE_VOLUMEID_HIGHPOINTRAID 0
  29. #define ENABLE_FEATURE_VOLUMEID_ISWRAID 0
  30. #define ENABLE_FEATURE_VOLUMEID_LSIRAID 0
  31. #define ENABLE_FEATURE_VOLUMEID_LVM 0
  32. #define ENABLE_FEATURE_VOLUMEID_NVIDIARAID 0
  33. #define ENABLE_FEATURE_VOLUMEID_PROMISERAID 0
  34. #define ENABLE_FEATURE_VOLUMEID_SILICONRAID 0
  35. #define ENABLE_FEATURE_VOLUMEID_VIARAID 0
  36. /* These filesystems also have no label or uuid: */
  37. #define ENABLE_FEATURE_VOLUMEID_MINIX 0
  38. #define ENABLE_FEATURE_VOLUMEID_HPFS 0
  39. #define ENABLE_FEATURE_VOLUMEID_UFS 0
  40. typedef int FAST_FUNC (*raid_probe_fptr)(struct volume_id *id, /*uint64_t off,*/ uint64_t size);
  41. typedef int FAST_FUNC (*probe_fptr)(struct volume_id *id /*, uint64_t off*/);
  42. static const raid_probe_fptr raid1[] = {
  43. #if ENABLE_FEATURE_VOLUMEID_LINUXRAID
  44. volume_id_probe_linux_raid,
  45. #endif
  46. #if ENABLE_FEATURE_VOLUMEID_ISWRAID
  47. volume_id_probe_intel_software_raid,
  48. #endif
  49. #if ENABLE_FEATURE_VOLUMEID_LSIRAID
  50. volume_id_probe_lsi_mega_raid,
  51. #endif
  52. #if ENABLE_FEATURE_VOLUMEID_VIARAID
  53. volume_id_probe_via_raid,
  54. #endif
  55. #if ENABLE_FEATURE_VOLUMEID_SILICONRAID
  56. volume_id_probe_silicon_medley_raid,
  57. #endif
  58. #if ENABLE_FEATURE_VOLUMEID_NVIDIARAID
  59. volume_id_probe_nvidia_raid,
  60. #endif
  61. #if ENABLE_FEATURE_VOLUMEID_PROMISERAID
  62. volume_id_probe_promise_fasttrack_raid,
  63. #endif
  64. #if ENABLE_FEATURE_VOLUMEID_HIGHPOINTRAID
  65. volume_id_probe_highpoint_45x_raid,
  66. #endif
  67. };
  68. static const probe_fptr raid2[] = {
  69. #if ENABLE_FEATURE_VOLUMEID_LVM
  70. volume_id_probe_lvm1,
  71. volume_id_probe_lvm2,
  72. #endif
  73. #if ENABLE_FEATURE_VOLUMEID_HIGHPOINTRAID
  74. volume_id_probe_highpoint_37x_raid,
  75. #endif
  76. #if ENABLE_FEATURE_VOLUMEID_LUKS
  77. volume_id_probe_luks,
  78. #endif
  79. };
  80. /* signature in the first block, only small buffer needed */
  81. static const probe_fptr fs1[] = {
  82. #if ENABLE_FEATURE_VOLUMEID_FAT
  83. volume_id_probe_vfat,
  84. #endif
  85. #if ENABLE_FEATURE_VOLUMEID_MAC
  86. volume_id_probe_mac_partition_map,
  87. #endif
  88. #if ENABLE_FEATURE_VOLUMEID_XFS
  89. volume_id_probe_xfs,
  90. #endif
  91. };
  92. /* fill buffer with maximum */
  93. static const probe_fptr fs2[] = {
  94. #if ENABLE_FEATURE_VOLUMEID_LINUXSWAP
  95. volume_id_probe_linux_swap,
  96. #endif
  97. #if ENABLE_FEATURE_VOLUMEID_EXT
  98. volume_id_probe_ext,
  99. #endif
  100. #if ENABLE_FEATURE_VOLUMEID_BTRFS
  101. volume_id_probe_btrfs,
  102. #endif
  103. #if ENABLE_FEATURE_VOLUMEID_REISERFS
  104. volume_id_probe_reiserfs,
  105. #endif
  106. #if ENABLE_FEATURE_VOLUMEID_JFS
  107. volume_id_probe_jfs,
  108. #endif
  109. #if ENABLE_FEATURE_VOLUMEID_UDF
  110. volume_id_probe_udf,
  111. #endif
  112. #if ENABLE_FEATURE_VOLUMEID_ISO9660
  113. volume_id_probe_iso9660,
  114. #endif
  115. #if ENABLE_FEATURE_VOLUMEID_HFS
  116. volume_id_probe_hfs_hfsplus,
  117. #endif
  118. #if ENABLE_FEATURE_VOLUMEID_UFS
  119. volume_id_probe_ufs,
  120. #endif
  121. #if ENABLE_FEATURE_VOLUMEID_NTFS
  122. volume_id_probe_ntfs,
  123. #endif
  124. #if ENABLE_FEATURE_VOLUMEID_CRAMFS
  125. volume_id_probe_cramfs,
  126. #endif
  127. #if ENABLE_FEATURE_VOLUMEID_ROMFS
  128. volume_id_probe_romfs,
  129. #endif
  130. #if ENABLE_FEATURE_VOLUMEID_HPFS
  131. volume_id_probe_hpfs,
  132. #endif
  133. #if ENABLE_FEATURE_VOLUMEID_SYSV
  134. volume_id_probe_sysv,
  135. #endif
  136. #if ENABLE_FEATURE_VOLUMEID_MINIX
  137. volume_id_probe_minix,
  138. #endif
  139. #if ENABLE_FEATURE_VOLUMEID_OCFS2
  140. volume_id_probe_ocfs2,
  141. #endif
  142. };
  143. int FAST_FUNC volume_id_probe_all(struct volume_id *id, /*uint64_t off,*/ uint64_t size)
  144. {
  145. unsigned i;
  146. /* probe for raid first, cause fs probes may be successful on raid members */
  147. if (size) {
  148. for (i = 0; i < ARRAY_SIZE(raid1); i++) {
  149. if (raid1[i](id, /*off,*/ size) == 0)
  150. goto ret;
  151. if (id->error)
  152. goto ret;
  153. }
  154. }
  155. for (i = 0; i < ARRAY_SIZE(raid2); i++) {
  156. if (raid2[i](id /*,off*/) == 0)
  157. goto ret;
  158. if (id->error)
  159. goto ret;
  160. }
  161. /* signature in the first block, only small buffer needed */
  162. for (i = 0; i < ARRAY_SIZE(fs1); i++) {
  163. if (fs1[i](id /*,off*/) == 0)
  164. goto ret;
  165. if (id->error)
  166. goto ret;
  167. }
  168. /* fill buffer with maximum */
  169. volume_id_get_buffer(id, 0, SB_BUFFER_SIZE);
  170. for (i = 0; i < ARRAY_SIZE(fs2); i++) {
  171. if (fs2[i](id /*,off*/) == 0)
  172. goto ret;
  173. if (id->error)
  174. goto ret;
  175. }
  176. ret:
  177. volume_id_free_buffer(id);
  178. return (- id->error); /* 0 or -1 */
  179. }
  180. /* open volume by device node */
  181. struct volume_id* FAST_FUNC volume_id_open_node(int fd)
  182. {
  183. struct volume_id *id;
  184. id = xzalloc(sizeof(struct volume_id));
  185. id->fd = fd;
  186. ///* close fd on device close */
  187. //id->fd_close = 1;
  188. return id;
  189. }
  190. #ifdef UNUSED
  191. /* open volume by major/minor */
  192. struct volume_id* FAST_FUNC volume_id_open_dev_t(dev_t devt)
  193. {
  194. struct volume_id *id;
  195. char *tmp_node[VOLUME_ID_PATH_MAX];
  196. tmp_node = xasprintf("/dev/.volume_id-%u-%u-%u",
  197. (unsigned)getpid(), (unsigned)major(devt), (unsigned)minor(devt));
  198. /* create temporary node to open block device */
  199. unlink(tmp_node);
  200. if (mknod(tmp_node, (S_IFBLK | 0600), devt) != 0)
  201. bb_perror_msg_and_die("can't mknod(%s)", tmp_node);
  202. id = volume_id_open_node(tmp_node);
  203. unlink(tmp_node);
  204. free(tmp_node);
  205. return id;
  206. }
  207. #endif
  208. void FAST_FUNC free_volume_id(struct volume_id *id)
  209. {
  210. if (id == NULL)
  211. return;
  212. //if (id->fd_close != 0) - always true
  213. close(id->fd);
  214. volume_id_free_buffer(id);
  215. #ifdef UNUSED_PARTITION_CODE
  216. free(id->partitions);
  217. #endif
  218. free(id);
  219. }