File: group.c

package info (click to toggle)
ocfs2-tools 1.6.4-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,644 kB
  • ctags: 8,411
  • sloc: ansic: 77,632; sh: 4,626; python: 2,380; makefile: 1,115
file content (320 lines) | stat: -rw-r--r-- 8,425 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * group.c
 *
 * group descriptor corruptions
 *
 * Copyright (C) 2006 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.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 021110-1307, USA.
 *
 */


/* This file will create the following errors for a group descriptor.
 *
 * Group minor field error:	GROUP_PARENT, GROUP_BLKNO, GROUP_CHAIN,
 *				GROUP_FREE_BITS
 *
 * Group generation error:	GROUP_GEN
 *
 * Group list error:		GROUP_UNEXPECTED_DESC, GROUP_EXPECTED_DESC
 *
 */
#include "main.h"

extern char *progname;

static void create_test_group_desc(ocfs2_filesys *fs, uint64_t *newblk,
				struct ocfs2_group_desc *clone)
{
	errcode_t ret;
	uint32_t n_clusters;
	char *buf = NULL;
	struct ocfs2_group_desc *bg = NULL;

	if (!clone)
		FSWRK_FATAL("Can't fake the null group descriptor");

	ret = ocfs2_new_clusters(fs, 1, 1, newblk, &n_clusters);
	if (ret)
		FSWRK_COM_FATAL(progname, ret);
	
	ret = ocfs2_malloc_block(fs->fs_io, &buf);
	if (ret)
		FSWRK_COM_FATAL(progname, ret);

	bg = (struct ocfs2_group_desc *)buf;
	*bg = *clone;
	bg->bg_blkno = cpu_to_le64(*newblk);
	bg->bg_next_group = 0;

	ret = io_write_block(fs->fs_io, *newblk, 1, buf);
	if(ret)
		FSWRK_COM_FATAL(progname, ret);

	return;
}

static void damage_group_desc(ocfs2_filesys *fs, uint64_t blkno,
				enum fsck_type type)
{
	errcode_t ret;
	char *buf = NULL, *bufgroup = NULL, *bufgroup1 = NULL;
	struct ocfs2_dinode *di;
	struct ocfs2_chain_list *cl;
	struct ocfs2_chain_rec *cr;
	struct ocfs2_group_desc *bg = NULL, *bg1 = NULL;
	uint64_t newblk;

	ret = ocfs2_malloc_block(fs->fs_io, &buf);
	if (ret)
		FSWRK_COM_FATAL(progname, ret);

	ret = ocfs2_read_inode(fs, blkno, buf);
	if (ret)
		FSWRK_COM_FATAL(progname, ret);

	di = (struct ocfs2_dinode *)buf;

	if (!(di->i_flags & OCFS2_BITMAP_FL))
		FSWRK_FATAL("not a bitmap");

	if (!(di->i_flags & OCFS2_CHAIN_FL))
		FSWRK_FATAL("not a chain group");

	cl = &(di->id2.i_chain);
	if (!cl->cl_next_free_rec) {
		FSWRK_WARN("No chain record found at inode#%"PRIu64
			",so can't corrupt it for type[%d].\n",
			 blkno, type);
		goto bail;
	}

	ret = ocfs2_malloc_block(fs->fs_io, &bufgroup);
	if (ret)
		FSWRK_COM_FATAL(progname, ret);
	
	cr = cl->cl_recs;
	ret = ocfs2_read_group_desc(fs, cr->c_blkno, bufgroup);
	if (ret)
		FSWRK_COM_FATAL(progname, ret);

	bg = (struct ocfs2_group_desc *)bufgroup;

	switch (type) {
	case GROUP_EXPECTED_DESC:
		fprintf(stdout, "Corrput GROUP_EXPECED_DESC: "
			"delete the group desciptor#%"PRIu64" from the chain "
			"#%d\n", bg->bg_next_group, bg->bg_chain);
		bg->bg_next_group = 0;
		break;
	case GROUP_UNEXPECTED_DESC:
		create_test_group_desc(fs, &newblk, bg);
		fprintf(stdout, "Corrput GROUP_UNEXPECED_DESC: "
			"Add a fake descriptor#%"PRIu64
			" in the chain#%d of inode#%"PRIu64"\n",
			newblk, bg->bg_chain, blkno);
		bg->bg_next_group = cpu_to_le64(newblk);
		break;
	case GROUP_GEN:
		fprintf(stdout, "Corrput GROUP_GEN: "
			"change group generation from %x to %x\n",
			bg->bg_generation, (bg->bg_generation + 10));
		bg->bg_generation += 10;
		/* crash next group correspondingly to verify fsck.ocfs2. */
		if (bg->bg_next_group) {
			ret = ocfs2_malloc_block(fs->fs_io, &bufgroup1);
			if (ret)
				FSWRK_COM_FATAL(progname, ret);
	
			ret = ocfs2_read_group_desc(fs, bg->bg_next_group,
							 bufgroup1);
			if (ret)
				FSWRK_COM_FATAL(progname, ret);

			bg1 = (struct ocfs2_group_desc *)bufgroup1;

			bg1->bg_generation += 10;
			fprintf(stdout, "Corrput GROUP_GEN: "
				"change group generation from %x to %x\n",
				bg1->bg_generation, (bg1->bg_generation + 10));

			ret = ocfs2_write_group_desc(fs, bg->bg_next_group,
							bufgroup1);
			if (ret)
				FSWRK_COM_FATAL(progname, ret);
		}
		break;
	case GROUP_PARENT:
		fprintf(stdout, "Corrput GROUP_PARENT: "
			"change group parent from %"PRIu64" to %"PRIu64"\n",
			bg->bg_parent_dinode, (bg->bg_parent_dinode + 10));
		bg->bg_parent_dinode += 10;
		break;
	case GROUP_BLKNO:
		fprintf(stdout, "Corrput GROUP_BLKNO: "
			"change group blkno from %"PRIu64" to %"PRIu64"\n",
			bg->bg_blkno, (bg->bg_blkno + 10));
		bg->bg_blkno += 10;
		break;
	case GROUP_CHAIN:
		fprintf(stdout, "Corrput GROUP_CHAIN: "
			"change group chain from %u to %u\n",
			bg->bg_chain, (bg->bg_chain + 10));
		bg->bg_chain += 10;
		break;
	case GROUP_FREE_BITS:
		fprintf(stdout, "Corrput GROUP_FREE_BITS: "
			"change group free bits from %u to %u\n",
			bg->bg_free_bits_count, (bg->bg_bits + 10));
		bg->bg_free_bits_count = bg->bg_bits + 10;
		break;
	default:
		FSWRK_FATAL("Invalid type[%d]\n", type);
	}

