Exemplo n.º 1
0
/**
 * Write the new rg information to disk.
 */
static unsigned initialize_new_portion(struct gfs2_sbd *sdp, lgfs2_rgrps_t rgs)
{
	unsigned rgcount = 0;
	uint64_t rgaddr = fssize;

	discard_blocks(sdp->device_fd, rgaddr * sdp->bsize, fsgrowth * sdp->bsize);
	/* Build the remaining resource groups */
	while (1) {
		int err = 0;
		lgfs2_rgrp_t rg;
		struct gfs2_rindex ri;
		rgaddr = lgfs2_rindex_entry_new(rgs, &ri, rgaddr, 0);
		if (rgaddr == 0)
			break;
		rg = lgfs2_rgrps_append(rgs, &ri);
		if (rg == NULL) {
			perror(_("Failed to create resource group"));
			return 0;
		}
		if (metafs_interrupted)
			return 0;
		if (!test)
			err = lgfs2_rgrp_write(sdp->device_fd, rg);
		if (err != 0) {
			perror(_("Failed to write resource group"));
			return 0;
		}
		rgcount++;
	}
	fsync(sdp->device_fd);
	return rgcount;
}
Exemplo n.º 2
0
int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret,
			   u64 max_block_count, int *mixed, int nodiscard)
{
	u64 block_count;
	u64 bytenr;
	struct stat st;
	int i, ret;

	ret = fstat(fd, &st);
	if (ret < 0) {
		fprintf(stderr, "unable to stat %s\n", file);
		exit(1);
	}

	block_count = btrfs_device_size(fd, &st);
	if (block_count == 0) {
		fprintf(stderr, "unable to find %s size\n", file);
		exit(1);
	}
	if (max_block_count)
		block_count = min(block_count, max_block_count);
	zero_end = 1;

	if (block_count < 1024 * 1024 * 1024 && !(*mixed)) {
		printf("SMALL VOLUME: forcing mixed metadata/data groups\n");
		*mixed = 1;
	}

	if (!nodiscard) {
		/*
		 * We intentionally ignore errors from the discard ioctl.  It is
		 * not necessary for the mkfs functionality but just an optimization.
		 */
		discard_blocks(fd, 0, block_count);
	}

	ret = zero_dev_start(fd);
	if (ret) {
		fprintf(stderr, "failed to zero device start %d\n", ret);
		exit(1);
	}

	for (i = 0 ; i < BTRFS_SUPER_MIRROR_MAX; i++) {
		bytenr = btrfs_sb_offset(i);
		if (bytenr >= block_count)
			break;
		zero_blocks(fd, bytenr, BTRFS_SUPER_INFO_SIZE);
	}

	if (zero_end) {
		ret = zero_dev_end(fd, block_count);
		if (ret) {
			fprintf(stderr, "failed to zero device end %d\n", ret);
			exit(1);
		}
	}
	*block_count_ret = block_count;
	return 0;
}