[libvirt] [PATCH 1/2] Introduce posix_fallocate() to allocate disk space

Daniel P. Berrange berrange at redhat.com
Thu Mar 19 10:39:05 UTC 2009


On Thu, Mar 19, 2009 at 12:45:24PM +0530, Amit Shah wrote:
> diff --git a/src/util.c b/src/util.c
> index 66ad9a4..b69d33a 100644
> --- a/src/util.c
> +++ b/src/util.c
> @@ -117,6 +117,26 @@ ssize_t safewrite(int fd, const void *buf, size_t count)
>          return nwritten;
>  }
>  
> +#if 1
> +int safezero(int fd, int flags, off_t offset, off_t len)
> +{
> +    return posix_fallocate(fd, offset, len);
> +}
> +#else
> +int safezero(int fd, int flags, off_t offset, off_t len)
> +{
> +    char *buf;
> +    int r;
> +
> +    buf = calloc(len, sizeof(char));
> +    if (buf == NULL)
> +        return -ENOMEM;
> +
> +    r = safewrite(fd, buf, len);
> +    return r;
> +}
> +#endif

For memory allocation you can use  

   if (VIR_ALLOC_N(buf, len) < 0)
      return -1;

Also, you'll need a VIR_FREE(buf) call in there after the safewrite().

I'm a little worried about scalability of this impl though. The later
patch will call this with 500 MB chunks if progress is turned on, or
even allocate the whole file in one chunk - we don't want to be 
allocating 20 GB of memory jus to write zero's to a file ! Is it
perhaps easier just to  do something like

   char * buf = mmap(NULL, len, MAP_SHARED, MAP_ANONYMOUS, fd, offset)
   memset(buf, 0, len);
   munmap(buf, len);

Or, do your calloc() of a 1 MB chunk, and then call safewrite in a
loop, just to avoid too large a memory allocation.

Daniel
-- 
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|




More information about the libvir-list mailing list