	ret = ocfs2_write_group_desc(fs, cr->c_blkno, bufgroup);
	if (ret)
		FSWRK_COM_FATAL(progname, ret);

	ret = ocfs2_write_inode(fs, blkno, buf);
	if (ret)
		FSWRK_COM_FATAL(progname, ret);

bail:
	if(bufgroup1)
		ocfs2_free(&bufgroup1);
	if (bufgroup)
		ocfs2_free(&bufgroup);
	if (buf)
		ocfs2_free(&buf);

	return;
}

static void mess_up_group_desc(ocfs2_filesys *fs, uint16_t slotnum,
			enum fsck_type type)
{
	errcode_t ret;
	char sysfile[OCFS2_MAX_FILENAME_LEN];
	uint64_t blkno;
	struct ocfs2_super_block *sb = OCFS2_RAW_SB(fs->fs_super);
	
	if (slotnum == UINT16_MAX)
		snprintf(sysfile, sizeof(sysfile), "%s",
		ocfs2_system_inodes[GLOBAL_BITMAP_SYSTEM_INODE].si_name);
	else
		snprintf(sysfile, sizeof(sysfile),
			ocfs2_system_inodes[INODE_ALLOC_SYSTEM_INODE].si_name,
			slotnum);

	ret = ocfs2_lookup(fs, sb->s_system_dir_blkno, sysfile,
				   strlen(sysfile), NULL, &blkno);
	if (ret)
		FSWRK_COM_FATAL(progname, ret);
	
	damage_group_desc(fs, blkno, type);

	return ;
}

void mess_up_group_minor(ocfs2_filesys *fs, enum fsck_type type,
			 uint16_t slotnum)
{
	mess_up_group_desc(fs, slotnum, type);
}

void mess_up_group_gen(ocfs2_filesys *fs, enum fsck_type type, uint16_t slotnum)
{

	mess_up_group_desc(fs, slotnum, type);
}

void mess_up_group_list(ocfs2_filesys *fs, enum fsck_type type,
			uint16_t slotnum)
{
	mess_up_group_desc(fs, slotnum, type);
}

/* We will allocate some clusters and corrupt the group descriptor
 * which stores the clusters and makes fsck run into error.
 */
void mess_up_cluster_group_desc(ocfs2_filesys *fs, enum fsck_type type,
				uint16_t slotnum)
{
	errcode_t ret;
	uint32_t found, start_cluster, old_free_bits, request = 100;
	uint64_t start_blk;
	uint64_t bg_blk;
	int cpg;
	char *buf = NULL;
	struct ocfs2_group_desc *bg;

	ret = ocfs2_new_clusters(fs, 1, request, &start_blk, &found);
	if (ret)
		FSWRK_COM_FATAL(progname, ret);

	start_cluster = ocfs2_blocks_to_clusters(fs, start_blk);
	cpg = ocfs2_group_bitmap_size(fs->fs_blocksize, 0,
		OCFS2_RAW_SB(fs->fs_super)->s_feature_incompat) * 8;
	bg_blk = ocfs2_which_cluster_group(fs, cpg, start_cluster);

	ret = ocfs2_malloc_block(fs->fs_io, &buf);
	if (ret)
		FSWRK_COM_FATAL(progname, ret);

	ret = ocfs2_read_group_desc(fs, bg_blk, buf);
	if (ret)
		FSWRK_COM_FATAL(progname, ret);

	bg = (struct ocfs2_group_desc *)buf;

	old_free_bits = bg->bg_free_bits_count;
	bg->bg_free_bits_count = bg->bg_bits + 10;

	ret = ocfs2_write_group_desc(fs, bg_blk, buf);
	if (ret)
		FSWRK_COM_FATAL(progname, ret);

	fprintf(stdout, "Corrput CLUSTER and GROUP_FREE_BITS: "
		"Allocating %u clusters and change group[%"PRIu64"]'s free bits"
		" from %u to %u\n",
		found, bg_blk, old_free_bits, bg->bg_free_bits_count);

	if (buf)
		ocfs2_free(&buf);
}

/*
 * Simply corrupt the global bitmap by allocating a new cluster, without
 * actually using it later.
 */
void mess_up_cluster_alloc_bits(ocfs2_filesys *fs, enum fsck_type type,
				uint16_t slotnum)
{
	errcode_t ret;
	uint32_t n_clusters;
	uint64_t blkno;

	ret = ocfs2_new_clusters(fs, 1, 1, &blkno, &n_clusters);
	if (ret) {
		FSWRK_COM_FATAL(progname, ret);
		ocfs2_free_clusters(fs, 1, blkno);
	}

	fprintf(stdout ,"Mark bits of global bitmap by unused "
		"block#%"PRIu64".\n", blkno);
}