[Ocfs2-tools-devel] [PATCH] fswreck: corrupt a given inode

Eric Ren zren at suse.com
Sun Mar 6 19:19:43 PST 2016


Hi Gang,

On 03/07/2016 10:54 AM, Gang He wrote:
> Hello Eric,
>
> I think that using extent file path is better than specifying a inode number, otherwise the user possibly specify a wrong inode number to bring rise.
OK. And I also realize some other problems, will resend later on. Thanks!

Eric
>
> Thanks
> Gang
>
>
>> From: Eric Ren <zren at suse.com>
>>
>> When we want to corrupt a given inode, fswreck now cannot do this instead
>> it creates one internally. So, make it possible by introducing option -i
>> which
>> specify the inode number.
>>
>> Signed-off-by: Eric Ren <zren at suse.com>
>> ---
>>   fswreck/corrupt.c         | 19 ++++++++++---------
>>   fswreck/include/corrupt.h | 16 ++++++++--------
>>   fswreck/inode.c           | 13 +++++++------
>>   fswreck/main.c            | 23 ++++++++++++++++++-----
>>   4 files changed, 43 insertions(+), 28 deletions(-)
>>
>> diff --git a/fswreck/corrupt.c b/fswreck/corrupt.c
>> index 2f6074e..49b99ba 100644
>> --- a/fswreck/corrupt.c
>> +++ b/fswreck/corrupt.c
>> @@ -56,10 +56,10 @@ void create_named_directory(ocfs2_filesys *fs, char
>> *dirname,
>>   	return;
>>   }
>>   
>> -void corrupt_file(ocfs2_filesys *fs, enum fsck_type type, uint16_t slotnum)
>> +void corrupt_file(ocfs2_filesys *fs, enum fsck_type type, uint16_t slotnum,
>> void *priv)
>>   {
>>   	void (*func)(ocfs2_filesys *fs, enum fsck_type type, uint64_t blkno) =
>> NULL;
>> -	uint64_t blkno;
>> +	uint64_t blkno = *(int *)priv;
>>   
>>   	switch (type) {
>>   	case EB_BLKNO:
>> @@ -235,7 +235,8 @@ void corrupt_file(ocfs2_filesys *fs, enum fsck_type type,
>> uint16_t slotnum)
>>   		FSWRK_FATAL("Invalid code=%d", type);
>>   	}
>>   
>> -	create_named_directory(fs, "tmp", &blkno);
>> +	if (blkno <= 0)
>> +		create_named_directory(fs, "tmp", &blkno);
>>   
>>   	if (func)
>>   		func(fs, type, blkno);
>> @@ -243,7 +244,7 @@ void corrupt_file(ocfs2_filesys *fs, enum fsck_type type,
>> uint16_t slotnum)
>>   	return;
>>   }
>>   
>> -void corrupt_sys_file(ocfs2_filesys *fs, enum fsck_type type, uint16_t
>> slotnum)
>> +void corrupt_sys_file(ocfs2_filesys *fs, enum fsck_type type, uint16_t
>> slotnum, void *priv)
>>   {
>>   	void (*func)(ocfs2_filesys *fs, enum fsck_type type, uint16_t slotnum) =
>> NULL;
>>   
>> @@ -320,7 +321,7 @@ void corrupt_sys_file(ocfs2_filesys *fs, enum fsck_type
>> type, uint16_t slotnum)
>>   }
>>   
>>   void corrupt_group_desc(ocfs2_filesys *fs, enum fsck_type type,
>> -			uint16_t slotnum)
>> +			uint16_t slotnum, void *priv)
>>   {
>>   	void (*func)(ocfs2_filesys *fs, enum fsck_type type, uint16_t slotnum) =
>> NULL;
>>   
>> @@ -366,7 +367,7 @@ void corrupt_group_desc(ocfs2_filesys *fs, enum fsck_type
>> type,
>>   }
>>   
>>   void corrupt_local_alloc(ocfs2_filesys *fs, enum fsck_type type,
>> -			 uint16_t slotnum)
>> +			 uint16_t slotnum, void *priv)
>>   {
>>   	void (*func)(ocfs2_filesys *fs, enum fsck_type type, uint16_t slotnum) =
>> NULL;
>>   
>> @@ -406,7 +407,7 @@ void corrupt_local_alloc(ocfs2_filesys *fs, enum
>> fsck_type type,
>>   }
>>   
>>   void corrupt_truncate_log(ocfs2_filesys *fs, enum fsck_type type,
>> -			  uint16_t slotnum)
>> +			  uint16_t slotnum, void *priv)
>>   {
>>   	void (*func)(ocfs2_filesys *fs, enum fsck_type type, uint16_t slotnum) =
>> NULL;
>>   
>> @@ -436,7 +437,7 @@ void corrupt_truncate_log(ocfs2_filesys *fs, enum
>> fsck_type type,
>>   	return;
>>   }
>>   
>> -void corrupt_refcount(ocfs2_filesys *fs, enum fsck_type type, uint16_t
>> slotnum)
>> +void corrupt_refcount(ocfs2_filesys *fs, enum fsck_type type, uint16_t
>> slotnum, void *priv)
>>   {
>>   	uint64_t blkno;
>>   	void (*func)(ocfs2_filesys *fs,
>> @@ -476,7 +477,7 @@ void corrupt_refcount(ocfs2_filesys *fs, enum fsck_type
>> type, uint16_t slotnum)
>>   }
>>   
>>   void corrupt_discontig_bg(ocfs2_filesys *fs, enum fsck_type type,
>> -			  uint16_t slotnum)
>> +			  uint16_t slotnum, void *priv)
>>   {
>>   	mess_up_discontig_bg(fs, type, slotnum);
>>   }
>> diff --git a/fswreck/include/corrupt.h b/fswreck/include/corrupt.h
>> index 7037b16..a7fa3b4 100644
>> --- a/fswreck/include/corrupt.h
>> +++ b/fswreck/include/corrupt.h
>> @@ -25,18 +25,18 @@
>>   #ifndef __CORRUPT_H
>>   #define __CORRUPT_H
>>   
>> -void corrupt_file(ocfs2_filesys *fs, enum fsck_type type, uint16_t slotnum);
>> -void corrupt_sys_file(ocfs2_filesys *fs, enum fsck_type type, uint16_t
>> slotnum);
>> +void corrupt_file(ocfs2_filesys *fs, enum fsck_type type, uint16_t slotnum,
>> void *priv);
>> +void corrupt_sys_file(ocfs2_filesys *fs, enum fsck_type type, uint16_t
>> slotnum, void *priv);
>>   void corrupt_group_desc(ocfs2_filesys *fs, enum fsck_type type,
>> -			uint16_t slotnum);
>> -void corrupt_inode(ocfs2_filesys *fs, enum fsck_type type, uint16_t
>> slotnum);
>> +			uint16_t slotnum, void *priv);
>> +void corrupt_inode(ocfs2_filesys *fs, enum fsck_type type, uint16_t
>> slotnum, void *priv);
>>   void corrupt_local_alloc(ocfs2_filesys *fs, enum fsck_type type,
>> -			 uint16_t slotnum);
>> +			 uint16_t slotnum, void *priv);
>>   void corrupt_truncate_log(ocfs2_filesys *fs, enum fsck_type type,
>> -			  uint16_t slotnum);
>> -void corrupt_refcount(ocfs2_filesys *fs, enum fsck_type type, uint16_t
>> slotnum);
>> +			  uint16_t slotnum, void *priv);
>> +void corrupt_refcount(ocfs2_filesys *fs, enum fsck_type type, uint16_t
>> slotnum, void *priv);
>>   void corrupt_discontig_bg(ocfs2_filesys *fs, enum fsck_type type,
>> -			  uint16_t slotnum);
>> +			  uint16_t slotnum, void *priv);
>>   void create_named_directory(ocfs2_filesys *fs, char *name, uint64_t
>> *blkno);
>>   
>>   #endif		/* __CORRUPT_H */
>> diff --git a/fswreck/inode.c b/fswreck/inode.c
>> index 83adae2..085092b 100644
>> --- a/fswreck/inode.c
>> +++ b/fswreck/inode.c
>> @@ -176,13 +176,14 @@ static void damage_inode(ocfs2_filesys *fs, uint64_t
>> blkno,
>>   void mess_up_inode_field(ocfs2_filesys *fs, enum fsck_type type, uint64_t
>> blkno)
>>   {
>>   	errcode_t ret;
>> -	uint64_t tmpblkno;
>> +	uint64_t tmpblkno = blkno;
>>   	uint32_t clusters = 10;
>>   
>>   	char *buf = NULL;
>>   	struct ocfs2_dinode *di;
>>   
>> -	create_file(fs, blkno, &tmpblkno);
>> +	if (tmpblkno <= 0)
>> +		create_file(fs, blkno, &tmpblkno);
>>   
>>   	if ((type == INODE_SPARSE_SIZE) || (type == INODE_SPARSE_CLUSTERS)) {
>>   
>> @@ -311,7 +312,7 @@ void mess_up_inline_flag(ocfs2_filesys *fs, enum
>> fsck_type type, uint64_t blkno)
>>   	int i;
>>   	errcode_t ret;
>>   	char *buf = NULL, file_type[20];
>> -	uint64_t inline_blkno;
>> +	uint64_t inline_blkno = blkno;
>>   	struct ocfs2_dinode *di;
>>   	struct ocfs2_super_block *osb;
>>   
>> @@ -325,7 +326,7 @@ void mess_up_inline_flag(ocfs2_filesys *fs, enum
>> fsck_type type, uint64_t blkno)
>>   		FSWRK_COM_FATAL(progname, ret);
>>   
>>   	for (i = 0; i < 2; i++) {
>> -		if (i == 0) {
>> +		if (i == 0 && inline_blkno <=0) {
>>   			create_file(fs, blkno, &inline_blkno);
>>   			snprintf(file_type, 20, "%s", "Regular file");
>>   		} else {
>> @@ -361,7 +362,7 @@ void mess_up_inline_inode(ocfs2_filesys *fs, enum
>> fsck_type type, uint64_t blkno
>>   	int i;
>>   	errcode_t ret;
>>   	char *buf = NULL, file_type[20];
>> -	uint64_t inline_blkno;
>> +	uint64_t inline_blkno = blkno;
>>   	struct ocfs2_dinode *di;
>>   	uint16_t max_inline_sz;
>>   	struct ocfs2_super_block *osb;
>> @@ -376,7 +377,7 @@ void mess_up_inline_inode(ocfs2_filesys *fs, enum
>> fsck_type type, uint64_t blkno
>>   		FSWRK_COM_FATAL(progname, ret);
>>   
>>   	for (i = 0; i < 2; i++) {
>> -		if (i == 0) {
>> +		if (i == 0 && inline_blkno <= 0) {
>>   			create_file(fs, blkno, &inline_blkno);
>>   			snprintf(file_type, 20, "%s", "Regular file");
>>   		} else {
>> diff --git a/fswreck/main.c b/fswreck/main.c
>> index 016fc03..8763f2d 100644
>> --- a/fswreck/main.c
>> +++ b/fswreck/main.c
>> @@ -29,6 +29,7 @@ char *progname = NULL;
>>   
>>   static char *device = NULL;
>>   static uint16_t slotnum = UINT16_MAX;
>> +static uint64_t ino = 0;	/* deem 0 as an invalid inode number */
>>   
>>   static int corrupt[NUM_FSCK_TYPE];
>>   
>> @@ -37,8 +38,11 @@ struct prompt_code {
>>   	char		*pc_codestr;
>>   	char		*pc_fsfeat;
>>   	int		pc_slots;
>> -	void		(*pc_func)(ocfs2_filesys *fs,
>> -				   enum fsck_type code, uint16_t slotnum);
>> +	/* @priv: different implementations of func decide how to use it,
>> +	 * now only corrupt_file use this to get inode number.
>> +	 */
>> +	void		(*pc_func)(ocfs2_filesys *fs, enum fsck_type code,
>> +				   uint16_t slotnum, void *priv);
>>   	char		*pc_desc;
>>   };
>>   
>> @@ -344,12 +348,13 @@ static void usage(void)
>>   	g_print("%s is a program to corrupt a filesystem\n", progname);
>>           g_print("***** THIS WILL DAMAGE YOUR FILESYSTEM.  USE AT YOUR OWN
>> RISK. *****\n");
>>   	g_print("Usage: %s [-c corrupt-string] [-C corrupt-number] [-L corrupt-number] "
>> \
>> -		"[-N slot-number] [-nlM] [DEVICE]\n", progname);
>> +		"[-N slot-number] [-i inode-number] [-nlM] [DEVICE]\n", progname);
>>   	g_print("	-c, -C, Corrupt the file system\n");
>>   	g_print("	-L, Prints the corresponsing corrupt-string\n");
>>   	g_print("	-l, Lists all the corrupt codes\n");
>>   	g_print("	-n, Prints the total number of corrupt codes\n");
>>   	g_print("	-M, Prints the mkfs options\n");
>> +	g_print("	-i, Corrupt this inode instead of creating one internally\n");
>>   	exit(0);
>>   }
>>   
>> @@ -451,6 +456,7 @@ static int read_options(int argc, char **argv)
>>   {
>>   	int i, c, listcode = 0, showmkfs = 0;
>>   	int ret = 0;
>> +	char *dummy = NULL;
>>   
>>   	progname = basename(argv[0]);
>>   
>> @@ -458,7 +464,7 @@ static int read_options(int argc, char **argv)
>>   		usage();
>>   
>>   	while (1) {
>> -		c = getopt(argc, argv, "lnc:C:N:L:M");
>> +		c = getopt(argc, argv, "lnc:C:N:L:i:M");
>>   		if (c == -1)
>>   			break;
>>   
>> @@ -495,6 +501,13 @@ static int read_options(int argc, char **argv)
>>   		case 'N':	/* slotnum */
>>   			slotnum = strtoul(optarg, NULL, 0);
>>   			break;
>> +		case 'i':	/* inode number */
>> +			ino = strtoul(optarg, &dummy, 0);
>> +			if (!ino || *dummy !='\0') {
>> +				fprintf(stderr, "Invalid inode number.\n");
>> +				exit(-1);
>> +			}
>> +			break;
>>   
>>   		default:
>>   			ret = -1;
>> @@ -597,7 +610,7 @@ int main(int argc, char **argv)
>>   				progname, device, prompt_codes[i].pc_codestr,
>>   				prompt_codes[i].pc_codenum);
>>   			prompt_codes[i].pc_func(fs, prompt_codes[i].pc_codenum,
>> -						slotnum);
>> +						slotnum, &ino);
>>   		}
>>   	}
>>   
>> -- 
>> 2.6.2




More information about the Ocfs2-tools-devel mailing list