chiark / gitweb /
volume_id: enable GFS probing code, add LABEL support
[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 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 "libvolume_id.h"
27 #include "util.h"
28
29 /* Common gfs/gfs2 constants: */
30 #define GFS_MAGIC               0x01161970
31 #define GFS_DEFAULT_BSIZE       4096
32 #define GFS_SUPERBLOCK_OFFSET   (0x10 * GFS_DEFAULT_BSIZE)
33 #define GFS_METATYPE_SB         1
34 #define GFS_FORMAT_SB           100
35 #define GFS_LOCKNAME_LEN        64
36
37 /* gfs1 constants: */
38 #define GFS_FORMAT_FS           1309
39 #define GFS_FORMAT_MULTI        1401
40 /* gfs2 constants: */
41 #define GFS2_FORMAT_FS          1801
42 #define GFS2_FORMAT_MULTI       1900
43
44 struct gfs2_meta_header {
45         uint32_t mh_magic;
46         uint32_t mh_type;
47         uint64_t __pad0;          /* Was generation number in gfs1 */
48         uint32_t mh_format;
49         uint32_t __pad1;          /* Was incarnation number in gfs1 */
50 };
51
52 struct gfs2_inum {
53         uint64_t no_formal_ino;
54         uint64_t no_addr;
55 };
56
57 struct gfs2_sb {
58         struct gfs2_meta_header sb_header;
59
60         uint32_t sb_fs_format;
61         uint32_t sb_multihost_format;
62         uint32_t  __pad0;  /* Was superblock flags in gfs1 */
63
64         uint32_t sb_bsize;
65         uint32_t sb_bsize_shift;
66         uint32_t __pad1;   /* Was journal segment size in gfs1 */
67
68         struct gfs2_inum sb_master_dir; /* Was jindex dinode in gfs1 */
69         struct gfs2_inum __pad2; /* Was rindex dinode in gfs1 */
70         struct gfs2_inum sb_root_dir;
71
72         char sb_lockproto[GFS_LOCKNAME_LEN];
73         char sb_locktable[GFS_LOCKNAME_LEN];
74         /* In gfs1, quota and license dinodes followed */
75 } PACKED;
76
77 static int volume_id_probe_gfs_generic(struct volume_id *id, uint64_t off, int vers)
78 {
79         struct gfs2_sb *sbd;
80
81         info("probing at offset 0x%llx\n", (unsigned long long) off);
82
83         sbd = (struct gfs2_sb *)
84                 volume_id_get_buffer(id, off + GFS_SUPERBLOCK_OFFSET, sizeof(struct gfs2_sb));
85         if (sbd == NULL)
86                 return -1;
87
88         if (be32_to_cpu(sbd->sb_header.mh_magic) == GFS_MAGIC &&
89                 be32_to_cpu(sbd->sb_header.mh_type) == GFS_METATYPE_SB) {
90                 if (vers == 1) {
91                         if (be32_to_cpu(sbd->sb_fs_format) != GFS_FORMAT_FS ||
92                                 be32_to_cpu(sbd->sb_multihost_format) != GFS_FORMAT_MULTI)
93                                 return -1; /* not gfs1 */
94                         id->type = "gfs";
95                 }
96                 else if (vers == 2) {
97                         if (be32_to_cpu(sbd->sb_fs_format) != GFS2_FORMAT_FS ||
98                                 be32_to_cpu(sbd->sb_multihost_format) != GFS2_FORMAT_MULTI)
99                                 return -1; /* not gfs2 */
100                         id->type = "gfs2";
101                 }
102                 else
103                         return -1;
104
105                 if (strlen(sbd->sb_locktable)) {
106                         uint8_t *label = (uint8_t *) sbd->sb_locktable;
107
108                         volume_id_set_label_raw(id, label, GFS_LOCKNAME_LEN);
109                         volume_id_set_label_string(id, label, GFS_LOCKNAME_LEN);
110                 }
111                 strcpy(id->type_version, "1");
112                 volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
113                 return 0;
114         }
115         return -1;
116 }
117
118 int volume_id_probe_gfs(struct volume_id *id, uint64_t off, uint64_t size)
119 {
120         return volume_id_probe_gfs_generic(id, off, 1);
121 }
122
123 int volume_id_probe_gfs2(struct volume_id *id, uint64_t off, uint64_t size)
124 {
125         return volume_id_probe_gfs_generic(id, off, 2);
126 }