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 FileRegistry *registry) 33 : Pager(provider->mapper(), flags), 34 _registry(registry), _provider(provider), fileid(fileid) 35 { 36 } 37 38 int FilePager::expected_items() 39 { 40 return MappedFileObject_expected_items; 41 } 42 43 ipc_server_handler_type FilePager::handler() 44 { 45 return (ipc_server_handler_type) handle_MappedFileObject; 46 } 47 48 49 50 /* Close the pager, removing the provider from the registry if appropriate. */ 51 52 void FilePager::close() 53 { 54 /* Notify other users of the file. */ 55 56 _provider->notify_others(_endpoint, NOTIFY_PEER_CLOSED); 57 58 /* Detach the pager, potentially removing the file provider. */ 59 60 _registry->detach(fileid, _provider); 61 } 62 63 64 65 /* File-specific operations. */ 66 67 long FilePager::flush(offset_t populated_size, offset_t *size) 68 { 69 long err = Pager::flush(populated_size, size); 70 71 if (_resized) 72 { 73 _provider->notify_others(_endpoint, NOTIFY_CONTENT_AVAILABLE); 74 _resized = false; 75 } 76 77 return err; 78 } 79 80 long FilePager::resize(offset_t *size) 81 { 82 offset_t old_size = get_data_size(); 83 long err = Pager::resize(size); 84 85 /* If the size has changed, notify other users, but only after data has been 86 flushed. */ 87 88 if (old_size < get_data_size()) 89 _resized = true; 90 91 return err; 92 } 93 94 long FilePager::mmap(offset_t position, offset_t length, offset_t *start_pos, 95 offset_t *end_pos, offset_t *size) 96 { 97 /* Set the limits of the paged region. */ 98 99 return Pager::mmap(position, length, start_pos, end_pos, size); 100 } 101 102 103 104 /* Generic pager operations. */ 105 106 long FilePager::map(offset_t offset, address_t hot_spot, map_flags_t flags, l4_snd_fpage_t *region) 107 { 108 return Pager::map(offset, hot_spot, flags, region); 109 } 110 111 112 113 /* Subscribe to notifications. */ 114 115 long FilePager::subscribe(l4_cap_idx_t endpoint, notify_flags_t flags) 116 { 117 /* Readers can subscribe to new data (flush) and file closed events. 118 Writers can subscribe to file closed events. */ 119 120 _endpoint = _provider->subscribe(endpoint, flags); 121 return L4_EOK; 122 } 123 124 long FilePager::unsubscribe(l4_cap_idx_t endpoint) 125 { 126 _provider->unsubscribe(_endpoint, endpoint); 127 return L4_EOK; 128 } 129 130 // vim: tabstop=4 expandtab shiftwidth=4