chiark / gitweb /
forgot the ChangeLog for 074
[elogind.git] / extras / volume_id / volume_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 #include "ext.h"
30
31 struct ext2_super_block {
32         uint32_t        s_inodes_count;
33         uint32_t        s_blocks_count;
34         uint32_t        s_r_blocks_count;
35         uint32_t        s_free_blocks_count;
36         uint32_t        s_free_inodes_count;
37         uint32_t        s_first_data_block;
38         uint32_t        s_log_block_size;
39         uint32_t        s_log_frag_size;
40         uint32_t        s_blocks_per_group;
41         uint32_t        s_frags_per_group;
42         uint32_t        s_inodes_per_group;
43         uint32_t        s_mtime;
44         uint32_t        s_wtime;
45         uint16_t        s_mnt_count;
46         uint16_t        s_max_mnt_count;
47         uint16_t        s_magic;
48         uint16_t        s_state;
49         uint16_t        s_errors;
50         uint16_t        s_minor_rev_level;
51         uint32_t        s_lastcheck;
52         uint32_t        s_checkinterval;
53         uint32_t        s_creator_os;
54         uint32_t        s_rev_level;
55         uint16_t        s_def_resuid;
56         uint16_t        s_def_resgid;
57         uint32_t        s_first_ino;
58         uint16_t        s_inode_size;
59         uint16_t        s_block_group_nr;
60         uint32_t        s_feature_compat;
61         uint32_t        s_feature_incompat;
62         uint32_t        s_feature_ro_compat;
63         uint8_t         s_uuid[16];
64         uint8_t         s_volume_name[16];
65 } __attribute__((__packed__));
66
67 #define EXT_SUPER_MAGIC                         0xEF53
68 #define EXT3_FEATURE_COMPAT_HAS_JOURNAL         0x00000004
69 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV       0x00000008
70 #define EXT_SUPERBLOCK_OFFSET                   0x400
71
72 #define EXT3_MIN_BLOCK_SIZE                     0x400
73 #define EXT3_MAX_BLOCK_SIZE                     0x1000
74
75 int volume_id_probe_ext(struct volume_id *id, uint64_t off)
76 {
77         struct ext2_super_block *es;
78         size_t bsize;
79
80         dbg("probing at offset 0x%llx", (unsigned long long) off);
81
82         es = (struct ext2_super_block *) volume_id_get_buffer(id, off + EXT_SUPERBLOCK_OFFSET, 0x200);
83         if (es == NULL)
84                 return -1;
85
86         if (es->s_magic != cpu_to_le16(EXT_SUPER_MAGIC))
87                 return -1;
88
89         bsize = 0x400 << le32_to_cpu(es->s_log_block_size);
90         dbg("ext blocksize 0x%zx", bsize);
91         if (bsize < EXT3_MIN_BLOCK_SIZE || bsize > EXT3_MAX_BLOCK_SIZE) {
92                 dbg("invalid ext blocksize");
93                 return -1;
94         }
95
96         volume_id_set_label_raw(id, es->s_volume_name, 16);
97         volume_id_set_label_string(id, es->s_volume_name, 16);
98         volume_id_set_uuid(id, es->s_uuid, UUID_DCE);
99         snprintf(id->type_version, sizeof(id->type_version)-1,
100                  "%u.%u", es->s_rev_level, es->s_minor_rev_level);
101
102         /* check for external journal device */
103         if ((le32_to_cpu(es->s_feature_incompat) & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) != 0) {
104                 volume_id_set_usage(id, VOLUME_ID_OTHER);
105                 id->type = "jbd";
106                 return 0;
107         }
108
109         /* check for ext2 / ext3 */
110         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
111         if ((le32_to_cpu(es->s_feature_compat) & EXT3_FEATURE_COMPAT_HAS_JOURNAL) != 0)
112                 id->type = "ext3";
113         else
114                 id->type = "ext2";
115
116         return 0;
117 }