paul@93 | 1 | /* |
paul@93 | 2 | * General functionality supporting file paging. |
paul@93 | 3 | * |
paul@93 | 4 | * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> |
paul@93 | 5 | * |
paul@93 | 6 | * This program is free software; you can redistribute it and/or |
paul@93 | 7 | * modify it under the terms of the GNU General Public License as |
paul@93 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@93 | 9 | * the License, or (at your option) any later version. |
paul@93 | 10 | * |
paul@93 | 11 | * This program is distributed in the hope that it will be useful, |
paul@93 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@93 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@93 | 14 | * GNU General Public License for more details. |
paul@93 | 15 | * |
paul@93 | 16 | * You should have received a copy of the GNU General Public License |
paul@93 | 17 | * along with this program; if not, write to the Free Software |
paul@93 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@93 | 19 | * Boston, MA 02110-1301, USA |
paul@93 | 20 | */ |
paul@93 | 21 | |
paul@79 | 22 | #include "file_pager.h" |
paul@81 | 23 | #include "file_paging.h" |
paul@9 | 24 | |
paul@85 | 25 | #include <systypes/fcntl.h> |
paul@85 | 26 | |
paul@79 | 27 | |
paul@79 | 28 | |
paul@81 | 29 | FilePaging::FilePaging(Pages *pages) |
paul@193 | 30 | : _pages(pages) |
paul@94 | 31 | { |
paul@94 | 32 | } |
paul@94 | 33 | |
paul@85 | 34 | |
paul@85 | 35 | |
paul@79 | 36 | /* Return any registered page mapper for the given 'fileid' or NULL if no such |
paul@79 | 37 | mapper is registered. */ |
paul@9 | 38 | |
paul@81 | 39 | PageMapper *FilePaging::get(fileid_t fileid) |
paul@9 | 40 | { |
paul@78 | 41 | FileMapping::iterator entry = _mappers.find(fileid); |
paul@9 | 42 | PageMapper *mapper; |
paul@9 | 43 | |
paul@9 | 44 | if (entry == _mappers.end()) |
paul@9 | 45 | mapper = NULL; |
paul@9 | 46 | else |
paul@9 | 47 | mapper = entry->second; |
paul@9 | 48 | |
paul@9 | 49 | return mapper; |
paul@9 | 50 | } |
paul@9 | 51 | |
paul@193 | 52 | /* Remove a notification manager for the given 'fileid'. */ |
paul@193 | 53 | |
paul@193 | 54 | void FilePaging::remove(fileid_t fileid, FileNotification *notifier) |
paul@193 | 55 | { |
paul@193 | 56 | _notifiers.erase(fileid); |
paul@193 | 57 | delete notifier; |
paul@193 | 58 | } |
paul@193 | 59 | |
paul@106 | 60 | /* Remove a page mapper and its resources for the given 'fileid'. */ |
paul@106 | 61 | |
paul@106 | 62 | void FilePaging::remove(fileid_t fileid, PageMapper *mapper) |
paul@106 | 63 | { |
paul@106 | 64 | _mappers.erase(fileid); |
paul@106 | 65 | delete mapper->accessor(); |
paul@106 | 66 | delete mapper; |
paul@106 | 67 | } |
paul@106 | 68 | |
paul@193 | 69 | /* Register a page 'mapper' and a notification manager for the given 'fileid'. */ |
paul@79 | 70 | |
paul@81 | 71 | void FilePaging::set(fileid_t fileid, PageMapper *mapper) |
paul@9 | 72 | { |
paul@78 | 73 | FileMapping::iterator entry = _mappers.find(fileid); |
paul@9 | 74 | |
paul@193 | 75 | if (entry != _mappers.end()) |
paul@193 | 76 | return; |
paul@193 | 77 | |
paul@193 | 78 | _mappers[fileid] = mapper; |
paul@193 | 79 | _notifiers[fileid] = new FileNotification; |
paul@79 | 80 | } |
paul@9 | 81 | |
paul@85 | 82 | |
paul@85 | 83 | |
paul@87 | 84 | /* Convert opening flags to map-compatible paging flags. */ |
paul@85 | 85 | |
paul@121 | 86 | map_flags_t FilePaging::get_flags(flags_t flags) |
paul@85 | 87 | { |
paul@87 | 88 | return flags & (O_WRONLY | O_RDWR) ? L4RE_DS_MAP_FLAG_RW : L4RE_DS_MAP_FLAG_RO; |
paul@85 | 89 | } |
paul@85 | 90 | |
paul@85 | 91 | |
paul@85 | 92 | |
paul@79 | 93 | /* Obtain a page mapper for the 'fileid' or register a new one in the |
paul@79 | 94 | paging object. */ |
paul@79 | 95 | |
paul@144 | 96 | long FilePaging::get_mapper(FileOpening *opening, const char *path, flags_t flags, fileid_t fileid, PageMapper **mapper) |
paul@79 | 97 | { |
paul@79 | 98 | /* Obtain any registered page mapper. */ |
paul@79 | 99 | |
paul@143 | 100 | *mapper = get(fileid); |
paul@79 | 101 | |
paul@143 | 102 | if (*mapper != NULL) |
paul@143 | 103 | return L4_EOK; |
paul@79 | 104 | |
paul@79 | 105 | /* Make an accessor and page mapper, registering the mapper. */ |
paul@79 | 106 | |
paul@143 | 107 | Accessor *accessor; |
paul@144 | 108 | long err = opening->make_accessor(path, flags, fileid, &accessor); |
paul@106 | 109 | |
paul@143 | 110 | if (err) |
paul@143 | 111 | return err; |
paul@106 | 112 | |
paul@143 | 113 | *mapper = new PageMapper(accessor, _pages); |
paul@79 | 114 | |
paul@143 | 115 | set(fileid, *mapper); |
paul@79 | 116 | |
paul@143 | 117 | return L4_EOK; |
paul@79 | 118 | } |
paul@79 | 119 | |
paul@193 | 120 | /* Obtain a file-specific notification manager. */ |
paul@193 | 121 | |
paul@193 | 122 | FileNotification *FilePaging::get_notifier(fileid_t file) |
paul@193 | 123 | { |
paul@193 | 124 | FileNotifiers::iterator entry = _notifiers.find(file); |
paul@193 | 125 | |
paul@193 | 126 | if (entry != _notifiers.end()) |
paul@193 | 127 | return _notifiers[file]; |
paul@193 | 128 | else |
paul@193 | 129 | return NULL; |
paul@193 | 130 | } |
paul@193 | 131 | |
paul@79 | 132 | |
paul@79 | 133 | |
paul@79 | 134 | /* Return a pager initialised with a page mapper. */ |
paul@79 | 135 | |
paul@156 | 136 | long FilePaging::get_pager(FileOpening *opening, const char *path, flags_t flags, fileid_t fileid, Pager **pager) |
paul@79 | 137 | { |
paul@79 | 138 | std::lock_guard<std::mutex> guard(_lock); |
paul@79 | 139 | |
paul@143 | 140 | /* Obtain any existing page mapper registered for the file, or make a new |
paul@143 | 141 | page mapper. */ |
paul@143 | 142 | |
paul@143 | 143 | PageMapper *mapper; |
paul@156 | 144 | long err = get_mapper(opening, path, flags, fileid, &mapper); |
paul@143 | 145 | |
paul@143 | 146 | if (err) |
paul@143 | 147 | return err; |
paul@143 | 148 | |
paul@79 | 149 | /* Initialise the pager with the mapper and a reference to this object for |
paul@79 | 150 | closing the mapper and accessor. */ |
paul@79 | 151 | |
paul@143 | 152 | *pager = new FilePager(fileid, mapper, get_flags(flags), this); |
paul@143 | 153 | return L4_EOK; |
paul@79 | 154 | } |
paul@79 | 155 | |
paul@79 | 156 | /* Detach a pager, potentially removing its resources. */ |
paul@79 | 157 | |
paul@81 | 158 | void FilePaging::detach_pager(fileid_t fileid, PageMapper *mapper) |
paul@79 | 159 | { |
paul@79 | 160 | std::lock_guard<std::mutex> guard(_lock); |
paul@79 | 161 | |
paul@79 | 162 | if (!mapper->detach()) |
paul@106 | 163 | remove(fileid, mapper); |
paul@193 | 164 | |
paul@193 | 165 | FileNotification *notifier = get_notifier(fileid); |
paul@193 | 166 | |
paul@193 | 167 | if ((notifier != NULL) && (!notifier->detach())) |
paul@193 | 168 | remove(fileid, notifier); |
paul@193 | 169 | } |
paul@193 | 170 | |
paul@193 | 171 | /* Obtain a file-specific notification manager. */ |
paul@193 | 172 | |
paul@193 | 173 | FileNotification *FilePaging::notifier(fileid_t file) |
paul@193 | 174 | { |
paul@193 | 175 | std::lock_guard<std::mutex> guard(_lock); |
paul@193 | 176 | |
paul@193 | 177 | return get_notifier(file); |
paul@9 | 178 | } |
paul@9 | 179 | |
paul@9 | 180 | // vim: tabstop=4 expandtab shiftwidth=4 |