chiark / gitweb /
forgot the ChangeLog for 074
[elogind.git] / extras / volume_id / volume_id / reiserfs.c
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
5  * Copyright (C) 2005 Tobias Klauser <tklauser@access.unizh.ch>
6  *
7  *      This program is free software; you can redistribute it and/or modify it
8  *      under the terms of the GNU General Public License as published by the
9  *      Free Software Foundation version 2 of the License.
10  */
11
12 #ifndef _GNU_SOURCE
13 #define _GNU_SOURCE 1
14 #endif
15
16 #ifdef HAVE_CONFIG_H
17 #  include <config.h>
18 #endif
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <ctype.h>
26
27 #include "volume_id.h"
28 #include "logging.h"
29 #include "util.h"
30 #include "reiserfs.h"
31
32 struct reiserfs_super_block {
33         uint32_t        blocks_count;
34         uint32_t        free_blocks;
35         uint32_t        root_block;
36         uint32_t        journal_block;
37         uint32_t        journal_dev;
38         uint32_t        orig_journal_size;
39         uint32_t        dummy2[5];
40         uint16_t        blocksize;
41         uint16_t        dummy3[3];
42         uint8_t         magic[12];
43         uint32_t        dummy4[5];
44         uint8_t         uuid[16];
45         uint8_t         label[16];
46 } __attribute__((__packed__));
47
48 struct reiser4_super_block {
49         uint8_t         magic[16];
50         uint16_t        dummy[2];
51         uint8_t         uuid[16];
52         uint8_t         label[16];
53         uint64_t        dummy2;
54 } __attribute__((__packed__));
55
56 #define REISERFS1_SUPERBLOCK_OFFSET             0x2000
57 #define REISERFS_SUPERBLOCK_OFFSET              0x10000
58
59 int volume_id_probe_reiserfs(struct volume_id *id, uint64_t off)
60 {
61         struct reiserfs_super_block *rs;
62         struct reiser4_super_block *rs4;
63         uint8_t  *buf;
64
65         dbg("probing at offset 0x%llx", (unsigned long long) off);
66
67         buf = volume_id_get_buffer(id, off + REISERFS_SUPERBLOCK_OFFSET, 0x200);
68         if (buf == NULL)
69                 return -1;
70
71         rs = (struct reiserfs_super_block *) buf;;
72         if (memcmp(rs->magic, "ReIsErFs", 8) == 0) {
73                 strcpy(id->type_version, "3.5");
74                 id->type = "reiserfs";
75                 goto found;
76         }
77         if (memcmp(rs->magic, "ReIsEr2Fs", 9) == 0) {
78                 strcpy(id->type_version, "3.6");
79                 id->type = "reiserfs";
80                 goto found_label;
81         }
82         if (memcmp(rs->magic, "ReIsEr3Fs", 9) == 0) {
83                 strcpy(id->type_version, "JR");
84                 id->type = "reiserfs";
85                 goto found_label;
86         }
87
88         rs4 = (struct reiser4_super_block *) buf;
89         if (memcmp(rs4->magic, "ReIsEr4", 7) == 0) {
90                 strcpy(id->type_version, "4");
91                 volume_id_set_label_raw(id, rs4->label, 16);
92                 volume_id_set_label_string(id, rs4->label, 16);
93                 volume_id_set_uuid(id, rs4->uuid, UUID_DCE);
94                 id->type = "reiser4";
95                 goto found;
96         }
97
98         buf = volume_id_get_buffer(id, off + REISERFS1_SUPERBLOCK_OFFSET, 0x200);
99         if (buf == NULL)
100                 return -1;
101
102         rs = (struct reiserfs_super_block *) buf;
103         if (memcmp(rs->magic, "ReIsErFs", 8) == 0) {
104                 strcpy(id->type_version, "3.5");
105                 id->type = "reiserfs";
106                 goto found;
107         }
108
109         return -1;
110
111 found_label:
112         volume_id_set_label_raw(id, rs->label, 16);
113         volume_id_set_label_string(id, rs->label, 16);
114         volume_id_set_uuid(id, rs->uuid, UUID_DCE);
115
116 found:
117         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
118
119         return 0;
120 }