# HG changeset patch # User Paul Boddie # Date 1634590762 -7200 # Node ID 4fb242f7d3604b0f458e388b3559a56b6944d659 # Parent d8a636f6f728569ac122da0f4c53cb6c5f4af648 Renamed the registry classes to be more descriptive. diff -r d8a636f6f728 -r 4fb242f7d360 libfsserver/include/fsserver/block_file_opener.h --- a/libfsserver/include/fsserver/block_file_opener.h Sun Oct 17 22:14:40 2021 +0200 +++ b/libfsserver/include/fsserver/block_file_opener.h Mon Oct 18 22:59:22 2021 +0200 @@ -30,7 +30,7 @@ class BlockFileOpener : public HostFileOpener { public: - explicit BlockFileOpener(FileObjectRegistry *registry) + explicit BlockFileOpener(ResourceRegistry *registry) : HostFileOpener(registry) { } diff -r d8a636f6f728 -r 4fb242f7d360 libfsserver/include/fsserver/directory_provider.h --- a/libfsserver/include/fsserver/directory_provider.h Sun Oct 17 22:14:40 2021 +0200 +++ b/libfsserver/include/fsserver/directory_provider.h Mon Oct 18 22:59:22 2021 +0200 @@ -34,7 +34,7 @@ DirectoryAccessor *_accessor; public: - explicit DirectoryProvider(fileid_t fileid, FileRegistry *registry, + explicit DirectoryProvider(fileid_t fileid, ProviderRegistry *registry, DirectoryAccessor *accessor); virtual ~DirectoryProvider(); diff -r d8a636f6f728 -r 4fb242f7d360 libfsserver/include/fsserver/ext2_file_opener.h --- a/libfsserver/include/fsserver/ext2_file_opener.h Sun Oct 17 22:14:40 2021 +0200 +++ b/libfsserver/include/fsserver/ext2_file_opener.h Mon Oct 18 22:59:22 2021 +0200 @@ -45,7 +45,7 @@ virtual bool accessing_file(const char *path, flags_t flags, fileid_t fileid); public: - explicit Ext2FileOpener(FileObjectRegistry *registry, Ext2FileOperations *ops, user_t user) + explicit Ext2FileOpener(ResourceRegistry *registry, Ext2FileOperations *ops, user_t user) : OpenerResource(registry), _ops(ops), _user(user) { } diff -r d8a636f6f728 -r 4fb242f7d360 libfsserver/include/fsserver/file_object_registry.h --- a/libfsserver/include/fsserver/file_object_registry.h Sun Oct 17 22:14:40 2021 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,70 +0,0 @@ -/* - * A registry of filesystem objects. - * - * Copyright (C) 2021 Paul Boddie - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA - */ - -#pragma once - -#include -#include -#include -#include -#include -#include - - - -/* A registry of filesystem objects. */ - -class FileObjectRegistry : public FileRegistry -{ -protected: - Pages *_pages; - - map_flags_t get_flags(flags_t flags); - - /* Provider initialisation methods. */ - - long make_directory_provider(FileOpening *opening, const char *path, - flags_t flags, fileid_t fileid, - Provider **provider); - - long make_file_provider(FileOpening *opening, const char *path, - flags_t flags, fileid_t fileid, - Provider **provider); - - long make_provider(FileOpening *opening, const char *path, - flags_t flags, fileid_t fileid, - Provider **provider); - -public: - explicit FileObjectRegistry(Pages *pages); - - /* Resource discovery methods. */ - - long find_provider(fileid_t fileid, Provider **provider); - - /* Resource initialisation methods. */ - - long get_resource(FileOpening *opening, const char *path, flags_t flags, - offset_t *size, object_flags_t *object_flags, - Resource **resource); -}; - -// vim: tabstop=4 expandtab shiftwidth=4 diff -r d8a636f6f728 -r 4fb242f7d360 libfsserver/include/fsserver/file_provider.h --- a/libfsserver/include/fsserver/file_provider.h Sun Oct 17 22:14:40 2021 +0200 +++ b/libfsserver/include/fsserver/file_provider.h Mon Oct 18 22:59:22 2021 +0200 @@ -36,7 +36,7 @@ public: explicit FileProvider(fileid_t fileid, map_flags_t flags, - FileRegistry *registry, PageMapper *mapper); + ProviderRegistry *registry, PageMapper *mapper); virtual ~FileProvider(); diff -r d8a636f6f728 -r 4fb242f7d360 libfsserver/include/fsserver/file_registry.h --- a/libfsserver/include/fsserver/file_registry.h Sun Oct 17 22:14:40 2021 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,61 +0,0 @@ -/* - * A registry of filesystem objects. - * - * Copyright (C) 2021 Paul Boddie - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA - */ - -#pragma once - -#include -#include - -#include -#include - - - -/* Mapping type from file identifiers to page mappers. */ - -typedef std::map FileMapping; -typedef std::pair FileMappingEntry; - - - -/* A registry of filesystem objects. */ - -class FileRegistry -{ -protected: - FileMapping _providers; - std::mutex _lock; - - /* Filesystem object access. */ - - Accountable *get(fileid_t fileid); - - void remove(fileid_t fileid, Accountable *obj); - - void set(fileid_t fileid, Accountable *obj); - -public: - /* Methods for resources. */ - - void detach(fileid_t fileid, Accountable *mapper); -}; - -// vim: tabstop=4 expandtab shiftwidth=4 diff -r d8a636f6f728 -r 4fb242f7d360 libfsserver/include/fsserver/filesystem_resource.h --- a/libfsserver/include/fsserver/filesystem_resource.h Sun Oct 17 22:14:40 2021 +0200 +++ b/libfsserver/include/fsserver/filesystem_resource.h Mon Oct 18 22:59:22 2021 +0200 @@ -21,15 +21,15 @@ #pragma once -#include #include #include +#include /* Support for providing access to user-specific filesystems. */ -class FilesystemResource : public Resource, public FileObjectRegistry, +class FilesystemResource : public Resource, public ResourceRegistry, public FilesystemObject { public: diff -r d8a636f6f728 -r 4fb242f7d360 libfsserver/include/fsserver/host_file_opener.h --- a/libfsserver/include/fsserver/host_file_opener.h Sun Oct 17 22:14:40 2021 +0200 +++ b/libfsserver/include/fsserver/host_file_opener.h Mon Oct 18 22:59:22 2021 +0200 @@ -55,7 +55,7 @@ virtual bool accessing_file(const char *path, flags_t flags, fileid_t fileid); public: - explicit HostFileOpener(FileObjectRegistry *registry) + explicit HostFileOpener(ResourceRegistry *registry) : OpenerResource(registry) { } diff -r d8a636f6f728 -r 4fb242f7d360 libfsserver/include/fsserver/opener_resource.h --- a/libfsserver/include/fsserver/opener_resource.h Sun Oct 17 22:14:40 2021 +0200 +++ b/libfsserver/include/fsserver/opener_resource.h Mon Oct 18 22:59:22 2021 +0200 @@ -21,11 +21,11 @@ #pragma once -#include #include #include #include #include +#include @@ -34,7 +34,7 @@ class OpenerResource : public Resource, public FileOpening, public Opener { protected: - FileObjectRegistry *_registry; + ResourceRegistry *_registry; /* Retrieval methods. */ @@ -45,7 +45,7 @@ virtual long notify_parent(const char *path); public: - explicit OpenerResource(FileObjectRegistry *registry); + explicit OpenerResource(ResourceRegistry *registry); virtual ~OpenerResource(); diff -r d8a636f6f728 -r 4fb242f7d360 libfsserver/include/fsserver/provider.h --- a/libfsserver/include/fsserver/provider.h Sun Oct 17 22:14:40 2021 +0200 +++ b/libfsserver/include/fsserver/provider.h Mon Oct 18 22:59:22 2021 +0200 @@ -22,8 +22,8 @@ #pragma once #include -#include #include +#include #include @@ -34,14 +34,14 @@ { protected: fileid_t _fileid; - FileRegistry *_registry; + ProviderRegistry *_registry; public: - explicit Provider(fileid_t fileid, FileRegistry *registry); + explicit Provider(fileid_t fileid, ProviderRegistry *registry); virtual ~Provider(); - virtual FileRegistry *registry(); + virtual ProviderRegistry *registry(); virtual long make_resource(offset_t *size, object_flags_t *object_flags, Resource **resource) = 0; diff -r d8a636f6f728 -r 4fb242f7d360 libfsserver/include/fsserver/provider_registry.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libfsserver/include/fsserver/provider_registry.h Mon Oct 18 22:59:22 2021 +0200 @@ -0,0 +1,61 @@ +/* + * A registry of filesystem object providers. + * + * Copyright (C) 2021 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#pragma once + +#include +#include + +#include +#include + + + +/* Mapping type from file identifiers to page mappers. */ + +typedef std::map FileMapping; +typedef std::pair FileMappingEntry; + + + +/* A registry of filesystem object providers. */ + +class ProviderRegistry +{ +protected: + FileMapping _providers; + std::mutex _lock; + + /* Filesystem object access. */ + + Accountable *get(fileid_t fileid); + + void remove(fileid_t fileid, Accountable *obj); + + void set(fileid_t fileid, Accountable *obj); + +public: + /* Methods for resources. */ + + void detach(fileid_t fileid, Accountable *mapper); +}; + +// vim: tabstop=4 expandtab shiftwidth=4 diff -r d8a636f6f728 -r 4fb242f7d360 libfsserver/include/fsserver/resource_registry.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libfsserver/include/fsserver/resource_registry.h Mon Oct 18 22:59:22 2021 +0200 @@ -0,0 +1,70 @@ +/* + * A registry of filesystem object resources. + * + * Copyright (C) 2021 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#pragma once + +#include +#include +#include +#include +#include +#include + + + +/* A registry of filesystem object resources. */ + +class ResourceRegistry : public ProviderRegistry +{ +protected: + Pages *_pages; + + map_flags_t get_flags(flags_t flags); + + /* Provider initialisation methods. */ + + long make_directory_provider(FileOpening *opening, const char *path, + flags_t flags, fileid_t fileid, + Provider **provider); + + long make_file_provider(FileOpening *opening, const char *path, + flags_t flags, fileid_t fileid, + Provider **provider); + + long make_provider(FileOpening *opening, const char *path, + flags_t flags, fileid_t fileid, + Provider **provider); + +public: + explicit ResourceRegistry(Pages *pages); + + /* Resource discovery methods. */ + + long find_provider(fileid_t fileid, Provider **provider); + + /* Resource initialisation methods. */ + + long get_resource(FileOpening *opening, const char *path, flags_t flags, + offset_t *size, object_flags_t *object_flags, + Resource **resource); +}; + +// vim: tabstop=4 expandtab shiftwidth=4 diff -r d8a636f6f728 -r 4fb242f7d360 libfsserver/include/fsserver/test_file_opener.h --- a/libfsserver/include/fsserver/test_file_opener.h Sun Oct 17 22:14:40 2021 +0200 +++ b/libfsserver/include/fsserver/test_file_opener.h Mon Oct 18 22:59:22 2021 +0200 @@ -39,7 +39,7 @@ virtual bool accessing_file(const char *path, flags_t flags, fileid_t fileid); public: - explicit TestFileOpener(FileObjectRegistry *registry, offset_t file_size=0); + explicit TestFileOpener(ResourceRegistry *registry, offset_t file_size=0); virtual ~TestFileOpener(); diff -r d8a636f6f728 -r 4fb242f7d360 libfsserver/lib/Makefile --- a/libfsserver/lib/Makefile Sun Oct 17 22:14:40 2021 +0200 +++ b/libfsserver/lib/Makefile Mon Oct 18 22:59:22 2021 +0200 @@ -56,10 +56,8 @@ files/ext2_file_opener.cc \ files/ext2_file_operations.cc \ files/ext2_filesystem.cc \ - files/file_object_registry.cc \ files/file_pager.cc \ files/file_provider.cc \ - files/file_registry.cc \ files/filesystem_resource.cc \ files/host_file_accessor.cc \ files/host_file_opener.cc \ @@ -69,6 +67,8 @@ files/test_file_opener.cc \ generic/accessor.cc \ generic/accountable.cc \ + generic/provider_registry.cc \ + generic/resource_registry.cc \ generic/notification.cc \ generic/pager.cc \ generic/provider.cc \ diff -r d8a636f6f728 -r 4fb242f7d360 libfsserver/lib/directories/directory_provider.cc --- a/libfsserver/lib/directories/directory_provider.cc Sun Oct 17 22:14:40 2021 +0200 +++ b/libfsserver/lib/directories/directory_provider.cc Mon Oct 18 22:59:22 2021 +0200 @@ -26,7 +26,8 @@ /* Initialise the provider. */ -DirectoryProvider::DirectoryProvider(fileid_t fileid, FileRegistry *registry, +DirectoryProvider::DirectoryProvider(fileid_t fileid, + ProviderRegistry *registry, DirectoryAccessor *accessor) : Provider(fileid, registry), _accessor(accessor) { diff -r d8a636f6f728 -r 4fb242f7d360 libfsserver/lib/files/file_object_registry.cc --- a/libfsserver/lib/files/file_object_registry.cc Sun Oct 17 22:14:40 2021 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,172 +0,0 @@ -/* - * File registry and opening functionality. - * - * Copyright (C) 2021 Paul Boddie - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA - */ - -#include "directory_resource.h" -#include "file_object_registry.h" -#include "file_pager.h" - -#include - - - -/* The file object registry provides a memory page collection to the page - mappers it creates. */ - -FileObjectRegistry::FileObjectRegistry(Pages *pages) -: _pages(pages) -{ -} - - - -/* Convert opening flags to map-compatible paging flags. */ - -map_flags_t FileObjectRegistry::get_flags(flags_t flags) -{ - return flags & (O_WRONLY | O_RDWR) ? L4RE_DS_MAP_FLAG_RW : L4RE_DS_MAP_FLAG_RO; -} - - - -/* Make a directory provider. */ - -long FileObjectRegistry::make_directory_provider(FileOpening *opening, - const char *path, flags_t flags, - fileid_t fileid, - Provider **provider) -{ - /* Make an accessor and a provider to encapsulate it. */ - - DirectoryAccessor *accessor; - long err = opening->make_directory_accessor(path, flags, fileid, &accessor); - - if (err) - return err; - - *provider = new DirectoryProvider(fileid, this, accessor); - return L4_EOK; -} - -/* Make a file provider. */ - -long FileObjectRegistry::make_file_provider(FileOpening *opening, - const char *path, flags_t flags, - fileid_t fileid, - Provider **provider) -{ - /* Make an accessor, page mapper, and a provider to encapsulate them. */ - - Accessor *accessor; - long err = opening->make_accessor(path, flags, fileid, &accessor); - - if (err) - return err; - - PageMapper *mapper = new PageMapper(accessor, _pages); - *provider = new FileProvider(fileid, get_flags(flags), this, mapper); - return L4_EOK; -} - -/* Make a provider of the appropriate type. */ - -long FileObjectRegistry::make_provider(FileOpening *opening, - const char *path, flags_t flags, - fileid_t fileid, - Provider **provider) -{ - long err = -L4_EIO; - - if (opening->accessing_directory(path, flags, fileid)) - err = make_directory_provider(opening, path, flags, fileid, provider); - else if (opening->accessing_file(path, flags, fileid)) - err = make_file_provider(opening, path, flags, fileid, provider); - - if (err) - return err; - - /* Register the provider. */ - - set(fileid, *provider); - return L4_EOK; -} - - - -/* Return a resource for a filesystem object. */ - -long FileObjectRegistry::get_resource(FileOpening *opening, const char *path, - flags_t flags, offset_t *size, - object_flags_t *object_flags, - Resource **resource) -{ - std::lock_guard guard(_lock); - - /* Obtain an identifier for the object, even for new files (subject to use - of the appropriate flag). */ - - fileid_t fileid; - long err = opening->get_fileid(path, flags, &fileid); - - if (err) - return err; - - /* Obtain a provider for the object. */ - - Provider *provider; - err = find_provider(fileid, &provider); - - /* Make a new provider if necessary. */ - - if (err == -L4_ENOENT) - err = make_provider(opening, path, flags, fileid, &provider); - - if (err) - return err; - - /* Make a resource for the provider. */ - - return provider->make_resource(size, object_flags, resource); -} - - - -/* Obtain any active provider for the given 'fileid'. */ - -long FileObjectRegistry::find_provider(fileid_t fileid, Provider **provider) -{ - /* Obtain any registered provider. */ - - Accountable *accountable = get(fileid); - - if (accountable != NULL) - { - *provider = dynamic_cast(accountable); - - if ((*provider) != NULL) - return L4_EOK; - else - return -L4_EIO; - } - - return -L4_ENOENT; -} - -// vim: tabstop=4 expandtab shiftwidth=4 diff -r d8a636f6f728 -r 4fb242f7d360 libfsserver/lib/files/file_provider.cc --- a/libfsserver/lib/files/file_provider.cc Sun Oct 17 22:14:40 2021 +0200 +++ b/libfsserver/lib/files/file_provider.cc Mon Oct 18 22:59:22 2021 +0200 @@ -27,7 +27,7 @@ /* Initialise the provider with a page 'mapper' for the file's contents. */ FileProvider::FileProvider(fileid_t fileid, map_flags_t flags, - FileRegistry *registry, PageMapper *mapper) + ProviderRegistry *registry, PageMapper *mapper) : Provider(fileid, registry), _flags(flags), _mapper(mapper) { } diff -r d8a636f6f728 -r 4fb242f7d360 libfsserver/lib/files/file_registry.cc --- a/libfsserver/lib/files/file_registry.cc Sun Oct 17 22:14:40 2021 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,76 +0,0 @@ -/* - * File registry functionality. - * - * Copyright (C) 2021 Paul Boddie - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA - */ - -#include "file_registry.h" - - - -/* Methods for use with the lock already acquired. */ - -/* Return any registered provider for the given 'fileid' or NULL if no such - provider is registered. */ - -Accountable *FileRegistry::get(fileid_t fileid) -{ - FileMapping::iterator entry = _providers.find(fileid); - Accountable *provider; - - if (entry == _providers.end()) - provider = NULL; - else - provider = entry->second; - - return provider; -} - -/* Remove a provider and its resources for the given 'fileid'. */ - -void FileRegistry::remove(fileid_t fileid, Accountable *provider) -{ - _providers.erase(fileid); - delete provider; -} - -/* Register a 'provider' for the given 'fileid'. */ - -void FileRegistry::set(fileid_t fileid, Accountable *provider) -{ - FileMapping::iterator entry = _providers.find(fileid); - - if (entry != _providers.end()) - return; - - _providers[fileid] = provider; -} - - - -/* Detach from a provider, potentially removing it from the registry. */ - -void FileRegistry::detach(fileid_t fileid, Accountable *provider) -{ - std::lock_guard guard(_lock); - - if (!provider->detach()) - remove(fileid, provider); -} - -// vim: tabstop=4 expandtab shiftwidth=4 diff -r d8a636f6f728 -r 4fb242f7d360 libfsserver/lib/files/filesystem_resource.cc --- a/libfsserver/lib/files/filesystem_resource.cc Sun Oct 17 22:14:40 2021 +0200 +++ b/libfsserver/lib/files/filesystem_resource.cc Mon Oct 18 22:59:22 2021 +0200 @@ -25,7 +25,7 @@ /* Support for providing access to user-specific filesystems. */ FilesystemResource::FilesystemResource(Pages *pages) -: FileObjectRegistry(pages) +: ResourceRegistry(pages) { } diff -r d8a636f6f728 -r 4fb242f7d360 libfsserver/lib/files/opener_resource.cc --- a/libfsserver/lib/files/opener_resource.cc Sun Oct 17 22:14:40 2021 +0200 +++ b/libfsserver/lib/files/opener_resource.cc Mon Oct 18 22:59:22 2021 +0200 @@ -27,7 +27,7 @@ /* Support for providing access to files. */ -OpenerResource::OpenerResource(FileObjectRegistry *registry) +OpenerResource::OpenerResource(ResourceRegistry *registry) : _registry(registry) { } diff -r d8a636f6f728 -r 4fb242f7d360 libfsserver/lib/files/test_file_opener.cc --- a/libfsserver/lib/files/test_file_opener.cc Sun Oct 17 22:14:40 2021 +0200 +++ b/libfsserver/lib/files/test_file_opener.cc Mon Oct 18 22:59:22 2021 +0200 @@ -26,7 +26,7 @@ /* Support for providing access to files. */ -TestFileOpener::TestFileOpener(FileObjectRegistry *registry, offset_t file_size) +TestFileOpener::TestFileOpener(ResourceRegistry *registry, offset_t file_size) : OpenerResource(registry), _file_size(file_size) { } diff -r d8a636f6f728 -r 4fb242f7d360 libfsserver/lib/generic/provider.cc --- a/libfsserver/lib/generic/provider.cc Sun Oct 17 22:14:40 2021 +0200 +++ b/libfsserver/lib/generic/provider.cc Mon Oct 18 22:59:22 2021 +0200 @@ -23,7 +23,7 @@ -Provider::Provider(fileid_t fileid, FileRegistry *registry) +Provider::Provider(fileid_t fileid, ProviderRegistry *registry) : NotificationSupport(), _fileid(fileid), _registry(registry) { } @@ -32,7 +32,7 @@ { } -FileRegistry *Provider::registry() +ProviderRegistry *Provider::registry() { return _registry; } diff -r d8a636f6f728 -r 4fb242f7d360 libfsserver/lib/generic/provider_registry.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libfsserver/lib/generic/provider_registry.cc Mon Oct 18 22:59:22 2021 +0200 @@ -0,0 +1,76 @@ +/* + * Filesystem provider registry. + * + * Copyright (C) 2021 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#include "provider_registry.h" + + + +/* Methods for use with the lock already acquired. */ + +/* Return any registered provider for the given 'fileid' or NULL if no such + provider is registered. */ + +Accountable *ProviderRegistry::get(fileid_t fileid) +{ + FileMapping::iterator entry = _providers.find(fileid); + Accountable *provider; + + if (entry == _providers.end()) + provider = NULL; + else + provider = entry->second; + + return provider; +} + +/* Remove a provider and its resources for the given 'fileid'. */ + +void ProviderRegistry::remove(fileid_t fileid, Accountable *provider) +{ + _providers.erase(fileid); + delete provider; +} + +/* Register a 'provider' for the given 'fileid'. */ + +void ProviderRegistry::set(fileid_t fileid, Accountable *provider) +{ + FileMapping::iterator entry = _providers.find(fileid); + + if (entry != _providers.end()) + return; + + _providers[fileid] = provider; +} + + + +/* Detach from a provider, potentially removing it from the registry. */ + +void ProviderRegistry::detach(fileid_t fileid, Accountable *provider) +{ + std::lock_guard guard(_lock); + + if (!provider->detach()) + remove(fileid, provider); +} + +// vim: tabstop=4 expandtab shiftwidth=4 diff -r d8a636f6f728 -r 4fb242f7d360 libfsserver/lib/generic/resource_registry.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libfsserver/lib/generic/resource_registry.cc Mon Oct 18 22:59:22 2021 +0200 @@ -0,0 +1,172 @@ +/* + * Filesystem resource registry incorporating opening functionality. + * + * Copyright (C) 2021 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#include "directory_resource.h" +#include "file_pager.h" +#include "resource_registry.h" + +#include + + + +/* The file object registry provides a memory page collection to the page + mappers it creates. */ + +ResourceRegistry::ResourceRegistry(Pages *pages) +: _pages(pages) +{ +} + + + +/* Convert opening flags to map-compatible paging flags. */ + +map_flags_t ResourceRegistry::get_flags(flags_t flags) +{ + return flags & (O_WRONLY | O_RDWR) ? L4RE_DS_MAP_FLAG_RW : L4RE_DS_MAP_FLAG_RO; +} + + + +/* Make a directory provider. */ + +long ResourceRegistry::make_directory_provider(FileOpening *opening, + const char *path, flags_t flags, + fileid_t fileid, + Provider **provider) +{ + /* Make an accessor and a provider to encapsulate it. */ + + DirectoryAccessor *accessor; + long err = opening->make_directory_accessor(path, flags, fileid, &accessor); + + if (err) + return err; + + *provider = new DirectoryProvider(fileid, this, accessor); + return L4_EOK; +} + +/* Make a file provider. */ + +long ResourceRegistry::make_file_provider(FileOpening *opening, + const char *path, flags_t flags, + fileid_t fileid, + Provider **provider) +{ + /* Make an accessor, page mapper, and a provider to encapsulate them. */ + + Accessor *accessor; + long err = opening->make_accessor(path, flags, fileid, &accessor); + + if (err) + return err; + + PageMapper *mapper = new PageMapper(accessor, _pages); + *provider = new FileProvider(fileid, get_flags(flags), this, mapper); + return L4_EOK; +} + +/* Make a provider of the appropriate type. */ + +long ResourceRegistry::make_provider(FileOpening *opening, + const char *path, flags_t flags, + fileid_t fileid, + Provider **provider) +{ + long err = -L4_EIO; + + if (opening->accessing_directory(path, flags, fileid)) + err = make_directory_provider(opening, path, flags, fileid, provider); + else if (opening->accessing_file(path, flags, fileid)) + err = make_file_provider(opening, path, flags, fileid, provider); + + if (err) + return err; + + /* Register the provider. */ + + set(fileid, *provider); + return L4_EOK; +} + + + +/* Obtain any active provider for the given 'fileid'. */ + +long ResourceRegistry::find_provider(fileid_t fileid, Provider **provider) +{ + /* Obtain any registered provider. */ + + Accountable *accountable = get(fileid); + + if (accountable != NULL) + { + *provider = dynamic_cast(accountable); + + if ((*provider) != NULL) + return L4_EOK; + else + return -L4_EIO; + } + + return -L4_ENOENT; +} + + + +/* Return a resource for a filesystem object. */ + +long ResourceRegistry::get_resource(FileOpening *opening, const char *path, + flags_t flags, offset_t *size, + object_flags_t *object_flags, + Resource **resource) +{ + std::lock_guard guard(_lock); + + /* Obtain an identifier for the object, even for new files (subject to use + of the appropriate flag). */ + + fileid_t fileid; + long err = opening->get_fileid(path, flags, &fileid); + + if (err) + return err; + + /* Obtain a provider for the object. */ + + Provider *provider; + err = find_provider(fileid, &provider); + + /* Make a new provider if necessary. */ + + if (err == -L4_ENOENT) + err = make_provider(opening, path, flags, fileid, &provider); + + if (err) + return err; + + /* Make a resource for the provider. */ + + return provider->make_resource(size, object_flags, resource); +} + +// vim: tabstop=4 expandtab shiftwidth=4 diff -r d8a636f6f728 -r 4fb242f7d360 servers/block_file_server.cc --- a/servers/block_file_server.cc Sun Oct 17 22:14:40 2021 +0200 +++ b/servers/block_file_server.cc Mon Oct 18 22:59:22 2021 +0200 @@ -66,7 +66,7 @@ MemoryIncremental mem(memory_pages); PageQueueShared queue; Pages pages(&mem, &queue); - FileObjectRegistry registry(&pages); + ResourceRegistry registry(&pages); BlockFileOpener opener(®istry); /* Register a server associating it with the given object. */ diff -r d8a636f6f728 -r 4fb242f7d360 servers/client_file_server.cc --- a/servers/client_file_server.cc Sun Oct 17 22:14:40 2021 +0200 +++ b/servers/client_file_server.cc Mon Oct 18 22:59:22 2021 +0200 @@ -67,7 +67,7 @@ MemoryIncremental mem(memory_pages); PageQueueShared queue; Pages pages(&mem, &queue); - FileObjectRegistry registry(&pages); + ResourceRegistry registry(&pages); ClientFileOpener opener(®istry); /* Register a server associating it with the given object. */ diff -r d8a636f6f728 -r 4fb242f7d360 servers/host_file_server.cc --- a/servers/host_file_server.cc Sun Oct 17 22:14:40 2021 +0200 +++ b/servers/host_file_server.cc Mon Oct 18 22:59:22 2021 +0200 @@ -66,7 +66,7 @@ MemoryIncremental mem(memory_pages); PageQueueShared queue; Pages pages(&mem, &queue); - FileObjectRegistry registry(&pages); + ResourceRegistry registry(&pages); HostFileOpener opener(®istry); /* Register a server associating it with the given object. */ diff -r d8a636f6f728 -r 4fb242f7d360 servers/test_file_server.cc --- a/servers/test_file_server.cc Sun Oct 17 22:14:40 2021 +0200 +++ b/servers/test_file_server.cc Mon Oct 18 22:59:22 2021 +0200 @@ -65,7 +65,7 @@ MemoryIncremental mem(memory_pages, page(REGION_PAGES)); PageQueueShared queue; Pages pages(&mem, &queue); - FileObjectRegistry registry(&pages); + ResourceRegistry registry(&pages); TestFileOpener opener(®istry, page(FILE_PAGES)); /* Register a server associating it with the given object. */