mirror of
https://github.com/trapexit/mergerfs.git
synced 2026-12-02 21:00:13 -05:00
- func.readdir=cor appended entries in whichever order branch workers finished. Directory offsets are positions in that list, so a reader reopening an unchanged directory and seeking to a previously issued cookie (NFS does this) could see a duplicate and miss a later entry. Read branches concurrently into their own buffers, then merge and deduplicate them in configured branch order. The merge mutex is gone. - Growing a dirents buffer overwrote the only pointer with realloc's result while recording the new capacity, so an allocation failure leaked the buffer and the next fill wrote through NULL. Grow both vectors without changing pointer, length or capacity until the allocation succeeds, and return ENOMEM. - seq and cosr ignored that failure and served a listing with entries missing. Every policy now returns the error, and libfuse resets the handle's buffer whenever a fill fails, so a later request at a nonzero offset re-enumerates instead of serving a partial or stale listing as success. cosr closes the directories its workers opened when it gives up early. - Document the fill-error contract, which doesn't promise that every branch was read: seq and cosr can treat a branch read error as the end of the directory. Document cor's memory use, and refer to func.getattr, not fuse.getattr. - TEST_readdir_cor_cookie_replay compares cor listings, offsets and inodes with seq and replays cookies on fresh handles. TEST_dirents_allocation injects allocation failures into the dirents code. TEST_readdir_cor_enomem fails one allocation in a mounted daemon for cor and seq and checks the same handle then serves the exact suffix. Performance: cor copies each entry twice and merges serially after the parallel reads. Listing 100,000 entries over 4 tmpfs branches took the same time before and after (81-83 ms per listing for cor:4 and seq). Peak daemon memory for cor:4 dropped from 27.4 MB to 18.7 MB.
275 lines
6 KiB
C++
275 lines
6 KiB
C++
#define _FILE_OFFSET_BITS 64
|
|
|
|
#include "fs_dirent64.hpp"
|
|
#include "fuse_attr.h"
|
|
#include "fuse_dirent.h"
|
|
#include "fuse_direntplus.h"
|
|
#include "fuse_dirents.hpp"
|
|
#include "fuse_entry.h"
|
|
#include "stat_utils.h"
|
|
|
|
#include <dirent.h>
|
|
#include <errno.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
|
|
/* 32KB - same as glibc getdents buffer size */
|
|
#define DENTS_BUF_EXPAND_SIZE (1024 * 32)
|
|
|
|
/* Initial pre-allocated capacity for directory-entry offset vector */
|
|
#define DENTS_OFFS_INITIAL_CAPACITY 64
|
|
|
|
// Grow through checked reallocations; kvec's unchecked growth loses the
|
|
// original pointer on failure. Offsets in the returned listing are uint32_t.
|
|
|
|
static
|
|
uint64_t
|
|
_round_up(const uint64_t number_,
|
|
const uint64_t multiple_)
|
|
{
|
|
return (((number_ + multiple_ - 1) / multiple_) * multiple_);
|
|
}
|
|
|
|
static
|
|
uint64_t
|
|
_align_uint64_t(uint64_t v_)
|
|
{
|
|
return ((v_ + sizeof(uint64_t) - 1) & ~(sizeof(uint64_t) - 1));
|
|
}
|
|
|
|
static
|
|
uint64_t
|
|
_dirent_size(const uint64_t namelen_)
|
|
{
|
|
uint64_t rv;
|
|
|
|
rv = offsetof(fuse_dirent_t,name);
|
|
rv += namelen_;
|
|
rv = _align_uint64_t(rv);
|
|
|
|
return rv;
|
|
}
|
|
|
|
// Grow the data buffer so one more entry of size_ bytes fits. The
|
|
// reallocation is checked: on failure the buffer's pointer, length
|
|
// and capacity are left exactly as they were, so a failed add neither
|
|
// loses written entries nor leaks the old buffer.
|
|
static
|
|
int
|
|
_dirents_buf_resize(fuse_dirents_t *d_,
|
|
const uint64_t size_)
|
|
{
|
|
if(size_ > (UINT32_MAX - kv_size(d_->data)))
|
|
return -ENOMEM;
|
|
|
|
if((kv_size(d_->data) + size_) > kv_max(d_->data))
|
|
{
|
|
uint64_t new_size;
|
|
char *new_data;
|
|
|
|
new_size = _round_up((kv_size(d_->data) + size_),DENTS_BUF_EXPAND_SIZE);
|
|
if(new_size > SIZE_MAX)
|
|
new_size = SIZE_MAX;
|
|
|
|
new_data = (char*)realloc(d_->data.a,new_size);
|
|
if(new_data == NULL)
|
|
return -ENOMEM;
|
|
|
|
d_->data.a = new_data;
|
|
kv_max(d_->data) = new_size;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Grow the offset vector so entries_ more entries fit. Checked like
|
|
// _dirents_buf_resize; growth mirrors kv_push's doubling.
|
|
static
|
|
int
|
|
_dirents_offs_resize(fuse_dirents_t *d_,
|
|
const uint64_t entries_)
|
|
{
|
|
uint64_t new_capacity;
|
|
uint32_t *new_offs;
|
|
|
|
if((kv_size(d_->offs) + entries_) <= kv_max(d_->offs))
|
|
return 0;
|
|
|
|
new_capacity = kv_max(d_->offs);
|
|
while((kv_size(d_->offs) + entries_) > new_capacity)
|
|
new_capacity = (new_capacity ? (new_capacity << 1) : 2);
|
|
|
|
if(new_capacity > SIZE_MAX / sizeof(uint32_t))
|
|
return -ENOMEM;
|
|
|
|
new_offs = (uint32_t*)realloc(d_->offs.a,(new_capacity * sizeof(uint32_t)));
|
|
if(new_offs == NULL)
|
|
return -ENOMEM;
|
|
|
|
d_->offs.a = new_offs;
|
|
kv_max(d_->offs) = new_capacity;
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Reserve both vectors before committing either length. A failed append
|
|
// can grow capacity, but never changes the listing or its cookies.
|
|
// offs stays one longer than the entry count: offs[0] is the zero
|
|
// sentinel and offs[k] is the data offset of entry k, so an entry's
|
|
// cookie is its index + 1, the offset the next readdir resumes from.
|
|
static
|
|
fuse_dirent_t*
|
|
_dirents_dirent_alloc(fuse_dirents_t *d_,
|
|
const uint64_t namelen_)
|
|
{
|
|
int rv;
|
|
uint64_t size;
|
|
fuse_dirent_t *d;
|
|
|
|
if(namelen_ > UINT32_MAX)
|
|
return NULL;
|
|
size = _dirent_size(namelen_);
|
|
|
|
rv = _dirents_offs_resize(d_,1);
|
|
if(rv)
|
|
return NULL;
|
|
|
|
rv = _dirents_buf_resize(d_,size);
|
|
if(rv)
|
|
return NULL;
|
|
|
|
d = (fuse_dirent_t*)&kv_end(d_->data);
|
|
kv_size(d_->data) += size;
|
|
|
|
d->off = kv_size(d_->offs);
|
|
d_->offs.a[d_->offs.n] = kv_size(d_->data);
|
|
kv_size(d_->offs)++;
|
|
|
|
return d;
|
|
}
|
|
|
|
int
|
|
fuse_dirents_add(fuse_dirents_t *d_,
|
|
const dirent *de_,
|
|
const uint64_t namelen_)
|
|
{
|
|
fuse_dirent_t *d;
|
|
|
|
d = _dirents_dirent_alloc(d_,namelen_);
|
|
if(d == NULL)
|
|
return -ENOMEM;
|
|
|
|
d->ino = de_->d_ino;
|
|
d->namelen = namelen_;
|
|
d->type = de_->d_type;
|
|
memcpy(d->name,de_->d_name,namelen_);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
fuse_dirents_add(fuse_dirents_t *d_,
|
|
const fs::dirent64 *de_,
|
|
const uint64_t namelen_)
|
|
{
|
|
fuse_dirent_t *d;
|
|
|
|
d = _dirents_dirent_alloc(d_,namelen_);
|
|
if(d == NULL)
|
|
return -ENOMEM;
|
|
|
|
d->ino = de_->ino;
|
|
d->namelen = namelen_;
|
|
d->type = de_->type;
|
|
memcpy(d->name,de_->name,namelen_);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
fuse_dirents_add(fuse_dirents_t *d_,
|
|
const fuse_dirent_t *de_,
|
|
const uint64_t namelen_)
|
|
{
|
|
fuse_dirent_t *d;
|
|
|
|
d = _dirents_dirent_alloc(d_,namelen_);
|
|
if(d == NULL)
|
|
return -ENOMEM;
|
|
|
|
d->ino = de_->ino;
|
|
d->namelen = namelen_;
|
|
d->type = de_->type;
|
|
memcpy(d->name,de_->name,namelen_);
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Restore the just-initialized state: empty data and a single offset
|
|
// sentinel. offs[0] is never overwritten by adds, so only lengths
|
|
// change and no reallocation happens. A fill that fails partway must
|
|
// be reset before the dirents are reused: a partial listing would
|
|
// otherwise be served at offsets belonging to a complete enumeration.
|
|
void
|
|
fuse_dirents_reset(fuse_dirents_t *d_)
|
|
{
|
|
kv_size(d_->data) = 0;
|
|
kv_size(d_->offs) = 1;
|
|
}
|
|
|
|
int
|
|
fuse_dirents_init(fuse_dirents_t *d_)
|
|
{
|
|
int rv;
|
|
|
|
kv_init(d_->data);
|
|
kv_init(d_->offs);
|
|
|
|
rv = _dirents_buf_resize(d_,DENTS_BUF_EXPAND_SIZE);
|
|
if(rv)
|
|
return rv;
|
|
|
|
rv = _dirents_offs_resize(d_,DENTS_OFFS_INITIAL_CAPACITY);
|
|
if(rv)
|
|
{
|
|
kv_destroy(d_->data);
|
|
kv_init(d_->data);
|
|
return rv;
|
|
}
|
|
|
|
d_->offs.a[d_->offs.n++] = 0;
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Like fuse_dirents_init but reserves nothing beyond the single zero
|
|
// offset sentinel. For scratch buffers filled immediately by the
|
|
// caller. Returns -ENOMEM on allocation failure with nothing left
|
|
// allocated.
|
|
int
|
|
fuse_dirents_init_noreserve(fuse_dirents_t *d_)
|
|
{
|
|
int rv;
|
|
|
|
kv_init(d_->data);
|
|
kv_init(d_->offs);
|
|
|
|
rv = _dirents_offs_resize(d_,1);
|
|
if(rv)
|
|
return rv;
|
|
|
|
d_->offs.a[d_->offs.n++] = 0;
|
|
|
|
return 0;
|
|
}
|
|
|
|
void
|
|
fuse_dirents_free(fuse_dirents_t *d_)
|
|
{
|
|
kv_destroy(d_->data);
|
|
kv_destroy(d_->offs);
|
|
}
|