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