llverfs.c

来自「lustre 1.6.5 source code」· C语言 代码 · 共 651 行 · 第 1/2 页

C
651
字号
/* * dir_write: This function writes directories and files on device. * it works for both full and fast modes. */static int dir_write(char *chunk_buf, size_t chunksize,		     time_t time_st, unsigned long dir_num){	char tempfile[PATH_MAX];	char tempdir[PATH_MAX];	struct stat64 file;	int file_num = 999999999;	ino_t inode_st = 0;#ifdef HAVE_EXT2FS_EXT2FS_H	if (!full && fsetflags(testdir, EXT2_TOPDIR_FL))		fprintf(stderr,			"\n%s: can't set TOPDIR_FL on %s: %s (ignoring)\n",			progname, testdir, strerror(errno));#endif	for (; dir_num < num_dirs; num_files++, file_num++) {		if (file_num >= files_in_dir) {			if (dir_num == num_dirs - 1)				break;			file_num = 0;			if (mkdir(new_dir(tempdir, dir_num), dirmode) < 0) {				if (errno == ENOSPC)					break;				if (errno != EEXIST) {					fprintf(stderr, "\n%s: mkdir %s : %s\n",						progname, tempdir,						strerror(errno));					return 1;				}			}			dir_num++;		}		fd = open_file(new_file(tempfile, tempdir, file_num),			       O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE);		if (fd >= 0 && fstat64(fd, &file) == 0) {			inode_st = file.st_ino;		} else {			fprintf(stderr, "\n%s: write stat64 to file %s: %s",				progname, tempfile, strerror(errno));			exit(1);		}		if (verbose > 1)			show_filename("write", tempfile);		if (write_chunks(fd, 0, file_size, chunk_buf, chunksize,				 time_st, inode_st, tempfile)) {			close(fd);			return 1;		}		close(fd);		if (errno_local == ENOSPC)			break;	}	if (verbose) {		verbose++;		show_filename("write", tempfile);		printf("\nwrite complete\n");		verbose--;	}	return 0;}/* * dir_read: This function reads directories and files on device. * it works for both full and fast modes. */static int dir_read(char *chunk_buf, size_t chunksize,		    time_t time_st, unsigned long dir_num){	char tempfile[PATH_MAX];	char tempdir[PATH_MAX];	unsigned long count = 0;	struct stat64 file;	int file_num = 0;	ino_t inode_st = 0;	for (count = 0; count < num_files && dir_num < num_dirs; count++) {		if (file_num == 0) {			if (dir_num == num_dirs - 1)				break;			new_dir(tempdir, dir_num);			dir_num++;		}		fd = open_file(new_file(tempfile, tempdir, file_num),			       O_RDONLY | O_LARGEFILE);		if (fd >= 0 && fstat64(fd, &file) == 0) {			inode_st = file.st_ino;		} else {			fprintf(stderr, "\n%s: read stat64 file '%s': %s\n",				progname, tempfile, strerror(errno));			return 1;		}		if (verbose > 1)			show_filename("read", tempfile);		if (count == num_files)			file_size = file.st_size;		if (read_chunks(fd, 0, file_size, chunk_buf, chunksize,				time_st, inode_st, tempfile)) {			close(fd);			return 1;		}		close(fd);		if (++file_num >= files_in_dir)			file_num = 0;	}	if (verbose > 1){		verbose++;		show_filename("read", tempfile);		printf("\nread complete\n");		verbose--;	}	return 0;}int main(int argc, char **argv){	time_t time_st = 0;		/* Default timestamp */	size_t chunksize = ONE_MB;	/* IO chunk size(defailt=1MB) */	char *chunk_buf;		/* chunk buffer */	int error = 0;	FILE *countfile = NULL;	char filecount[PATH_MAX];	unsigned long dir_num = 0, dir_num_orig = 0;/* starting directory */	char c;	progname = strrchr(argv[0], '/') ? strrchr(argv[0], '/') + 1 : argv[0];	while ((c = (char)getopt_long(argc, argv, "t:rwvplo:h",				      longopts, NULL)) != -1) {		switch (c) {		case 'c':			chunksize = (strtoul(optarg, NULL, 0) * ONE_MB);			if (!chunksize) {				fprintf(stderr, "%s: Chunk size value should be"					"a multiple of 1MB\n", progname);				return -1;			}			break;		case 'l':			full = 1;			break;		case 'o': /* offset */			dir_num = strtoul(optarg, NULL, 0);			break;		case 'p':			full = 0;			break;		case 'q':			verbose = 0;			break;		case 'r':			readoption = 1;			break;		case 't':			time_st = (time_t)strtoul(optarg, NULL, 0);			break;		case 'w':			writeoption = 1;			break;		case 'v':			verbose++;			break;		case 'h':		default:			usage(1);			return 0;		}	}	testdir = argv[optind];	if (!testdir) {		fprintf(stderr, "%s: pathname not given\n", progname);		usage(1);		return -1;	}	if (!readoption && !writeoption) {		readoption = 1;		writeoption = 1;	}	if (!time_st)		(void) time(&time_st);	printf("Timestamp: %lu\n", (unsigned long )time_st);	isatty_flag = isatty(STDOUT_FILENO);	if (!full) {#ifdef HAVE_EXT2FS_EXT2FS_H		struct mntent *tempmnt;		FILE *fp = NULL;		ext2_filsys fs;		if ((fp = setmntent("/etc/mtab", "r")) == NULL){			fprintf(stderr, "%s: fail to open /etc/mtab in read"				"mode :%s\n", progname, strerror(errno));			goto guess;		}		/* find device name using filesystem */		while ((tempmnt = getmntent(fp)) != NULL) {			if (strcmp(tempmnt->mnt_dir, testdir) == 0)				break;		}		if (tempmnt == NULL) {			fprintf(stderr, "%s: no device found for '%s'\n",				progname, testdir);			endmntent(fp);			goto guess;		}		if (ext2fs_open(tempmnt->mnt_fsname, 0, 0, 0,				unix_io_manager, &fs)) {			fprintf(stderr, "%s: unable to open ext3 fs on '%s'\n",				progname, testdir);			endmntent(fp);			goto guess;		}		endmntent(fp);		num_dirs = (fs->super->s_blocks_count +			    fs->super->s_blocks_per_group - 1) /			fs->super->s_blocks_per_group;		if (verbose)			printf("ext3 block groups: %u, fs blocks: %u "			       "blocks per group: %u\n",			       num_dirs, fs->super->s_blocks_count,			       fs->super->s_blocks_per_group);		ext2fs_close(fs);#else                goto guess;#endif		if (0) { /* ugh */			struct statfs64 statbuf;		guess:			if (statfs64(testdir, &statbuf) == 0) {				num_dirs = (long long)statbuf.f_blocks *					statbuf.f_bsize / (128ULL << 20);				if (verbose)					printf("dirs: %u, fs blocks: %llu\n",					       num_dirs,					       (long long)statbuf.f_blocks);			} else {				fprintf(stderr, "%s: unable to stat '%s': %s\n",					progname, testdir, strerror(errno));				if (verbose)					printf("dirs: %u\n", num_dirs);			}		}		file_size = ONE_MB;		chunksize = ONE_MB;		files_in_dir = 1;	}	chunk_buf = (char *)calloc(chunksize, 1);	if (chunk_buf == NULL) {		fprintf(stderr, "Memory allocation failed for chunk_buf\n");		return 4;	}	sprintf(filecount, "%s/%s.filecount", testdir, progname);	if (writeoption) {		(void)mkdir(testdir, dirmode);		unlink(filecount);		if (dir_num != 0) {			num_files = dir_num * files_in_dir;			if (verbose)				printf("\n%s: %lu files already written\n",				       progname, num_files);		}		if (dir_write(chunk_buf, chunksize, time_st, dir_num)) {			error = 3;			goto out;		}		countfile = fopen(filecount, "w");		if (countfile != NULL) {			if (fprintf(countfile, "%lu", num_files) < 1 ||			    fflush(countfile) != 0) {				fprintf(stderr, "\n%s: writing %s failed :%s\n",					progname, filecount, strerror(errno));			}			fclose(countfile);		}		dir_num = dir_num_orig;	}	if (readoption) {		if (!writeoption) {			countfile = fopen(filecount, "r");			if (countfile == NULL ||			    fscanf(countfile, "%lu", &num_files) != 1) {				fprintf(stderr, "\n%s: reading %s failed :%s\n",					progname, filecount, strerror(errno));				num_files = num_dirs * files_in_dir;			} else {				num_files -= (dir_num * files_in_dir);			}			if (countfile)				fclose(countfile);		}		if (dir_read(chunk_buf, chunksize, time_st, dir_num)) {			fprintf(stderr, "\n%s: Data verification failed\n",				progname) ;			error = 2;			goto out;		}	}	error = 0;out:	free(chunk_buf);	return error;}

⌨️ 快捷键说明

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