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. */ 58 59 _provider->notify_others(_endpoint, NOTIFY_PEER_CLOSED); 60 61 /* Detach the pager, potentially removing the file provider. */ 62 63 _provider->registry()->detach(fileid, _provider); 64 } 65 66 67 68 /* File-specific operations. */ 69 70 long FilePager::flush(offset_t populated_size, offset_t *size) 71 { 72 long err = Pager::flush(populated_size, size); 73 74 if (_resized) 75 { 76 _provider->notify_others(_endpoint, NOTIFY_CONTENT_AVAILABLE); 77 _resized = false; 78 } 79 80 return err; 81 } 82 83 long FilePager::resize(offset_t *size) 84 { 85 offset_t old_size = get_data_size(); 86 long err = Pager::resize(size); 87 88 /* If the size has changed, notify other users, but only after data has been 89 flushed. */ 90 91 if (old_size < get_data_size()) 92 _resized = true; 93 94 return err; 95 } 96 97 long FilePager::mmap(offset_t position, offset_t length, 98 offset_t start_visible, offset_t end_visible, 99 offset_t *start_pos, offset_t *end_pos, offset_t *size) 100 { 101 /* Set the limits of the paged region. */ 102 103 long err = Pager::mmap(position, length, start_visible, end_visible, start_pos, 104 end_pos, size); 105 106 if (err) 107 return err; 108 109 /* Impose copy-on-write semantics where appropriate. */ 110 111 if ((_mapper != _mapper_copied) && copy_on_write()) 112 { 113 _mapper_copied = new CopiedPageMapper(_mapper); 114 _mapper = _mapper_copied; 115 } 116 117 return L4_EOK; 118 } 119 120 /* Return whether the pager should employ copy-on-write semantics. */ 121 122 bool FilePager::copy_on_write() 123 { 124 return (_flags == O_RDONLY) && (_map_flags & L4RE_DS_F_W); 125 } 126 127 128 129 /* Generic pager operations. */ 130 131 long FilePager::map(offset_t offset, address_t hot_spot, map_flags_t map_flags, l4_snd_fpage_t *region) 132 { 133 return Pager::map(offset, hot_spot, map_flags, region); 134 } 135 136 137 138 /* Subscribe to notifications. */ 139 140 long FilePager::subscribe(l4_cap_idx_t notifier, notify_flags_t flags) 141 { 142 /* Readers can subscribe to new data (flush) and file closed events. 143 Writers can subscribe to file closed events. */ 144 145 return _provider->subscribe(notifier, flags, this, &_endpoint); 146 } 147 148 /* Unsubscribe from notifications. */ 149 150 long FilePager::unsubscribe() 151 { 152 _provider->unsubscribe(_endpoint, this); 153 return L4_EOK; 154 } 155 156 // vim: tabstop=4 expandtab shiftwidth=4