bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen#include "lib.h"
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen#include "ioloop.h"
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen#include "memarea.h"
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen#include "mmap-util.h"
0536ccb51d41e3078c3a9fa33e509fb4b2420f95Timo Sirainen#include "istream-private.h"
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen#include <unistd.h>
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen#include <sys/stat.h>
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainenstruct mmap_memarea {
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen void *mmap_base;
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen size_t mmap_size;
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen};
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainenstruct mmap_istream {
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen struct istream_private istream;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen struct timeval fstat_cache_stamp;
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen off_t mmap_offset;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen uoff_t v_size;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool autoclose_fd:1;
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen};
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainenstatic size_t mmap_pagemask = 0;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainenstatic void i_stream_mmap_close(struct iostream_private *stream,
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen bool close_parent ATTR_UNUSED)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen{
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen struct mmap_istream *mstream = (struct mmap_istream *) stream;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen if (mstream->autoclose_fd && mstream->istream.fd != -1) {
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen if (close(mstream->istream.fd) < 0) {
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen i_error("mmap_istream.close(%s) failed: %m",
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen i_stream_get_name(&mstream->istream.istream));
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen }
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen }
2c25e1360d4b5cc55eda969a3a7204d950de5a8fTimo Sirainen mstream->istream.fd = -1;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen}
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainenstatic void i_stream_munmap(struct mmap_istream *mstream)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen{
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen struct istream_private *_stream = &mstream->istream;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (_stream->buffer != NULL) {
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen if (_stream->memarea != NULL)
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen memarea_unref(&_stream->memarea);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen _stream->buffer = NULL;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen _stream->buffer_size = 0;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen mstream->mmap_offset = 0;
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen } else {
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen i_assert(_stream->memarea == NULL);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen }
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen}
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainenstatic void i_stream_mmap_destroy(struct iostream_private *stream)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen{
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen struct mmap_istream *mstream = (struct mmap_istream *) stream;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen i_stream_munmap(mstream);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen}
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainenstatic size_t mstream_get_mmap_block_size(struct istream_private *stream)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen{
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen return (stream->max_buffer_size + mmap_get_page_size() - 1) & ~
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen (mmap_get_page_size() - 1);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen}
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainenstatic void i_stream_mmap_marea_free(struct mmap_memarea *marea)
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen{
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen if (munmap(marea->mmap_base, marea->mmap_size) < 0)
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen i_error("mmap_istream.munmap() failed: %m");
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen}
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainenstatic ssize_t i_stream_mmap_read(struct istream_private *stream)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen{
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen struct mmap_istream *mstream = (struct mmap_istream *) stream;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen size_t aligned_skip;
af81f402ddc897c74c1e85abd02879612ce44882Timo Sirainen uoff_t top;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (stream->pos < stream->buffer_size) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen /* more bytes available without needing to mmap() */
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen stream->pos = stream->buffer_size;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen return stream->pos - stream->skip;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen }
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen
9e095dd6a77097356aca8216356d4d71ef1bea45Timo Sirainen if (stream->istream.v_offset >= mstream->v_size) {
9e095dd6a77097356aca8216356d4d71ef1bea45Timo Sirainen stream->istream.eof = TRUE;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen return -1;
9e095dd6a77097356aca8216356d4d71ef1bea45Timo Sirainen }
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen aligned_skip = stream->skip & ~mmap_pagemask;
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen if (aligned_skip == 0 && stream->buffer != NULL) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen /* didn't skip enough bytes */
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen return -2;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen }
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen stream->skip -= aligned_skip;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen mstream->mmap_offset += aligned_skip;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen if (stream->memarea != NULL)
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen memarea_unref(&stream->memarea);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
b3142c8e513bc78da821fa70f479016148fa95e5Timo Sirainen top = mstream->v_size - mstream->mmap_offset;
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen stream->buffer_size = I_MIN(top, mstream_get_mmap_block_size(stream));
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen i_assert((uoff_t)mstream->mmap_offset + stream->buffer_size <=
b3142c8e513bc78da821fa70f479016148fa95e5Timo Sirainen mstream->v_size);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
67c24901ac5e1521e38a91efc452faeb3e2135a1Timo Sirainen if (stream->buffer_size == 0) {
67c24901ac5e1521e38a91efc452faeb3e2135a1Timo Sirainen /* don't bother even trying mmap */
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen stream->buffer = NULL;
67c24901ac5e1521e38a91efc452faeb3e2135a1Timo Sirainen } else {
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen void *mmap_base =
67c24901ac5e1521e38a91efc452faeb3e2135a1Timo Sirainen mmap(NULL, stream->buffer_size, PROT_READ, MAP_PRIVATE,
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen stream->fd, mstream->mmap_offset);
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen if (mmap_base == MAP_FAILED) {
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen i_assert(errno != 0);
67c24901ac5e1521e38a91efc452faeb3e2135a1Timo Sirainen stream->istream.stream_errno = errno;
67c24901ac5e1521e38a91efc452faeb3e2135a1Timo Sirainen stream->buffer = NULL;
67c24901ac5e1521e38a91efc452faeb3e2135a1Timo Sirainen stream->buffer_size = 0;
67c24901ac5e1521e38a91efc452faeb3e2135a1Timo Sirainen stream->skip = stream->pos = 0;
1c6f6f5bef70f16546b3bc8f4cd5f93f373e82a2Timo Sirainen io_stream_set_error(&stream->iostream,
1c6f6f5bef70f16546b3bc8f4cd5f93f373e82a2Timo Sirainen "mmap() failed: %m");
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen i_error("mmap_istream.mmap(%s) failed: %m",
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen i_stream_get_name(&stream->istream));
67c24901ac5e1521e38a91efc452faeb3e2135a1Timo Sirainen return -1;
67c24901ac5e1521e38a91efc452faeb3e2135a1Timo Sirainen }
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen stream->buffer = mmap_base;
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen struct mmap_memarea *marea = i_new(struct mmap_memarea, 1);
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen marea->mmap_base = mmap_base;
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen marea->mmap_size = stream->buffer_size;
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen stream->memarea = memarea_init(mmap_base, marea->mmap_size,
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen i_stream_mmap_marea_free, marea);
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen if (stream->buffer_size > mmap_get_page_size()) {
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen if (madvise(mmap_base, stream->buffer_size,
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen MADV_SEQUENTIAL) < 0) {
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen i_error("mmap_istream.madvise(%s): %m",
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen i_stream_get_name(&stream->istream));
d74c9f4bf7ee37f3f58a895032ea013d07294865Timo Sirainen }
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen }
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen }
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen stream->pos = stream->buffer_size;
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen i_assert(stream->pos - stream->skip > 0);
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen return stream->pos - stream->skip;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen}
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainenstatic void i_stream_mmap_seek(struct istream_private *stream, uoff_t v_offset,
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen bool mark ATTR_UNUSED)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen{
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen struct mmap_istream *mstream = (struct mmap_istream *) stream;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (stream->buffer_size != 0 &&
b3142c8e513bc78da821fa70f479016148fa95e5Timo Sirainen (uoff_t)mstream->mmap_offset <= v_offset &&
b3142c8e513bc78da821fa70f479016148fa95e5Timo Sirainen (uoff_t)mstream->mmap_offset + stream->buffer_size > v_offset) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen /* already mmaped */
b3142c8e513bc78da821fa70f479016148fa95e5Timo Sirainen stream->skip = stream->pos = v_offset - mstream->mmap_offset;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen } else {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen /* force reading next time */
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen i_stream_munmap(mstream);
b3142c8e513bc78da821fa70f479016148fa95e5Timo Sirainen stream->skip = stream->pos = v_offset;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen }
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen stream->istream.v_offset = v_offset;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen}
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainenstatic void i_stream_mmap_sync(struct istream_private *stream)
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen{
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen struct mmap_istream *mstream = (struct mmap_istream *) stream;
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen i_stream_munmap(mstream);
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen stream->skip = stream->pos = stream->istream.v_offset;
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen mstream->fstat_cache_stamp.tv_sec = 0;
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen}
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainenstatic int fstat_cached(struct mmap_istream *mstream)
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen{
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen if (mstream->fstat_cache_stamp.tv_sec == ioloop_timeval.tv_sec &&
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen mstream->fstat_cache_stamp.tv_usec == ioloop_timeval.tv_usec)
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen return 0;
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen if (fstat(mstream->istream.fd, &mstream->istream.statbuf) < 0) {
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen i_error("mmap_istream.fstat(%s) failed: %m",
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen i_stream_get_name(&mstream->istream.istream));
90126be6a0a7a44f67bdf6bb983b29f6d8e3685aAki Tuomi mstream->istream.istream.stream_errno = errno;
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen return -1;
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen }
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen mstream->fstat_cache_stamp = ioloop_timeval;
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen return 0;
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen}
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen
01f4ee4a0243f3fe9af763e1a540cd5cff0d63f5Timo Sirainenstatic int
a94936bafd127680184da114c6a177b37ff656e5Timo Siraineni_stream_mmap_stat(struct istream_private *stream, bool exact ATTR_UNUSED)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen{
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen struct mmap_istream *mstream = (struct mmap_istream *) stream;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen
01f4ee4a0243f3fe9af763e1a540cd5cff0d63f5Timo Sirainen return fstat_cached(mstream);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen}
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
fe363b433b8038a69b55169da9dca27892ad7d18Timo Sirainenstruct istream *i_stream_create_mmap(int fd, size_t block_size,
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen uoff_t start_offset, uoff_t v_size,
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen bool autoclose_fd)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen{
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen struct mmap_istream *mstream;
03f5c621d06d6b6d77a145196c9633a7aa64dc78Timo Sirainen struct istream *istream;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen struct stat st;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
e11a64ffc7f08b4cb05bcc27668d154d33d0c2e0Timo Sirainen if (mmap_pagemask == 0)
e11a64ffc7f08b4cb05bcc27668d154d33d0c2e0Timo Sirainen mmap_pagemask = mmap_get_page_size()-1;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
90126be6a0a7a44f67bdf6bb983b29f6d8e3685aAki Tuomi mstream = i_new(struct mmap_istream, 1);
90126be6a0a7a44f67bdf6bb983b29f6d8e3685aAki Tuomi
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (v_size == 0) {
90126be6a0a7a44f67bdf6bb983b29f6d8e3685aAki Tuomi if (fstat(fd, &st) < 0) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen i_error("i_stream_create_mmap(): fstat() failed: %m");
90126be6a0a7a44f67bdf6bb983b29f6d8e3685aAki Tuomi mstream->istream.istream.eof = TRUE;
90126be6a0a7a44f67bdf6bb983b29f6d8e3685aAki Tuomi mstream->istream.istream.stream_errno = errno;
90126be6a0a7a44f67bdf6bb983b29f6d8e3685aAki Tuomi } else {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen v_size = st.st_size;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (start_offset > v_size)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen start_offset = v_size;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen v_size -= start_offset;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen }
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen }
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen mstream->autoclose_fd = autoclose_fd;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen mstream->v_size = v_size;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen mstream->istream.iostream.close = i_stream_mmap_close;
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen mstream->istream.iostream.destroy = i_stream_mmap_destroy;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen mstream->istream.max_buffer_size = block_size;
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen mstream->istream.read = i_stream_mmap_read;
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen mstream->istream.seek = i_stream_mmap_seek;
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen mstream->istream.sync = i_stream_mmap_sync;
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen mstream->istream.stat = i_stream_mmap_stat;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
da2aa032ccfa8e7e4a4380ef738014549f4d2c2dTimo Sirainen mstream->istream.istream.readable_fd = TRUE;
d28179fd78550a58be44dcb1e3e830ab7d33172dTimo Sirainen mstream->istream.start_offset = start_offset;
2974dca6be5120e49279f06c8aa952e5fac56048Timo Sirainen istream = i_stream_create(&mstream->istream, NULL, fd, 0);
03f5c621d06d6b6d77a145196c9633a7aa64dc78Timo Sirainen istream->mmaped = TRUE;
c06f4017027263cf3a08becc551f5126409e2a83Timo Sirainen istream->blocking = TRUE;
411d6baa37f31d90730e90c4a28c43e1974bbe58Timo Sirainen istream->seekable = TRUE;
03f5c621d06d6b6d77a145196c9633a7aa64dc78Timo Sirainen return istream;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen}