paul@93 | 1 | /* |
paul@93 | 2 | * File-specific pager functionality. |
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@8 | 22 | #include "file_pager.h" |
paul@10 | 23 | #include "mapped_file_object_server.h" |
paul@8 | 24 | |
paul@93 | 25 | |
paul@93 | 26 | |
paul@8 | 27 | /* Initialise a pager for a file with a unique file identifier and shared page |
paul@8 | 28 | mapper for moderating access to loaded pages. */ |
paul@8 | 29 | |
paul@121 | 30 | FilePager::FilePager(fileid_t fileid, PageMapper *mapper, map_flags_t flags, |
paul@81 | 31 | FilePaging *paging) |
paul@79 | 32 | : Pager(mapper, flags), _paging(paging), fileid(fileid) |
paul@8 | 33 | { |
paul@8 | 34 | } |
paul@8 | 35 | |
paul@10 | 36 | int FilePager::expected_items() |
paul@10 | 37 | { |
paul@10 | 38 | return MappedFileObject_expected_items; |
paul@10 | 39 | } |
paul@10 | 40 | |
paul@10 | 41 | ipc_server_handler_type FilePager::handler() |
paul@10 | 42 | { |
paul@10 | 43 | return (ipc_server_handler_type) handle_MappedFileObject; |
paul@10 | 44 | } |
paul@10 | 45 | |
paul@78 | 46 | |
paul@78 | 47 | |
paul@79 | 48 | /* Close the pager, removing the mapper from the paging registry if |
paul@79 | 49 | appropriate. */ |
paul@79 | 50 | |
paul@79 | 51 | void FilePager::close() |
paul@79 | 52 | { |
paul@79 | 53 | _paging->detach_pager(fileid, _mapper); |
paul@141 | 54 | |
paul@141 | 55 | /* Notify other users of the file. */ |
paul@141 | 56 | |
paul@193 | 57 | FileNotification *notifier = _paging->notifier(fileid); |
paul@193 | 58 | |
paul@193 | 59 | if (notifier != NULL) |
paul@193 | 60 | notifier->notify_others(_endpoint, NOTIFY_PEER_CLOSED); |
paul@79 | 61 | } |
paul@79 | 62 | |
paul@79 | 63 | |
paul@79 | 64 | |
paul@78 | 65 | /* File-specific operations. */ |
paul@78 | 66 | |
paul@53 | 67 | long FilePager::flush(offset_t populated_size, offset_t *size) |
paul@53 | 68 | { |
paul@141 | 69 | long err = Pager::flush(populated_size, size); |
paul@141 | 70 | |
paul@141 | 71 | if (_resized) |
paul@141 | 72 | { |
paul@193 | 73 | FileNotification *notifier = _paging->notifier(fileid); |
paul@193 | 74 | |
paul@193 | 75 | if (notifier != NULL) |
paul@193 | 76 | notifier->notify_others(_endpoint, NOTIFY_CONTENT_AVAILABLE); |
paul@193 | 77 | |
paul@141 | 78 | _resized = false; |
paul@141 | 79 | } |
paul@141 | 80 | |
paul@141 | 81 | return err; |
paul@53 | 82 | } |
paul@53 | 83 | |
paul@55 | 84 | long FilePager::resize(offset_t *size) |
paul@55 | 85 | { |
paul@141 | 86 | offset_t old_size = get_data_size(); |
paul@141 | 87 | long err = Pager::resize(size); |
paul@141 | 88 | |
paul@141 | 89 | /* If the size has changed, notify other users, but only after data has been |
paul@141 | 90 | flushed. */ |
paul@141 | 91 | |
paul@141 | 92 | if (old_size < get_data_size()) |
paul@141 | 93 | _resized = true; |
paul@141 | 94 | |
paul@141 | 95 | return err; |
paul@55 | 96 | } |
paul@55 | 97 | |
paul@90 | 98 | long FilePager::mmap(offset_t position, offset_t length, offset_t *start_pos, |
paul@90 | 99 | offset_t *end_pos, offset_t *size) |
paul@8 | 100 | { |
paul@8 | 101 | /* Set the limits of the paged region. */ |
paul@8 | 102 | |
paul@90 | 103 | return Pager::mmap(position, length, start_pos, end_pos, size); |
paul@8 | 104 | } |
paul@8 | 105 | |
paul@78 | 106 | |
paul@78 | 107 | |
paul@78 | 108 | /* Generic pager operations. */ |
paul@78 | 109 | |
paul@121 | 110 | long FilePager::map(offset_t offset, address_t hot_spot, map_flags_t flags, l4_snd_fpage_t *region) |
paul@16 | 111 | { |
paul@16 | 112 | return Pager::map(offset, hot_spot, flags, region); |
paul@16 | 113 | } |
paul@16 | 114 | |
paul@141 | 115 | |
paul@141 | 116 | |
paul@141 | 117 | /* Subscribe to notifications. */ |
paul@141 | 118 | |
paul@193 | 119 | long FilePager::subscribe(l4_cap_idx_t endpoint, notify_flags_t flags) |
paul@141 | 120 | { |
paul@141 | 121 | /* Readers can subscribe to new data (at end), and pipe closed events. |
paul@141 | 122 | Writers can subscribe to new space and pipe closed events. */ |
paul@141 | 123 | |
paul@193 | 124 | FileNotification *notifier = _paging->notifier(fileid); |
paul@193 | 125 | |
paul@193 | 126 | if (notifier == NULL) |
paul@193 | 127 | return -L4_ENOSYS; |
paul@193 | 128 | |
paul@193 | 129 | _endpoint = notifier->subscribe(endpoint, flags); |
paul@141 | 130 | return L4_EOK; |
paul@141 | 131 | } |
paul@141 | 132 | |
paul@193 | 133 | long FilePager::unsubscribe(l4_cap_idx_t endpoint) |
paul@141 | 134 | { |
paul@193 | 135 | FileNotification *notifier = _paging->notifier(fileid); |
paul@193 | 136 | |
paul@193 | 137 | if (notifier != NULL) |
paul@193 | 138 | notifier->unsubscribe(_endpoint, endpoint); |
paul@193 | 139 | |
paul@141 | 140 | return L4_EOK; |
paul@141 | 141 | } |
paul@141 | 142 | |
paul@8 | 143 | // vim: tabstop=4 expandtab shiftwidth=4 |