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