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