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