chiark / gitweb /
update file headers
[elogind.git] / extras / volume_id / lib / gfs.c
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2006 Red Hat, Inc. <redhat.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef _GNU_SOURCE
21 #define _GNU_SOURCE 1
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <ctype.h>
30
31 #include "libvolume_id.h"
32 #include "libvolume_id-private.h"
33
34 /* Common gfs/gfs2 constants: */
35 #define GFS_MAGIC               0x01161970
36 #define GFS_DEFAULT_BSIZE       4096
37 #define GFS_SUPERBLOCK_OFFSET   (0x10 * GFS_DEFAULT_BSIZE)
38 #define GFS_METATYPE_SB         1
39 #define GFS_FORMAT_SB           100
40 #define GFS_LOCKNAME_LEN        64
41
42 /* gfs1 constants: */
43 #define GFS_FORMAT_FS           1309
44 #define GFS_FORMAT_MULTI        1401
45 /* gfs2 constants: */
46 #define GFS2_FORMAT_FS          1801
47 #define GFS2_FORMAT_MULTI       1900
48
49 struct gfs2_meta_header {
50         uint32_t mh_magic;
51         uint32_t mh_type;
52         uint64_t __pad0;          /* Was generation number in gfs1 */
53         uint32_t mh_format;
54         uint32_t __pad1;          /* Was incarnation number in gfs1 */
55 };
56
57 struct gfs2_inum {
58         uint64_t no_formal_ino;
59         uint64_t no_addr;
60 };
61
62 struct gfs2_sb {
63         struct gfs2_meta_header sb_header;
64
65         uint32_t sb_fs_format;
66         uint32_t sb_multihost_format;
67         uint32_t  __pad0;  /* Was superblock flags in gfs1 */
68
69         uint32_t sb_bsize;
70         uint32_t sb_bsize_shift;
71         uint32_t __pad1;   /* Was journal segment size in gfs1 */
72
73         struct gfs2_inum sb_master_dir; /* Was jindex dinode in gfs1 */
74         struct gfs2_inum __pad2; /* Was rindex dinode in gfs1 */
75         struct gfs2_inum sb_root_dir;
76
77         char sb_lockproto[GFS_LOCKNAME_LEN];
78         char sb_locktable[GFS_LOCKNAME_LEN];
79         /* In gfs1, quota and license dinodes followed */
80 } PACKED;
81
82 static int volume_id_probe_gfs_generic(struct volume_id *id, uint64_t off, int vers)
83 {
84         struct gfs2_sb *sbd;
85
86         info("probing at offset 0x%llx\n", (unsigned long long) off);
87
88         sbd = (struct gfs2_sb *)
89                 volume_id_get_buffer(id, off + GFS_SUPERBLOCK_OFFSET, sizeof(struct gfs2_sb));
90         if (sbd == NULL)
91                 return -1;
92
93         if (be32_to_cpu(sbd->sb_header.mh_magic) == GFS_MAGIC &&
94                 be32_to_cpu(sbd->sb_header.mh_type) == GFS_METATYPE_SB) {
95                 if (vers == 1) {
96                         if (be32_to_cpu(sbd->sb_fs_format) != GFS_FORMAT_FS ||
97                                 be32_to_cpu(sbd->sb_multihost_format) != GFS_FORMAT_MULTI)
98                                 return -1; /* not gfs1 */
99                         id->type = "gfs";
100                 }
101                 else if (vers == 2) {
102                         if (be32_to_cpu(sbd->sb_fs_format) != GFS2_FORMAT_FS ||
103                                 be32_to_cpu(sbd->sb_multihost_format) != GFS2_FORMAT_MULTI)
104                                 return -1; /* not gfs2 */
105                         id->type = "gfs2";
106                 }
107                 else
108                         return -1;
109
110                 if (strlen(sbd->sb_locktable)) {
111                         uint8_t *label = (uint8_t *) sbd->sb_locktable;
112
113                         volume_id_set_label_raw(id, label, GFS_LOCKNAME_LEN);
114                         volume_id_set_label_string(id, label, GFS_LOCKNAME_LEN);
115                 }
116                 strcpy(id->type_version, "1");
117                 volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
118                 return 0;
119         }
120         return -1;
121 }
122
123 int volume_id_probe_gfs(struct volume_id *id, uint64_t off, uint64_t size)
124 {
125         return volume_id_probe_gfs_generic(id, off, 1);
126 }
127
128 int volume_id_probe_gfs2(struct volume_id *id, uint64_t off, uint64_t size)
129 {
130         return volume_id_probe_gfs_generic(id, off, 2);
131 }