chiark / gitweb /
[PATCH] volume_id: Fix label/uuid reading for reiserfs
[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 library is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU Lesser General Public
9  *      License as published by the Free Software Foundation; either
10  *      version 2.1 of the License, or (at your option) any later version.
11  *
12  *      This library is distributed in the hope that it will be useful,
13  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  *      Lesser General Public License for more details.
16  *
17  *      You should have received a copy of the GNU Lesser General Public
18  *      License along with this library; if not, write to the Free Software
19  *      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21
22 #ifndef _GNU_SOURCE
23 #define _GNU_SOURCE 1
24 #endif
25
26 #ifdef HAVE_CONFIG_H
27 #  include <config.h>
28 #endif
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <ctype.h>
36 #include <asm/types.h>
37
38 #include "volume_id.h"
39 #include "logging.h"
40 #include "util.h"
41 #include "reiserfs.h"
42
43 struct reiserfs_super_block {
44         __u32   blocks_count;
45         __u32   free_blocks;
46         __u32   root_block;
47         __u32   journal_block;
48         __u32   journal_dev;
49         __u32   orig_journal_size;
50         __u32   dummy2[5];
51         __u16   blocksize;
52         __u16   dummy3[3];
53         __u8    magic[12];
54         __u32   dummy4[5];
55         __u8    uuid[16];
56         __u8    label[16];
57 } __attribute__((__packed__));
58
59 struct reiser4_super_block {
60         __u8    magic[16];
61         __u16   dummy[2];
62         __u8    uuid[16];
63         __u8    label[16];
64         __u64   dummy2;
65 } __attribute__((__packed__));
66
67 #define REISERFS1_SUPERBLOCK_OFFSET             0x2000
68 #define REISERFS_SUPERBLOCK_OFFSET              0x10000
69
70 int volume_id_probe_reiserfs(struct volume_id *id, __u64 off)
71 {
72         struct reiserfs_super_block *rs;
73         struct reiser4_super_block *rs4;
74         __u8 *buf;
75
76         dbg("probing at offset 0x%llx", (unsigned long long) off);
77
78         buf = volume_id_get_buffer(id, off + REISERFS_SUPERBLOCK_OFFSET, 0x200);
79         if (buf == NULL)
80                 return -1;
81
82         rs = (struct reiserfs_super_block *) buf;;
83         if (memcmp(rs->magic, "ReIsErFs", 8) == 0) {
84                 strcpy(id->type_version, "3.5");
85                 goto found;
86         }
87         if (memcmp(rs->magic, "ReIsEr2Fs", 9) == 0) {
88                 strcpy(id->type_version, "3.6");
89                 goto found_v3;
90         }
91         if (memcmp(rs->magic, "ReIsEr3Fs", 9) == 0) {
92                 strcpy(id->type_version, "JR");
93                 goto found_v3;
94         }
95
96         rs4 = (struct reiser4_super_block *) buf;
97         if (memcmp(rs4->magic, "ReIsEr4", 7) == 0) {
98                 strcpy(id->type_version, "4");
99                 volume_id_set_label_raw(id, rs4->label, 16);
100                 volume_id_set_label_string(id, rs4->label, 16);
101                 volume_id_set_uuid(id, rs4->uuid, UUID_DCE);
102                 goto found;
103         }
104
105         rs = (struct reiserfs_super_block *) volume_id_get_buffer(id, off + REISERFS1_SUPERBLOCK_OFFSET, 0x200);
106         if (rs == NULL)
107                 return -1;
108
109         if (memcmp(rs->magic, "ReIsErFs", 8) == 0) {
110                 strcpy(id->type_version, "3.5");
111                 goto found;
112         }
113
114         return -1;
115
116 found_v3:
117         volume_id_set_label_raw(id, rs->label, 16);
118         volume_id_set_label_string(id, rs->label, 16);
119         volume_id_set_uuid(id, rs->uuid, UUID_DCE);
120
121 found:
122         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
123         id->type = "reiserfs";
124
125         return 0;
126 }