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