Add -o readonly option to FUSE driver

This commit is contained in:
Marcus Holland-Moritz 2020-12-08 22:53:35 +01:00
parent 88dd18781b
commit 04c43ec421
4 changed files with 21 additions and 0 deletions

View File

@ -64,6 +64,16 @@ options:
will also consume more memory to hold the hardlink count table. will also consume more memory to hold the hardlink count table.
This will be 4 bytes for every regular file inode. This will be 4 bytes for every regular file inode.
* `-o readonly`
Show all file system entries as read-only. By default, DwarFS
will preserve the original writeability, which is obviously a
lie as it's a read-only file system. However, this is needed
for overlays to work correctly, as otherwise directories are
seen as read-only by the overlay and it'll be impossible to
create new files even in a writeable overlay. If you don't use
overlays and want the file system to reflect its read-only
state, you can set this option.
* `-o (no_)cache_image` * `-o (no_)cache_image`
By default, `dwarfs` tries to ensure that the compressed file By default, `dwarfs` tries to ensure that the compressed file
system image will not be cached by the kernel (i.e. the default system image will not be cached by the kernel (i.e. the default

View File

@ -38,6 +38,7 @@ struct block_cache_options {
struct metadata_options { struct metadata_options {
bool enable_nlink{false}; bool enable_nlink{false};
bool readonly{false};
}; };
struct filesystem_options { struct filesystem_options {

View File

@ -53,6 +53,7 @@ struct options {
const char* mlock_str; // TODO: const?? -> use string? const char* mlock_str; // TODO: const?? -> use string?
const char* decompress_ratio_str; // TODO: const?? -> use string? const char* decompress_ratio_str; // TODO: const?? -> use string?
int enable_nlink; int enable_nlink;
int readonly;
int cache_image; int cache_image;
int cache_files; int cache_files;
size_t cachesize; size_t cachesize;
@ -79,6 +80,7 @@ const struct fuse_opt dwarfs_opts[] = {
DWARFS_OPT("mlock=%s", mlock_str, 0), DWARFS_OPT("mlock=%s", mlock_str, 0),
DWARFS_OPT("decratio=%s", decompress_ratio_str, 0), DWARFS_OPT("decratio=%s", decompress_ratio_str, 0),
DWARFS_OPT("enable_nlink", enable_nlink, 1), DWARFS_OPT("enable_nlink", enable_nlink, 1),
DWARFS_OPT("readonly", readonly, 1),
DWARFS_OPT("cache_image", cache_image, 1), DWARFS_OPT("cache_image", cache_image, 1),
DWARFS_OPT("no_cache_image", cache_image, 0), DWARFS_OPT("no_cache_image", cache_image, 0),
DWARFS_OPT("cache_files", cache_files, 1), DWARFS_OPT("cache_files", cache_files, 1),
@ -105,6 +107,7 @@ void op_init(void* /*userdata*/, struct fuse_conn_info* /*conn*/) {
fsopts.block_cache.decompress_ratio = s_opts.decompress_ratio; fsopts.block_cache.decompress_ratio = s_opts.decompress_ratio;
fsopts.block_cache.mm_release = !s_opts.cache_image; fsopts.block_cache.mm_release = !s_opts.cache_image;
fsopts.metadata.enable_nlink = bool(s_opts.enable_nlink); fsopts.metadata.enable_nlink = bool(s_opts.enable_nlink);
fsopts.metadata.readonly = bool(s_opts.readonly);
s_fs = std::make_shared<filesystem_v2>( s_fs = std::make_shared<filesystem_v2>(
s_lgr, std::make_shared<mmap>(s_opts.fsimage), fsopts, s_lgr, std::make_shared<mmap>(s_opts.fsimage), fsopts,
&s_opts.stat_defaults, FUSE_ROOT_ID); &s_opts.stat_defaults, FUSE_ROOT_ID);
@ -400,6 +403,7 @@ void usage(const char* progname) {
<< " -o mlock=NAME mlock mode: (none), try, must\n" << " -o mlock=NAME mlock mode: (none), try, must\n"
<< " -o decratio=NUM ratio for full decompression (0.8)\n" << " -o decratio=NUM ratio for full decompression (0.8)\n"
<< " -o enable_nlink show correct hardlink numbers\n" << " -o enable_nlink show correct hardlink numbers\n"
<< " -o readonly show read-only file system\n"
<< " -o (no_)cache_image (don't) keep image in kernel cache\n" << " -o (no_)cache_image (don't) keep image in kernel cache\n"
<< " -o (no_)cache_files (don't) keep files in kernel cache\n" << " -o (no_)cache_files (don't) keep files in kernel cache\n"
<< " -o debuglevel=NAME error, warn, (info), debug, trace\n" << " -o debuglevel=NAME error, warn, (info), debug, trace\n"

View File

@ -95,6 +95,8 @@ void analyze_frozen(std::ostream& os,
os << '\n'; os << '\n';
} }
const uint16_t READ_ONLY_MASK = ~(S_IWUSR | S_IWGRP | S_IWOTH);
} // namespace } // namespace
template <typename LoggerPolicy> template <typename LoggerPolicy>
@ -637,6 +639,10 @@ int metadata_<LoggerPolicy>::getattr(entry_view entry,
stbuf->st_mode = mode; stbuf->st_mode = mode;
if (options_.readonly) {
stbuf->st_mode &= READ_ONLY_MASK;
}
stbuf->st_size = S_ISDIR(mode) ? make_directory_view(entry).entry_count() stbuf->st_size = S_ISDIR(mode) ? make_directory_view(entry).entry_count()
: file_size(entry, mode); : file_size(entry, mode);
stbuf->st_ino = inode + inode_offset_; stbuf->st_ino = inode + inode_offset_;