⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 corruption.c

📁 reiserfsprogs-3.6.19.tar.gz 源码 给有需要的人!
💻 C
📖 第 1 页 / 共 3 页
字号:
			continue;		}		should_be_corrupted = (unsigned int) get_rand ((double)0, (double)1);		if (should_be_corrupted == 0)		{			brelse (bh);			continue;		}		nr_ih_cr = get_rand (1, get_blkh_nr_items (B_BLK_HEAD (bh))); 		for (j = 0; j < nr_ih_cr; j ++ ) {			should_be_corrupted = (unsigned int) get_rand ((double)0, (double)1);			if (should_be_corrupted == 0)				continue;			if ((data(fs)->log_file_name) && (data(fs)->log))				fprintf (data(fs)->log, "# block %lu , item header %d\n",						 bh->b_blocknr, j);			printf ("# block %lu , item header %d\n", bh->b_blocknr, j);						ih = B_N_PITEM_HEAD (bh, j);			do_one_ih_random_corrupt (ih);		}		mark_buffer_dirty (bh);		bwrite(bh);		brelse (bh);		nr_leaves ++ ;	}	if ((data(fs)->log_file_name) && (data(fs)->log))		fprintf (data(fs)->log, "item headers in %lu leaves WERE corrupted\n",				 nr_leaves);	printf ("item headers in %lu leaves WERE corrupted\n", nr_leaves);	reiserfs_close_ondisk_bitmap (fs);}/* corrupt item */void do_one_item_random_corrupt (struct buffer_head * bh,								 struct item_head * ih){	unsigned int i;	unsigned int from;	unsigned int count;	char * p;	p = (char *)B_I_PITEM(bh, ih);	from = get_rand (0, get_ih_item_len(ih)  - 1);	count = get_rand (1, get_ih_item_len(ih));	if (from + count > get_ih_item_len(ih))		count = get_ih_item_len(ih) - from;	for (i = from; i < from + count; i ++)		((char *)p)[i] = get_rand(0, 255);	if ((data(fs)->log_file_name) && (data(fs)->log))		fprintf (data(fs)->log, "item body \tfrom %u ( %u )\n", from, count);	printf ("item body \tfrom %u ( %u )\n",  from, count);	return ;}/* corrupt the random number of directory items in random number of leaves */void do_dir_random_corrupt (reiserfs_filsys_t * fs,							unsigned long nr_leaves_cr) { 	unsigned int nr_ih_cr;	unsigned int i, j;	struct buffer_head * bh;	struct item_head * ih;	unsigned long nr_leaves = 0;	unsigned int should_be_corrupted = 0;	srand (time (0));	printf ("DIR items in %lu leaves will be corrupted\n", nr_leaves_cr);	if ((data(fs)->log_file_name) && (data(fs)->log)) {		fprintf (data(fs)->log, "DIR items in %lu leaves will be corrupted\n", nr_leaves_cr);	}    if (reiserfs_open_ondisk_bitmap (fs) < 0)		reiserfs_exit (1, "Could not open ondisk bitmap");    for (i = 0; (i < get_sb_block_count (fs->fs_ondisk_sb)) &&			 nr_leaves < nr_leaves_cr; i ++) {		if (!reiserfs_bitmap_test_bit (fs->fs_bitmap2, i))			continue;		bh = bread (fs->fs_dev, i, fs->fs_blocksize);		if (!bh) {			reiserfs_warning (stderr, "could not read block %lu\n", i);			continue;		}		if (who_is_this (bh->b_data, bh->b_size) != THE_LEAF) {			brelse (bh);				continue;		}		if ((!is_leaf_node (bh)) || (block_of_journal (fs, i))) {			brelse (bh);				continue;		}		should_be_corrupted = (unsigned int) get_rand ((double)0, (double)1);		if (should_be_corrupted == 0)		{			brelse (bh);			continue;		}		/* get next item, look is it a DIR */		nr_ih_cr = get_rand (1, get_blkh_nr_items (B_BLK_HEAD (bh))); 		for (j = 0; j < nr_ih_cr; j ++ ) {			should_be_corrupted = (unsigned int) get_rand ((double)0, (double)1);			if (should_be_corrupted == 0)				continue;			if ((data(fs)->log_file_name) && (data(fs)->log))				fprintf (data(fs)->log, "# block %lu , item %d\n",						 bh->b_blocknr, j);			printf ("# block %lu , item %d\n", bh->b_blocknr, j);						ih = B_N_PITEM_HEAD (bh, j);			if (get_type (&ih->ih_key) != TYPE_DIRENTRY)				continue;			do_one_item_random_corrupt (bh, ih);		}		mark_buffer_dirty (bh);		bwrite(bh);		brelse (bh);		nr_leaves ++ ;	}	if ((data(fs)->log_file_name) && (data(fs)->log))		fprintf (data(fs)->log, "DIR items in %lu leaves WERE corrupted\n",				 nr_leaves);	printf ("DIR items in %lu leaves WERE corrupted\n", nr_leaves);	reiserfs_close_ondisk_bitmap (fs);}/* corrupt the random number of stat data items in random number of leaves */void do_sd_random_corrupt (reiserfs_filsys_t * fs,						   unsigned long nr_leaves_cr) { 	unsigned int nr_ih_cr;	unsigned int i, j;	struct buffer_head * bh;	struct item_head * ih;	unsigned long nr_leaves = 0;	unsigned int should_be_corrupted = 0;	srand (time (0));	printf ("SD items in %lu leaves will be corrupted\n", nr_leaves_cr);	if ((data(fs)->log_file_name) && (data(fs)->log)) {		fprintf (data(fs)->log, "SD items in %lu leaves will be corrupted\n", nr_leaves_cr);	}    if (reiserfs_open_ondisk_bitmap (fs) < 0)		reiserfs_exit (1, "Could not open ondisk bitmap");    for (i = 0; (i < get_sb_block_count (fs->fs_ondisk_sb)) &&			 nr_leaves < nr_leaves_cr; i ++) {		if (!reiserfs_bitmap_test_bit (fs->fs_bitmap2, i))			continue;		bh = bread (fs->fs_dev, i, fs->fs_blocksize);		if (!bh) {			reiserfs_warning (stderr, "could not read block %lu\n", i);			continue;		}		if (who_is_this (bh->b_data, bh->b_size) != THE_LEAF) {			brelse (bh);				continue;		}		if ((!is_leaf_node (bh)) || (block_of_journal (fs, i))) {			brelse (bh);				continue;		}				should_be_corrupted = (unsigned int) get_rand ((double)0, (double)1);		if (should_be_corrupted == 0)		{			brelse (bh);			continue;		}		/* get next item, look is it a SD */		nr_ih_cr = get_rand (1, get_blkh_nr_items (B_BLK_HEAD (bh))); 		for (j = 0; j < nr_ih_cr; j ++ ) {			should_be_corrupted = (unsigned int) get_rand ((double)0, (double)1);			if (should_be_corrupted == 0)				continue;			if ((data(fs)->log_file_name) && (data(fs)->log))				fprintf (data(fs)->log, "# block %lu , item %d\n",						 bh->b_blocknr, j);			printf ("# block %lu , item %d\n", bh->b_blocknr, j);						ih = B_N_PITEM_HEAD (bh, j);			if (get_type (&ih->ih_key) != TYPE_STAT_DATA)				continue;			do_one_item_random_corrupt (bh, ih);		}		mark_buffer_dirty (bh);		bwrite(bh);		brelse (bh);		nr_leaves ++ ;	}	if ((data(fs)->log_file_name) && (data(fs)->log))		fprintf (data(fs)->log, "SD items in %lu leaves WERE corrupted\n",				 nr_leaves);	printf ("SD items in %lu leaves WERE corrupted\n", nr_leaves);	reiserfs_close_ondisk_bitmap (fs);}/* corrupt the random number of indierct items in random number of leaves */void do_ind_random_corrupt (reiserfs_filsys_t * fs,						   unsigned long nr_leaves_cr) { 	unsigned int nr_ih_cr;	unsigned int i, j;	struct buffer_head * bh;	struct item_head * ih;	unsigned long nr_leaves = 0;	unsigned int should_be_corrupted = 0;	srand (time (0));	printf ("IND items in %lu leaves will be corrupted\n", nr_leaves_cr);	if ((data(fs)->log_file_name) && (data(fs)->log)) {		fprintf (data(fs)->log, "IND items in %lu leaves will be corrupted\n", nr_leaves_cr);	}    if (reiserfs_open_ondisk_bitmap (fs) < 0)		reiserfs_exit (1, "Could not open ondisk bitmap");    for (i = 0; (i < get_sb_block_count (fs->fs_ondisk_sb)) &&			 nr_leaves < nr_leaves_cr; i ++) {		if (!reiserfs_bitmap_test_bit (fs->fs_bitmap2, i))			continue;		bh = bread (fs->fs_dev, i, fs->fs_blocksize);		if (!bh) {			reiserfs_warning (stderr, "could not read block %lu\n", i);			continue;		}		if (who_is_this (bh->b_data, bh->b_size) != THE_LEAF) {			brelse (bh);				continue;		}		if ((!is_leaf_node (bh)) || (block_of_journal (fs, i))) {			brelse (bh);				continue;		}				should_be_corrupted = (unsigned int) get_rand ((double)0, (double)1);		if (should_be_corrupted == 0)		{			brelse (bh);			continue;		}		/* get next item, look is it an IND */		nr_ih_cr = get_rand (1, get_blkh_nr_items (B_BLK_HEAD (bh))); 		for (j = 0; j < nr_ih_cr; j ++ ) {			should_be_corrupted = (unsigned int) get_rand ((double)0, (double)1);			if (should_be_corrupted == 0)				continue;			ih = B_N_PITEM_HEAD (bh, j);			if (get_type (&ih->ih_key) != TYPE_INDIRECT)				continue;			if ((data(fs)->log_file_name) && (data(fs)->log))				fprintf (data(fs)->log, "# block %lu , item %d\n",						 bh->b_blocknr, j);			printf ("# block %lu , item %d\n", bh->b_blocknr, j);			do_one_item_random_corrupt (bh, ih);		}		mark_buffer_dirty (bh);		bwrite(bh);		brelse (bh);		nr_leaves ++ ;	}	if ((data(fs)->log_file_name) && (data(fs)->log))		fprintf (data(fs)->log, "IND items in %lu leaves WERE corrupted\n",				 nr_leaves);	printf ("IND items in %lu leaves WERE corrupted\n", nr_leaves);	reiserfs_close_ondisk_bitmap (fs);}/* this reads list of desired corruptions from stdin and performs the   corruptions. Format of that list:   B           - the random number of bitmap to be corrupted   L nr_leaves - block headers in nr_leaves leaves to be corupted   H nr_leaves - the random number of item headers in nr_leaves to be corrupted   S nr_leaves - the random number of stat data items in nr_leaves to be                 corrupted   D nr_leaves - the random number of directory items in nr_leaves to be                 corrupted   I nr_leaves - the random number of indirect items in nr_leaves to be                 corrupted*/void what_to_corrupt (reiserfs_filsys_t * fs, char * corruption_command){	unsigned long nr_leaves_cr;	char code;	switch (corruption_command [0]){	case 'B' :		/* bitmap */		do_bitmap_corruption (fs);		break;	case 'L' :		/* leaves */		if (sscanf (corruption_command, "%c %lu\n", &code, &nr_leaves_cr) != 2) {			printf ("Wrong format \'%c\'\n", corruption_command [0]);			return;		}		do_leaves_corruption (fs, nr_leaves_cr);				break;	case 'H' :		/* item headers */		if (sscanf (corruption_command, "%c %lu\n", &code, &nr_leaves_cr) != 2) {			printf ("Wrong format \'%c\'\n", corruption_command [0]);			return;		}		do_ih_random_corrupt (fs, nr_leaves_cr);		break;	case 'D' :		/* directory items */		if (sscanf (corruption_command, "%c %lu\n", &code, &nr_leaves_cr) != 2) {			printf ("Wrong format \'%c\'\n", corruption_command [0]);			return;		}		do_dir_random_corrupt (fs, nr_leaves_cr);		break;	case 'S' :		/* stat data items */		if (sscanf (corruption_command, "%c %lu\n", &code, &nr_leaves_cr) != 2) {			printf ("Wrong format \'%c\'\n", corruption_command [0]);			return;		}		do_sd_random_corrupt (fs, nr_leaves_cr);		break;	case 'I' :		/* indirect items */		if (sscanf (corruption_command, "%c %lu\n", &code, &nr_leaves_cr) != 2) {			printf ("Wrong format \'%c\'\n", corruption_command [0]);			return;		}		do_ind_random_corrupt (fs, nr_leaves_cr);		break;	default :		printf ("Unknown command specified\n");	}}void do_fs_random_corrupt (reiserfs_filsys_t * fs){    char * line = 0;    size_t n = 0;    printf ("Corrupting fs. Please insert one of the following command\n"			" B - the random number of bitmap to be corrupted\n"			" L nr_leaves - block headers in nr_leaves leaves to be corupted\n"			" H nr_leaves - the random number of item headers in nr_leaves to be corrupted\n"			" S nr_leaves - the random number of stat data items in nr_leaves to be corrupted\n"			" D nr_leaves - the random number of directory items in nr_leaves to be corrupted\n"			" I nr_leaves - the random number of indirect items in nr_leaves to be corrupted\n"			".. ->\n");	/* Get list of corruptions from stdin */	while (getline (&line, &n, stdin) != -1) {		if ( line [0] == '\n' ) {			free (line);			line = 0;			n = 0;			break;		}		printf ("################## command : %s", line);		if ((data(fs)->log_file_name) && (data(fs)->log)) {			fprintf (data(fs)->log, "################## command : %s", line);		}		what_to_corrupt (fs, line);				free (line);		line = 0;		n = 0;	}}/*   Local variables:   c-indentation-style: "K&R"   mode-name: "LC"   c-basic-offset: 4   tab-width: 4   fill-column: 80   End:*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -