volume_id.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. //kbuild:lib-$(CONFIG_VOLUMEID) += volume_id.o util.o
  21. #include "volume_id_internal.h"
  22. /* Some detection routines do not set label or uuid anyway,
  23. * so they are disabled. */
  24. /* Looks for partitions, we don't use it: */
  25. #define ENABLE_FEATURE_VOLUMEID_MAC 0
  26. /* #define ENABLE_FEATURE_VOLUMEID_MSDOS 0 - NB: this one
  27. * was not properly added to probe table anyway - ??! */
  28. /* None of RAIDs have label or uuid, except LinuxRAID: */
  29. #define ENABLE_FEATURE_VOLUMEID_HIGHPOINTRAID 0
  30. #define ENABLE_FEATURE_VOLUMEID_ISWRAID 0
  31. #define ENABLE_FEATURE_VOLUMEID_LSIRAID 0
  32. #define ENABLE_FEATURE_VOLUMEID_LVM 0
  33. #define ENABLE_FEATURE_VOLUMEID_NVIDIARAID 0
  34. #define ENABLE_FEATURE_VOLUMEID_PROMISERAID 0
  35. #define ENABLE_FEATURE_VOLUMEID_SILICONRAID 0
  36. #define ENABLE_FEATURE_VOLUMEID_VIARAID 0
  37. /* These filesystems also have no label or uuid: */
  38. #define ENABLE_FEATURE_VOLUMEID_MINIX 0
  39. #define ENABLE_FEATURE_VOLUMEID_HPFS 0
  40. #define ENABLE_FEATURE_VOLUMEID_UFS 0
  41. typedef int FAST_FUNC (*raid_probe_fptr)(struct volume_id *id, /*uint64_t off,*/ uint64_t size);
  42. typedef int FAST_FUNC (*probe_fptr)(struct volume_id *id /*, uint64_t off*/);
  43. static const raid_probe_fptr raid1[] = {
  44. #if ENABLE_FEATURE_VOLUMEID_LINUXRAID
  45. volume_id_probe_linux_raid,
  46. #endif
  47. #if ENABLE_FEATURE_VOLUMEID_ISWRAID
  48. volume_id_probe_intel_software_raid,
  49. #endif
  50. #if ENABLE_FEATURE_VOLUMEID_LSIRAID
  51. volume_id_probe_lsi_mega_raid,
  52. #endif
  53. #if ENABLE_FEATURE_VOLUMEID_VIARAID
  54. volume_id_probe_via_raid,
  55. #endif
  56. #if ENABLE_FEATURE_VOLUMEID_SILICONRAID
  57. volume_id_probe_silicon_medley_raid,
  58. #endif
  59. #if ENABLE_FEATURE_VOLUMEID_NVIDIARAID
  60. volume_id_probe_nvidia_raid,
  61. #endif
  62. #if ENABLE_FEATURE_VOLUMEID_PROMISERAID
  63. volume_id_probe_promise_fasttrack_raid,
  64. #endif
  65. #if ENABLE_FEATURE_VOLUMEID_HIGHPOINTRAID
  66. volume_id_probe_highpoint_45x_raid,
  67. #endif
  68. };
  69. static const probe_fptr raid2[] = {
  70. #if ENABLE_FEATURE_VOLUMEID_LVM
  71. volume_id_probe_lvm1,
  72. volume_id_probe_lvm2,
  73. #endif
  74. #if ENABLE_FEATURE_VOLUMEID_HIGHPOINTRAID
  75. volume_id_probe_highpoint_37x_raid,
  76. #endif
  77. #if ENABLE_FEATURE_VOLUMEID_LUKS
  78. volume_id_probe_luks,
  79. #endif
  80. };
  81. /* signature in the first block, only small buffer needed */
  82. static const probe_fptr fs1[] = {
  83. #if ENABLE_FEATURE_VOLUMEID_FAT
  84. volume_id_probe_vfat,
  85. #endif
  86. #if ENABLE_FEATURE_VOLUMEID_EXFAT
  87. volume_id_probe_exfat,
  88. #endif
  89. #if ENABLE_FEATURE_VOLUMEID_MAC
  90. volume_id_probe_mac_partition_map,
  91. #endif
  92. #if ENABLE_FEATURE_VOLUMEID_SQUASHFS
  93. volume_id_probe_squashfs,
  94. #endif
  95. #if ENABLE_FEATURE_VOLUMEID_XFS
  96. volume_id_probe_xfs,
  97. #endif
  98. #if ENABLE_FEATURE_VOLUMEID_BCACHE
  99. volume_id_probe_bcache,
  100. #endif
  101. };
  102. /* fill buffer with maximum */
  103. static const probe_fptr fs2[] = {
  104. #if ENABLE_FEATURE_VOLUMEID_LINUXSWAP
  105. volume_id_probe_linux_swap,
  106. #endif
  107. #if ENABLE_FEATURE_VOLUMEID_EXT
  108. volume_id_probe_ext,
  109. #endif
  110. #if ENABLE_FEATURE_VOLUMEID_BTRFS
  111. volume_id_probe_btrfs,
  112. #endif
  113. #if ENABLE_FEATURE_VOLUMEID_REISERFS
  114. volume_id_probe_reiserfs,
  115. #endif
  116. #if ENABLE_FEATURE_VOLUMEID_JFS
  117. volume_id_probe_jfs,
  118. #endif
  119. #if ENABLE_FEATURE_VOLUMEID_UDF
  120. volume_id_probe_udf,
  121. #endif
  122. #if ENABLE_FEATURE_VOLUMEID_ISO9660
  123. volume_id_probe_iso9660,
  124. #endif
  125. #if ENABLE_FEATURE_VOLUMEID_HFS
  126. volume_id_probe_hfs_hfsplus,
  127. #endif
  128. #if ENABLE_FEATURE_VOLUMEID_UFS
  129. volume_id_probe_ufs,
  130. #endif
  131. #if ENABLE_FEATURE_VOLUMEID_F2FS
  132. volume_id_probe_f2fs,
  133. #endif
  134. #if ENABLE_FEATURE_VOLUMEID_NILFS
  135. volume_id_probe_nilfs,
  136. #endif
  137. #if ENABLE_FEATURE_VOLUMEID_NTFS
  138. volume_id_probe_ntfs,
  139. #endif
  140. #if ENABLE_FEATURE_VOLUMEID_CRAMFS
  141. volume_id_probe_cramfs,
  142. #endif
  143. #if ENABLE_FEATURE_VOLUMEID_ROMFS
  144. volume_id_probe_romfs,
  145. #endif
  146. #if ENABLE_FEATURE_VOLUMEID_HPFS
  147. volume_id_probe_hpfs,
  148. #endif
  149. #if ENABLE_FEATURE_VOLUMEID_SYSV
  150. volume_id_probe_sysv,
  151. #endif
  152. #if ENABLE_FEATURE_VOLUMEID_MINIX
  153. volume_id_probe_minix,
  154. #endif
  155. #if ENABLE_FEATURE_VOLUMEID_OCFS2
  156. volume_id_probe_ocfs2,
  157. #endif
  158. #if ENABLE_FEATURE_VOLUMEID_UBIFS
  159. volume_id_probe_ubifs,
  160. #endif
  161. };
  162. int FAST_FUNC volume_id_probe_all(struct volume_id *id, /*uint64_t off,*/ uint64_t size)
  163. {
  164. unsigned i;
  165. /* probe for raid first, cause fs probes may be successful on raid members */
  166. if (size) {
  167. for (i = 0; i < ARRAY_SIZE(raid1); i++) {
  168. if (raid1[i](id, /*off,*/ size) == 0)
  169. goto ret;
  170. if (id->error)
  171. goto ret;
  172. }
  173. }
  174. for (i = 0; i < ARRAY_SIZE(raid2); i++) {
  175. if (raid2[i](id /*,off*/) == 0)
  176. goto ret;
  177. if (id->error)
  178. goto ret;
  179. }
  180. /* signature in the first block, only small buffer needed */
  181. for (i = 0; i < ARRAY_SIZE(fs1); i++) {
  182. if (fs1[i](id /*,off*/) == 0)
  183. goto ret;
  184. if (id->error)
  185. goto ret;
  186. }
  187. /* fill buffer with maximum */
  188. volume_id_get_buffer(id, 0, SB_BUFFER_SIZE);
  189. for (i = 0; i < ARRAY_SIZE(fs2); i++) {
  190. if (fs2[i](id /*,off*/) == 0)
  191. goto ret;
  192. if (id->error)
  193. goto ret;
  194. }
  195. ret:
  196. volume_id_free_buffer(id);
  197. return (- id->error); /* 0 or -1 */
  198. }
  199. /* open volume by device node */
  200. struct volume_id* FAST_FUNC volume_id_open_node(int fd)
  201. {
  202. struct volume_id *id;
  203. id = xzalloc(sizeof(struct volume_id));
  204. id->fd = fd;
  205. ///* close fd on device close */
  206. //id->fd_close = 1;
  207. return id;
  208. }
  209. #ifdef UNUSED
  210. /* open volume by major/minor */
  211. struct volume_id* FAST_FUNC volume_id_open_dev_t(dev_t devt)
  212. {
  213. struct volume_id *id;
  214. char *tmp_node[VOLUME_ID_PATH_MAX];
  215. tmp_node = xasprintf("/dev/.volume_id-%u-%u-%u",
  216. (unsigned)getpid(), (unsigned)major(devt), (unsigned)minor(devt));
  217. /* create temporary node to open block device */
  218. unlink(tmp_node);
  219. if (mknod(tmp_node, (S_IFBLK | 0600), devt) != 0)
  220. bb_perror_msg_and_die("can't mknod(%s)", tmp_node);
  221. id = volume_id_open_node(tmp_node);
  222. unlink(tmp_node);
  223. free(tmp_node);
  224. return id;
  225. }
  226. #endif
  227. void FAST_FUNC free_volume_id(struct volume_id *id)
  228. {
  229. if (id == NULL)
  230. return;
  231. //if (id->fd_close != 0) - always true
  232. close(id->fd);
  233. volume_id_free_buffer(id);
  234. #ifdef UNUSED_PARTITION_CODE
  235. free(id->partitions);
  236. #endif
  237. free(id);
  238. }