1 /* 2 * File-specific pager functionality. 3 * 4 * Copyright (C) 2021, 2022 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <fsclient/file.h> /* file_region_flags */ 23 #include <systypes/fcntl.h> 24 25 #include "copied_page_mapper.h" 26 #include "file_pager.h" 27 #include "mapped_file_object_server.h" 28 29 30 31 /* Initialise a pager for a file with a unique file identifier, file provider, 32 opening flags and a file registry. The provider offers a shared page mapper 33 for moderating access to loaded pages. */ 34 35 FilePager::FilePager(fileid_t fileid, FileProvider *provider, flags_t flags) 36 : Pager(provider->mapper(), file_region_flags(flags)), 37 _provider(provider), _flags(flags), fileid(fileid) 38 { 39 } 40 41 int FilePager::expected_items() 42 { 43 return MappedFileObject_expected_items; 44 } 45 46 ipc_server_handler_type FilePager::handler() 47 { 48 return (ipc_server_handler_type) handle_MappedFileObject; 49 } 50 51 52 53 /* Close the pager, removing the provider from the registry if appropriate. */ 54 55 void FilePager::close() 56 { 57 /* Notify other users of the file and unsubscribe. */ 58 59 _provider->notify_others(_endpoint, NOTIFY_PEER_CLOSED); 60 unsubscribe(); 61 62 /* Detach the pager, potentially removing the file provider. */ 63 64 _provider->registry()->detach(fileid, _provider); 65 } 66 67 68 69 /* File-specific operations. */ 70 71 long FilePager::flush(offset_t populated_size, offset_t *size) 72 { 73 long err = Pager::flush(populated_size, size); 74 75 if (_resized) 76 { 77 _provider->notify_others(_endpoint, NOTIFY_CONTENT_AVAILABLE); 78 _resized = false; 79 } 80 81 return err; 82 } 83 84 long FilePager::resize(offset_t *size) 85 { 86 offset_t old_size = get_data_size(); 87 long err = Pager::resize(size); 88 89 /* If the size has changed, notify other users, but only after data has been 90 flushed. */ 91 92 if (old_size < get_data_size()) 93 _resized = true; 94 95 return err; 96 } 97 98 long FilePager::mmap(offset_t position, offset_t length, 99 offset_t start_visible, offset_t end_visible, 100 offset_t *start_pos, offset_t *end_pos, offset_t *size) 101 { 102 /* Set the limits of the paged region. */ 103 104 long err = Pager::mmap(position, length, start_visible, end_visible, start_pos, 105 end_pos, size); 106 107 if (err) 108 return err; 109 110 /* Impose copy-on-write semantics where appropriate. */ 111 112 if ((_mapper != _mapper_copied) && copy_on_write()) 113 { 114 _mapper_copied = new CopiedPageMapper(_mapper); 115 _mapper = _mapper_copied; 116 } 117 118 return L4_EOK; 119 } 120 121 /* Return whether the pager should employ copy-on-write semantics. */ 122 123 bool FilePager::copy_on_write() 124 { 125 return (_flags == O_RDONLY) && (_map_flags & L4RE_DS_F_W); 126 } 127 128 129 130 /* Generic pager operations. */ 131 132 long FilePager::map(offset_t offset, address_t hot_spot, map_flags_t map_flags, l4_snd_fpage_t *region) 133 { 134 return Pager::map(offset, hot_spot, map_flags, region); 135 } 136 137 138 139 /* Subscribe to notifications. */ 140 141 long FilePager::subscribe(l4_cap_idx_t notifier, notify_flags_t flags) 142 { 143 /* Readers can subscribe to new data (flush) and file closed events. 144 Writers can subscribe to file closed events. */ 145 146 return _provider->subscribe(notifier, flags, this, &_endpoint); 147 } 148 149 /* Unsubscribe from notifications. */ 150 151 long FilePager::unsubscribe() 152 { 153 _provider->unsubscribe(_endpoint, this); 154 return L4_EOK; 155 } 156 157 // vim: tabstop=4 expandtab shiftwidth=4