paul@93 | 1 | /* |
paul@202 | 2 | * File registry and opening functionality. |
paul@93 | 3 | * |
paul@93 | 4 | * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> |
paul@93 | 5 | * |
paul@93 | 6 | * This program is free software; you can redistribute it and/or |
paul@93 | 7 | * modify it under the terms of the GNU General Public License as |
paul@93 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@93 | 9 | * the License, or (at your option) any later version. |
paul@93 | 10 | * |
paul@93 | 11 | * This program is distributed in the hope that it will be useful, |
paul@93 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@93 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@93 | 14 | * GNU General Public License for more details. |
paul@93 | 15 | * |
paul@93 | 16 | * You should have received a copy of the GNU General Public License |
paul@93 | 17 | * along with this program; if not, write to the Free Software |
paul@93 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@93 | 19 | * Boston, MA 02110-1301, USA |
paul@93 | 20 | */ |
paul@93 | 21 | |
paul@202 | 22 | #include "directory_resource.h" |
paul@79 | 23 | #include "file_pager.h" |
paul@81 | 24 | #include "file_paging.h" |
paul@9 | 25 | |
paul@85 | 26 | #include <systypes/fcntl.h> |
paul@85 | 27 | |
paul@79 | 28 | |
paul@79 | 29 | |
paul@200 | 30 | /* The file paging coordinator provides a memory page collection to the page |
paul@200 | 31 | mappers it creates. */ |
paul@200 | 32 | |
paul@200 | 33 | FilePaging::FilePaging(Pages *pages) |
paul@200 | 34 | : _pages(pages) |
paul@94 | 35 | { |
paul@94 | 36 | } |
paul@94 | 37 | |
paul@85 | 38 | |
paul@85 | 39 | |
paul@87 | 40 | /* Convert opening flags to map-compatible paging flags. */ |
paul@85 | 41 | |
paul@121 | 42 | map_flags_t FilePaging::get_flags(flags_t flags) |
paul@85 | 43 | { |
paul@87 | 44 | return flags & (O_WRONLY | O_RDWR) ? L4RE_DS_MAP_FLAG_RW : L4RE_DS_MAP_FLAG_RO; |
paul@85 | 45 | } |
paul@85 | 46 | |
paul@85 | 47 | |
paul@85 | 48 | |
paul@208 | 49 | /* Obtain any active provider for 'fileid'. */ |
paul@79 | 50 | |
paul@208 | 51 | long FilePaging::find_directory_provider(fileid_t fileid, |
paul@208 | 52 | DirectoryProvider **directory_provider) |
paul@79 | 53 | { |
paul@200 | 54 | /* Obtain any registered provider. */ |
paul@200 | 55 | |
paul@200 | 56 | Accountable *provider = get(fileid); |
paul@79 | 57 | |
paul@200 | 58 | if (provider != NULL) |
paul@200 | 59 | { |
paul@202 | 60 | *directory_provider = dynamic_cast<DirectoryProvider *>(provider); |
paul@202 | 61 | |
paul@202 | 62 | if ((*directory_provider) != NULL) |
paul@202 | 63 | return L4_EOK; |
paul@202 | 64 | else |
paul@202 | 65 | return -L4_EIO; |
paul@202 | 66 | } |
paul@202 | 67 | |
paul@208 | 68 | return -L4_ENOENT; |
paul@208 | 69 | } |
paul@208 | 70 | |
paul@208 | 71 | /* Obtain a provider for the 'fileid' or register a new one. */ |
paul@208 | 72 | |
paul@208 | 73 | long FilePaging::get_directory_provider(FileOpening *opening, const char *path, |
paul@208 | 74 | flags_t flags, fileid_t fileid, |
paul@208 | 75 | DirectoryProvider **directory_provider) |
paul@208 | 76 | { |
paul@208 | 77 | long err = find_directory_provider(fileid, directory_provider); |
paul@208 | 78 | |
paul@208 | 79 | if (err != -L4_ENOENT) |
paul@208 | 80 | return err; |
paul@208 | 81 | |
paul@202 | 82 | /* Make an accessor and a provider to encapsulate it. */ |
paul@202 | 83 | |
paul@202 | 84 | DirectoryAccessor *accessor; |
paul@208 | 85 | err = opening->make_directory_accessor(path, flags, fileid, &accessor); |
paul@202 | 86 | |
paul@202 | 87 | if (err) |
paul@202 | 88 | return err; |
paul@202 | 89 | |
paul@202 | 90 | *directory_provider = new DirectoryProvider(accessor); |
paul@200 | 91 | |
paul@202 | 92 | /* Register the provider. */ |
paul@202 | 93 | |
paul@202 | 94 | set(fileid, *directory_provider); |
paul@202 | 95 | return L4_EOK; |
paul@202 | 96 | } |
paul@202 | 97 | |
paul@202 | 98 | /* Obtain a provider for the 'fileid' or register a new one. */ |
paul@202 | 99 | |
paul@202 | 100 | long FilePaging::get_file_provider(FileOpening *opening, const char *path, |
paul@202 | 101 | flags_t flags, fileid_t fileid, |
paul@202 | 102 | FileProvider **file_provider) |
paul@202 | 103 | { |
paul@202 | 104 | /* Obtain any registered provider. */ |
paul@202 | 105 | |
paul@202 | 106 | Accountable *provider = get(fileid); |
paul@202 | 107 | |
paul@202 | 108 | if (provider != NULL) |
paul@202 | 109 | { |
paul@200 | 110 | *file_provider = dynamic_cast<FileProvider *>(provider); |
paul@79 | 111 | |
paul@200 | 112 | if ((*file_provider) != NULL) |
paul@200 | 113 | return L4_EOK; |
paul@200 | 114 | else |
paul@200 | 115 | return -L4_EIO; |
paul@200 | 116 | } |
paul@79 | 117 | |
paul@200 | 118 | /* Make an accessor, page mapper, and a provider to encapsulate them. */ |
paul@79 | 119 | |
paul@143 | 120 | Accessor *accessor; |
paul@144 | 121 | long err = opening->make_accessor(path, flags, fileid, &accessor); |
paul@106 | 122 | |
paul@143 | 123 | if (err) |
paul@143 | 124 | return err; |
paul@106 | 125 | |
paul@200 | 126 | PageMapper *mapper = new PageMapper(accessor, _pages); |
paul@200 | 127 | *file_provider = new FileProvider(mapper); |
paul@79 | 128 | |
paul@200 | 129 | /* Register the provider. */ |
paul@79 | 130 | |
paul@200 | 131 | set(fileid, *file_provider); |
paul@143 | 132 | return L4_EOK; |
paul@79 | 133 | } |
paul@79 | 134 | |
paul@79 | 135 | |
paul@79 | 136 | |
paul@202 | 137 | /* Return a directory resource initialised with a provider. */ |
paul@202 | 138 | |
paul@202 | 139 | long FilePaging::get_directory(FileOpening *opening, const char *path, |
paul@202 | 140 | flags_t flags, fileid_t fileid, |
paul@202 | 141 | Resource **resource) |
paul@202 | 142 | { |
paul@202 | 143 | std::lock_guard<std::mutex> guard(_lock); |
paul@202 | 144 | |
paul@202 | 145 | /* Obtain any existing provider registered for the object, or make a new |
paul@202 | 146 | provider. */ |
paul@202 | 147 | |
paul@202 | 148 | DirectoryProvider *provider; |
paul@202 | 149 | long err = get_directory_provider(opening, path, flags, fileid, &provider); |
paul@202 | 150 | |
paul@202 | 151 | if (err) |
paul@202 | 152 | return err; |
paul@202 | 153 | |
paul@202 | 154 | /* Initialise the resource with the provider and a reference to this object |
paul@202 | 155 | for detaching from the provider. */ |
paul@202 | 156 | |
paul@202 | 157 | provider->attach(); |
paul@202 | 158 | *resource = new DirectoryResource(fileid, provider, this); |
paul@202 | 159 | return L4_EOK; |
paul@202 | 160 | } |
paul@202 | 161 | |
paul@200 | 162 | /* Return a pager initialised with a provider, page mapper and accessor. */ |
paul@79 | 163 | |
paul@202 | 164 | long FilePaging::get_pager(FileOpening *opening, const char *path, |
paul@202 | 165 | flags_t flags, fileid_t fileid, Pager **pager) |
paul@79 | 166 | { |
paul@79 | 167 | std::lock_guard<std::mutex> guard(_lock); |
paul@79 | 168 | |
paul@200 | 169 | /* Obtain any existing provider registered for the file, or make a new |
paul@200 | 170 | provider. */ |
paul@143 | 171 | |
paul@200 | 172 | FileProvider *provider; |
paul@202 | 173 | long err = get_file_provider(opening, path, flags, fileid, &provider); |
paul@143 | 174 | |
paul@143 | 175 | if (err) |
paul@143 | 176 | return err; |
paul@143 | 177 | |
paul@200 | 178 | /* Initialise the pager with the provider and a reference to this object for |
paul@202 | 179 | detaching from the provider. */ |
paul@79 | 180 | |
paul@200 | 181 | provider->attach(); |
paul@200 | 182 | *pager = new FilePager(fileid, provider, get_flags(flags), this); |
paul@143 | 183 | return L4_EOK; |
paul@79 | 184 | } |
paul@79 | 185 | |
paul@9 | 186 | // vim: tabstop=4 expandtab shiftwidth=4 |