[1] | 1 | /* |
---|
[611] | 2 | * mapper.h - Kernel cache for VFS files/directories definition. |
---|
[1] | 3 | * |
---|
| 4 | * Authors Mohamed Lamine Karaoui (2015) |
---|
[440] | 5 | * Alain Greiner (2016,2017,2018) |
---|
[1] | 6 | * |
---|
| 7 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 8 | * |
---|
| 9 | * This file is part of ALMOS-MKH. |
---|
| 10 | * |
---|
| 11 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
| 12 | * under the terms of the GNU General Public License as published by |
---|
| 13 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 14 | * |
---|
| 15 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 18 | * General Public License for more details. |
---|
| 19 | * |
---|
| 20 | * You should have received a copy of the GNU General Public License |
---|
| 21 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
| 22 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #ifndef _MAPPER_H_ |
---|
| 26 | #define _MAPPER_H_ |
---|
| 27 | |
---|
[457] | 28 | #include <hal_kernel_types.h> |
---|
[1] | 29 | #include <hal_atomic.h> |
---|
| 30 | #include <xlist.h> |
---|
| 31 | #include <grdxt.h> |
---|
| 32 | #include <rwlock.h> |
---|
| 33 | |
---|
| 34 | /**** Forward declarations ****/ |
---|
| 35 | |
---|
| 36 | struct page_s; |
---|
| 37 | struct vfs_inode_s; |
---|
| 38 | |
---|
| 39 | /******************************************************************************************* |
---|
[612] | 40 | * The mapper implements the kernel cache for a given VFS file or directory. |
---|
[23] | 41 | * There is one mapper per file/dir. It is implemented as a three levels radix tree, |
---|
[1] | 42 | * entirely stored in the same cluster as the inode representing the file/dir. |
---|
[18] | 43 | * - The fast retrieval key is the page index in the file. |
---|
[1] | 44 | * The ix1_width, ix2_width, ix3_width sub-indexes are configuration parameters. |
---|
| 45 | * - The leaves are pointers on physical page descriptors, dynamically allocated |
---|
| 46 | * in the local cluster. |
---|
[610] | 47 | * - The mapper is protected by a "remote_rwlock", to support several simultaneous |
---|
| 48 | * "readers", and only one "writer". |
---|
| 49 | * - A "reader" thread, calling the mapper_remote_get_page() function to get a page |
---|
| 50 | * descriptor pointer from the page index in file, can be remote (running in any cluster). |
---|
| 51 | * - A "writer" thread, calling the mapper_handle_miss() function to handle a page miss |
---|
| 52 | * must be local (running in the mapper cluster). |
---|
[265] | 53 | * - The vfs_mapper_move_page() function access the file system to handle a mapper miss, |
---|
[246] | 54 | * or update a dirty page on device. |
---|
[610] | 55 | * - The vfs_mapper_load_all() functions is used to load all pages of a directory |
---|
| 56 | * into the mapper (prefetch). |
---|
[265] | 57 | * - the mapper_move_user() function is used to move data to or from an user buffer. |
---|
[238] | 58 | * This user space buffer can be physically distributed in several clusters. |
---|
[313] | 59 | * - the mapper_move_kernel() function is used to move data to or from a remote kernel |
---|
| 60 | * buffer, that can be physically located in any cluster. |
---|
[246] | 61 | * - In the present implementation the cache size for a given file increases on demand, |
---|
| 62 | * and the allocated memory is only released when the mapper/inode is destroyed. |
---|
[612] | 63 | * |
---|
| 64 | * TODO : the mapper being only used to implement the VFS cache(s), the mapper.c |
---|
| 65 | * and mapper.h file should beerro trandfered to the vfs directory. |
---|
[1] | 66 | ******************************************************************************************/ |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | /******************************************************************************************* |
---|
| 70 | * This structure defines the mapper descriptor. |
---|
| 71 | ******************************************************************************************/ |
---|
| 72 | |
---|
| 73 | typedef struct mapper_s |
---|
| 74 | { |
---|
[23] | 75 | struct vfs_inode_s * inode; /*! owner inode */ |
---|
[246] | 76 | uint32_t type; /*! file system type */ |
---|
[606] | 77 | grdxt_t rt; /*! embedded pages cache descriptor (radix tree) */ |
---|
| 78 | remote_rwlock_t lock; /*! several readers / only one writer */ |
---|
[1] | 79 | uint32_t refcount; /*! several vsegs can refer the same file */ |
---|
| 80 | xlist_entry_t vsegs_root; /*! root of list of vsegs refering this mapper */ |
---|
| 81 | xlist_entry_t wait_root; /*! root of list of threads waiting on mapper */ |
---|
| 82 | list_entry_t dirty_root; /*! root of list of dirty pages */ |
---|
| 83 | } |
---|
| 84 | mapper_t; |
---|
| 85 | |
---|
| 86 | /******************************************************************************************* |
---|
| 87 | * This structure defines a "fragment". It is used to move data between the kernel mapper, |
---|
| 88 | * and an user buffer, that can be split in several distributed physical pages located |
---|
| 89 | * in different clusters. A fragment is a set of contiguous bytes in the file. |
---|
[18] | 90 | * - It can be stored in one single physical page in the user buffer. |
---|
[1] | 91 | * - It can spread two successive physical pages in the kernel mapper. |
---|
| 92 | ******************************************************************************************/ |
---|
| 93 | |
---|
| 94 | typedef struct fragment_s |
---|
| 95 | { |
---|
| 96 | uint32_t file_offset; /*! offset of fragment in file (i.e. in mapper) */ |
---|
| 97 | uint32_t size; /*! number of bytes in fragment */ |
---|
| 98 | cxy_t buf_cxy; /*! user buffer cluster identifier */ |
---|
[23] | 99 | void * buf_ptr; /*! local pointer on first byte in user buffer */ |
---|
[1] | 100 | } |
---|
| 101 | fragment_t; |
---|
| 102 | |
---|
| 103 | /******************************************************************************************* |
---|
[18] | 104 | * This function allocates physical memory for a mapper descriptor, and initializes it |
---|
[1] | 105 | * (refcount <= 0) / inode <= NULL). |
---|
| 106 | * It must be executed by a thread running in the cluster containing the mapper. |
---|
| 107 | ******************************************************************************************* |
---|
[513] | 108 | * @ type : type of the mapper to create. |
---|
| 109 | * @ return : pointer on created mapper if success / return NULL if no memory |
---|
[1] | 110 | ******************************************************************************************/ |
---|
[513] | 111 | mapper_t * mapper_create( vfs_fs_type_t type ); |
---|
[1] | 112 | |
---|
| 113 | /******************************************************************************************* |
---|
[606] | 114 | * This function releases all physical memory allocated for a mapper. |
---|
| 115 | * Both the mapper descriptor and the radix tree are released. |
---|
| 116 | * It does NOT synchronize dirty pages. Use the vfs_sync_inode() function if required. |
---|
[1] | 117 | * It must be executed by a thread running in the cluster containing the mapper. |
---|
| 118 | ******************************************************************************************* |
---|
| 119 | * @ mapper : target mapper. |
---|
| 120 | ******************************************************************************************/ |
---|
[606] | 121 | void mapper_destroy( mapper_t * mapper ); |
---|
[1] | 122 | |
---|
| 123 | /******************************************************************************************* |
---|
[606] | 124 | * This function load from device a missing page identified by the <page_id> argument |
---|
| 125 | * into the mapper identified by the <mapper> local pointer. |
---|
| 126 | * It allocates a physical page from the local cluster, initialise by accessing device, |
---|
| 127 | * and register the page in the mapper radix tree. |
---|
| 128 | * It must be executed by a thread running in the cluster containing the mapper. |
---|
| 129 | * WARNING : the calling function mapper_remote_get_page() is supposed to take and release |
---|
| 130 | * the lock protecting the mapper in WRITE_MODE. |
---|
| 131 | ******************************************************************************************* |
---|
| 132 | * @ mapper : [in] target mapper. |
---|
| 133 | * @ page_id : [in] missing page index in file. |
---|
| 134 | * @ page_xp : [out] buffer for extended pointer on missing page descriptor. |
---|
| 135 | * @ return 0 if success / return -1 if a dirty page cannot be updated on device. |
---|
| 136 | ******************************************************************************************/ |
---|
| 137 | error_t mapper_handle_miss( mapper_t * mapper, |
---|
| 138 | uint32_t page_id, |
---|
| 139 | xptr_t * page_xp ); |
---|
| 140 | |
---|
| 141 | /******************************************************************************************* |
---|
[610] | 142 | * This function move data between a remote mapper, dentified by the <mapper_xp> argument, |
---|
| 143 | * and a distributed user buffer. It can be called by a thread running in any cluster. |
---|
[606] | 144 | * It is called by the vfs_user_move() to implement sys_read() and sys_write() syscalls. |
---|
[313] | 145 | * If required, the data transfer is split in "fragments", where one fragment contains |
---|
[265] | 146 | * contiguous bytes in the same mapper page. |
---|
[313] | 147 | * It uses "hal_uspace" accesses to move a fragment to/from the user buffer. |
---|
[1] | 148 | * In case of write, the dirty bit is set for all pages written in the mapper. |
---|
[610] | 149 | * The mapper being an extendable cache, it is automatically extended when required. |
---|
[606] | 150 | * The "offset" field in the file descriptor, and the "size" field in inode descriptor |
---|
| 151 | * are not modified by this function. |
---|
[1] | 152 | ******************************************************************************************* |
---|
[610] | 153 | * @ mapper_xp : extended pointer on mapper. |
---|
[265] | 154 | * @ to_buffer : mapper -> buffer if true / buffer -> mapper if false. |
---|
[23] | 155 | * @ file_offset : first byte to move in file. |
---|
[407] | 156 | * @ u_buf : user space pointer on user buffer. |
---|
[23] | 157 | * @ size : number of bytes to move. |
---|
[606] | 158 | * returns O if success / returns -1 if error. |
---|
[1] | 159 | ******************************************************************************************/ |
---|
[610] | 160 | error_t mapper_move_user( xptr_t mappe_xp, |
---|
[313] | 161 | bool_t to_buffer, |
---|
| 162 | uint32_t file_offset, |
---|
[407] | 163 | void * u_buf, |
---|
[313] | 164 | uint32_t size ); |
---|
| 165 | |
---|
[606] | 166 | /******************************************************************************************** |
---|
| 167 | * This function move data between a remote mapper and a remote kernel buffer. |
---|
| 168 | * It can be called by a thread running any cluster. |
---|
[313] | 169 | * If required, the data transfer is split in "fragments", where one fragment contains |
---|
| 170 | * contiguous bytes in the same mapper page. |
---|
| 171 | * It uses a "remote_memcpy" to move a fragment to/from the kernel buffer. |
---|
| 172 | * In case of write, the dirty bit is set for all pages written in the mapper. |
---|
| 173 | ******************************************************************************************* |
---|
[606] | 174 | * @ mapper_xp : extended pointer on mapper. |
---|
[313] | 175 | * @ to_buffer : mapper -> buffer if true / buffer -> mapper if false. |
---|
| 176 | * @ file_offset : first byte to move in file. |
---|
| 177 | * @ buffer_xp : extended pointer on kernel buffer. |
---|
| 178 | * @ size : number of bytes to move. |
---|
[606] | 179 | * returns O if success / returns -1 if error. |
---|
[313] | 180 | ******************************************************************************************/ |
---|
[606] | 181 | error_t mapper_move_kernel( xptr_t mapper_xp, |
---|
[265] | 182 | bool_t to_buffer, |
---|
| 183 | uint32_t file_offset, |
---|
[313] | 184 | xptr_t buffer_xp, |
---|
[265] | 185 | uint32_t size ); |
---|
[1] | 186 | |
---|
| 187 | /******************************************************************************************* |
---|
[606] | 188 | * This function removes a physical page from the mapper, and releases |
---|
| 189 | * the page to the local PPM. It is called by the mapper_destroy() function. |
---|
[1] | 190 | * It must be executed by a thread running in the cluster containing the mapper. |
---|
[606] | 191 | * It takes the mapper lock in WRITE_MODE to update the mapper. |
---|
[1] | 192 | ******************************************************************************************* |
---|
| 193 | * @ mapper : local pointer on the mapper. |
---|
| 194 | * @ page : pointer on page to remove. |
---|
| 195 | ******************************************************************************************/ |
---|
[606] | 196 | void mapper_release_page( mapper_t * mapper, |
---|
| 197 | struct page_s * page ); |
---|
[1] | 198 | |
---|
| 199 | /******************************************************************************************* |
---|
[611] | 200 | * This function returns an extended pointer on a page descriptor. |
---|
| 201 | * The - possibly remote - mapper is identified by the <mapper_xp> argument. |
---|
| 202 | * The page is identified by <page_id> argument (page index in the file). |
---|
| 203 | * It can be executed by a thread running in any cluster, as it uses remote |
---|
[606] | 204 | * access primitives to scan the mapper. |
---|
| 205 | * In case of miss, this function takes the mapper lock in WRITE_MODE, and call the |
---|
| 206 | * mapper_handle_miss() to load the missing page from device to mapper, using an RPC |
---|
| 207 | * when the mapper is remote. |
---|
| 208 | ******************************************************************************************* |
---|
| 209 | * @ mapper_xp : extended pointer on the mapper. |
---|
| 210 | * @ page_id : page index in file |
---|
[611] | 211 | * @ returns extended pointer on page descriptor if success / return XPTR_NULL if error. |
---|
[606] | 212 | ******************************************************************************************/ |
---|
| 213 | xptr_t mapper_remote_get_page( xptr_t mapper_xp, |
---|
| 214 | uint32_t page_id ); |
---|
| 215 | |
---|
| 216 | /******************************************************************************************* |
---|
| 217 | * This function allows to read a single word in a mapper seen as and array of uint32_t. |
---|
[611] | 218 | * It has bee designed to support remote access to the FAT mapper of the FATFS. |
---|
[606] | 219 | * It can be called by any thread running in any cluster. |
---|
[1] | 220 | * In case of miss, it takes the mapper lock in WRITE_MODE, load the missing |
---|
[606] | 221 | * page from device to mapper, and release the mapper lock. |
---|
[1] | 222 | ******************************************************************************************* |
---|
[606] | 223 | * @ mapper_xp : [in] extended pointer on the mapper. |
---|
[611] | 224 | * @ word_id : [in] 32 bits word index in file. |
---|
[606] | 225 | * @ p_value : [out] local pointer on destination buffer. |
---|
| 226 | * @ returns 0 if success / return -1 if error. |
---|
[1] | 227 | ******************************************************************************************/ |
---|
[606] | 228 | error_t mapper_remote_get_32( xptr_t mapper_xp, |
---|
| 229 | uint32_t word_id, |
---|
| 230 | uint32_t * p_value ); |
---|
[1] | 231 | |
---|
[606] | 232 | /******************************************************************************************* |
---|
| 233 | * This function allows to write a single word to a mapper seen as and array of uint32_t. |
---|
| 234 | * It has bee designed to support remote access tho the FAT mapper of the FATFS. |
---|
| 235 | * It can be called by any thread running in any cluster. |
---|
| 236 | * In case of miss, it takes the mapper lock in WRITE_MODE, load the missing |
---|
| 237 | * page from device to mapper, and release the mapper lock. |
---|
| 238 | ******************************************************************************************* |
---|
| 239 | * @ mapper_xp : [in] extended pointer on the mapper. |
---|
[611] | 240 | * @ word_id : [in] 32 bits word index in file. |
---|
| 241 | * @ value : [in] value to be written. |
---|
[606] | 242 | * @ returns 0 if success / return -1 if error. |
---|
| 243 | ******************************************************************************************/ |
---|
| 244 | error_t mapper_remote_set_32( xptr_t mapper_xp, |
---|
| 245 | uint32_t word_id, |
---|
| 246 | uint32_t value ); |
---|
[18] | 247 | |
---|
[611] | 248 | /******************************************************************************************* |
---|
| 249 | * This debug function displays the content of a given page of a given mapper. |
---|
| 250 | * - the mapper is identified by the <mapper_xp> argument. |
---|
| 251 | * - the page is identified by the <page_id> argument. |
---|
| 252 | * - the number of bytes to display in page is defined by the <nbytes> argument. |
---|
| 253 | * The format is eigth (32 bits) words per line in hexadecimal. |
---|
| 254 | * It can be called by any thread running in any cluster. |
---|
| 255 | * In case of miss in mapper, it load the missing page from device to mapper. |
---|
| 256 | ******************************************************************************************* |
---|
| 257 | * @ mapper_xp : [in] extended pointer on the mapper. |
---|
| 258 | * @ page_id : [in] page index in file. |
---|
| 259 | * @ nbytes : [in] value to be written. |
---|
| 260 | * @ string : [in] string printed in header. |
---|
| 261 | * @ returns 0 if success / return -1 if error. |
---|
| 262 | ******************************************************************************************/ |
---|
| 263 | error_t mapper_display_page( xptr_t mapper_xp, |
---|
| 264 | uint32_t page_id, |
---|
| 265 | uint32_t nbytes, |
---|
| 266 | char * string ); |
---|
| 267 | |
---|
| 268 | |
---|
[1] | 269 | #endif /* _MAPPER_H_ */ |
---|