1 /* 2 * File registry and 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_object_registry.h" 24 #include "file_pager.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 FileObjectRegistry::FileObjectRegistry(Pages *pages) 34 : _pages(pages) 35 { 36 } 37 38 39 40 /* Convert opening flags to map-compatible paging flags. */ 41 42 map_flags_t FileObjectRegistry::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 /* Obtain any active provider for 'fileid'. */ 50 51 long FileObjectRegistry::find_directory_provider(fileid_t fileid, 52 DirectoryProvider **directory_provider) 53 { 54 /* Obtain any registered provider. */ 55 56 Accountable *provider = get(fileid); 57 58 if (provider != NULL) 59 { 60 *directory_provider = dynamic_cast<DirectoryProvider *>(provider); 61 62 if ((*directory_provider) != NULL) 63 return L4_EOK; 64 else 65 return -L4_EIO; 66 } 67 68 return -L4_ENOENT; 69 } 70 71 /* Obtain a provider for the 'fileid' or register a new one. */ 72 73 long FileObjectRegistry::get_directory_provider(FileOpening *opening, 74 const char *path, flags_t flags, 75 fileid_t fileid, 76 DirectoryProvider **directory_provider) 77 { 78 long err = find_directory_provider(fileid, directory_provider); 79 80 if (err != -L4_ENOENT) 81 return err; 82 83 /* Make an accessor and a provider to encapsulate it. */ 84 85 DirectoryAccessor *accessor; 86 err = opening->make_directory_accessor(path, flags, fileid, &accessor); 87 88 if (err) 89 return err; 90 91 *directory_provider = new DirectoryProvider(accessor); 92 93 /* Register the provider. */ 94 95 set(fileid, *directory_provider); 96 return L4_EOK; 97 } 98 99 /* Obtain a provider for the 'fileid' or register a new one. */ 100 101 long FileObjectRegistry::get_file_provider(FileOpening *opening, 102 const char *path, flags_t flags, 103 fileid_t fileid, 104 FileProvider **file_provider) 105 { 106 /* Obtain any registered provider. */ 107 108 Accountable *provider = get(fileid); 109 110 if (provider != NULL) 111 { 112 *file_provider = dynamic_cast<FileProvider *>(provider); 113 114 if ((*file_provider) != NULL) 115 return L4_EOK; 116 else 117 return -L4_EIO; 118 } 119 120 /* Make an accessor, page mapper, and a provider to encapsulate them. */ 121 122 Accessor *accessor; 123 long err = opening->make_accessor(path, flags, fileid, &accessor); 124 125 if (err) 126 return err; 127 128 PageMapper *mapper = new PageMapper(accessor, _pages); 129 *file_provider = new FileProvider(mapper); 130 131 /* Register the provider. */ 132 133 set(fileid, *file_provider); 134 return L4_EOK; 135 } 136 137 138 139 /* Return a directory resource initialised with a provider. */ 140 141 long FileObjectRegistry::get_directory(FileOpening *opening, const char *path, 142 flags_t flags, fileid_t fileid, 143 Resource **resource) 144 { 145 std::lock_guard<std::mutex> guard(_lock); 146 147 /* Obtain any existing provider registered for the object, or make a new 148 provider. */ 149 150 DirectoryProvider *provider; 151 long err = get_directory_provider(opening, path, flags, fileid, &provider); 152 153 if (err) 154 return err; 155 156 /* Initialise the resource with the provider and a reference to this object 157 for detaching from the provider. */ 158 159 provider->attach(); 160 *resource = new DirectoryResource(fileid, provider, this); 161 return L4_EOK; 162 } 163 164 /* Return a pager initialised with a provider, page mapper and accessor. */ 165 166 long FileObjectRegistry::get_pager(FileOpening *opening, const char *path, 167 flags_t flags, fileid_t fileid, 168 Pager **pager) 169 { 170 std::lock_guard<std::mutex> guard(_lock); 171 172 /* Obtain any existing provider registered for the file, or make a new 173 provider. */ 174 175 FileProvider *provider; 176 long err = get_file_provider(opening, path, flags, fileid, &provider); 177 178 if (err) 179 return err; 180 181 /* Initialise the pager with the provider and a reference to this object for 182 detaching from the provider. */ 183 184 provider->attach(); 185 *pager = new FilePager(fileid, provider, get_flags(flags), this); 186 return L4_EOK; 187 } 188 189 // vim: tabstop=4 expandtab shiftwidth=4