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@195 | 29 | FilePaging::FilePaging(Pages *pages, FileNotifierRegistry *notifiers) |
paul@195 | 30 | : _pages(pages), _notifiers(notifiers) |
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@106 | 52 | /* Remove a page mapper and its resources for the given 'fileid'. */ |
paul@106 | 53 | |
paul@106 | 54 | void FilePaging::remove(fileid_t fileid, PageMapper *mapper) |
paul@106 | 55 | { |
paul@106 | 56 | _mappers.erase(fileid); |
paul@106 | 57 | delete mapper->accessor(); |
paul@106 | 58 | delete mapper; |
paul@106 | 59 | } |
paul@106 | 60 | |
paul@193 | 61 | /* Register a page 'mapper' and a notification manager for the given 'fileid'. */ |
paul@79 | 62 | |
paul@81 | 63 | void FilePaging::set(fileid_t fileid, PageMapper *mapper) |
paul@9 | 64 | { |
paul@78 | 65 | FileMapping::iterator entry = _mappers.find(fileid); |
paul@9 | 66 | |
paul@193 | 67 | if (entry != _mappers.end()) |
paul@193 | 68 | return; |
paul@193 | 69 | |
paul@193 | 70 | _mappers[fileid] = mapper; |
paul@79 | 71 | } |
paul@9 | 72 | |
paul@85 | 73 | |
paul@85 | 74 | |
paul@87 | 75 | /* Convert opening flags to map-compatible paging flags. */ |
paul@85 | 76 | |
paul@121 | 77 | map_flags_t FilePaging::get_flags(flags_t flags) |
paul@85 | 78 | { |
paul@87 | 79 | return flags & (O_WRONLY | O_RDWR) ? L4RE_DS_MAP_FLAG_RW : L4RE_DS_MAP_FLAG_RO; |
paul@85 | 80 | } |
paul@85 | 81 | |
paul@85 | 82 | |
paul@85 | 83 | |
paul@79 | 84 | /* Obtain a page mapper for the 'fileid' or register a new one in the |
paul@79 | 85 | paging object. */ |
paul@79 | 86 | |
paul@144 | 87 | long FilePaging::get_mapper(FileOpening *opening, const char *path, flags_t flags, fileid_t fileid, PageMapper **mapper) |
paul@79 | 88 | { |
paul@79 | 89 | /* Obtain any registered page mapper. */ |
paul@79 | 90 | |
paul@143 | 91 | *mapper = get(fileid); |
paul@79 | 92 | |
paul@143 | 93 | if (*mapper != NULL) |
paul@143 | 94 | return L4_EOK; |
paul@79 | 95 | |
paul@79 | 96 | /* Make an accessor and page mapper, registering the mapper. */ |
paul@79 | 97 | |
paul@143 | 98 | Accessor *accessor; |
paul@144 | 99 | long err = opening->make_accessor(path, flags, fileid, &accessor); |
paul@106 | 100 | |
paul@143 | 101 | if (err) |
paul@143 | 102 | return err; |
paul@106 | 103 | |
paul@143 | 104 | *mapper = new PageMapper(accessor, _pages); |
paul@79 | 105 | |
paul@143 | 106 | set(fileid, *mapper); |
paul@195 | 107 | _notifiers->attach(fileid); |
paul@79 | 108 | |
paul@143 | 109 | return L4_EOK; |
paul@79 | 110 | } |
paul@79 | 111 | |
paul@79 | 112 | |
paul@79 | 113 | |
paul@79 | 114 | /* Return a pager initialised with a page mapper. */ |
paul@79 | 115 | |
paul@156 | 116 | long FilePaging::get_pager(FileOpening *opening, const char *path, flags_t flags, fileid_t fileid, Pager **pager) |
paul@79 | 117 | { |
paul@79 | 118 | std::lock_guard<std::mutex> guard(_lock); |
paul@79 | 119 | |
paul@143 | 120 | /* Obtain any existing page mapper registered for the file, or make a new |
paul@143 | 121 | page mapper. */ |
paul@143 | 122 | |
paul@143 | 123 | PageMapper *mapper; |
paul@156 | 124 | long err = get_mapper(opening, path, flags, fileid, &mapper); |
paul@143 | 125 | |
paul@143 | 126 | if (err) |
paul@143 | 127 | return err; |
paul@143 | 128 | |
paul@79 | 129 | /* Initialise the pager with the mapper and a reference to this object for |
paul@79 | 130 | closing the mapper and accessor. */ |
paul@79 | 131 | |
paul@143 | 132 | *pager = new FilePager(fileid, mapper, get_flags(flags), this); |
paul@143 | 133 | return L4_EOK; |
paul@79 | 134 | } |
paul@79 | 135 | |
paul@79 | 136 | /* Detach a pager, potentially removing its resources. */ |
paul@79 | 137 | |
paul@81 | 138 | void FilePaging::detach_pager(fileid_t fileid, PageMapper *mapper) |
paul@79 | 139 | { |
paul@79 | 140 | std::lock_guard<std::mutex> guard(_lock); |
paul@79 | 141 | |
paul@79 | 142 | if (!mapper->detach()) |
paul@106 | 143 | remove(fileid, mapper); |
paul@193 | 144 | |
paul@195 | 145 | _notifiers->detach(fileid); |
paul@193 | 146 | } |
paul@193 | 147 | |
paul@193 | 148 | /* Obtain a file-specific notification manager. */ |
paul@193 | 149 | |
paul@195 | 150 | FileNotification *FilePaging::notifier(fileid_t fileid) |
paul@193 | 151 | { |
paul@195 | 152 | return _notifiers->get(fileid); |
paul@9 | 153 | } |
paul@9 | 154 | |
paul@9 | 155 | // vim: tabstop=4 expandtab shiftwidth=4 |