File: blocktype.c

package info (click to toggle)
ocfs2-tools 1.8.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 6,232 kB
  • sloc: ansic: 86,865; sh: 5,781; python: 2,380; makefile: 1,305
file content (161 lines) | stat: -rw-r--r-- 4,081 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* -*- mode: c; c-basic-offset: 8; -*-
 * vim: noexpandtab sw=8 ts=8 sts=0:
 *
 * blocktype.c
 *
 * Detect various metadata block types, etc.
 *
 * Copyright (C) 2011 Oracle.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

#define _XOPEN_SOURCE 600 /* Triggers magic in features.h */
#define _LARGEFILE64_SOURCE

#include <string.h>

#include "ocfs2/ocfs2.h"

struct block_types {
	enum ocfs2_block_type	bt_type;
	char 			bt_signature[8];
	int			bt_offset;
};

static struct block_types bts[] = {
	{
		OCFS2_BLOCK_INODE, OCFS2_INODE_SIGNATURE,
		offsetof(struct ocfs2_dinode, i_signature),
	},
	{
		OCFS2_BLOCK_SUPERBLOCK, OCFS2_SUPER_BLOCK_SIGNATURE,
		offsetof(struct ocfs2_dinode, i_signature),
	},
	{
		OCFS2_BLOCK_EXTENT_BLOCK, OCFS2_EXTENT_BLOCK_SIGNATURE,
		offsetof(struct ocfs2_extent_block, h_signature),
	},
	{
		OCFS2_BLOCK_GROUP_DESCRIPTOR, OCFS2_GROUP_DESC_SIGNATURE,
		offsetof(struct ocfs2_group_desc, bg_signature),
	},
	{
		OCFS2_BLOCK_DIR_BLOCK, OCFS2_DIR_TRAILER_SIGNATURE,
		offsetof(struct ocfs2_dir_block_trailer, db_signature),
	},
	{
		OCFS2_BLOCK_XATTR, OCFS2_XATTR_BLOCK_SIGNATURE,
		offsetof(struct ocfs2_xattr_block, xb_signature),
	},
	{
		OCFS2_BLOCK_REFCOUNT, OCFS2_REFCOUNT_BLOCK_SIGNATURE,
		offsetof(struct ocfs2_refcount_block, rf_signature),
	},
	{
		OCFS2_BLOCK_DXROOT, OCFS2_DX_ROOT_SIGNATURE,
		offsetof(struct ocfs2_dx_root_block, dr_signature),
	},
	{
		OCFS2_BLOCK_DXLEAF, OCFS2_DX_LEAF_SIGNATURE,
		offsetof(struct ocfs2_dx_leaf, dl_signature),
	},
};

enum ocfs2_block_type ocfs2_detect_block(char *buf)
{
	int i, numtypes = sizeof(bts)/sizeof(struct block_types);

	for (i = 0; i < numtypes; ++i) {
		if (!strncmp(buf + bts[i].bt_offset, bts[i].bt_signature,
			     strlen(bts[i].bt_signature)))
			return bts[i].bt_type;
	}

	return OCFS2_BLOCK_UNKNOWN;
}

static void ocfs2_swap_block(ocfs2_filesys *fs, void *block, int to_cpu)
{
	enum ocfs2_block_type bt = ocfs2_detect_block(block);

	switch (bt) {
		case OCFS2_BLOCK_INODE:
		case OCFS2_BLOCK_SUPERBLOCK:
			if (to_cpu)
				ocfs2_swap_inode_to_cpu(fs, block);
			else
				ocfs2_swap_inode_from_cpu(fs, block);
			break;
		case OCFS2_BLOCK_EXTENT_BLOCK:
			if (to_cpu)
				ocfs2_swap_extent_block_to_cpu(fs, block);
			else
				ocfs2_swap_extent_block_from_cpu(fs, block);
			break;
		case OCFS2_BLOCK_GROUP_DESCRIPTOR:
			if (to_cpu)
				ocfs2_swap_group_desc_to_cpu(fs, block);
			else
				ocfs2_swap_group_desc_from_cpu(fs, block);
			break;
		case OCFS2_BLOCK_DIR_BLOCK:
			if (to_cpu)
				ocfs2_swap_dir_entries_to_cpu(block,
							fs->fs_blocksize);
			else
				ocfs2_swap_dir_entries_from_cpu(block,
							fs->fs_blocksize);
			break;
		case OCFS2_BLOCK_XATTR:
			if (to_cpu)
				ocfs2_swap_xattr_block_to_cpu(fs, block);
			else
				ocfs2_swap_xattr_block_from_cpu(fs, block);
			break;
		case OCFS2_BLOCK_REFCOUNT:
			if (to_cpu)
				ocfs2_swap_refcount_block_to_cpu(fs, block);
			else
				ocfs2_swap_refcount_block_from_cpu(fs, block);
			break;
		case OCFS2_BLOCK_DXROOT:
			if (to_cpu)
				ocfs2_swap_dx_root_to_cpu(fs, block);
			else
				ocfs2_swap_dx_root_from_cpu(fs, block);
			break;
		case OCFS2_BLOCK_DXLEAF:
			if (to_cpu)
				ocfs2_swap_dx_leaf_to_cpu(block);
			else
				ocfs2_swap_dx_leaf_from_cpu(block);
			break;
		default:
			break;
	}

	return ;
}

/*
 * ocfs2_swap_block...() silently ignores unknown block types. The caller
 * needs to detect unknown blocks.
 */
void ocfs2_swap_block_from_cpu(ocfs2_filesys *fs, void *block)
{
	ocfs2_swap_block(fs, block, 0);
}

void ocfs2_swap_block_to_cpu(ocfs2_filesys *fs, void *block)
{
	ocfs2_swap_block(fs, block, 1);
}