chiark / gitweb /
99aed6c96b608ba97192f2b10f346380d02bb0d8
[elogind.git] / extras / volume_id / volume_id.c
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      The superblock structs are taken from the linux kernel sources
7  *      and the libblkid living inside the e2fsprogs. This is a simple
8  *      straightforward implementation for reading the label strings of the
9  *      most common filesystems.
10  *
11  *      This library is free software; you can redistribute it and/or
12  *      modify it under the terms of the GNU Lesser General Public
13  *      License as published by the Free Software Foundation; either
14  *      version 2.1 of the License, or (at your option) any later version.
15  *
16  *      This library is distributed in the hope that it will be useful,
17  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  *      Lesser General Public License for more details.
20  *
21  *      You should have received a copy of the GNU Lesser General Public
22  *      License along with this library; if not, write to the Free Software
23  *      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24  */
25
26 #ifndef _GNU_SOURCE
27 #define _GNU_SOURCE 1
28 #endif
29
30 #ifdef HAVE_CONFIG_H
31 #  include <config.h>
32 #endif
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <ctype.h>
40 #include <fcntl.h>
41 #include <sys/stat.h>
42 #include <asm/types.h>
43
44 #include "volume_id.h"
45 #include "volume_id_logging.h"
46
47 #define bswap16(x) (__u16)((((__u16)(x) & 0x00ffu) << 8) | \
48                            (((__u16)(x) & 0xff00u) >> 8))
49
50 #define bswap32(x) (__u32)((((__u32)(x) & 0xff000000u) >> 24) | \
51                            (((__u32)(x) & 0x00ff0000u) >>  8) | \
52                            (((__u32)(x) & 0x0000ff00u) <<  8) | \
53                            (((__u32)(x) & 0x000000ffu) << 24))
54
55 #define bswap64(x) (__u64)((((__u64)(x) & 0xff00000000000000ull) >> 56) | \
56                            (((__u64)(x) & 0x00ff000000000000ull) >> 40) | \
57                            (((__u64)(x) & 0x0000ff0000000000ull) >> 24) | \
58                            (((__u64)(x) & 0x000000ff00000000ull) >>  8) | \
59                            (((__u64)(x) & 0x00000000ff000000ull) <<  8) | \
60                            (((__u64)(x) & 0x0000000000ff0000ull) << 24) | \
61                            (((__u64)(x) & 0x000000000000ff00ull) << 40) | \
62                            (((__u64)(x) & 0x00000000000000ffull) << 56))
63
64 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
65 #define le16_to_cpu(x) (x)
66 #define le32_to_cpu(x) (x)
67 #define le64_to_cpu(x) (x)
68 #define be16_to_cpu(x) bswap16(x)
69 #define be32_to_cpu(x) bswap32(x)
70 #elif (__BYTE_ORDER == __BIG_ENDIAN)
71 #define le16_to_cpu(x) bswap16(x)
72 #define le32_to_cpu(x) bswap32(x)
73 #define le64_to_cpu(x) bswap64(x)
74 #define be16_to_cpu(x) (x)
75 #define be32_to_cpu(x) (x)
76 #endif
77
78 /* size of superblock buffer, reiserfs block is at 64k */
79 #define SB_BUFFER_SIZE                          0x11000
80 /* size of seek buffer 4k */
81 #define SEEK_BUFFER_SIZE                        0x1000
82
83
84 static void set_label_raw(struct volume_id *id,
85                           const __u8 *buf, unsigned int count)
86 {
87         memcpy(id->label_raw, buf, count);
88         id->label_raw_len = count;
89 }
90
91 static void set_label_string(struct volume_id *id,
92                              const __u8 *buf, unsigned int count)
93 {
94         unsigned int i;
95
96         memcpy(id->label, buf, count);
97
98         /* remove trailing whitespace */
99         i = strnlen(id->label, count);
100         while (i--) {
101                 if (! isspace(id->label[i]))
102                         break;
103         }
104         id->label[i+1] = '\0';
105 }
106
107 #define LE              0
108 #define BE              1
109 static void set_label_unicode16(struct volume_id *id,
110                                 const __u8 *buf,
111                                 unsigned int endianess,
112                                 unsigned int count)
113 {
114         unsigned int i, j;
115         __u16 c;
116
117         j = 0;
118         for (i = 0; i + 2 <= count; i += 2) {
119                 if (endianess == LE)
120                         c = (buf[i+1] << 8) | buf[i];
121                 else
122                         c = (buf[i] << 8) | buf[i+1];
123                 if (c == 0) {
124                         id->label[j] = '\0';
125                         break;
126                 } else if (c < 0x80) {
127                         id->label[j++] = (__u8) c;
128                 } else if (c < 0x800) {
129                         id->label[j++] = (__u8) (0xc0 | (c >> 6));
130                         id->label[j++] = (__u8) (0x80 | (c & 0x3f));
131                 } else {
132                         id->label[j++] = (__u8) (0xe0 | (c >> 12));
133                         id->label[j++] = (__u8) (0x80 | ((c >> 6) & 0x3f));
134                         id->label[j++] = (__u8) (0x80 | (c & 0x3f));
135                 }
136         }
137 }
138
139 enum uuid_format {
140         UUID_DCE,
141         UUID_DOS,
142         UUID_NTFS,
143         UUID_HFS,
144 };
145
146 static void set_uuid(struct volume_id *id, const __u8 *buf, enum uuid_format format)
147 {
148         unsigned int i;
149         unsigned int count = 0;
150
151         switch(format) {
152         case UUID_DOS:
153                 count = 4;
154                 break;
155         case UUID_NTFS:
156         case UUID_HFS:
157                 count = 8;
158                 break;
159         case UUID_DCE:
160                 count = 16;
161         }
162         memcpy(id->uuid_raw, buf, count);
163
164         /* if set, create string in the same format, the native platform uses */
165         for (i = 0; i < count; i++)
166                 if (buf[i] != 0)
167                         goto set;
168         return;
169
170 set:
171         switch(format) {
172         case UUID_DOS:
173                 sprintf(id->uuid, "%02X%02X-%02X%02X",
174                         buf[3], buf[2], buf[1], buf[0]);
175                 break;
176         case UUID_NTFS:
177                 sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
178                         buf[7], buf[6], buf[5], buf[4],
179                         buf[3], buf[2], buf[1], buf[0]);
180                 break;
181         case UUID_HFS:
182                 sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
183                         buf[0], buf[1], buf[2], buf[3],
184                         buf[4], buf[5], buf[6], buf[7]);
185                 break;
186         case UUID_DCE:
187                 sprintf(id->uuid,
188                         "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
189                         buf[0], buf[1], buf[2], buf[3],
190                         buf[4], buf[5],
191                         buf[6], buf[7],
192                         buf[8], buf[9],
193                         buf[10], buf[11], buf[12], buf[13], buf[14],buf[15]);
194                 break;
195         }
196 }
197
198 static __u8 *get_buffer(struct volume_id *id, __u64 off, unsigned int len)
199 {
200         unsigned int buf_len;
201
202         dbg("get buffer off 0x%llx, len 0x%x", off, len);
203         /* check if requested area fits in superblock buffer */
204         if (off + len <= SB_BUFFER_SIZE) {
205                 if (id->sbbuf == NULL) {
206                         id->sbbuf = malloc(SB_BUFFER_SIZE);
207                         if (id->sbbuf == NULL)
208                                 return NULL;
209                 }
210
211                 /* check if we need to read */
212                 if ((off + len) > id->sbbuf_len) {
213                         dbg("read sbbuf len:0x%llx", off + len);
214                         lseek(id->fd, 0, SEEK_SET);
215                         buf_len = read(id->fd, id->sbbuf, off + len);
216                         dbg("got 0x%x (%i) bytes", buf_len, buf_len);
217                         id->sbbuf_len = buf_len;
218                         if (buf_len < off + len)
219                                 return NULL;
220                 }
221
222                 return &(id->sbbuf[off]);
223         } else {
224                 if (len > SEEK_BUFFER_SIZE)
225                         len = SEEK_BUFFER_SIZE;
226
227                 /* get seek buffer */
228                 if (id->seekbuf == NULL) {
229                         id->seekbuf = malloc(SEEK_BUFFER_SIZE);
230                         if (id->seekbuf == NULL)
231                                 return NULL;
232                 }
233
234                 /* check if we need to read */
235                 if ((off < id->seekbuf_off) ||
236                     ((off + len) > (id->seekbuf_off + id->seekbuf_len))) {
237                         dbg("read seekbuf off:0x%llx len:0x%x", off, len);
238                         if (lseek(id->fd, off, SEEK_SET) == -1)
239                                 return NULL;
240                         buf_len = read(id->fd, id->seekbuf, len);
241                         dbg("got 0x%x (%i) bytes", buf_len, buf_len);
242                         id->seekbuf_off = off;
243                         id->seekbuf_len = buf_len;
244                         if (buf_len < len)
245                                 return NULL;
246                 }
247
248                 return &(id->seekbuf[off - id->seekbuf_off]);
249         }
250 }
251
252 static void free_buffer(struct volume_id *id)
253 {
254         if (id->sbbuf != NULL) {
255                 free(id->sbbuf);
256                 id->sbbuf = NULL;
257                 id->sbbuf_len = 0;
258         }
259         if (id->seekbuf != NULL) {
260                 free(id->seekbuf);
261                 id->seekbuf = NULL;
262                 id->seekbuf_len = 0;
263         }
264 }
265
266 #define LVM1_SB_OFF                     0x400
267 #define LVM1_MAGIC                      "HM"
268 static int probe_lvm1(struct volume_id *id, __u64 off)
269 {
270         struct lvm2_super_block {
271                 __u8    id[2];
272         } __attribute__((packed)) *lvm;
273
274         const __u8 *buf;
275
276         buf = get_buffer(id, off + LVM1_SB_OFF, 0x800);
277         if (buf == NULL)
278                 return -1;
279
280         lvm = (struct lvm2_super_block *) buf;
281
282         if (strncmp(lvm->id, LVM1_MAGIC, 2) != 0)
283                 return -1;
284
285         id->usage_id = VOLUME_ID_RAID;
286         id->type_id = VOLUME_ID_LVM1;
287         id->type = "LVM1_member";
288
289         return 0;
290 }
291
292 #define LVM2_LABEL_ID                   "LABELONE"
293 #define LVM2LABEL_SCAN_SECTORS          4
294 static int probe_lvm2(struct volume_id *id, __u64 off)
295 {
296         struct lvm2_super_block {
297                 __u8    id[8];
298                 __u64   sector_xl;
299                 __u32   crc_xl;
300                 __u32   offset_xl;
301                 __u8    type[8];
302         } __attribute__((packed)) *lvm;
303
304         const __u8 *buf;
305         unsigned int soff;
306
307         buf = get_buffer(id, off, LVM2LABEL_SCAN_SECTORS * 0x200);
308         if (buf == NULL)
309                 return -1;
310
311
312         for (soff = 0; soff < LVM2LABEL_SCAN_SECTORS * 0x200; soff += 0x200) {
313                 lvm = (struct lvm2_super_block *) &buf[soff];
314
315                 if (strncmp(lvm->id, LVM2_LABEL_ID, 8) == 0)
316                         goto found;
317         }
318
319         return -1;
320
321 found:
322         strncpy(id->type_version, lvm->type, 8);
323         id->usage_id = VOLUME_ID_RAID;
324         id->type_id = VOLUME_ID_LVM1;
325         id->type = "LVM2_member";
326
327         return 0;
328 }
329
330 #define MD_RESERVED_BYTES               0x10000
331 #define MD_MAGIC                        0xa92b4efc
332 static int probe_linux_raid(struct volume_id *id, __u64 off, __u64 size)
333 {
334         struct mdp_super_block {
335                 __u32   md_magic;
336                 __u32   major_version;
337                 __u32   minor_version;
338                 __u32   patch_version;
339                 __u32   gvalid_words;
340                 __u32   set_uuid0;
341                 __u32   ctime;
342                 __u32   level;
343                 __u32   size;
344                 __u32   nr_disks;
345                 __u32   raid_disks;
346                 __u32   md_minor;
347                 __u32   not_persistent;
348                 __u32   set_uuid1;
349                 __u32   set_uuid2;
350                 __u32   set_uuid3;
351         } __attribute__((packed)) *mdp;
352
353         const __u8 *buf;
354         __u64 sboff;
355         __u8 uuid[16];
356
357         if (size < 0x10000)
358                 return -1;
359
360         sboff = (size & ~(MD_RESERVED_BYTES - 1)) - MD_RESERVED_BYTES;
361         buf = get_buffer(id, off + sboff, 0x800);
362         if (buf == NULL)
363                 return -1;
364
365         mdp = (struct mdp_super_block *) buf;
366
367         if (le32_to_cpu(mdp->md_magic) != MD_MAGIC)
368                 return -1;
369
370         memcpy(uuid, &mdp->set_uuid0, 4);
371         memcpy(&uuid[4], &mdp->set_uuid1, 12);
372         set_uuid(id, uuid, UUID_DCE);
373
374         snprintf(id->type_version, VOLUME_ID_FORMAT_SIZE-1, "%u.%u.%u",
375                  le32_to_cpu(mdp->major_version),
376                  le32_to_cpu(mdp->minor_version),
377                  le32_to_cpu(mdp->patch_version));
378
379         dbg("found raid signature");
380         id->usage_id = VOLUME_ID_RAID;
381         id->type = "linux_raid_member";
382
383         return 0;
384 }
385
386 #define MSDOS_MAGIC                     "\x55\xaa"
387 #define MSDOS_PARTTABLE_OFFSET          0x1be
388 #define MSDOS_SIG_OFF                   0x1fe
389 #define BSIZE                           0x200
390 #define DOS_EXTENDED_PARTITION          0x05
391 #define LINUX_EXTENDED_PARTITION        0x85
392 #define WIN98_EXTENDED_PARTITION        0x0f
393 #define LINUX_RAID_PARTITION            0xfd
394 #define is_extended(type) \
395         (type == DOS_EXTENDED_PARTITION ||      \
396          type == WIN98_EXTENDED_PARTITION ||    \
397          type == LINUX_EXTENDED_PARTITION)
398 #define is_raid(type) \
399         (type == LINUX_RAID_PARTITION)
400 static int probe_msdos_part_table(struct volume_id *id, __u64 off)
401 {
402         struct msdos_partition_entry {
403                 __u8    boot_ind;
404                 __u8    head;
405                 __u8    sector;
406                 __u8    cyl;
407                 __u8    sys_ind;
408                 __u8    end_head;
409                 __u8    end_sector;
410                 __u8    end_cyl;
411                 __u32   start_sect;
412                 __u32   nr_sects;
413         } __attribute__((packed)) *part;
414
415         const __u8 *buf;
416         int i;
417         __u64 poff;
418         __u64 plen;
419         __u64 extended = 0;
420         __u64 current;
421         __u64 next;
422         int limit;
423         int empty = 1;
424         struct volume_id_partition *p;
425
426         buf = get_buffer(id, off, 0x200);
427         if (buf == NULL)
428                 return -1;
429
430         if (strncmp(&buf[MSDOS_SIG_OFF], MSDOS_MAGIC, 2) != 0)
431                 return -1;
432
433         /* check flags on all entries for a valid partition table */
434         part = (struct msdos_partition_entry*) &buf[MSDOS_PARTTABLE_OFFSET];
435         for (i = 0; i < 4; i++) {
436                 if (part[i].boot_ind != 0 &&
437                     part[i].boot_ind != 0x80)
438                         return -1;
439
440                 if (le32_to_cpu(part[i].nr_sects) != 0)
441                         empty = 0;
442         }
443         if (empty == 1)
444                 return -1;
445
446         if (id->partitions != NULL)
447                 free(id->partitions);
448         id->partitions = malloc(VOLUME_ID_PARTITIONS_MAX *
449                                 sizeof(struct volume_id_partition));
450         if (id->partitions == NULL)
451                 return -1;
452         memset(id->partitions, 0x00,
453                VOLUME_ID_PARTITIONS_MAX * sizeof(struct volume_id_partition));
454
455         for (i = 0; i < 4; i++) {
456                 poff = (__u64) le32_to_cpu(part[i].start_sect) * BSIZE;
457                 plen = (__u64) le32_to_cpu(part[i].nr_sects) * BSIZE;
458
459                 if (plen == 0)
460                         continue;
461
462                 p = &id->partitions[i];
463
464                 if (is_extended(part[i].sys_ind)) {
465                         dbg("found extended partition at 0x%llx", poff);
466                         p->usage_id = VOLUME_ID_PARTITIONTABLE;
467                         p->type_id = VOLUME_ID_MSDOSEXTENDED;
468                         p->type = "msdos_extended_partition";
469                         if (extended == 0)
470                                 extended = off + poff;
471                 } else {
472                         dbg("found 0x%x data partition at 0x%llx, len 0x%llx",
473                             part[i].sys_ind, poff, plen);
474
475                         if (is_raid(part[i].sys_ind))
476                                 p->usage_id = VOLUME_ID_RAID;
477                         else
478                                 p->usage_id = VOLUME_ID_UNPROBED;
479                 }
480
481                 p->off = off + poff;
482                 p->len = plen;
483                 id->partition_count = i+1;
484         }
485
486         next = extended;
487         current = extended;
488         limit = 50;
489
490         /* follow extended partition chain and add data partitions */
491         while (next != 0) {
492                 if (limit-- == 0) {
493                         dbg("extended chain limit reached");
494                         break;
495                 }
496
497                 buf = get_buffer(id, current, 0x200);
498                 if (buf == NULL)
499                         break;
500
501                 part = (struct msdos_partition_entry*) &buf[MSDOS_PARTTABLE_OFFSET];
502
503                 if (strncmp(&buf[MSDOS_SIG_OFF], MSDOS_MAGIC, 2) != 0)
504                         break;
505
506                 next = 0;
507
508                 for (i = 0; i < 4; i++) {
509                         poff = (__u64) le32_to_cpu(part[i].start_sect) * BSIZE;
510                         plen = (__u64) le32_to_cpu(part[i].nr_sects) * BSIZE;
511
512                         if (plen == 0)
513                                 continue;
514
515                         if (is_extended(part[i].sys_ind)) {
516                                 dbg("found extended partition at 0x%llx", poff);
517                                 if (next == 0)
518                                         next = extended + poff;
519                         } else {
520                                 dbg("found 0x%x data partition at 0x%llx, len 0x%llx",
521                                         part[i].sys_ind, poff, plen);
522
523                                 /* we always start at the 5th entry */
524                                 while (id->partition_count < 4)
525                                         id->partitions[id->partition_count++].usage_id =
526                                                 VOLUME_ID_UNUSED;
527
528                                 p = &id->partitions[id->partition_count];
529
530                                 if (is_raid(part[i].sys_ind))
531                                         p->usage_id = VOLUME_ID_RAID;
532                                 else
533                                         p->usage_id = VOLUME_ID_UNPROBED;
534
535                                 p->off = current + poff;
536                                 p->len = plen;
537                                 id->partition_count++;
538                                 if (id->partition_count >= VOLUME_ID_PARTITIONS_MAX) {
539                                         dbg("to many partitions");
540                                         next = 0;
541                                 }
542                         }
543                 }
544
545                 current = next;
546         }
547
548         id->usage_id = VOLUME_ID_PARTITIONTABLE;
549         id->type_id = VOLUME_ID_MSDOSPARTTABLE;
550         id->type = "msdos_partition_table";
551
552         return 0;
553 }
554
555 #define EXT3_FEATURE_COMPAT_HAS_JOURNAL         0x00000004
556 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV       0x00000008
557 #define EXT_SUPERBLOCK_OFFSET                   0x400
558 static int probe_ext(struct volume_id *id, __u64 off)
559 {
560         struct ext2_super_block {
561                 __u32   inodes_count;
562                 __u32   blocks_count;
563                 __u32   r_blocks_count;
564                 __u32   free_blocks_count;
565                 __u32   free_inodes_count;
566                 __u32   first_data_block;
567                 __u32   log_block_size;
568                 __u32   dummy3[7];
569                 __u8    magic[2];
570                 __u16   state;
571                 __u32   dummy5[8];
572                 __u32   feature_compat;
573                 __u32   feature_incompat;
574                 __u32   feature_ro_compat;
575                 __u8    uuid[16];
576                 __u8    volume_name[16];
577         } __attribute__((__packed__)) *es;
578
579         es = (struct ext2_super_block *)
580              get_buffer(id, off + EXT_SUPERBLOCK_OFFSET, 0x200);
581         if (es == NULL)
582                 return -1;
583
584         if (es->magic[0] != 0123 ||
585             es->magic[1] != 0357)
586                 return -1;
587
588         set_label_raw(id, es->volume_name, 16);
589         set_label_string(id, es->volume_name, 16);
590         set_uuid(id, es->uuid, UUID_DCE);
591
592         if ((le32_to_cpu(es->feature_compat) &
593              EXT3_FEATURE_COMPAT_HAS_JOURNAL) != 0) {
594                 id->usage_id = VOLUME_ID_FILESYSTEM;
595                 id->type_id = VOLUME_ID_EXT3;
596                 id->type = "ext3";
597         } else {
598                 id->usage_id = VOLUME_ID_FILESYSTEM;
599                 id->type_id = VOLUME_ID_EXT2;
600                 id->type = "ext2";
601         }
602
603         return 0;
604 }
605
606 #define REISERFS1_SUPERBLOCK_OFFSET             0x2000
607 #define REISERFS_SUPERBLOCK_OFFSET              0x10000
608 static int probe_reiserfs(struct volume_id *id, __u64 off)
609 {
610         struct reiserfs_super_block {
611                 __u32   blocks_count;
612                 __u32   free_blocks;
613                 __u32   root_block;
614                 __u32   journal_block;
615                 __u32   journal_dev;
616                 __u32   orig_journal_size;
617                 __u32   dummy2[5];
618                 __u16   blocksize;
619                 __u16   dummy3[3];
620                 __u8    magic[12];
621                 __u32   dummy4[5];
622                 __u8    uuid[16];
623                 __u8    label[16];
624         } __attribute__((__packed__)) *rs;
625
626         rs = (struct reiserfs_super_block *)
627              get_buffer(id, off + REISERFS_SUPERBLOCK_OFFSET, 0x200);
628         if (rs == NULL)
629                 return -1;
630
631         if (strncmp(rs->magic, "ReIsEr2Fs", 9) == 0) {
632                 strcpy(id->type_version, "3.6");
633                 goto found;
634         }
635
636         if (strncmp(rs->magic, "ReIsEr3Fs", 9) == 0) {
637                 strcpy(id->type_version, "JR");
638                 goto found;
639         }
640
641         rs = (struct reiserfs_super_block *)
642              get_buffer(id, off + REISERFS1_SUPERBLOCK_OFFSET, 0x200);
643         if (rs == NULL)
644                 return -1;
645
646         if (strncmp(rs->magic, "ReIsErFs", 8) == 0) {
647                 strcpy(id->type_version, "3.5");
648                 goto found;
649         }
650
651         return -1;
652
653 found:
654         set_label_raw(id, rs->label, 16);
655         set_label_string(id, rs->label, 16);
656         set_uuid(id, rs->uuid, UUID_DCE);
657
658         id->usage_id = VOLUME_ID_FILESYSTEM;
659         id->type_id = VOLUME_ID_REISERFS;
660         id->type = "reiserfs";
661
662         return 0;
663 }
664
665 static int probe_xfs(struct volume_id *id, __u64 off)
666 {
667         struct xfs_super_block {
668                 __u8    magic[4];
669                 __u32   blocksize;
670                 __u64   dblocks;
671                 __u64   rblocks;
672                 __u32   dummy1[2];
673                 __u8    uuid[16];
674                 __u32   dummy2[15];
675                 __u8    fname[12];
676                 __u32   dummy3[2];
677                 __u64   icount;
678                 __u64   ifree;
679                 __u64   fdblocks;
680         } __attribute__((__packed__)) *xs;
681
682         xs = (struct xfs_super_block *) get_buffer(id, off, 0x200);
683         if (xs == NULL)
684                 return -1;
685
686         if (strncmp(xs->magic, "XFSB", 4) != 0)
687                 return -1;
688
689         set_label_raw(id, xs->fname, 12);
690         set_label_string(id, xs->fname, 12);
691         set_uuid(id, xs->uuid, UUID_DCE);
692
693         id->usage_id = VOLUME_ID_FILESYSTEM;
694         id->type_id = VOLUME_ID_XFS;
695         id->type = "xfs";
696
697         return 0;
698 }
699
700 #define JFS_SUPERBLOCK_OFFSET                   0x8000
701 static int probe_jfs(struct volume_id *id, __u64 off)
702 {
703         struct jfs_super_block {
704                 __u8    magic[4];
705                 __u32   version;
706                 __u64   size;
707                 __u32   bsize;
708                 __u32   dummy1;
709                 __u32   pbsize;
710                 __u32   dummy2[27];
711                 __u8    uuid[16];
712                 __u8    label[16];
713                 __u8    loguuid[16];
714         } __attribute__((__packed__)) *js;
715
716         js = (struct jfs_super_block *)
717              get_buffer(id, off + JFS_SUPERBLOCK_OFFSET, 0x200);
718         if (js == NULL)
719                 return -1;
720
721         if (strncmp(js->magic, "JFS1", 4) != 0)
722                 return -1;
723
724         set_label_raw(id, js->label, 16);
725         set_label_string(id, js->label, 16);
726         set_uuid(id, js->uuid, UUID_DCE);
727
728         id->usage_id = VOLUME_ID_FILESYSTEM;
729         id->type_id = VOLUME_ID_JFS;
730         id->type = "jfs";
731
732         return 0;
733 }
734
735 #define FAT12_MAX                       0xff5
736 #define FAT16_MAX                       0xfff5
737 #define FAT_ATTR_VOLUME                 0x08
738 static int probe_vfat(struct volume_id *id, __u64 off)
739 {
740         struct vfat_super_block {
741                 __u8    boot_jump[3];
742                 __u8    sysid[8];
743                 __u16   sector_size;
744                 __u8    sectors_per_cluster;
745                 __u16   reserved;
746                 __u8    fats;
747                 __u16   dir_entries;
748                 __u16   sectors;
749                 __u8    media;
750                 __u16   fat_length;
751                 __u16   secs_track;
752                 __u16   heads;
753                 __u32   hidden;
754                 __u32   total_sect;
755                 union {
756                         struct fat_super_block {
757                                 __u8    unknown[3];
758                                 __u8    serno[4];
759                                 __u8    label[11];
760                                 __u8    magic[8];
761                                 __u8    dummy2[192];
762                                 __u8    pmagic[2];
763                         } __attribute__((__packed__)) fat;
764                         struct fat32_super_block {
765                                 __u32   fat32_length;
766                                 __u16   flags;
767                                 __u8    version[2];
768                                 __u32   root_cluster;
769                                 __u16   insfo_sector;
770                                 __u16   backup_boot;
771                                 __u16   reserved2[6];
772                                 __u8    unknown[3];
773                                 __u8    serno[4];
774                                 __u8    label[11];
775                                 __u8    magic[8];
776                                 __u8    dummy2[164];
777                                 __u8    pmagic[2];
778                         } __attribute__((__packed__)) fat32;
779                 } __attribute__((__packed__)) type;
780         } __attribute__((__packed__)) *vs;
781
782         struct vfat_dir_entry {
783                 __u8    name[11];
784                 __u8    attr;
785                 __u16   time_creat;
786                 __u16   date_creat;
787                 __u16   time_acc;
788                 __u16   date_acc;
789                 __u16   cluster_high;
790                 __u16   time_write;
791                 __u16   date_write;
792                 __u16   cluster_low;
793                 __u32   size;
794         } __attribute__((__packed__)) *dir;
795
796         __u16 sector_size;
797         __u16 dir_entries;
798         __u32 sect_count;
799         __u16 reserved;
800         __u16 fat_size;
801         __u32 root_cluster;
802         __u32 dir_size;
803         __u32 cluster_count;
804         __u32 fat_length;
805         __u64 root_start;
806         __u32 start_data_sect;
807         __u16 root_dir_entries;
808         __u8 *buf;
809         __u32 buf_size;
810         __u8 *label = NULL;
811         __u32 next;
812         int maxloop;
813         int i;
814
815         vs = (struct vfat_super_block *) get_buffer(id, off, 0x200);
816         if (vs == NULL)
817                 return -1;
818
819         /* believe only that's fat, don't trust the version
820          * the cluster_count will tell us
821          */
822         if (strncmp(vs->type.fat32.magic, "MSWIN", 5) == 0)
823                 goto valid;
824
825         if (strncmp(vs->type.fat32.magic, "FAT32   ", 8) == 0)
826                 goto valid;
827
828         if (strncmp(vs->type.fat.magic, "FAT16   ", 8) == 0)
829                 goto valid;
830
831         if (strncmp(vs->type.fat.magic, "MSDOS", 5) == 0)
832                 goto valid;
833
834         if (strncmp(vs->type.fat.magic, "FAT12   ", 8) == 0)
835                 goto valid;
836
837         /*
838          * There are old floppies out there without a magic, so we check
839          * for well known values and guess if it's a fat volume
840          */
841
842         /* boot jump address check */
843         if ((vs->boot_jump[0] != 0xeb || vs->boot_jump[2] != 0x90) &&
844              vs->boot_jump[0] != 0xe9)
845                 return -1;
846
847         /* heads check */
848         if (vs->heads == 0)
849                 return -1;
850
851         /* cluster size check*/ 
852         if (vs->sectors_per_cluster == 0 ||
853             (vs->sectors_per_cluster & (vs->sectors_per_cluster-1)))
854                 return -1;
855
856         /* media check */
857         if (vs->media < 0xf8 && vs->media != 0xf0)
858                 return -1;
859
860         /* fat count*/
861         if (vs->fats != 2)
862                 return -1;
863
864 valid:
865         /* sector size check */
866         sector_size = le16_to_cpu(vs->sector_size);
867         if (sector_size != 0x200 && sector_size != 0x400 &&
868             sector_size != 0x800 && sector_size != 0x1000)
869                 return -1;
870
871         dbg("sector_size 0x%x", sector_size);
872         dbg("sectors_per_cluster 0x%x", vs->sectors_per_cluster);
873
874         dir_entries = le16_to_cpu(vs->dir_entries);
875         reserved = le16_to_cpu(vs->reserved);
876         dbg("reserved 0x%x", reserved);
877
878         sect_count = le16_to_cpu(vs->sectors);
879         if (sect_count == 0)
880                 sect_count = le32_to_cpu(vs->total_sect);
881         dbg("sect_count 0x%x", sect_count);
882
883         fat_length = le16_to_cpu(vs->fat_length);
884         if (fat_length == 0)
885                 fat_length = le32_to_cpu(vs->type.fat32.fat32_length);
886         dbg("fat_length 0x%x", fat_length);
887
888         fat_size = fat_length * vs->fats;
889         dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) +
890                         (sector_size-1)) / sector_size;
891         dbg("dir_size 0x%x", dir_size);
892
893         cluster_count = sect_count - (reserved + fat_size + dir_size);
894         cluster_count /= vs->sectors_per_cluster;
895         dbg("cluster_count 0x%x", cluster_count);
896
897         if (cluster_count < FAT12_MAX) {
898                 strcpy(id->type_version, "FAT12");
899         } else if (cluster_count < FAT16_MAX) {
900                 strcpy(id->type_version, "FAT16");
901         } else {
902                 strcpy(id->type_version, "FAT32");
903                 goto fat32;
904         }
905
906         /* the label may be an attribute in the root directory */
907         root_start = (reserved + fat_size) * sector_size;
908         dbg("root dir start 0x%llx", root_start);
909         root_dir_entries = le16_to_cpu(vs->dir_entries);
910         dbg("expected entries 0x%x", root_dir_entries);
911
912         buf_size = root_dir_entries * sizeof(struct vfat_dir_entry);
913         buf = get_buffer(id, off + root_start, buf_size);
914         if (buf == NULL)
915                 goto found;
916
917         dir = (struct vfat_dir_entry*) buf;
918
919         for (i = 0; i <= root_dir_entries; i++) {
920                 /* end marker */
921                 if (dir[i].attr == 0x00) {
922                         dbg("end of dir");
923                         break;
924                 }
925
926                 /* empty entry */
927                 if (dir[i].attr == 0xe5)
928                         continue;
929
930                 if (dir[i].attr == FAT_ATTR_VOLUME) {
931                         dbg("found ATTR_VOLUME id in root dir");
932                         label = dir[i].name;
933                 }
934
935                 dbg("skip dir entry");
936         }
937
938         if (label != NULL && strncmp(label, "NO NAME    ", 11) != 0) {
939                 set_label_raw(id, label, 11);
940                 set_label_string(id, label, 11);
941         } else if (strncmp(vs->type.fat.label, "NO NAME    ", 11) != 0) {
942                 set_label_raw(id, vs->type.fat.label, 11);
943                 set_label_string(id, vs->type.fat.label, 11);
944         }
945         set_uuid(id, vs->type.fat.serno, UUID_DOS);
946         goto found;
947
948 fat32:
949         /* FAT32 root dir is a cluster chain like any other directory */
950         buf_size = vs->sectors_per_cluster * sector_size;
951         root_cluster = le32_to_cpu(vs->type.fat32.root_cluster);
952         dbg("root dir cluster %u", root_cluster);
953         start_data_sect = reserved + fat_size;
954
955         next = root_cluster;
956         maxloop = 100;
957         while (--maxloop) {
958                 __u32 next_sect_off;
959                 __u64 next_off;
960                 __u64 fat_entry_off;
961                 int count;
962
963                 dbg("next cluster %u", next);
964                 next_sect_off = (next - 2) * vs->sectors_per_cluster;
965                 next_off = (start_data_sect + next_sect_off) * sector_size;
966                 dbg("cluster offset 0x%llx", next_off);
967
968                 /* get cluster */
969                 buf = get_buffer(id, off + next_off, buf_size);
970                 if (buf == NULL)
971                         goto found;
972
973                 dir = (struct vfat_dir_entry*) buf;
974                 count = buf_size / sizeof(struct vfat_dir_entry);
975                 dbg("expected entries 0x%x", count);
976
977                 for (i = 0; i <= count; i++) {
978                         /* end marker */
979                         if (dir[i].attr == 0x00) {
980                                 dbg("end of dir");
981                                 goto fat32_label;
982                         }
983
984                         /* empty entry */
985                         if (dir[i].attr == 0xe5)
986                                 continue;
987
988                         if (dir[i].attr == FAT_ATTR_VOLUME) {
989                                 dbg("found ATTR_VOLUME id in root dir");
990                                 label = dir[i].name;
991                                 goto fat32_label;
992                         }
993
994                         dbg("skip dir entry");
995                 }
996
997                 /* get FAT entry */
998                 fat_entry_off = (reserved * sector_size) + (next * sizeof(__u32));
999                 buf = get_buffer(id, off + fat_entry_off, buf_size);
1000                 if (buf == NULL)
1001                         goto found;
1002
1003                 /* set next cluster */
1004                 next = le32_to_cpu(*((__u32 *) buf) & 0x0fffffff);
1005                 if (next == 0)
1006                         break;
1007         }
1008         if (maxloop == 0)
1009                 dbg("reached maximum follow count of root cluster chain, give up");
1010
1011 fat32_label:
1012         if (label != NULL && strncmp(label, "NO NAME    ", 11) != 0) {
1013                 set_label_raw(id, label, 11);
1014                 set_label_string(id, label, 11);
1015         } else if (strncmp(vs->type.fat32.label, "NO NAME    ", 11) == 0) {
1016                 set_label_raw(id, vs->type.fat32.label, 11);
1017                 set_label_string(id, vs->type.fat32.label, 11);
1018         }
1019         set_uuid(id, vs->type.fat32.serno, UUID_DCE);
1020
1021 found:
1022         id->usage_id = VOLUME_ID_FILESYSTEM;
1023         id->type_id = VOLUME_ID_VFAT;
1024         id->type = "vfat";
1025
1026         return 0;
1027 }
1028
1029 #define UDF_VSD_OFFSET                  0x8000
1030 static int probe_udf(struct volume_id *id, __u64 off)
1031 {
1032         struct volume_descriptor {
1033                 struct descriptor_tag {
1034                         __u16   id;
1035                         __u16   version;
1036                         __u8    checksum;
1037                         __u8    reserved;
1038                         __u16   serial;
1039                         __u16   crc;
1040                         __u16   crc_len;
1041                         __u32   location;
1042                 } __attribute__((__packed__)) tag;
1043                 union {
1044                         struct anchor_descriptor {
1045                                 __u32   length;
1046                                 __u32   location;
1047                         } __attribute__((__packed__)) anchor;
1048                         struct primary_descriptor {
1049                                 __u32   seq_num;
1050                                 __u32   desc_num;
1051                                 struct dstring {
1052                                         __u8    clen;
1053                                         __u8    c[31];
1054                                 } __attribute__((__packed__)) ident;
1055                         } __attribute__((__packed__)) primary;
1056                 } __attribute__((__packed__)) type;
1057         } __attribute__((__packed__)) *vd;
1058
1059         struct volume_structure_descriptor {
1060                 __u8    type;
1061                 __u8    id[5];
1062                 __u8    version;
1063         } *vsd;
1064
1065         unsigned int bs;
1066         unsigned int b;
1067         unsigned int type;
1068         unsigned int count;
1069         unsigned int loc;
1070         unsigned int clen;
1071
1072         vsd = (struct volume_structure_descriptor *)
1073               get_buffer(id, off + UDF_VSD_OFFSET, 0x200);
1074         if (vsd == NULL)
1075                 return -1;
1076
1077         if (strncmp(vsd->id, "NSR02", 5) == 0)
1078                 goto blocksize;
1079         if (strncmp(vsd->id, "NSR03", 5) == 0)
1080                 goto blocksize;
1081         if (strncmp(vsd->id, "BEA01", 5) == 0)
1082                 goto blocksize;
1083         if (strncmp(vsd->id, "BOOT2", 5) == 0)
1084                 goto blocksize;
1085         if (strncmp(vsd->id, "CD001", 5) == 0)
1086                 goto blocksize;
1087         if (strncmp(vsd->id, "CDW02", 5) == 0)
1088                 goto blocksize;
1089         if (strncmp(vsd->id, "TEA03", 5) == 0)
1090                 goto blocksize;
1091         return -1;
1092
1093 blocksize:
1094         /* search the next VSD to get the logical block size of the volume */
1095         for (bs = 0x800; bs < 0x8000; bs += 0x800) {
1096                 vsd = (struct volume_structure_descriptor *)
1097                       get_buffer(id, off + UDF_VSD_OFFSET + bs, 0x800);
1098                 if (vsd == NULL)
1099                         return -1;
1100                 dbg("test for blocksize: 0x%x", bs);
1101                 if (vsd->id[0] != '\0')
1102                         goto nsr;
1103         }
1104         return -1;
1105
1106 nsr:
1107         /* search the list of VSDs for a NSR descriptor */
1108         for (b = 0; b < 64; b++) {
1109                 vsd = (struct volume_structure_descriptor *)
1110                       get_buffer(id, off + UDF_VSD_OFFSET + (b * bs), 0x800);
1111                 if (vsd == NULL)
1112                         return -1;
1113
1114                 dbg("vsd: %c%c%c%c%c",
1115                     vsd->id[0], vsd->id[1], vsd->id[2], vsd->id[3], vsd->id[4]);
1116
1117                 if (vsd->id[0] == '\0')
1118                         return -1;
1119                 if (strncmp(vsd->id, "NSR02", 5) == 0)
1120                         goto anchor;
1121                 if (strncmp(vsd->id, "NSR03", 5) == 0)
1122                         goto anchor;
1123         }
1124         return -1;
1125
1126 anchor:
1127         /* read anchor volume descriptor */
1128         vd = (struct volume_descriptor *)
1129                 get_buffer(id, off + (256 * bs), 0x200);
1130         if (vd == NULL)
1131                 return -1;
1132
1133         type = le16_to_cpu(vd->tag.id);
1134         if (type != 2) /* TAG_ID_AVDP */
1135                 goto found;
1136
1137         /* get desriptor list address and block count */
1138         count = le32_to_cpu(vd->type.anchor.length) / bs;
1139         loc = le32_to_cpu(vd->type.anchor.location);
1140         dbg("0x%x descriptors starting at logical secor 0x%x", count, loc);
1141
1142         /* pick the primary descriptor from the list */
1143         for (b = 0; b < count; b++) {
1144                 vd = (struct volume_descriptor *)
1145                      get_buffer(id, off + ((loc + b) * bs), 0x200);
1146                 if (vd == NULL)
1147                         return -1;
1148
1149                 type = le16_to_cpu(vd->tag.id);
1150                 dbg("descriptor type %i", type);
1151
1152                 /* check validity */
1153                 if (type == 0)
1154                         goto found;
1155                 if (le32_to_cpu(vd->tag.location) != loc + b)
1156                         goto found;
1157
1158                 if (type == 1) /* TAG_ID_PVD */
1159                         goto pvd;
1160         }
1161         goto found;
1162
1163 pvd:
1164         set_label_raw(id, &(vd->type.primary.ident.clen), 32);
1165
1166         clen = vd->type.primary.ident.clen;
1167         dbg("label string charsize=%i bit", clen);
1168         if (clen == 8)
1169                 set_label_string(id, vd->type.primary.ident.c, 31);
1170         else if (clen == 16)
1171                 set_label_unicode16(id, vd->type.primary.ident.c, BE,31);
1172
1173 found:
1174         id->usage_id = VOLUME_ID_FILESYSTEM;
1175         id->type_id = VOLUME_ID_UDF;
1176         id->type = "udf";
1177
1178         return 0;
1179 }
1180
1181 #define ISO_SUPERBLOCK_OFFSET           0x8000
1182 static int probe_iso9660(struct volume_id *id, __u64 off)
1183 {
1184         union iso_super_block {
1185                 struct iso_header {
1186                         __u8    type;
1187                         __u8    id[5];
1188                         __u8    version;
1189                         __u8    unused1;
1190                         __u8            system_id[32];
1191                         __u8            volume_id[32];
1192                 } __attribute__((__packed__)) iso;
1193                 struct hs_header {
1194                         __u8    foo[8];
1195                         __u8    type;
1196                         __u8    id[4];
1197                         __u8    version;
1198                 } __attribute__((__packed__)) hs;
1199         } __attribute__((__packed__)) *is;
1200
1201         is = (union iso_super_block *)
1202              get_buffer(id, off + ISO_SUPERBLOCK_OFFSET, 0x200);
1203         if (is == NULL)
1204                 return -1;
1205
1206         if (strncmp(is->iso.id, "CD001", 5) == 0) {
1207                 set_label_raw(id, is->iso.volume_id, 32);
1208                 set_label_string(id, is->iso.volume_id, 32);
1209                 goto found;
1210         }
1211         if (strncmp(is->hs.id, "CDROM", 5) == 0)
1212                 goto found;
1213         return -1;
1214
1215 found:
1216         id->usage_id = VOLUME_ID_FILESYSTEM;
1217         id->type_id = VOLUME_ID_ISO9660;
1218         id->type = "iso9660";
1219
1220         return 0;
1221 }
1222
1223 #define UFS_MAGIC                       0x00011954
1224 #define UFS2_MAGIC                      0x19540119
1225 #define UFS_MAGIC_FEA                   0x00195612
1226 #define UFS_MAGIC_LFN                   0x00095014
1227
1228
1229 static int probe_ufs(struct volume_id *id, __u64 off)
1230 {
1231         struct ufs_super_block {
1232                 __u32   fs_link;
1233                 __u32   fs_rlink;
1234                 __u32   fs_sblkno;
1235                 __u32   fs_cblkno;
1236                 __u32   fs_iblkno;
1237                 __u32   fs_dblkno;
1238                 __u32   fs_cgoffset;
1239                 __u32   fs_cgmask;
1240                 __u32   fs_time;
1241                 __u32   fs_size;
1242                 __u32   fs_dsize;
1243                 __u32   fs_ncg; 
1244                 __u32   fs_bsize;
1245                 __u32   fs_fsize;
1246                 __u32   fs_frag;
1247                 __u32   fs_minfree;
1248                 __u32   fs_rotdelay;
1249                 __u32   fs_rps; 
1250                 __u32   fs_bmask;
1251                 __u32   fs_fmask;
1252                 __u32   fs_bshift;
1253                 __u32   fs_fshift;
1254                 __u32   fs_maxcontig;
1255                 __u32   fs_maxbpg;
1256                 __u32   fs_fragshift;
1257                 __u32   fs_fsbtodb;
1258                 __u32   fs_sbsize;
1259                 __u32   fs_csmask;
1260                 __u32   fs_csshift;
1261                 __u32   fs_nindir;
1262                 __u32   fs_inopb;
1263                 __u32   fs_nspf;
1264                 __u32   fs_optim;
1265                 __u32   fs_npsect_state;
1266                 __u32   fs_interleave;
1267                 __u32   fs_trackskew;
1268                 __u32   fs_id[2];
1269                 __u32   fs_csaddr;
1270                 __u32   fs_cssize;
1271                 __u32   fs_cgsize;
1272                 __u32   fs_ntrak;
1273                 __u32   fs_nsect;
1274                 __u32   fs_spc; 
1275                 __u32   fs_ncyl;
1276                 __u32   fs_cpg;
1277                 __u32   fs_ipg;
1278                 __u32   fs_fpg;
1279                 struct ufs_csum {
1280                         __u32   cs_ndir;
1281                         __u32   cs_nbfree;
1282                         __u32   cs_nifree;
1283                         __u32   cs_nffree;
1284                 } __attribute__((__packed__)) fs_cstotal;
1285                 __s8    fs_fmod;
1286                 __s8    fs_clean;
1287                 __s8    fs_ronly;
1288                 __s8    fs_flags;
1289                 union {
1290                         struct {
1291                                 __s8    fs_fsmnt[512];
1292                                 __u32   fs_cgrotor;
1293                                 __u32   fs_csp[31];
1294                                 __u32   fs_maxcluster;
1295                                 __u32   fs_cpc;
1296                                 __u16   fs_opostbl[16][8];
1297                         } __attribute__((__packed__)) fs_u1;
1298                         struct {
1299                                 __s8  fs_fsmnt[468];
1300                                 __u8   fs_volname[32];
1301                                 __u64  fs_swuid;
1302                                 __s32  fs_pad;
1303                                 __u32   fs_cgrotor;
1304                                 __u32   fs_ocsp[28];
1305                                 __u32   fs_contigdirs;
1306                                 __u32   fs_csp; 
1307                                 __u32   fs_maxcluster;
1308                                 __u32   fs_active;
1309                                 __s32   fs_old_cpc;
1310                                 __s32   fs_maxbsize;
1311                                 __s64   fs_sparecon64[17];
1312                                 __s64   fs_sblockloc;
1313                                 struct  ufs2_csum_total {
1314                                         __u64   cs_ndir;
1315                                         __u64   cs_nbfree;
1316                                         __u64   cs_nifree;
1317                                         __u64   cs_nffree;
1318                                         __u64   cs_numclusters;
1319                                         __u64   cs_spare[3];
1320                                 } __attribute__((__packed__)) fs_cstotal;
1321                                 struct  ufs_timeval {
1322                                         __s32   tv_sec;
1323                                         __s32   tv_usec;
1324                                 } __attribute__((__packed__)) fs_time;
1325                                 __s64    fs_size;
1326                                 __s64    fs_dsize;
1327                                 __u64    fs_csaddr;
1328                                 __s64    fs_pendingblocks;
1329                                 __s32    fs_pendinginodes;
1330                         } __attribute__((__packed__)) fs_u2;
1331                 }  fs_u11;
1332                 union {
1333                         struct {
1334                                 __s32   fs_sparecon[53];
1335                                 __s32   fs_reclaim;
1336                                 __s32   fs_sparecon2[1];
1337                                 __s32   fs_state;
1338                                 __u32   fs_qbmask[2];
1339                                 __u32   fs_qfmask[2];
1340                         } __attribute__((__packed__)) fs_sun;
1341                         struct {
1342                                 __s32   fs_sparecon[53];
1343                                 __s32   fs_reclaim;
1344                                 __s32   fs_sparecon2[1];
1345                                 __u32   fs_npsect;
1346                                 __u32   fs_qbmask[2];
1347                                 __u32   fs_qfmask[2];
1348                         } __attribute__((__packed__)) fs_sunx86;
1349                         struct {
1350                                 __s32   fs_sparecon[50];
1351                                 __s32   fs_contigsumsize;
1352                                 __s32   fs_maxsymlinklen;
1353                                 __s32   fs_inodefmt;
1354                                 __u32   fs_maxfilesize[2];
1355                                 __u32   fs_qbmask[2];
1356                                 __u32   fs_qfmask[2];
1357                                 __s32   fs_state;
1358                         } __attribute__((__packed__)) fs_44;
1359                 } fs_u2;
1360                 __s32   fs_postblformat;
1361                 __s32   fs_nrpos;
1362                 __s32   fs_postbloff;
1363                 __s32   fs_rotbloff;
1364                 __u32   fs_magic;
1365                 __u8    fs_space[1];
1366         } __attribute__((__packed__)) *ufs;
1367
1368         __u32   magic;
1369         int     i;
1370         int     offsets[] = {0, 8, 64, 256, -1};
1371
1372         for (i = 0; offsets[i] >= 0; i++) {     
1373                 ufs = (struct ufs_super_block *)
1374                         get_buffer(id, off + (offsets[i] * 0x400), 0x800);
1375                 if (ufs == NULL)
1376                         return -1;
1377
1378                 dbg("offset 0x%x", offsets[i] * 0x400);
1379                 magic = be32_to_cpu(ufs->fs_magic);
1380                 if ((magic == UFS_MAGIC) ||
1381                     (magic == UFS2_MAGIC) ||
1382                     (magic == UFS_MAGIC_FEA) ||
1383                     (magic == UFS_MAGIC_LFN)) {
1384                         dbg("magic 0x%08x(be)", magic);
1385                         goto found;
1386                 }
1387                 magic = le32_to_cpu(ufs->fs_magic);
1388                 if ((magic == UFS_MAGIC) ||
1389                     (magic == UFS2_MAGIC) ||
1390                     (magic == UFS_MAGIC_FEA) ||
1391                     (magic == UFS_MAGIC_LFN)) {
1392                         dbg("magic 0x%08x(le)", magic);
1393                         goto found;
1394                 }
1395         }
1396         return -1;
1397
1398 found:
1399         id->usage_id = VOLUME_ID_FILESYSTEM;
1400         id->type_id = VOLUME_ID_UFS;
1401         id->type = "ufs";
1402
1403         return 0;
1404 }
1405
1406 static int probe_mac_partition_map(struct volume_id *id, __u64 off)
1407 {
1408         struct mac_driver_desc {
1409                 __u8    signature[2];
1410                 __u16   block_size;
1411                 __u32   block_count;
1412         } __attribute__((__packed__)) *driver;
1413
1414         struct mac_partition {
1415                 __u8    signature[2];
1416                 __u16   res1;
1417                 __u32   map_count;
1418                 __u32   start_block;
1419                 __u32   block_count;
1420                 __u8    name[32];
1421                 __u8    type[32];
1422         } __attribute__((__packed__)) *part;
1423
1424         const __u8 *buf;
1425
1426         buf = get_buffer(id, off, 0x200);
1427         if (buf == NULL)
1428                 return -1;
1429
1430         part = (struct mac_partition *) buf;
1431         if ((strncmp(part->signature, "PM", 2) == 0) &&
1432             (strncmp(part->type, "Apple_partition_map", 19) == 0)) {
1433                 /* linux creates an own subdevice for the map
1434                  * just return the type if the drive header is missing */
1435                 id->usage_id = VOLUME_ID_PARTITIONTABLE;
1436                 id->type_id = VOLUME_ID_MACPARTMAP;
1437                 id->type = "mac_partition_map";
1438                 return 0;
1439         }
1440
1441         driver = (struct mac_driver_desc *) buf;
1442         if (strncmp(driver->signature, "ER", 2) == 0) {
1443                 /* we are on a main device, like a CD
1444                  * just try to probe the first partition from the map */
1445                 unsigned int bsize = be16_to_cpu(driver->block_size);
1446                 int part_count;
1447                 int i;
1448
1449                 /* get first entry of partition table */
1450                 buf = get_buffer(id, off +  bsize, 0x200);
1451                 if (buf == NULL)
1452                         return -1;
1453
1454                 part = (struct mac_partition *) buf;
1455                 if (strncmp(part->signature, "PM", 2) != 0)
1456                         return -1;
1457
1458                 part_count = be32_to_cpu(part->map_count);
1459                 dbg("expecting %d partition entries", part_count);
1460
1461                 if (id->partitions != NULL)
1462                         free(id->partitions);
1463                 id->partitions =
1464                         malloc(part_count * sizeof(struct volume_id_partition));
1465                 if (id->partitions == NULL)
1466                         return -1;
1467                 memset(id->partitions, 0x00, sizeof(struct volume_id_partition));
1468
1469                 id->partition_count = part_count;
1470
1471                 for (i = 0; i < part_count; i++) {
1472                         __u64 poff;
1473                         __u64 plen;
1474
1475                         buf = get_buffer(id, off + ((i+1) * bsize), 0x200);
1476                         if (buf == NULL)
1477                                 return -1;
1478
1479                         part = (struct mac_partition *) buf;
1480                         if (strncmp(part->signature, "PM", 2) != 0)
1481                                 return -1;
1482
1483                         poff = be32_to_cpu(part->start_block) * bsize;
1484                         plen = be32_to_cpu(part->block_count) * bsize;
1485                         dbg("found '%s' partition entry at 0x%llx, len 0x%llx",
1486                             part->type, poff, plen);
1487
1488                         id->partitions[i].off = poff;
1489                         id->partitions[i].len = plen;
1490
1491                         if (strncmp(part->type, "Apple_Free", 10) == 0) {
1492                                 id->partitions[i].usage_id = VOLUME_ID_UNUSED;
1493                         } else if (strncmp(part->type, "Apple_partition_map", 19) == 0) {
1494                                 id->partitions[i].usage_id = VOLUME_ID_PARTITIONTABLE;
1495                                 id->partitions[i].type_id = VOLUME_ID_MACPARTMAP;
1496                         } else {
1497                                 id->partitions[i].usage_id = VOLUME_ID_UNPROBED;
1498                         }
1499                 }
1500                 id->usage_id = VOLUME_ID_PARTITIONTABLE;
1501                 id->type_id = VOLUME_ID_MACPARTMAP;
1502                 id->type = "mac_partition_map";
1503                 return 0;
1504         }
1505
1506         return -1;
1507 }
1508
1509 #define HFS_SUPERBLOCK_OFFSET           0x400
1510 #define HFS_NODE_LEAF                   0xff
1511 #define HFSPLUS_POR_CNID                1
1512 #define HFSPLUS_EXTENT_COUNT            8
1513 static int probe_hfs_hfsplus(struct volume_id *id, __u64 off)
1514 {
1515         struct hfs_finder_info{
1516                 __u32   boot_folder;
1517                 __u32   start_app;
1518                 __u32   open_folder;
1519                 __u32   os9_folder;
1520                 __u32   reserved;
1521                 __u32   osx_folder;
1522                 __u8    id[8];
1523         } __attribute__((__packed__));
1524
1525         struct hfs_mdb {
1526                 __u8    signature[2];
1527                 __u32   cr_date;
1528                 __u32   ls_Mod;
1529                 __u16   atrb;
1530                 __u16   nm_fls;
1531                 __u16   vbm_st;
1532                 __u16   alloc_ptr;
1533                 __u16   nm_al_blks;
1534                 __u32   al_blk_size;
1535                 __u32   clp_size;
1536                 __u16   al_bl_st;
1537                 __u32   nxt_cnid;
1538                 __u16   free_bks;
1539                 __u8    label_len;
1540                 __u8    label[27];
1541                 __u32   vol_bkup;
1542                 __u16   vol_seq_num;
1543                 __u32   wr_cnt;
1544                 __u32   xt_clump_size;
1545                 __u32   ct_clump_size;
1546                 __u16   num_root_dirs;
1547                 __u32   file_count;
1548                 __u32   dir_count;
1549                 struct hfs_finder_info finder_info;
1550                 __u8    embed_sig[2];
1551                 __u16   embed_startblock;
1552                 __u16   embed_blockcount;
1553         } __attribute__((__packed__)) *hfs;
1554
1555         struct hfsplus_bnode_descriptor {
1556                 __u32   next;
1557                 __u32   prev;
1558                 __u8    type;
1559                 __u8    height;
1560                 __u16   num_recs;
1561                 __u16   reserved;
1562         } __attribute__((__packed__));
1563
1564         struct hfsplus_bheader_record {
1565                 __u16   depth;
1566                 __u32   root;
1567                 __u32   leaf_count;
1568                 __u32   leaf_head;
1569                 __u32   leaf_tail;
1570                 __u16   node_size;
1571         } __attribute__((__packed__));
1572
1573         struct hfsplus_catalog_key {
1574                 __u16   key_len;
1575                 __u32   parent_id;
1576                 __u16   unicode_len;
1577                 __u8    unicode[255 * 2];
1578         } __attribute__((__packed__));
1579
1580         struct hfsplus_extent {
1581                 __u32 start_block;
1582                 __u32 block_count;
1583         } __attribute__((__packed__));
1584
1585         struct hfsplus_fork {
1586                 __u64 total_size;
1587                 __u32 clump_size;
1588                 __u32 total_blocks;
1589                 struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
1590         } __attribute__((__packed__));
1591
1592         struct hfsplus_vol_header {
1593                 __u8    signature[2];
1594                 __u16   version;
1595                 __u32   attributes;
1596                 __u32   last_mount_vers;
1597                 __u32   reserved;
1598                 __u32   create_date;
1599                 __u32   modify_date;
1600                 __u32   backup_date;
1601                 __u32   checked_date;
1602                 __u32   file_count;
1603                 __u32   folder_count;
1604                 __u32   blocksize;
1605                 __u32   total_blocks;
1606                 __u32   free_blocks;
1607                 __u32   next_alloc;
1608                 __u32   rsrc_clump_sz;
1609                 __u32   data_clump_sz;
1610                 __u32   next_cnid;
1611                 __u32   write_count;
1612                 __u64   encodings_bmp;
1613                 struct hfs_finder_info finder_info;
1614                 struct hfsplus_fork alloc_file;
1615                 struct hfsplus_fork ext_file;
1616                 struct hfsplus_fork cat_file;
1617                 struct hfsplus_fork attr_file;
1618                 struct hfsplus_fork start_file;
1619         } __attribute__((__packed__)) *hfsplus;
1620
1621         unsigned int blocksize;
1622         unsigned int cat_block;
1623         unsigned int ext_block_start;
1624         unsigned int ext_block_count;
1625         int ext;
1626         unsigned int leaf_node_head;
1627         unsigned int leaf_node_count;
1628         unsigned int leaf_node_size;
1629         unsigned int leaf_block;
1630         __u64 leaf_off;
1631         unsigned int alloc_block_size;
1632         unsigned int alloc_first_block;
1633         unsigned int embed_first_block;
1634         unsigned int record_count;
1635         struct hfsplus_bnode_descriptor *descr;
1636         struct hfsplus_bheader_record *bnode;
1637         struct hfsplus_catalog_key *key;
1638         unsigned int    label_len;
1639         struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
1640         const __u8 *buf;
1641
1642         buf = get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
1643         if (buf == NULL)
1644                 return -1;
1645
1646         hfs = (struct hfs_mdb *) buf;
1647         if (strncmp(hfs->signature, "BD", 2) != 0)
1648                 goto checkplus;
1649
1650         /* it may be just a hfs wrapper for hfs+ */
1651         if (strncmp(hfs->embed_sig, "H+", 2) == 0) {
1652                 alloc_block_size = be32_to_cpu(hfs->al_blk_size);
1653                 dbg("alloc_block_size 0x%x", alloc_block_size);
1654
1655                 alloc_first_block = be16_to_cpu(hfs->al_bl_st);
1656                 dbg("alloc_first_block 0x%x", alloc_first_block);
1657
1658                 embed_first_block = be16_to_cpu(hfs->embed_startblock);
1659                 dbg("embed_first_block 0x%x", embed_first_block);
1660
1661                 off += (alloc_first_block * 512) +
1662                        (embed_first_block * alloc_block_size);
1663                 dbg("hfs wrapped hfs+ found at offset 0x%llx", off);
1664
1665                 buf = get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
1666                 if (buf == NULL)
1667                         return -1;
1668                 goto checkplus;
1669         }
1670
1671         if (hfs->label_len > 0 && hfs->label_len < 28) {
1672                 set_label_raw(id, hfs->label, hfs->label_len);
1673                 set_label_string(id, hfs->label, hfs->label_len) ;
1674         }
1675
1676         set_uuid(id, hfs->finder_info.id, UUID_HFS);
1677
1678         id->usage_id = VOLUME_ID_FILESYSTEM;
1679         id->type_id = VOLUME_ID_HFS;
1680         id->type = "hfs";
1681
1682         return 0;
1683
1684 checkplus:
1685         hfsplus = (struct hfsplus_vol_header *) buf;
1686         if (strncmp(hfsplus->signature, "H+", 2) == 0)
1687                 goto hfsplus;
1688         if (strncmp(hfsplus->signature, "HX", 2) == 0)
1689                 goto hfsplus;
1690         return -1;
1691
1692 hfsplus:
1693         set_uuid(id, hfsplus->finder_info.id, UUID_HFS);
1694
1695         blocksize = be32_to_cpu(hfsplus->blocksize);
1696         dbg("blocksize %u", blocksize);
1697
1698         memcpy(extents, hfsplus->cat_file.extents, sizeof(extents));
1699         cat_block = be32_to_cpu(extents[0].start_block);
1700         dbg("catalog start block 0x%x", cat_block);
1701
1702         buf = get_buffer(id, off + (cat_block * blocksize), 0x2000);
1703         if (buf == NULL)
1704                 goto found;
1705
1706         bnode = (struct hfsplus_bheader_record *)
1707                 &buf[sizeof(struct hfsplus_bnode_descriptor)];
1708
1709         leaf_node_head = be32_to_cpu(bnode->leaf_head);
1710         dbg("catalog leaf node 0x%x", leaf_node_head);
1711
1712         leaf_node_size = be16_to_cpu(bnode->node_size);
1713         dbg("leaf node size 0x%x", leaf_node_size);
1714
1715         leaf_node_count = be32_to_cpu(bnode->leaf_count);
1716         dbg("leaf node count 0x%x", leaf_node_count);
1717         if (leaf_node_count == 0)
1718                 goto found;
1719
1720         leaf_block = (leaf_node_head * leaf_node_size) / blocksize;
1721
1722         /* get physical location */
1723         for (ext = 0; ext < HFSPLUS_EXTENT_COUNT; ext++) {
1724                 ext_block_start = be32_to_cpu(extents[ext].start_block);
1725                 ext_block_count = be32_to_cpu(extents[ext].block_count);
1726                 dbg("extent start block 0x%x, count 0x%x", ext_block_start, ext_block_count);
1727
1728                 if (ext_block_count == 0)
1729                         goto found;
1730
1731                 /* this is our extent */
1732                 if (leaf_block < ext_block_count)
1733                         break;
1734
1735                 leaf_block -= ext_block_count;
1736         }
1737         if (ext == HFSPLUS_EXTENT_COUNT)
1738                 goto found;
1739         dbg("found block in extent %i", ext);
1740
1741         leaf_off = (ext_block_start + leaf_block) * blocksize;
1742
1743         buf = get_buffer(id, off + leaf_off, leaf_node_size);
1744         if (buf == NULL)
1745                 goto found;
1746
1747         descr = (struct hfsplus_bnode_descriptor *) buf;
1748         dbg("descriptor type 0x%x", descr->type);
1749
1750         record_count = be16_to_cpu(descr->num_recs);
1751         dbg("number of records %u", record_count);
1752         if (record_count == 0)
1753                 goto found;
1754
1755         if (descr->type != HFS_NODE_LEAF)
1756                 goto found;
1757
1758         key = (struct hfsplus_catalog_key *)
1759                 &buf[sizeof(struct hfsplus_bnode_descriptor)];
1760
1761         dbg("parent id 0x%x", be32_to_cpu(key->parent_id));
1762         if (be32_to_cpu(key->parent_id) != HFSPLUS_POR_CNID)
1763                 goto found;
1764
1765         label_len = be16_to_cpu(key->unicode_len) * 2;
1766         dbg("label unicode16 len %i", label_len);
1767         set_label_raw(id, key->unicode, label_len);
1768         set_label_unicode16(id, key->unicode, BE, label_len);
1769
1770 found:
1771         id->usage_id = VOLUME_ID_FILESYSTEM;
1772         id->type_id = VOLUME_ID_HFSPLUS;
1773         id->type = "hfsplus";
1774
1775         return 0;
1776 }
1777
1778 #define MFT_RECORD_VOLUME                       3
1779 #define MFT_RECORD_ATTR_VOLUME_NAME             0x60
1780 #define MFT_RECORD_ATTR_VOLUME_INFO             0x70
1781 #define MFT_RECORD_ATTR_OBJECT_ID               0x40
1782 #define MFT_RECORD_ATTR_END                     0xffffffffu
1783 static int probe_ntfs(struct volume_id *id, __u64 off)
1784 {
1785         struct ntfs_super_block {
1786                 __u8    jump[3];
1787                 __u8    oem_id[8];
1788                 __u16   bytes_per_sector;
1789                 __u8    sectors_per_cluster;
1790                 __u16   reserved_sectors;
1791                 __u8    fats;
1792                 __u16   root_entries;
1793                 __u16   sectors;
1794                 __u8    media_type;
1795                 __u16   sectors_per_fat;
1796                 __u16   sectors_per_track;
1797                 __u16   heads;
1798                 __u32   hidden_sectors;
1799                 __u32   large_sectors;
1800                 __u16   unused[2];
1801                 __u64   number_of_sectors;
1802                 __u64   mft_cluster_location;
1803                 __u64   mft_mirror_cluster_location;
1804                 __s8    cluster_per_mft_record;
1805                 __u8    reserved1[3];
1806                 __s8    cluster_per_index_record;
1807                 __u8    reserved2[3];
1808                 __u8    volume_serial[8];
1809                 __u16   checksum;
1810         } __attribute__((__packed__)) *ns;
1811
1812         struct master_file_table_record {
1813                 __u8    magic[4];
1814                 __u16   usa_ofs;
1815                 __u16   usa_count;
1816                 __u64   lsn;
1817                 __u16   sequence_number;
1818                 __u16   link_count;
1819                 __u16   attrs_offset;
1820                 __u16   flags;
1821                 __u32   bytes_in_use;
1822                 __u32   bytes_allocated;
1823         } __attribute__((__packed__)) *mftr;
1824
1825         struct file_attribute {
1826                 __u32   type;
1827                 __u32   len;
1828                 __u8    non_resident;
1829                 __u8    name_len;
1830                 __u16   name_offset;
1831                 __u16   flags;
1832                 __u16   instance;
1833                 __u32   value_len;
1834                 __u16   value_offset;
1835         } __attribute__((__packed__)) *attr;
1836
1837         struct volume_info {
1838                 __u64 reserved;
1839                 __u8 major_ver;
1840                 __u8 minor_ver;
1841         } __attribute__((__packed__)) *info;
1842
1843         unsigned int sector_size;
1844         unsigned int cluster_size;
1845         __u64 mft_cluster;
1846         __u64 mft_off;
1847         unsigned int mft_record_size;
1848         unsigned int attr_type;
1849         unsigned int attr_off;
1850         unsigned int attr_len;
1851         unsigned int val_off;
1852         unsigned int val_len;
1853         const __u8 *buf;
1854         const __u8 *val;
1855
1856         ns = (struct ntfs_super_block *) get_buffer(id, off, 0x200);
1857         if (ns == NULL)
1858                 return -1;
1859
1860         if (strncmp(ns->oem_id, "NTFS", 4) != 0)
1861                 return -1;
1862
1863         set_uuid(id, ns->volume_serial, UUID_NTFS);
1864
1865         sector_size = le16_to_cpu(ns->bytes_per_sector);
1866         cluster_size = ns->sectors_per_cluster * sector_size;
1867         mft_cluster = le64_to_cpu(ns->mft_cluster_location);
1868         mft_off = mft_cluster * cluster_size;
1869
1870         if (ns->cluster_per_mft_record < 0)
1871                 /* size = -log2(mft_record_size); normally 1024 Bytes */
1872                 mft_record_size = 1 << -ns->cluster_per_mft_record;
1873         else
1874                 mft_record_size = ns->cluster_per_mft_record * cluster_size;
1875
1876         dbg("sectorsize  0x%x", sector_size);
1877         dbg("clustersize 0x%x", cluster_size);
1878         dbg("mftcluster  %lli", mft_cluster);
1879         dbg("mftoffset  0x%llx", mft_off);
1880         dbg("cluster per mft_record  %i", ns->cluster_per_mft_record);
1881         dbg("mft record size  %i", mft_record_size);
1882
1883         buf = get_buffer(id, off + mft_off + (MFT_RECORD_VOLUME * mft_record_size),
1884                          mft_record_size);
1885         if (buf == NULL)
1886                 goto found;
1887
1888         mftr = (struct master_file_table_record*) buf;
1889
1890         dbg("mftr->magic '%c%c%c%c'", mftr->magic[0], mftr->magic[1], mftr->magic[2], mftr->magic[3]);
1891         if (strncmp(mftr->magic, "FILE", 4) != 0)
1892                 goto found;
1893
1894         attr_off = le16_to_cpu(mftr->attrs_offset);
1895         dbg("file $Volume's attributes are at offset %i", attr_off);
1896
1897         while (1) {
1898                 attr = (struct file_attribute*) &buf[attr_off];
1899                 attr_type = le32_to_cpu(attr->type);
1900                 attr_len = le16_to_cpu(attr->len);
1901                 val_off = le16_to_cpu(attr->value_offset);
1902                 val_len = le32_to_cpu(attr->value_len);
1903                 attr_off += attr_len;
1904
1905                 if (attr_len == 0)
1906                         break;
1907
1908                 if (attr_off >= mft_record_size)
1909                         break;
1910
1911                 if (attr_type == MFT_RECORD_ATTR_END)
1912                         break;
1913
1914                 dbg("found attribute type 0x%x, len %i, at offset %i",
1915                     attr_type, attr_len, attr_off);
1916
1917                 if (attr_type == MFT_RECORD_ATTR_VOLUME_INFO) {
1918                         dbg("found info, len %i", val_len);
1919                         info = (struct volume_info*) (((__u8 *) attr) + val_off);
1920                         snprintf(id->type_version, VOLUME_ID_FORMAT_SIZE-1,
1921                                  "%u.%u", info->major_ver, info->minor_ver);
1922                 }
1923
1924                 if (attr_type == MFT_RECORD_ATTR_VOLUME_NAME) {
1925                         dbg("found label, len %i", val_len);
1926                         if (val_len > VOLUME_ID_LABEL_SIZE)
1927                                 val_len = VOLUME_ID_LABEL_SIZE;
1928
1929                         val = ((__u8 *) attr) + val_off;
1930                         set_label_raw(id, val, val_len);
1931                         set_label_unicode16(id, val, LE, val_len);
1932                 }
1933         }
1934
1935 found:
1936         id->usage_id = VOLUME_ID_FILESYSTEM;
1937         id->type_id = VOLUME_ID_NTFS;
1938         id->type = "ntfs";
1939
1940         return 0;
1941 }
1942
1943 #define LARGEST_PAGESIZE                        0x4000
1944 static int probe_swap(struct volume_id *id, __u64 off)
1945 {
1946         const __u8 *sig;
1947         unsigned int page;
1948
1949         /* huhh, the swap signature is on the end of the PAGE_SIZE */
1950         for (page = 0x1000; page <= LARGEST_PAGESIZE; page <<= 1) {
1951                         sig = get_buffer(id, off + page-10, 10);
1952                         if (sig == NULL)
1953                                 return -1;
1954
1955                         if (strncmp(sig, "SWAP-SPACE", 10) == 0) {
1956                                 strcpy(id->type_version, "1");
1957                                 goto found;
1958                         }
1959                         if (strncmp(sig, "SWAPSPACE2", 10) == 0) {
1960                                 strcpy(id->type_version, "2");
1961                                 goto found;
1962                         }
1963         }
1964         return -1;
1965
1966 found:
1967         id->usage_id = VOLUME_ID_OTHER;
1968         id->type_id = VOLUME_ID_SWAP;
1969         id->type = "swap";
1970
1971         return 0;
1972 }
1973
1974 /* probe volume for filesystem type and try to read label+uuid */
1975 int volume_id_probe(struct volume_id *id,
1976                     enum volume_id_type type,
1977                     unsigned long long off,
1978                     unsigned long long size)
1979 {
1980         int rc;
1981
1982         if (id == NULL)
1983                 return -EINVAL;
1984
1985         switch (type) {
1986         case VOLUME_ID_MSDOSPARTTABLE:
1987                 rc = probe_msdos_part_table(id, off);
1988                 break;
1989         case VOLUME_ID_EXT3:
1990         case VOLUME_ID_EXT2:
1991                 rc = probe_ext(id, off);
1992                 break;
1993         case VOLUME_ID_REISERFS:
1994                 rc = probe_reiserfs(id, off);
1995                 break;
1996         case VOLUME_ID_XFS:
1997                 rc = probe_xfs(id, off);
1998                 break;
1999         case VOLUME_ID_JFS:
2000                 rc = probe_jfs(id, off);
2001                 break;
2002         case VOLUME_ID_VFAT:
2003                 rc = probe_vfat(id, off);
2004                 break;
2005         case VOLUME_ID_UDF:
2006                 rc = probe_udf(id, off);
2007                 break;
2008         case VOLUME_ID_ISO9660:
2009                 rc = probe_iso9660(id, off);
2010                 break;
2011         case VOLUME_ID_MACPARTMAP:
2012                 rc = probe_mac_partition_map(id, off);
2013                 break;
2014         case VOLUME_ID_HFS:
2015         case VOLUME_ID_HFSPLUS:
2016                 rc = probe_hfs_hfsplus(id, off);
2017                 break;
2018         case VOLUME_ID_UFS:
2019                 rc = probe_ufs(id, off);
2020                 break;
2021         case VOLUME_ID_NTFS:
2022                 rc = probe_ntfs(id, off);
2023                 break;
2024         case VOLUME_ID_SWAP:
2025                 rc = probe_swap(id, off);
2026                 break;
2027         case VOLUME_ID_LINUX_RAID:
2028                 rc = probe_linux_raid(id, off, size);
2029                 break;
2030         case VOLUME_ID_LVM1:
2031                 rc = probe_lvm1(id, off);
2032                 break;
2033         case VOLUME_ID_LVM2:
2034                 rc = probe_lvm2(id, off);
2035                 break;
2036         case VOLUME_ID_ALL:
2037         default:
2038                 rc = probe_linux_raid(id, off, size);
2039                 if (rc == 0)
2040                         break;
2041
2042                 /* signature in the first block */
2043                 rc = probe_ntfs(id, off);
2044                 if (rc == 0)
2045                         break;
2046                 rc = probe_vfat(id, off);
2047                 if (rc == 0)
2048                         break;
2049                 rc = probe_msdos_part_table(id, off);
2050                 if (rc == 0)
2051                         break;
2052                 rc = probe_mac_partition_map(id, off);
2053                 if (rc == 0)
2054                         break;
2055                 rc = probe_xfs(id, off);
2056                 if (rc == 0)
2057                         break;
2058
2059                 /* fill buffer with maximum */
2060                 get_buffer(id, 0, SB_BUFFER_SIZE);
2061
2062                 rc = probe_swap(id, off);
2063                 if (rc == 0)
2064                         break;
2065                 rc = probe_ext(id, off);
2066                 if (rc == 0)
2067                         break;
2068                 rc = probe_reiserfs(id, off);
2069                 if (rc == 0)
2070                         break;
2071                 rc = probe_jfs(id, off);
2072                 if (rc == 0)
2073                         break;
2074                 rc = probe_udf(id, off);
2075                 if (rc == 0)
2076                         break;
2077                 rc = probe_iso9660(id, off);
2078                 if (rc == 0)
2079                         break;
2080                 rc = probe_hfs_hfsplus(id, off);
2081                 if (rc == 0)
2082                         break;
2083                 rc = probe_ufs(id, off);
2084                 if (rc == 0)
2085                         break;
2086                 rc = probe_lvm1(id, off);
2087                 if (rc == 0)
2088                         break;
2089                 rc = probe_lvm2(id, off);
2090                 if (rc == 0)
2091                         break;
2092
2093                 rc = -1;
2094         }
2095
2096         /* If the filestystem in recognized, we free the allocated buffers,
2097            otherwise they will stay in place for the possible next probe call */
2098         if (rc == 0)
2099                 free_buffer(id);
2100
2101         return rc;
2102 }
2103
2104 /* open volume by already open file descriptor */
2105 struct volume_id *volume_id_open_fd(int fd)
2106 {
2107         struct volume_id *id;
2108
2109         id = malloc(sizeof(struct volume_id));
2110         if (id == NULL)
2111                 return NULL;
2112         memset(id, 0x00, sizeof(struct volume_id));
2113
2114         id->fd = fd;
2115
2116         return id;
2117 }
2118
2119 /* open volume by device node */
2120 struct volume_id *volume_id_open_node(const char *path)
2121 {
2122         struct volume_id *id;
2123         int fd;
2124
2125         fd = open(path, O_RDONLY | O_NONBLOCK);
2126         if (fd < 0) {
2127                 dbg("unable to open '%s'", path);
2128                 return NULL;
2129         }
2130
2131         id = volume_id_open_fd(fd);
2132         if (id == NULL)
2133                 return NULL;
2134
2135         /* close fd on device close */
2136         id->fd_close = 1;
2137
2138         return id;
2139 }
2140
2141 /* open volume by major/minor */
2142 struct volume_id *volume_id_open_dev_t(dev_t devt)
2143 {
2144         struct volume_id *id;
2145         __u8 tmp_node[VOLUME_ID_PATH_MAX];
2146
2147         snprintf(tmp_node, VOLUME_ID_PATH_MAX,
2148                  "/tmp/volume-%u-%u-%u", getpid(), major(devt), minor(devt));
2149         tmp_node[VOLUME_ID_PATH_MAX] = '\0';
2150
2151         /* create tempory node to open the block device */
2152         unlink(tmp_node);
2153         if (mknod(tmp_node, (S_IFBLK | 0600), devt) != 0)
2154                 return NULL;
2155
2156         id = volume_id_open_node(tmp_node);
2157
2158         unlink(tmp_node);
2159
2160         return id;
2161 }
2162
2163 /* free allocated volume info */
2164 void volume_id_close(struct volume_id *id)
2165 {
2166         if (id == NULL)
2167                 return;
2168
2169         if (id->fd_close != 0)
2170                 close(id->fd);
2171
2172         free_buffer(id);
2173
2174         if (id->partitions != NULL)
2175                 free(id->partitions);
2176
2177         free(id);
2178 }