1 /* 2 * Filesystem resource registry incorporating opening functionality. 3 * 4 * Copyright (C) 2021 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 const char *path, 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(path, 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 const char *path, 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(path, 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 const char *path, flags_t flags, 92 fileid_t fileid, 93 Provider **provider) 94 { 95 long err = -L4_EIO; 96 97 if (opening->accessing_directory(path, flags, fileid)) 98 err = make_directory_provider(opening, path, flags, fileid, provider); 99 else if (opening->accessing_file(path, flags, fileid)) 100 err = make_file_provider(opening, path, 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). */ 147 148 fileid_t fileid; 149 long err = opening->get_fileid(path, flags, &fileid); 150 151 if (err) 152 return err; 153 154 /* Obtain a provider for the object. */ 155 156 Provider *provider; 157 err = find_provider(fileid, &provider); 158 159 /* Make a new provider if necessary. */ 160 161 if (err == -L4_ENOENT) 162 err = make_provider(opening, path, flags, fileid, &provider); 163 164 if (err) 165 return err; 166 167 /* Make a resource for the provider. */ 168 169 return provider->make_resource(size, object_flags, resource); 170 } 171 172 /* Request the removal of a filesystem object through any active provider or 173 perform the direct removal of any inactive object. */ 174 175 long ResourceRegistry::remove_provider(FileOpening *opening, const char *path) 176 { 177 std::lock_guard<std::mutex> guard(_lock); 178 179 /* Obtain an identifier for any recognised object. */ 180 181 fileid_t fileid; 182 long err = opening->get_fileid(path, 0, &fileid); 183 184 if (err) 185 return err; 186 187 return _remove_provider(opening, path, fileid); 188 } 189 190 long ResourceRegistry::_remove_provider(FileOpening *opening, const char *path, 191 fileid_t fileid) 192 { 193 /* Obtain a provider for the object. */ 194 195 Provider *provider; 196 long err = find_provider(fileid, &provider); 197 198 if (err && (err != -L4_ENOENT)) 199 return err; 200 201 /* Unlink the object regardless of whether it can be removed. */ 202 203 err = opening->unlink_object(path, fileid); 204 205 if (err) 206 return err; 207 208 /* Without a provider being active, remove the object directly. */ 209 210 if (err) 211 return opening->remove_object(path, fileid); 212 213 /* With a provider active, request the object's removal upon closure. */ 214 215 provider->remove_pending(true); 216 return L4_EOK; 217 } 218 219 /* Request the renaming of a filesystem object. */ 220 221 long ResourceRegistry::rename_provider(FileOpening *opening, const char *source, 222 const char *target) 223 { 224 std::lock_guard<std::mutex> guard(_lock); 225 226 long err; 227 fileid_t source_fileid, target_fileid; 228 229 /* Obtain an identifier for any recognised object. */ 230 231 err = opening->get_fileid(source, 0, &source_fileid); 232 233 if (err) 234 return err; 235 236 /* If source is a directory, the target must be a new directory or an empty 237 directory. An empty directory will be replaced by the source 238 directory. */ 239 240 if (opening->accessing_directory(source, 0, source_fileid)) 241 { 242 err = opening->get_fileid(target, 0, &target_fileid); 243 244 if (err && (err != -L4_ENOENT)) 245 return err; 246 247 /* Any existing directory must be empty. */ 248 249 if (!err) 250 { 251 /* NOTE: Should really use the equivalent of ENOTDIR. */ 252 253 if (!opening->accessing_directory(target, 0, target_fileid)) 254 return -L4_EIO; 255 256 /* NOTE: Test for empty directory. */ 257 258 /* The existing directory will be removed. */ 259 260 err = _remove_provider(opening, target, target_fileid); 261 262 if (err) 263 return err; 264 } 265 266 /* The source directory is moved within the filesystem to the parent of 267 the indicated target. */ 268 269 return opening->rename_object(source, source_fileid, target); 270 } 271 272 /* If source is a file, the target must be a new or existing file, not a 273 directory. An existing file will be replaced. */ 274 275 else if (opening->accessing_file(source, 0, source_fileid)) 276 { 277 err = opening->get_fileid(target, 0, &target_fileid); 278 279 if (err && (err != -L4_ENOENT)) 280 return err; 281 282 if (!err) 283 { 284 /* NOTE: Should really use the equivalent of EISDIR. */ 285 286 if (!opening->accessing_file(target, 0, target_fileid)) 287 return -L4_EIO; 288 289 /* The existing file will be removed. */ 290 291 err = _remove_provider(opening, target, target_fileid); 292 293 if (err) 294 return err; 295 } 296 297 /* The source file is moved within the filesystem to the parent of the 298 indicated target. */ 299 300 return opening->rename_object(source, source_fileid, target); 301 } 302 303 /* NOTE: Other object types are to be supported. */ 304 305 else 306 return -L4_EIO; 307 } 308 309 // vim: tabstop=4 expandtab shiftwidth=4