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 and shared page 28 mapper for moderating access to loaded pages. */ 29 30 FilePager::FilePager(fileid_t fileid, PageMapper *mapper, map_flags_t flags, 31 FilePaging *paging) 32 : Pager(mapper, flags), _paging(paging), fileid(fileid) 33 { 34 } 35 36 int FilePager::expected_items() 37 { 38 return MappedFileObject_expected_items; 39 } 40 41 ipc_server_handler_type FilePager::handler() 42 { 43 return (ipc_server_handler_type) handle_MappedFileObject; 44 } 45 46 47 48 /* Close the pager, removing the mapper from the paging registry if 49 appropriate. */ 50 51 void FilePager::close() 52 { 53 _paging->detach_pager(fileid, _mapper); 54 55 /* Notify other users of the file. */ 56 57 FileNotification *notifier = _paging->notifier(fileid); 58 59 if (notifier != NULL) 60 notifier->notify_others(_endpoint, NOTIFY_PEER_CLOSED); 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 FileNotification *notifier = _paging->notifier(fileid); 74 75 if (notifier != NULL) 76 notifier->notify_others(_endpoint, NOTIFY_CONTENT_AVAILABLE); 77 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, offset_t *start_pos, 99 offset_t *end_pos, offset_t *size) 100 { 101 /* Set the limits of the paged region. */ 102 103 return Pager::mmap(position, length, start_pos, end_pos, size); 104 } 105 106 107 108 /* Generic pager operations. */ 109 110 long FilePager::map(offset_t offset, address_t hot_spot, map_flags_t flags, l4_snd_fpage_t *region) 111 { 112 return Pager::map(offset, hot_spot, flags, region); 113 } 114 115 116 117 /* Subscribe to notifications. */ 118 119 long FilePager::subscribe(l4_cap_idx_t endpoint, notify_flags_t flags) 120 { 121 /* Readers can subscribe to new data (at end), and pipe closed events. 122 Writers can subscribe to new space and pipe closed events. */ 123 124 FileNotification *notifier = _paging->notifier(fileid); 125 126 if (notifier == NULL) 127 return -L4_ENOSYS; 128 129 _endpoint = notifier->subscribe(endpoint, flags); 130 return L4_EOK; 131 } 132 133 long FilePager::unsubscribe(l4_cap_idx_t endpoint) 134 { 135 FileNotification *notifier = _paging->notifier(fileid); 136 137 if (notifier != NULL) 138 notifier->unsubscribe(_endpoint, endpoint); 139 140 return L4_EOK; 141 } 142 143 // vim: tabstop=4 expandtab shiftwidth=4