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