mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-03-07 17:28:15 +00:00
virfile: safezero: align mmap offset to page size
mmap's offset must be aligned to page size or mapping will fail. mmap-based safezero is only used if posix_fallocate isn't available. Signed-off-by: Oskari Saarenmaa <os@ohmu.fi>
This commit is contained in:
parent
11d9dd7ba0
commit
edc61bf63e
@ -1038,9 +1038,16 @@ safezero(int fd, off_t offset, off_t len)
|
|||||||
int
|
int
|
||||||
safezero(int fd, off_t offset, off_t len)
|
safezero(int fd, off_t offset, off_t len)
|
||||||
{
|
{
|
||||||
|
static long pagemask = 0;
|
||||||
|
off_t map_skip;
|
||||||
int r;
|
int r;
|
||||||
char *buf;
|
char *buf;
|
||||||
|
|
||||||
|
/* align offset and length, rounding offset down and length up */
|
||||||
|
if (pagemask == 0)
|
||||||
|
pagemask = ~(sysconf(_SC_PAGESIZE) - 1);
|
||||||
|
map_skip = offset - (offset & pagemask);
|
||||||
|
|
||||||
/* memset wants the mmap'ed file to be present on disk so create a
|
/* memset wants the mmap'ed file to be present on disk so create a
|
||||||
* sparse file
|
* sparse file
|
||||||
*/
|
*/
|
||||||
@ -1048,12 +1055,13 @@ safezero(int fd, off_t offset, off_t len)
|
|||||||
if (r < 0)
|
if (r < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
buf = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
|
buf = mmap(NULL, len + map_skip, PROT_READ | PROT_WRITE, MAP_SHARED,
|
||||||
|
fd, offset - map_skip);
|
||||||
if (buf == MAP_FAILED)
|
if (buf == MAP_FAILED)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
memset(buf, 0, len);
|
memset(buf + map_skip, 0, len);
|
||||||
munmap(buf, len);
|
munmap(buf, len + map_skip);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user