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