1 /* 2 * Filesystem resource registry incorporating opening functionality. 3 * 4 * Copyright (C) 2021, 2022 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include "directory_resource.h" 23 #include "file_pager.h" 24 #include "resource_registry.h" 25 26 #include <systypes/fcntl.h> 27 28 29 30 /* The file object registry provides a memory page collection to the page 31 mappers it creates. */ 32 33 ResourceRegistry::ResourceRegistry(Pages *pages) 34 : _pages(pages) 35 { 36 } 37 38 39 40 /* Convert opening flags to map-compatible paging flags. */ 41 42 map_flags_t ResourceRegistry::get_flags(flags_t flags) 43 { 44 return flags & (O_WRONLY | O_RDWR) ? L4RE_DS_MAP_FLAG_RW : L4RE_DS_MAP_FLAG_RO; 45 } 46 47 48 49 /* Make a directory provider. */ 50 51 long ResourceRegistry::make_directory_provider(FileOpening *opening, 52 flags_t flags, 53 fileid_t fileid, 54 Provider **provider) 55 { 56 /* Make an accessor and a provider to encapsulate it. */ 57 58 DirectoryAccessor *accessor; 59 long err = opening->make_directory_accessor(flags, fileid, &accessor); 60 61 if (err) 62 return err; 63 64 *provider = new DirectoryProvider(fileid, this, accessor); 65 return L4_EOK; 66 } 67 68 /* Make a file provider. */ 69 70 long ResourceRegistry::make_file_provider(FileOpening *opening, 71 flags_t flags, 72 fileid_t fileid, 73 Provider **provider) 74 { 75 /* Make an accessor, page mapper, and a provider to encapsulate them. */ 76 77 Accessor *accessor; 78 long err = opening->make_accessor(flags, fileid, &accessor); 79 80 if (err) 81 return err; 82 83 PageMapper *mapper = new PageMapper(accessor, _pages); 84 *provider = new FileProvider(fileid, get_flags(flags), this, mapper); 85 return L4_EOK; 86 } 87 88 /* Make a provider of the appropriate type. */ 89 90 long ResourceRegistry::make_provider(FileOpening *opening, 91 flags_t flags, 92 fileid_t fileid, 93 Provider **provider) 94 { 95 long err = -L4_EIO; 96 97 if (opening->accessing_directory(flags, fileid)) 98 err = make_directory_provider(opening, flags, fileid, provider); 99 else if (opening->accessing_file(flags, fileid)) 100 err = make_file_provider(opening, flags, fileid, provider); 101 102 if (err) 103 return err; 104 105 /* Register the provider. */ 106 107 set(fileid, *provider); 108 return L4_EOK; 109 } 110 111 112 113 /* Obtain any active provider for the given 'fileid'. */ 114 115 long ResourceRegistry::find_provider(fileid_t fileid, Provider **provider) 116 { 117 /* Obtain any registered provider. */ 118 119 Accountable *accountable = get(fileid); 120 121 if (accountable != NULL) 122 { 123 *provider = dynamic_cast<Provider *>(accountable); 124 125 if ((*provider) != NULL) 126 return L4_EOK; 127 else 128 return -L4_EIO; 129 } 130 131 return -L4_ENOENT; 132 } 133 134 135 136 /* Return a resource for a filesystem object. */ 137 138 long ResourceRegistry::get_resource(FileOpening *opening, const char *path, 139 flags_t flags, offset_t *size, 140 object_flags_t *object_flags, 141 Resource **resource) 142 { 143 std::lock_guard<std::mutex> guard(_lock); 144 145 /* Obtain an identifier for the object, even for new files (subject to use 146 of the appropriate flag). This also checks the access rights for any 147 configured user. */ 148 149 fileid_t fileid; 150 long err = opening->get_fileid(path, flags, &fileid); 151 152 if (err) 153 return err; 154 155 /* Obtain a provider for the object. */ 156 157 Provider *provider; 158 err = find_provider(fileid, &provider); 159 160 /* Make a new provider if necessary. */ 161 162 if (err == -L4_ENOENT) 163 err = make_provider(opening, flags, fileid, &provider); 164 165 if (err) 166 return err; 167 168 /* Make a resource for the provider. */ 169 170 return provider->make_resource(size, object_flags, resource); 171 } 172 173 /* Request the removal of a filesystem object through any active provider or 174 perform the direct removal of any inactive object. */ 175 176 long ResourceRegistry::remove_provider(FileOpening *opening, const char *path) 177 { 178 std::lock_guard<std::mutex> guard(_lock); 179 180 /* Obtain an identifier for any recognised object. This also checks the 181 access rights for any configured user. */ 182 183 fileid_t fileid; 184 long err = opening->get_fileid(path, 0, &fileid); 185 186 if (err) 187 return err; 188 189 return _remove_provider(opening, path, fileid); 190 } 191 192 long ResourceRegistry::_remove_provider(FileOpening *opening, const char *path, 193 fileid_t fileid) 194 { 195 /* Obtain a provider for the object. */ 196 197 Provider *provider; 198 long err = find_provider(fileid, &provider), lookup_err = err; 199 200 if (err && (err != -L4_ENOENT)) 201 return err; 202 203 fileid_t parent_fileid; 204 205 err = opening->get_parent(path, &parent_fileid); 206 207 if (err) 208 return err; 209 210 /* Unlink the object regardless of whether it will be removed. */ 211 212 err = opening->unlink_object(parent_fileid, fileid); 213 214 if (err) 215 return err; 216 217 /* Without a provider being active, remove the object directly. */ 218 219 if (lookup_err) 220 return opening->remove_object(fileid); 221 222 /* With a provider active, request the object's removal upon closure. */ 223 224 provider->remove_pending(true); 225 return L4_EOK; 226 } 227 228 /* Request the renaming of a filesystem object. */ 229 230 long ResourceRegistry::rename_provider(FileOpening *opening, const char *source, 231 const char *target) 232 { 233 std::lock_guard<std::mutex> guard(_lock); 234 235 long err; 236 fileid_t source_fileid, target_fileid; 237 238 /* Obtain an identifier for any recognised object. This also checks the 239 access rights for any configured user. */ 240 241 err = opening->get_fileid(source, 0, &source_fileid); 242 243 if (err) 244 return err; 245 246 /* If source is a directory, the target must be a new directory or an empty 247 directory. An empty directory will be replaced by the source 248 directory. */ 249 250 if (opening->accessing_directory(0, source_fileid)) 251 { 252 err = opening->get_fileid(target, 0, &target_fileid); 253 254 if (err && (err != -L4_ENOENT)) 255 return err; 256 257 /* Any existing directory must be empty. */ 258 259 if (!err) 260 { 261 /* NOTE: Should really use the equivalent of ENOTDIR. */ 262 263 if (!opening->accessing_directory(0, target_fileid)) 264 return -L4_EIO; 265 266 /* NOTE: Test for empty directory. */ 267 268 /* The existing directory will be removed. */ 269 270 err = _remove_provider(opening, target, target_fileid); 271 272 if (err) 273 return err; 274 } 275 276 /* The source directory is moved within the filesystem to the parent of 277 the indicated target. */ 278 279 return opening->rename_object(source, target); 280 } 281 282 /* If source is a file, the target must be a new or existing file, not a 283 directory. An existing file will be replaced. */ 284 285 else if (opening->accessing_file(0, source_fileid)) 286 { 287 err = opening->get_fileid(target, 0, &target_fileid); 288 289 if (err && (err != -L4_ENOENT)) 290 return err; 291 292 if (!err) 293 { 294 /* NOTE: Should really use the equivalent of EISDIR. */ 295 296 if (!opening->accessing_file(0, target_fileid)) 297 return -L4_EIO; 298 299 /* The existing file will be removed. */ 300 301 err = _remove_provider(opening, target, target_fileid); 302 303 if (err) 304 return err; 305 } 306 307 /* The source file is moved within the filesystem to the parent of the 308 indicated target. */ 309 310 return opening->rename_object(source, target); 311 } 312 313 /* NOTE: Other object types are to be supported. */ 314 315 else 316 return -L4_EIO; 317 } 318 319 // vim: tabstop=4 expandtab shiftwidth=4