source: trunk/kernel/mm/mapper.h @ 275

Last change on this file since 275 was 265, checked in by alain, 7 years ago

Fix several bugs in VFS.

File size: 9.5 KB
Line 
1/*
2 * mapper.h - Map memory, file or device in process virtual address space.
3 *
4 * Authors   Mohamed Lamine Karaoui (2015)
5 *           Alain Greiner (2016)
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
28#include <hal_types.h>
29#include <hal_atomic.h>
30#include <xlist.h>
31#include <grdxt.h>
32#include <rwlock.h>
33
34/****  Forward declarations ****/
35
36struct page_s;
37struct vfs_inode_s;
38
39/*******************************************************************************************
40 * The mapper implements the kernel cache for a given file or directory.
41 * There is one mapper per file/dir. It is implemented as a three levels radix tree,
42 * entirely stored in the same cluster as the inode representing the file/dir.
43 * - The fast retrieval key is the page index in the file.
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.
47 * - In a given cluster, a mapper is a "private" structure: a thread accessing the mapper
48 *   must be running in the cluster containing it (can be a local thread or a RPC thread).
49 * - The mapper is protected by a blocking "rwlock", to support several simultaneous
50 *   readers, and only one writer. This lock implement a busy waiting policy.
51 * - The mapper_get_page() function that return a page descriptor pointer from a page
52 *   index in file is in charge of handling the miss on the mapper cache.
53 * - The vfs_mapper_move_page() function access the file system to handle a mapper miss,
54 *   or update a dirty page on device.
55 * - The vfs_mapper_load_all() functions is used to load all pages of a given file
56 *   or directory into the mapper.
57 * - the mapper_move_user() function is used to move data to or from an user buffer.
58 *   This user space buffer can be physically distributed in several clusters.
59 * - In the present implementation the cache size for a given file increases on demand,
60 *   and the  allocated memory is only released when the mapper/inode is destroyed.
61 ******************************************************************************************/
62
63
64/*******************************************************************************************
65 * This structure defines the mapper descriptor.
66 ******************************************************************************************/
67
68typedef struct mapper_s
69{
70        struct vfs_inode_s * inode;           /*! owner inode                                     */
71    uint32_t             type;        /*! file system type                                */
72        grdxt_t              radix;           /*! pages cache implemented as a radix tree         */
73        rwlock_t             lock;        /*! several readers / only one writer               */
74        uint32_t                 refcount;    /*! several vsegs can refer the same file           */
75        xlist_entry_t        vsegs_root;  /*! root of list of vsegs refering this mapper      */
76        xlist_entry_t        wait_root;   /*! root of list of threads waiting on mapper       */
77    list_entry_t         dirty_root;  /*! root of list of dirty pages                     */
78}
79mapper_t;
80
81/*******************************************************************************************
82 * This structure defines a "fragment". It is used to move data between the kernel mapper,
83 * and an user buffer, that can be split in several distributed physical pages located
84 * in different clusters. A fragment is a set of contiguous bytes in the file.
85 * - It can be stored in one single physical page in the user buffer.
86 * - It can spread two successive physical pages in the kernel mapper.
87 ******************************************************************************************/
88
89typedef struct fragment_s
90{
91    uint32_t    file_offset;         /*! offset of fragment in file (i.e. in mapper)      */
92    uint32_t    size;                /*! number of bytes in fragment                      */
93    cxy_t       buf_cxy;             /*! user buffer cluster identifier                   */
94    void      * buf_ptr;             /*! local pointer on first byte in user buffer       */
95}
96fragment_t;
97
98/*******************************************************************************************
99 * This function allocates physical memory for a mapper descriptor, and initializes it
100 * (refcount <= 0) / inode <= NULL).
101 * It must be executed by a thread running in the cluster containing the mapper.
102 *******************************************************************************************
103 * @ return pointer on created mapper if success / return NULL if no memory
104 ******************************************************************************************/
105mapper_t * mapper_create();
106
107/*******************************************************************************************
108 * This function releases all physical pages allocated for the mapper.
109 * It synchronizes all dirty pages (i.e. update the file on disk) if required.
110 * The mapper descriptor and the radix tree themselves are released.
111 * It must be executed by a thread running in the cluster containing the mapper.
112 *******************************************************************************************
113 * @ mapper      : target mapper.
114 * @ return 0 if success / return EIO if a dirty page cannot be updated on device.
115 ******************************************************************************************/
116error_t mapper_destroy( mapper_t * mapper );
117
118/*******************************************************************************************
119 * This function move data between a mapper and an user or kernel buffer.
120 * It must be called by a thread running in the cluster containing the mapper.
121 * - A kernel buffer must be entirely contained in the same cluster as the mapper.
122 * - An user buffer can be physically distributed in several clusters.
123 * In both cases, the data transfer is split in "fragments": one fragment contains
124 * contiguous bytes in the same mapper page.
125 * - It uses "hal_uspace" accesses to move a fragment to/from the user buffer.
126 * - It uses a simple memcpy" access to move a fragment to/from a kernel buffer.
127 * In case of write, the dirty bit is set for all pages written in the mapper.
128 * The offset in the file descriptor is not modified by this function.
129 *******************************************************************************************
130 * @ mapper       : local pointer on mapper.
131 * @ to_buffer    : mapper -> buffer if true / buffer -> mapper if false.
132 * @ is_user      : user space buffer if true / kernel local buffer if false.
133 * @ file_offset  : first byte to move in file.
134 * @ buffer       : pointer on buffer (local kernel buffer or user spaceaddress in user space.
135 * @ size         : number of bytes to move.
136 * returns O if success / returns EINVAL if error.
137 ******************************************************************************************/
138error_t mapper_move_buffer( mapper_t * mapper,
139                            bool_t     to_buffer,
140                            bool_t     is_user,
141                            uint32_t   file_offset,
142                            void     * buffer,
143                            uint32_t   size );
144
145/*******************************************************************************************
146 * This function removes a physical page from the mapper, update the FS if the page
147 * is dirty, and releases the page to PPM. It is called by the mapper_destroy() function.
148 * It must be executed by a thread running in the cluster containing the mapper.
149 * It takes both the page lock and the mapper lock in WRITE_MODE to release the page.
150 *******************************************************************************************
151 * @ mapper     : local pointer on the mapper.
152 * @ page       : pointer on page to remove.
153 * @ return 0 if success / return EIO if a dirty page cannot be copied to FS.
154 ******************************************************************************************/
155error_t mapper_release_page( mapper_t      * mapper,
156                             struct page_s * page );
157
158/*******************************************************************************************
159 * This function searches a physical page descriptor from its index in mapper.
160 * It must be executed by a thread running in the cluster containing the mapper.
161 * In case of miss, it takes the mapper lock in WRITE_MODE, load the missing
162 * page from device to the mapper, and release the mapper lock.
163 *******************************************************************************************
164 * @ mapper     : local pointer on the mapper.
165 * @ index      : page index in file
166 * @ returns pointer on page descriptor if success / return NULL if error.
167 ******************************************************************************************/
168struct page_s * mapper_get_page( mapper_t * mapper,
169                                 uint32_t   index );
170
171 
172
173#endif /* _MAPPER_H_ */
Note: See TracBrowser for help on using the repository browser.