#include "db_config.h"
#include "db_int.h"
int
__os_zerofill(dbenv, fhp)
DB_ENV *dbenv;
DB_FH *fhp;
{
#ifdef HAVE_FILESYSTEM_NOTZERO
off_t stat_offset, write_offset;
size_t blen, nw;
u_int32_t bytes, mbytes;
int group_sync, need_free, ret;
u_int8_t buf[8 * 1024], *bp;
if (dbenv != NULL && FLD_ISSET(dbenv->verbose, DB_VERB_FILEOPS_ALL))
__db_msg(dbenv, "fileops: zero-fill %s", fhp->name);
write_offset = (off_t)fhp->pgno * fhp->pgsize + fhp->offset;
if ((ret = __os_ioinfo(dbenv, NULL, fhp, &mbytes, &bytes, NULL)) != 0)
return (ret);
stat_offset = (off_t)mbytes * MEGABYTE + bytes;
if (stat_offset >= write_offset)
return (0);
#undef ZF_LARGE_WRITE
#define ZF_LARGE_WRITE (64 * 1024)
if (write_offset - stat_offset > ZF_LARGE_WRITE) {
if ((ret = __os_calloc(dbenv, 1, ZF_LARGE_WRITE, &bp)) != 0)
return (ret);
blen = ZF_LARGE_WRITE;
need_free = 1;
} else {
bp = buf;
blen = sizeof(buf);
need_free = 0;
memset(buf, 0, sizeof(buf));
}
if ((ret = __os_seek(dbenv, fhp, mbytes, MEGABYTE, bytes)) != 0)
goto err;
for (group_sync = 0; stat_offset < write_offset; group_sync = 1) {
if (write_offset - stat_offset <= (off_t)blen) {
blen = (size_t)(write_offset - stat_offset);
if (group_sync && (ret = __os_fsync(dbenv, fhp)) != 0)
goto err;
}
if ((ret = __os_physwrite(dbenv, fhp, bp, blen, &nw)) != 0)
goto err;
stat_offset += blen;
}
if ((ret = __os_fsync(dbenv, fhp)) != 0)
goto err;
mbytes = (u_int32_t)(write_offset / MEGABYTE);
bytes = (u_int32_t)(write_offset % MEGABYTE);
ret = __os_seek(dbenv, fhp, mbytes, MEGABYTE, bytes);
err: if (need_free)
__os_free(dbenv, bp);
return (ret);
#else
COMPQUIET(dbenv, NULL);
COMPQUIET(fhp, NULL);
return (0);
#endif
}