1 /* 2 * General functionality supporting file paging. 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 "file_pager.h" 23 #include "file_paging.h" 24 25 #include <systypes/fcntl.h> 26 27 28 29 FilePaging::FilePaging(Pages *pages) 30 : NotificationSupport(), _pages(pages) 31 { 32 } 33 34 35 36 /* Return any registered page mapper for the given 'fileid' or NULL if no such 37 mapper is registered. */ 38 39 PageMapper *FilePaging::get(fileid_t fileid) 40 { 41 FileMapping::iterator entry = _mappers.find(fileid); 42 PageMapper *mapper; 43 44 if (entry == _mappers.end()) 45 mapper = NULL; 46 else 47 mapper = entry->second; 48 49 return mapper; 50 } 51 52 /* Remove a page mapper and its resources for the given 'fileid'. */ 53 54 void FilePaging::remove(fileid_t fileid, PageMapper *mapper) 55 { 56 _mappers.erase(fileid); 57 delete mapper->accessor(); 58 delete mapper; 59 } 60 61 /* Register a page 'mapper' for the given 'fileid'. */ 62 63 void FilePaging::set(fileid_t fileid, PageMapper *mapper) 64 { 65 FileMapping::iterator entry = _mappers.find(fileid); 66 67 if (entry == _mappers.end()) 68 _mappers[fileid] = mapper; 69 } 70 71 72 73 /* Convert opening flags to map-compatible paging flags. */ 74 75 map_flags_t FilePaging::get_flags(flags_t flags) 76 { 77 return flags & (O_WRONLY | O_RDWR) ? L4RE_DS_MAP_FLAG_RW : L4RE_DS_MAP_FLAG_RO; 78 } 79 80 81 82 /* Obtain a page mapper for the 'fileid' or register a new one in the 83 paging object. */ 84 85 long FilePaging::get_mapper(FileOpening *opening, const char *path, flags_t flags, fileid_t fileid, PageMapper **mapper) 86 { 87 /* Obtain any registered page mapper. */ 88 89 *mapper = get(fileid); 90 91 if (*mapper != NULL) 92 return L4_EOK; 93 94 /* Make an accessor and page mapper, registering the mapper. */ 95 96 Accessor *accessor; 97 long err = opening->make_accessor(path, flags, fileid, &accessor); 98 99 if (err) 100 return err; 101 102 *mapper = new PageMapper(accessor, _pages); 103 104 set(fileid, *mapper); 105 106 return L4_EOK; 107 } 108 109 110 111 /* Return a pager initialised with a page mapper. */ 112 113 long FilePaging::get_pager(FileOpening *opening, const char *path, flags_t flags, fileid_t fileid, Pager **pager) 114 { 115 std::lock_guard<std::mutex> guard(_lock); 116 117 /* Obtain any existing page mapper registered for the file, or make a new 118 page mapper. */ 119 120 PageMapper *mapper; 121 long err = get_mapper(opening, path, flags, fileid, &mapper); 122 123 if (err) 124 return err; 125 126 /* Initialise the pager with the mapper and a reference to this object for 127 closing the mapper and accessor. */ 128 129 *pager = new FilePager(fileid, mapper, get_flags(flags), this); 130 return L4_EOK; 131 } 132 133 /* Detach a pager, potentially removing its resources. */ 134 135 void FilePaging::detach_pager(fileid_t fileid, PageMapper *mapper) 136 { 137 std::lock_guard<std::mutex> guard(_lock); 138 139 if (!mapper->detach()) 140 remove(fileid, mapper); 141 } 142 143 // vim: tabstop=4 expandtab shiftwidth=4