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

📄 corruption.c

📁 reiserfsprogs-3.6.19.tar.gz 源码 给有需要的人!
💻 C
📖 第 1 页 / 共 3 页
字号:
			/* look for property objectid */			if (get_key_objectid (&ih->ih_key) != objectid || !is_direntry_ih (ih))				continue;			deh = B_I_DEH (bh, ih);			set_deh_objectid (deh, new_objectid);			break;		}  		break;			case 'T': /* set type of item */	    if (sscanf (corruption_command, "%c %d %d %d\n", &code, &item_num, &type, &format) != 4) {			printf ("Wrong format \'%c\'\n", corruption_command [0]);			return;	    }	    if (item_num > get_blkh_nr_items (B_BLK_HEAD (bh))) {			printf ("Wrong format \'%c\', wrong item_num \n", corruption_command [0]);			return;	    }			    ih = B_N_PITEM_HEAD (bh, item_num);	    set_ih_key_format (ih, format);	    set_type (format, &ih->ih_key, type);			    break;			case 'J': /* set objectid : used to simulate objectid sharing problem */	    if (sscanf (corruption_command, "%c %d %d\n", &code, &item_num, &objectid) != 3) {			printf ("Wrong format \'%c\'\n", corruption_command [0]);			return;	    }	    if (item_num > get_blkh_nr_items (B_BLK_HEAD (bh))) {			printf ("Wrong format \'%c\', wrong item_num \n", corruption_command [0]);			return;	    }	    ih = B_N_PITEM_HEAD (bh, item_num);	    set_key_objectid (&ih->ih_key, objectid);	    break;			case 'I': /* break unformatted node pointer */	    if (sscanf (corruption_command, "%c %d %u\n", &code, &item_num, &pos_in_item) != 3) {			printf ("Wrong format \'%c\'\n", corruption_command [0]);			return;	    }	    if (item_num > get_blkh_nr_items (B_BLK_HEAD (bh))) {			printf ("Wrong format \'%c\', wrong item_num \n", corruption_command [0]);			return;	    }	    ih = B_N_PITEM_HEAD (bh, item_num);	    if (!is_indirect_ih (ih) || pos_in_item >= I_UNFM_NUM (ih)) {	        reiserfs_warning (stderr, "Not an indirect item or there is "							  "not so many unfm ptrs in it\n");			return;	    }	    d32_put((__u32 *)B_I_PITEM (bh, ih), pos_in_item, 		    get_sb_block_count (fs->fs_ondisk_sb) + 100);	    break;	    	case 'D': /* delete item */	    if (sscanf (corruption_command, "%c %d\n", &code, &item_num) != 2) {			printf ("Wrong format \'%c\'\n", corruption_command [0]);			return;	    }	    if (item_num > get_blkh_nr_items (B_BLK_HEAD (bh))) {			printf ("Wrong format \'%c\', wrong item_num \n", corruption_command [0]);			return;	    }	    delete_item (fs, bh, item_num);	    break;			case 'O': /* make item out of order */		{			struct key * key;			if (sscanf (corruption_command, "%c %d\n", &code, &item_num) != 2) {				printf ("Wrong format \'%c\'\n", corruption_command [0]);				return;			}			if (item_num > get_blkh_nr_items (B_BLK_HEAD (bh))) {				printf ("Wrong format \'%c\', wrong item_num \n", corruption_command [0]);				return;			}						/* destroy item order */						if (item_num == get_blkh_nr_items (B_BLK_HEAD (bh)) - 1) {				printf ("can not destroy order\n");				return;			}			ih = B_N_PITEM_HEAD (bh, item_num);			key = &(ih + 1)->ih_key;			set_key_dirid (&ih->ih_key, get_key_dirid (key) + 1);						break;		}  	case 'S': /* corrupt st_size */		{			/* fixme: old stat data only */			struct stat_data_v1 * sd;						if (sscanf (corruption_command, "%c %d %d\n", &code, &item_num, &value) != 3) {				printf ("Wrong format \'%c\'\n", corruption_command [0]);				return;			}			if (item_num > get_blkh_nr_items (B_BLK_HEAD (bh))) {				printf ("Wrong format \'%c\', wrong item_num \n", corruption_command [0]);				return;			}			ih = B_N_PITEM_HEAD (bh, item_num);						sd = (struct stat_data_v1 *)B_I_PITEM (bh, ih); 			reiserfs_warning (stderr, "Changing sd_size of %k from %d to %d\n",							  &ih->ih_key, sd_v1_size(sd), value);			set_sd_v1_size( sd, value );			break;		}		    case 'F': /*         st_first_direct_byte */		{			/* fixme: old stat data only */			struct stat_data_v1 * sd;						if (sscanf (corruption_command, "%c %d %d\n", &code, &item_num, &value) != 3) {				printf ("Wrong format \'%c\'\n", corruption_command [0]);				return;			}			if (item_num > get_blkh_nr_items (B_BLK_HEAD (bh))) {				printf ("Wrong format \'%c\', wrong item_num \n",						corruption_command [0]);				return;			}			ih = B_N_PITEM_HEAD (bh, item_num);						sd = (struct stat_data_v1 *)B_I_PITEM (bh, ih); 			reiserfs_warning (stderr, "Changing sd_first_direct_byte of %k from %d to %d\n",							  &ih->ih_key, sd_v1_first_direct_byte(sd), value);					set_sd_v1_first_direct_byte( sd, value );			break;		}			case 'N': /* corrupt N number of items */	    if (sscanf (corruption_command, "%c %d %d\n",					&code, &item_numbers, &bytes_to_corrupt) != 3) {			printf ("Wrong format \'%c\'\n", corruption_command [0]);			return;	    }			    if (item_numbers > get_blkh_nr_items (B_BLK_HEAD (bh))) {			printf ("Wrong item_numbers %d expected not more then %d\n",					item_numbers,					get_blkh_nr_items (B_BLK_HEAD (bh)));			return;	    }			    for (i = 0; i < item_numbers; i++) { 			printf ("Do corruptions :  %d item header; \n", i );			ih = B_N_PITEM_HEAD (bh, i);			do_one_ih_corrupt (ih, bytes_to_corrupt);			printf ("Ok\n");	    }			    break;	case 'B' :		{			struct block_head * blkh;			unsigned int offset;						if (sscanf (corruption_command, "%c %d %d\n",						&code, &offset, &bytes_to_corrupt) != 3) {				printf ("Wrong format \'%c\'\n", corruption_command [0]);				return;			}						blkh = B_BLK_HEAD (bh);			corrupt_block_header (blkh, offset, bytes_to_corrupt);			break;		}			default:		printf ("Unknown command found\n");	}     mark_buffer_dirty (bh);    return;}/* corrupt first nr_bytes bytes in item header */int do_one_ih_corrupt (struct item_head * ih, unsigned int nr_bytes){	if (nr_bytes > IH_SIZE) {		printf ("Bad byte number %u expected not more then %lu\n", nr_bytes,				(unsigned long)IH_SIZE);		exit (1);	}	if (memset ((char *)ih, 0, nr_bytes) != ih ) {		perror ("do_one_ih_corrupt: memset failed");		exit (1);	}	if ((data(fs)->log_file_name) && (data(fs)->log))		fprintf (data(fs)->log, "\tfirst %u bytes corrupted\n", nr_bytes);	printf ("\tfirst %u bytes corrupted\n", nr_bytes);	return 0;}/* item header random corruption */int do_one_ih_random_corrupt (struct item_head * ih){	unsigned int i;	unsigned int from;	unsigned int count;			from = get_rand (0,  IH_SIZE - 1);	count = get_rand (1, IH_SIZE);	if (from + count > IH_SIZE)		count = IH_SIZE - from;	for (i = from; i < from + count; i ++)		((char *)ih)[i] = get_rand(0, 255);	if ((data(fs)->log_file_name) && (data(fs)->log))		fprintf (data(fs)->log, "\tfrom %u ( %u )\n", from, count);	printf ("\tfrom %u ( %u )\n",  from, count);	return 0;}/* Corrupt n bytes in block header */int corrupt_block_header (struct block_head * blkh, unsigned int offset, 			  unsigned int bytes) {	if ((offset + bytes) > BLKH_SIZE) {		printf ("Bad offset number: %u or bad bytes number: %u, the suumary "			"value expected not more then %lu\n", offset, bytes, 			(unsigned long)BLKH_SIZE);		exit (1);	} 	if (memset ((char *)blkh, 0, bytes) != blkh ) {		perror ("corrupt_block_head: memset failed");		exit (1);   	}	printf ("offset : %u, corrupt %u bytes\n", offset, bytes);	return 0;}/* corrupt random number of bytes within block header started from random   offset */void do_one_blkh_random_corrupt (struct buffer_head * bh){	struct block_head * blkh;	unsigned int from;	unsigned int count;	unsigned int i;	from = get_rand (0, BLKH_SIZE - 1);	count = get_rand (1, BLKH_SIZE);	blkh = B_BLK_HEAD (bh);	if (from + count > BLKH_SIZE)		count = BLKH_SIZE - from;	for (i = from; i < from + count; i ++)		((char *)blkh)[i] = get_rand(0, 255);	if ((data(fs)->log_file_name) && (data(fs)->log)) {		fprintf (data(fs)->log, "# : %lu #    ", bh->b_blocknr);		fprintf (data(fs)->log, "from %u (%u)\n", from, count);	}	printf ("# : %lu #   ", bh->b_blocknr);	printf ("from %u (%u)\n", from, count);}void do_leaves_corruption (reiserfs_filsys_t * fs,						   unsigned long nr_leaves_cr) 						   {	struct buffer_head * bh;    unsigned long nr_leaves = 0;	unsigned int i, should_be_corrupted;	srand (time (0));	printf ("%lu leaves will be corrupted\n", nr_leaves_cr);	if ((data(fs)->log_file_name) && (data(fs)->log)) {		fprintf (data(fs)->log, "Block headers 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;		}		do_one_blkh_random_corrupt (bh);		/* do_one_block_random_corrupt (bh); */		mark_buffer_dirty (bh);		bwrite(bh);		brelse (bh);		nr_leaves ++;	}	if ((data(fs)->log_file_name) && (data(fs)->log)) {		fprintf (data(fs)->log,				 "%lu leaves WERE corrupted\n",				 nr_leaves);	}	printf ("%lu leaves WERE corrupted\n", nr_leaves);	reiserfs_close_ondisk_bitmap (fs);	return;}void do_one_block_random_corrupt (struct buffer_head * bh) {	unsigned int from = get_rand (0, bh->b_size - 1);	unsigned int count = get_rand (1, bh->b_size);	unsigned int i;	if (from + count > bh->b_size)		count = bh->b_size - from;	for (i = from; i < from + count; i ++)		((char *)bh->b_data)[i] = get_rand(0, 255);	if ((data(fs)->log_file_name) && (data(fs)->log)) {		fprintf (data(fs)->log, "# block %lu: ", bh->b_blocknr);		fprintf (data(fs)->log, "from %u ( %u )\n", from, count);	}	printf ("# block %lu: ", bh->b_blocknr);	printf ("from %u ( %u )\n", from, count);	}void do_bitmap_corruption (reiserfs_filsys_t * fs){	unsigned long first = fs->fs_super_bh->b_blocknr + 1;	unsigned long nr_bitmap_to_corrupt;	unsigned long block;	struct buffer_head * bh;	unsigned int i;	nr_bitmap_to_corrupt = (unsigned long)get_rand (1, get_sb_bmap_nr (fs->fs_ondisk_sb) - 1);	if ((data(fs)->log_file_name) && (data(fs)->log)) {		fprintf (data(fs)->log, "%lu bitmaps will be corrupted\n", nr_bitmap_to_corrupt);	}	printf ("%lu bitmaps will be corrupted\n", nr_bitmap_to_corrupt);	for (i = 0; i < nr_bitmap_to_corrupt; i ++ )		{			block = ( i == 0 ) ? first 				: fs->fs_blocksize * 8 * i;			bh = bread (fs->fs_dev, block, fs->fs_blocksize);			if (!bh) {				printf ("do_bitmap_corruption: bread failed for bitmap %d: %lu\n",						i, block); 				exit (1);			}			do_one_block_random_corrupt (bh);			mark_buffer_dirty (bh);			bwrite(bh);			brelse (bh);		}} /* corrupt the random number of item headers in random number of leaves */void do_ih_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 ("item headers in %lu leaves will be corrupted\n", nr_leaves_cr);	if ((data(fs)->log_file_name) && (data(fs)->log)) {		fprintf (data(fs)->log, "item headers 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);	

⌨️ 快捷键说明

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