1 /* 2 * File-specific pager functionality. 3 * 4 * Copyright (C) 2021 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 "file_pager.h" 23 #include "mapped_file_object_server.h" 24 25 26 27 /* Initialise a pager for a file with a unique file identifier, file provider, 28 mapping flags and a file registry. The provider offers a shared page mapper 29 for moderating access to loaded pages. */ 30 31 FilePager::FilePager(fileid_t fileid, FileProvider *provider, map_flags_t flags) 32 : Pager(provider->mapper(), flags), 33 _provider(provider), fileid(fileid) 34 { 35 } 36 37 int FilePager::expected_items() 38 { 39 return MappedFileObject_expected_items; 40 } 41 42 ipc_server_handler_type FilePager::handler() 43 { 44 return (ipc_server_handler_type) handle_MappedFileObject; 45 } 46 47 48 49 /* Close the pager, removing the provider from the registry if appropriate. */ 50 51 void FilePager::close() 52 { 53 /* Notify other users of the file. */ 54 55 _provider->notify_others(_endpoint, NOTIFY_PEER_CLOSED); 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, offset_t *start_pos, 94 offset_t *end_pos, offset_t *size) 95 { 96 /* Set the limits of the paged region. */ 97 98 return Pager::mmap(position, length, start_pos, end_pos, size); 99 } 100 101 102 103 /* Generic pager operations. */ 104 105 long FilePager::map(offset_t offset, address_t hot_spot, map_flags_t flags, l4_snd_fpage_t *region) 106 { 107 return Pager::map(offset, hot_spot, flags, region); 108 } 109 110 111 112 /* Subscribe to notifications. */ 113 114 long FilePager::subscribe(l4_cap_idx_t endpoint, notify_flags_t flags, 115 l4_cap_idx_t *peer) 116 { 117 /* Readers can subscribe to new data (flush) and file closed events. 118 Writers can subscribe to file closed events. */ 119 120 return _provider->subscribe(endpoint, flags, peer, &_endpoint); 121 } 122 123 // vim: tabstop=4 expandtab shiftwidth=4