return -EADDRNOTAVAIL;
}
- return mmap_cache_get(f->mmap, f->cache_fd, f->prot, type_to_context(type), keep_always, offset, size, &f->last_stat, ret, ret_size);
+ return mmap_cache_get(f->mmap, f->cache_fd, type_to_context(type), keep_always, offset, size, &f->last_stat, ret, ret_size);
}
static uint64_t minimum_header_size(Object *o) {
.mode = mode,
.flags = flags,
- .prot = prot_from_flags(flags),
.writable = (flags & O_ACCMODE) != O_RDONLY,
#if HAVE_ZSTD
goto fail;
}
- f->cache_fd = mmap_cache_add_fd(f->mmap, f->fd);
+ f->cache_fd = mmap_cache_add_fd(f->mmap, f->fd, prot_from_flags(flags));
if (!f->cache_fd) {
r = -ENOMEM;
goto fail;
goto fail;
}
- r = mmap_cache_get(f->mmap, f->cache_fd, f->prot, CONTEXT_HEADER, true, 0, PAGE_ALIGN(sizeof(Header)), &f->last_stat, &h, NULL);
+ r = mmap_cache_get(f->mmap, f->cache_fd, CONTEXT_HEADER, true, 0, PAGE_ALIGN(sizeof(Header)), &f->last_stat, &h, NULL);
if (r == -EINVAL) {
/* Some file systems (jffs2 or p9fs) don't support mmap() properly (or only read-only
* mmap()), and return EINVAL in that case. Let's propagate that as a more recognizable error
bool keep_always:1;
bool in_unused:1;
- int prot;
void *ptr;
uint64_t offset;
size_t size;
struct MMapFileDescriptor {
MMapCache *cache;
int fd;
+ int prot;
bool sigbus;
LIST_HEAD(Window, windows);
};
static void window_invalidate(Window *w) {
assert(w);
+ assert(w->fd);
if (w->invalidated)
return;
* trigger any further SIGBUS, possibly overrunning the sigbus
* queue. */
- assert_se(mmap(w->ptr, w->size, w->prot, MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0) == w->ptr);
+ assert_se(mmap(w->ptr, w->size, w->fd->prot, MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0) == w->ptr);
w->invalidated = true;
}
free(w);
}
-_pure_ static bool window_matches(Window *w, int prot, uint64_t offset, size_t size) {
+_pure_ static bool window_matches(Window *w, uint64_t offset, size_t size) {
assert(w);
assert(size > 0);
return
- prot == w->prot &&
offset >= w->offset &&
offset + size <= w->offset + w->size;
}
-_pure_ static bool window_matches_fd(Window *w, MMapFileDescriptor *f, int prot, uint64_t offset, size_t size) {
+_pure_ static bool window_matches_fd(Window *w, MMapFileDescriptor *f, uint64_t offset, size_t size) {
assert(w);
assert(f);
return
- w->fd &&
- f->fd == w->fd->fd &&
- window_matches(w, prot, offset, size);
+ w->fd == f &&
+ window_matches(w, offset, size);
}
-static Window *window_add(MMapCache *m, MMapFileDescriptor *f, int prot, bool keep_always, uint64_t offset, size_t size, void *ptr) {
+static Window *window_add(MMapCache *m, MMapFileDescriptor *f, bool keep_always, uint64_t offset, size_t size, void *ptr) {
Window *w;
assert(m);
*w = (Window) {
.cache = m,
.fd = f,
- .prot = prot,
.keep_always = keep_always,
.offset = offset,
.size = size,
static int try_context(
MMapCache *m,
MMapFileDescriptor *f,
- int prot,
unsigned context,
bool keep_always,
uint64_t offset,
if (!c->window)
return 0;
- if (!window_matches_fd(c->window, f, prot, offset, size)) {
+ if (!window_matches_fd(c->window, f, offset, size)) {
/* Drop the reference to the window, since it's unnecessary now */
context_detach_window(c);
static int find_mmap(
MMapCache *m,
MMapFileDescriptor *f,
- int prot,
unsigned context,
bool keep_always,
uint64_t offset,
return -EIO;
LIST_FOREACH(by_fd, w, f->windows)
- if (window_matches(w, prot, offset, size))
+ if (window_matches(w, offset, size))
break;
if (!w)
return 1;
}
-static int mmap_try_harder(MMapCache *m, void *addr, MMapFileDescriptor *f, int prot, int flags, uint64_t offset, size_t size, void **res) {
+static int mmap_try_harder(MMapCache *m, void *addr, MMapFileDescriptor *f, int flags, uint64_t offset, size_t size, void **res) {
void *ptr;
assert(m);
for (;;) {
int r;
- ptr = mmap(addr, size, prot, flags, f->fd, offset);
+ ptr = mmap(addr, size, f->prot, flags, f->fd, offset);
if (ptr != MAP_FAILED)
break;
if (errno != ENOMEM)
static int add_mmap(
MMapCache *m,
MMapFileDescriptor *f,
- int prot,
unsigned context,
bool keep_always,
uint64_t offset,
wsize = PAGE_ALIGN(st->st_size - woffset);
}
- r = mmap_try_harder(m, NULL, f, prot, MAP_SHARED, woffset, wsize, &d);
+ r = mmap_try_harder(m, NULL, f, MAP_SHARED, woffset, wsize, &d);
if (r < 0)
return r;
if (!c)
goto outofmem;
- w = window_add(m, f, prot, keep_always, woffset, wsize, d);
+ w = window_add(m, f, keep_always, woffset, wsize, d);
if (!w)
goto outofmem;
int mmap_cache_get(
MMapCache *m,
MMapFileDescriptor *f,
- int prot,
unsigned context,
bool keep_always,
uint64_t offset,
assert(context < MMAP_CACHE_MAX_CONTEXTS);
/* Check whether the current context is the right one already */
- r = try_context(m, f, prot, context, keep_always, offset, size, ret, ret_size);
+ r = try_context(m, f, context, keep_always, offset, size, ret, ret_size);
if (r != 0) {
m->n_context_cache_hit++;
return r;
}
/* Search for a matching mmap */
- r = find_mmap(m, f, prot, context, keep_always, offset, size, ret, ret_size);
+ r = find_mmap(m, f, context, keep_always, offset, size, ret, ret_size);
if (r != 0) {
m->n_window_list_hit++;
return r;
m->n_missed++;
/* Create a new mmap */
- return add_mmap(m, f, prot, context, keep_always, offset, size, st, ret, ret_size);
+ return add_mmap(m, f, context, keep_always, offset, size, st, ret, ret_size);
}
void mmap_cache_stats_log_debug(MMapCache *m) {
return f->sigbus;
}
-MMapFileDescriptor* mmap_cache_add_fd(MMapCache *m, int fd) {
+MMapFileDescriptor* mmap_cache_add_fd(MMapCache *m, int fd, int prot) {
MMapFileDescriptor *f;
int r;
f->cache = m;
f->fd = fd;
+ f->prot = prot;
r = hashmap_put(m->fds, FD_TO_PTR(fd), f);
if (r < 0)
assert_se(x >= 0);
unlink(px);
- assert_se(fx = mmap_cache_add_fd(m, x));
+ assert_se(fx = mmap_cache_add_fd(m, x, PROT_READ));
y = mkostemp_safe(py);
assert_se(y >= 0);
assert_se(z >= 0);
unlink(pz);
- r = mmap_cache_get(m, fx, PROT_READ, 0, false, 1, 2, NULL, &p, NULL);
+ r = mmap_cache_get(m, fx, 0, false, 1, 2, NULL, &p, NULL);
assert_se(r >= 0);
- r = mmap_cache_get(m, fx, PROT_READ, 0, false, 2, 2, NULL, &q, NULL);
+ r = mmap_cache_get(m, fx, 0, false, 2, 2, NULL, &q, NULL);
assert_se(r >= 0);
assert_se((uint8_t*) p + 1 == (uint8_t*) q);
- r = mmap_cache_get(m, fx, PROT_READ, 1, false, 3, 2, NULL, &q, NULL);
+ r = mmap_cache_get(m, fx, 1, false, 3, 2, NULL, &q, NULL);
assert_se(r >= 0);
assert_se((uint8_t*) p + 2 == (uint8_t*) q);
- r = mmap_cache_get(m, fx, PROT_READ, 0, false, 16ULL*1024ULL*1024ULL, 2, NULL, &p, NULL);
+ r = mmap_cache_get(m, fx, 0, false, 16ULL*1024ULL*1024ULL, 2, NULL, &p, NULL);
assert_se(r >= 0);
- r = mmap_cache_get(m, fx, PROT_READ, 1, false, 16ULL*1024ULL*1024ULL+1, 2, NULL, &q, NULL);
+ r = mmap_cache_get(m, fx, 1, false, 16ULL*1024ULL*1024ULL+1, 2, NULL, &q, NULL);
assert_se(r >= 0);
assert_se((uint8_t*) p + 1 == (uint8_t*) q);