[sheepdog] [PATCH v2 1/4] sheep: get the free disk space and store it in the config file

MORITA Kazutaka morita.kazutaka at lab.ntt.co.jp
Sat Aug 4 08:04:05 CEST 2012


At Fri,  3 Aug 2012 20:50:08 +0800,
levin li wrote:
> 
> From: levin li <xingke.lwp at taobao.com>
> 
> We only specify the free disk space for the first boot, after that
> we store the size into the config file.
> 
> Signed-off-by: levin li <xingke.lwp at taobao.com>
> ---
>  sheep/sheep_priv.h |    3 ++
>  sheep/store.c      |   81 ++++++++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 84 insertions(+), 0 deletions(-)
> 
> diff --git a/sheep/sheep_priv.h b/sheep/sheep_priv.h
> index 0c0e588..2a92f0a 100644
> --- a/sheep/sheep_priv.h
> +++ b/sheep/sheep_priv.h
> @@ -88,6 +88,7 @@ struct cluster_info {
>  	uint16_t flags;
>  
>  	uint64_t cache_size;
> +	uint64_t disk_space;

If you use uint64_t here,

>  struct sheepdog_config {
>  	uint64_t ctime;
> +	uint32_t space;

use the same type here.

>  	uint16_t flags;
>  	uint8_t copies;
>  	uint8_t store[STORE_LEN];
> @@ -443,6 +445,32 @@ static int init_store_driver(void)
>  	return sd_store->init(obj_path);
>  }
>  
> +static int init_disk_space(const char *base_path)
> +{
> +	int ret = SD_RES_SUCCESS;
> +	uint32_t space_size = 0;

Same here.

> +	struct statfs sf;
> +
> +	ret = get_cluster_space(&space_size);
> +	if (space_size != 0) {
> +		sys->disk_space = space_size;
> +		goto out;
> +	}
> +
> +	ret = statfs(base_path, &sf);
> +	if (ret < 0) {
> +		dprintf("get disk space failed %m\n");
> +		ret = SD_RES_EIO;
> +		goto out;
> +	}
> +
> +	sys->disk_space = sf.f_bfree * 4 / 1024;
> +	ret = set_cluster_space(sys->disk_space);
> +out:
> +	dprintf("disk free space is %" PRIu64 "M\n", sys->disk_space);
> +	return ret;
> +}
> +
>  int init_store(const char *d, int enable_write_cache)
>  {
>  	int ret;
> @@ -467,6 +495,10 @@ int init_store(const char *d, int enable_write_cache)
>  	if (ret)
>  		return ret;
>  
> +	ret = init_disk_space(d);
> +	if (ret)
> +		return ret;
> +
>  	if (!sys->gateway_only) {
>  		ret = init_store_driver();
>  		if (ret)
> @@ -716,3 +748,52 @@ int get_cluster_store(char *buf)
>  out:
>  	return ret;
>  }
> +
> +int set_cluster_space(uint32_t space)

Same here.

> +{
> +	int fd, ret = SD_RES_EIO;
> +	void *jd;
> +
> +	fd = open(config_path, O_DSYNC | O_WRONLY);
> +	if (fd < 0)
> +		goto out;
> +
> +	jd = jrnl_begin(&space, sizeof(space),
> +			offsetof(struct sheepdog_config, space),
> +			config_path, jrnl_path);
> +	if (!jd) {
> +		ret = SD_RES_EIO;
> +		goto err;
> +	}
> +	ret = xpwrite(fd, &space, sizeof(space),
> +		      offsetof(struct sheepdog_config, space));
> +	if (ret != sizeof(space))
> +		ret = SD_RES_EIO;
> +	else
> +		ret = SD_RES_SUCCESS;
> +	jrnl_end(jd);
> +err:
> +	close(fd);
> +out:
> +	return ret;
> +}
> +
> +int get_cluster_space(uint32_t *space)

Same here.

Thanks,

Kazutaka

> +{
> +	int fd, ret = SD_RES_EIO;
> +
> +	fd = open(config_path, O_RDONLY);
> +	if (fd < 0)
> +		goto out;
> +
> +	ret = xpread(fd, space, sizeof(*space),
> +		     offsetof(struct sheepdog_config, space));
> +	if (ret != sizeof(*space))
> +		ret = SD_RES_EIO;
> +	else
> +		ret = SD_RES_SUCCESS;
> +
> +	close(fd);
> +out:
> +	return ret;
> +}
> -- 
> 1.7.1
> 
> -- 
> sheepdog mailing list
> sheepdog at lists.wpkg.org
> http://lists.wpkg.org/mailman/listinfo/sheepdog



More information about the sheepdog mailing list