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
44struct 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
52struct gfs2_inum {
53	uint64_t no_formal_ino;
54	uint64_t no_addr;
55};
56
57struct 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
77static 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", (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		be32_to_cpu(sbd->sb_header.mh_format) == GFS_FORMAT_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		strcpy(id->type_version, "1");
106		volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
107		return 0;
108	}
109	return -1;
110}
111
112int volume_id_probe_gfs(struct volume_id *id, uint64_t off, uint64_t size)
113{
114	return volume_id_probe_gfs_generic(id, off, 1);
115}
116
117int volume_id_probe_gfs2(struct volume_id *id, uint64_t off, uint64_t size)
118{
119	return volume_id_probe_gfs_generic(id, off, 2);
120}
121