chiark / gitweb /
Merge branch 'master' of gregkh@master.kernel.org:/pub/scm/linux/hotplug/udev
[elogind.git] / extras / volume_id / libvolume_id / ext.c
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  */
10
11 #ifndef _GNU_SOURCE
12 #define _GNU_SOURCE 1
13 #endif
14
15 #ifdef HAVE_CONFIG_H
16 #  include <config.h>
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <ctype.h>
25
26 #include "volume_id.h"
27 #include "util.h"
28 #include "logging.h"
29
30 struct ext2_super_block {
31         uint32_t        s_inodes_count;
32         uint32_t        s_blocks_count;
33         uint32_t        s_r_blocks_count;
34         uint32_t        s_free_blocks_count;
35         uint32_t        s_free_inodes_count;
36         uint32_t        s_first_data_block;
37         uint32_t        s_log_block_size;
38         uint32_t        s_log_frag_size;
39         uint32_t        s_blocks_per_group;
40         uint32_t        s_frags_per_group;
41         uint32_t        s_inodes_per_group;
42         uint32_t        s_mtime;
43         uint32_t        s_wtime;
44         uint16_t        s_mnt_count;
45         uint16_t        s_max_mnt_count;
46         uint16_t        s_magic;
47         uint16_t        s_state;
48         uint16_t        s_errors;
49         uint16_t        s_minor_rev_level;
50         uint32_t        s_lastcheck;
51         uint32_t        s_checkinterval;
52         uint32_t        s_creator_os;
53         uint32_t        s_rev_level;
54         uint16_t        s_def_resuid;
55         uint16_t        s_def_resgid;
56         uint32_t        s_first_ino;
57         uint16_t        s_inode_size;
58         uint16_t        s_block_group_nr;
59         uint32_t        s_feature_compat;
60         uint32_t        s_feature_incompat;
61         uint32_t        s_feature_ro_compat;
62         uint8_t         s_uuid[16];
63         uint8_t         s_volume_name[16];
64 } __attribute__((__packed__));
65
66 #define EXT_SUPER_MAGIC                         0xEF53
67 #define EXT3_FEATURE_COMPAT_HAS_JOURNAL         0x00000004
68 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV       0x00000008
69 #define EXT_SUPERBLOCK_OFFSET                   0x400
70
71 #define EXT3_MIN_BLOCK_SIZE                     0x400
72 #define EXT3_MAX_BLOCK_SIZE                     0x1000
73
74 int volume_id_probe_ext(struct volume_id *id, uint64_t off)
75 {
76         struct ext2_super_block *es;
77         size_t bsize;
78
79         dbg("probing at offset 0x%llx", (unsigned long long) off);
80
81         es = (struct ext2_super_block *) volume_id_get_buffer(id, off + EXT_SUPERBLOCK_OFFSET, 0x200);
82         if (es == NULL)
83                 return -1;
84
85         if (es->s_magic != cpu_to_le16(EXT_SUPER_MAGIC))
86                 return -1;
87
88         bsize = 0x400 << le32_to_cpu(es->s_log_block_size);
89         dbg("ext blocksize 0x%zx", bsize);
90         if (bsize < EXT3_MIN_BLOCK_SIZE || bsize > EXT3_MAX_BLOCK_SIZE) {
91                 dbg("invalid ext blocksize");
92                 return -1;
93         }
94
95         volume_id_set_label_raw(id, es->s_volume_name, 16);
96         volume_id_set_label_string(id, es->s_volume_name, 16);
97         volume_id_set_uuid(id, es->s_uuid, UUID_DCE);
98         snprintf(id->type_version, sizeof(id->type_version)-1,
99                  "%u.%u", es->s_rev_level, es->s_minor_rev_level);
100
101         /* check for external journal device */
102         if ((le32_to_cpu(es->s_feature_incompat) & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) != 0) {
103                 volume_id_set_usage(id, VOLUME_ID_OTHER);
104                 id->type = "jbd";
105                 return 0;
106         }
107
108         /* check for ext2 / ext3 */
109         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
110         if ((le32_to_cpu(es->s_feature_compat) & EXT3_FEATURE_COMPAT_HAS_JOURNAL) != 0)
111                 id->type = "ext3";
112         else
113                 id->type = "ext2";
114
115         return 0;
116 }