paul@93 | 1 | /* |
paul@93 | 2 | * File-specific pager functionality. |
paul@93 | 3 | * |
paul@339 | 4 | * Copyright (C) 2021, 2022 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@346 | 22 | #include <fsclient/file.h> /* file_region_flags */ |
paul@346 | 23 | #include <systypes/fcntl.h> |
paul@346 | 24 | |
paul@346 | 25 | #include "copied_page_mapper.h" |
paul@8 | 26 | #include "file_pager.h" |
paul@10 | 27 | #include "mapped_file_object_server.h" |
paul@8 | 28 | |
paul@93 | 29 | |
paul@93 | 30 | |
paul@200 | 31 | /* Initialise a pager for a file with a unique file identifier, file provider, |
paul@346 | 32 | opening flags and a file registry. The provider offers a shared page mapper |
paul@202 | 33 | for moderating access to loaded pages. */ |
paul@8 | 34 | |
paul@346 | 35 | FilePager::FilePager(fileid_t fileid, FileProvider *provider, flags_t flags) |
paul@346 | 36 | : Pager(provider->mapper(), file_region_flags(flags)), |
paul@346 | 37 | _provider(provider), _flags(flags), fileid(fileid) |
paul@8 | 38 | { |
paul@8 | 39 | } |
paul@8 | 40 | |
paul@10 | 41 | int FilePager::expected_items() |
paul@10 | 42 | { |
paul@10 | 43 | return MappedFileObject_expected_items; |
paul@10 | 44 | } |
paul@10 | 45 | |
paul@10 | 46 | ipc_server_handler_type FilePager::handler() |
paul@10 | 47 | { |
paul@10 | 48 | return (ipc_server_handler_type) handle_MappedFileObject; |
paul@10 | 49 | } |
paul@10 | 50 | |
paul@78 | 51 | |
paul@78 | 52 | |
paul@201 | 53 | /* Close the pager, removing the provider from the registry if appropriate. */ |
paul@79 | 54 | |
paul@79 | 55 | void FilePager::close() |
paul@79 | 56 | { |
paul@141 | 57 | /* Notify other users of the file. */ |
paul@141 | 58 | |
paul@200 | 59 | _provider->notify_others(_endpoint, NOTIFY_PEER_CLOSED); |
paul@193 | 60 | |
paul@200 | 61 | /* Detach the pager, potentially removing the file provider. */ |
paul@200 | 62 | |
paul@222 | 63 | _provider->registry()->detach(fileid, _provider); |
paul@79 | 64 | } |
paul@79 | 65 | |
paul@79 | 66 | |
paul@79 | 67 | |
paul@78 | 68 | /* File-specific operations. */ |
paul@78 | 69 | |
paul@53 | 70 | long FilePager::flush(offset_t populated_size, offset_t *size) |
paul@53 | 71 | { |
paul@141 | 72 | long err = Pager::flush(populated_size, size); |
paul@141 | 73 | |
paul@141 | 74 | if (_resized) |
paul@141 | 75 | { |
paul@200 | 76 | _provider->notify_others(_endpoint, NOTIFY_CONTENT_AVAILABLE); |
paul@141 | 77 | _resized = false; |
paul@141 | 78 | } |
paul@141 | 79 | |
paul@141 | 80 | return err; |
paul@53 | 81 | } |
paul@53 | 82 | |
paul@55 | 83 | long FilePager::resize(offset_t *size) |
paul@55 | 84 | { |
paul@141 | 85 | offset_t old_size = get_data_size(); |
paul@141 | 86 | long err = Pager::resize(size); |
paul@141 | 87 | |
paul@141 | 88 | /* If the size has changed, notify other users, but only after data has been |
paul@141 | 89 | flushed. */ |
paul@141 | 90 | |
paul@141 | 91 | if (old_size < get_data_size()) |
paul@141 | 92 | _resized = true; |
paul@141 | 93 | |
paul@141 | 94 | return err; |
paul@55 | 95 | } |
paul@55 | 96 | |
paul@339 | 97 | long FilePager::mmap(offset_t position, offset_t length, |
paul@339 | 98 | offset_t start_visible, offset_t end_visible, |
paul@332 | 99 | offset_t *start_pos, offset_t *end_pos, offset_t *size) |
paul@8 | 100 | { |
paul@8 | 101 | /* Set the limits of the paged region. */ |
paul@8 | 102 | |
paul@346 | 103 | long err = Pager::mmap(position, length, start_visible, end_visible, start_pos, |
paul@346 | 104 | end_pos, size); |
paul@346 | 105 | |
paul@346 | 106 | if (err) |
paul@346 | 107 | return err; |
paul@346 | 108 | |
paul@346 | 109 | /* Impose copy-on-write semantics where appropriate. */ |
paul@346 | 110 | |
paul@346 | 111 | if ((_mapper != _mapper_copied) && copy_on_write()) |
paul@346 | 112 | { |
paul@346 | 113 | _mapper_copied = new CopiedPageMapper(_mapper); |
paul@346 | 114 | _mapper = _mapper_copied; |
paul@346 | 115 | } |
paul@346 | 116 | |
paul@346 | 117 | return L4_EOK; |
paul@346 | 118 | } |
paul@346 | 119 | |
paul@346 | 120 | /* Return whether the pager should employ copy-on-write semantics. */ |
paul@346 | 121 | |
paul@346 | 122 | bool FilePager::copy_on_write() |
paul@346 | 123 | { |
paul@346 | 124 | return (_flags == O_RDONLY) && (_map_flags & L4RE_DS_F_W); |
paul@8 | 125 | } |
paul@8 | 126 | |
paul@78 | 127 | |
paul@78 | 128 | |
paul@78 | 129 | /* Generic pager operations. */ |
paul@78 | 130 | |
paul@346 | 131 | long FilePager::map(offset_t offset, address_t hot_spot, map_flags_t map_flags, l4_snd_fpage_t *region) |
paul@16 | 132 | { |
paul@346 | 133 | return Pager::map(offset, hot_spot, map_flags, region); |
paul@16 | 134 | } |
paul@16 | 135 | |
paul@141 | 136 | |
paul@141 | 137 | |
paul@141 | 138 | /* Subscribe to notifications. */ |
paul@141 | 139 | |
paul@290 | 140 | long FilePager::subscribe(l4_cap_idx_t notifier, notify_flags_t flags) |
paul@141 | 141 | { |
paul@206 | 142 | /* Readers can subscribe to new data (flush) and file closed events. |
paul@206 | 143 | Writers can subscribe to file closed events. */ |
paul@141 | 144 | |
paul@290 | 145 | return _provider->subscribe(notifier, flags, this, &_endpoint); |
paul@290 | 146 | } |
paul@290 | 147 | |
paul@290 | 148 | /* Unsubscribe from notifications. */ |
paul@290 | 149 | |
paul@290 | 150 | long FilePager::unsubscribe() |
paul@290 | 151 | { |
paul@290 | 152 | _provider->unsubscribe(_endpoint, this); |
paul@290 | 153 | return L4_EOK; |
paul@141 | 154 | } |
paul@141 | 155 | |
paul@8 | 156 | // vim: tabstop=4 expandtab shiftwidth=4 |