| 1 | /* | 
|---|
| 2 | * fatfs.h - FATFS file system API definition. | 
|---|
| 3 | * | 
|---|
| 4 | * Author    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 _FATFS_H_ | 
|---|
| 26 | #define _FATFS_H_ | 
|---|
| 27 |  | 
|---|
| 28 | #include <hal_types.h> | 
|---|
| 29 | #include <rwlock.h> | 
|---|
| 30 |  | 
|---|
| 31 | /****  Forward declarations  ****/ | 
|---|
| 32 |  | 
|---|
| 33 | struct mapper_s; | 
|---|
| 34 | struct device_s; | 
|---|
| 35 | struct vfs_inode_s; | 
|---|
| 36 | struct vfs_ctx_s; | 
|---|
| 37 | struct page_s; | 
|---|
| 38 |  | 
|---|
| 39 | /***************************************************************************************** | 
|---|
| 40 | * This structure defines a FATFS specific context extension. | 
|---|
| 41 | ****************************************************************************************/ | 
|---|
| 42 |  | 
|---|
| 43 | typedef struct fatfs_ctx_s | 
|---|
| 44 | { | 
|---|
| 45 | rwlock_t          lock;                  /*! TODO protect what ???                  */ | 
|---|
| 46 | uint32_t          fat_begin_lba;         /*! first lba of FAT region                */ | 
|---|
| 47 | uint32_t          fat_sectors_count;     /*! number of sectors in FAT region        */ | 
|---|
| 48 | uint32_t          bytes_per_sector;      /*!                                        */ | 
|---|
| 49 | uint32_t          bytes_per_cluster;     /*!                                        */ | 
|---|
| 50 | uint32_t          cluster_begin_lba;     /*! first lba of data region on device     */ | 
|---|
| 51 | uint32_t          sectors_per_cluster;   /*!                                        */ | 
|---|
| 52 | uint32_t          rootdir_first_cluster; /*                                         */ | 
|---|
| 53 | uint32_t          last_allocated_sector; /*!                                        */ | 
|---|
| 54 | uint32_t          last_allocated_index;  /*! TODO last allocated cluster ???        */ | 
|---|
| 55 | xptr_t            fat_mapper_xp;         /*! FAT mapper (in IO cluster)             */ | 
|---|
| 56 | } | 
|---|
| 57 | fatfs_ctx_t; | 
|---|
| 58 |  | 
|---|
| 59 | /***************************************************************************************** | 
|---|
| 60 | * This structure defines the FAT specific inode extension (versus the VFS inode). | 
|---|
| 61 | ****************************************************************************************/ | 
|---|
| 62 |  | 
|---|
| 63 | typedef struct fatfs_inode_s | 
|---|
| 64 | { | 
|---|
| 65 | struct fatfs_ctx_s * ctx;                /*! local pointer on the FATFS context     */ | 
|---|
| 66 | uint32_t             first_cluster;      /*! first cluster for this file/dir        */ | 
|---|
| 67 | } | 
|---|
| 68 | fatfs_inode_t; | 
|---|
| 69 |  | 
|---|
| 70 |  | 
|---|
| 71 |  | 
|---|
| 72 | /***************************************************************************************** | 
|---|
| 73 | * This function returns the LBA of the first sector of a FAT cluster. | 
|---|
| 74 | * This function can be called by any thread running in any cluster. | 
|---|
| 75 | ***************************************************************************************** | 
|---|
| 76 | * @ ctx          :     pointer on FATFS context. | 
|---|
| 77 | * @ cluster  : cluster index in FATFS. | 
|---|
| 78 | * # return the lba value. | 
|---|
| 79 | ****************************************************************************************/ | 
|---|
| 80 | inline uint32_t fatfs_lba_from_cluster( fatfs_ctx_t * ctx, | 
|---|
| 81 | uint32_t      cluster ); | 
|---|
| 82 |  | 
|---|
| 83 | /***************************************************************************************** | 
|---|
| 84 | * This function scan the FAT (File Allocation Table), stored in the FAT mapper, | 
|---|
| 85 | * and return the FATFS cluster index for a given page of a given file. | 
|---|
| 86 | * It must be called by a thread running in the cluster containing the FAT mapper | 
|---|
| 87 | * (can be a local thread or a RPC thread). | 
|---|
| 88 | * The FAT is actually an array of uint32_t slots. Each slot in this array contains the | 
|---|
| 89 | * index of another slot in this array, to form one linked list for each file stored on | 
|---|
| 90 | * device in the RAMFS file system. This index in the FAT array is also the index of the | 
|---|
| 91 | * FATFS cluster on the device. One FATFS cluster is supposed to contain one PPM page. | 
|---|
| 92 | * For a given file, the entry point in the FAT is simply the index of the FATFS cluster | 
|---|
| 93 | * containing the first page of the file. The mapper being a cache, this function | 
|---|
| 94 | * automatically updates the FAT mapper from informations stored on device in case of miss. | 
|---|
| 95 | ***************************************************************************************** | 
|---|
| 96 | * @ mapper      : local pointer on the FAT mapper. | 
|---|
| 97 | * @ first       : index of the first FATFS cluster allocated to the file. | 
|---|
| 98 | * @ page    : index of searched page in the file. | 
|---|
| 99 | * @ cluster : [out] pointer on buffer for the found FATFS cluster index. | 
|---|
| 100 | * @ return 0 if success / return EIO if FAT mapper miss cannot be solved. | 
|---|
| 101 | ****************************************************************************************/ | 
|---|
| 102 | error_t fatfs_get_cluster( struct mapper_s * mapper, | 
|---|
| 103 | uint32_t          first_cluster, | 
|---|
| 104 | uint32_t          searched_page, | 
|---|
| 105 | uint32_t        * cluster ); | 
|---|
| 106 |  | 
|---|
| 107 | /***************************************************************************************** | 
|---|
| 108 | * This function allocates memory for a FATFS inode, initializes it, | 
|---|
| 109 | * and link it to the VFS inode. | 
|---|
| 110 | ***************************************************************************************** | 
|---|
| 111 | * @ inode   : local pointer on vfs_inode. | 
|---|
| 112 | * @ return 0 if success / return ENOMEM if error. | 
|---|
| 113 | ****************************************************************************************/ | 
|---|
| 114 | error_t fatfs_inode_create( struct vfs_inode_s * inode ); | 
|---|
| 115 |  | 
|---|
| 116 | /***************************************************************************************** | 
|---|
| 117 | * This function releases memory allocated for a FATFS inode. | 
|---|
| 118 | ***************************************************************************************** | 
|---|
| 119 | * @ inode   : local pointer on vfs_inode. | 
|---|
| 120 | ****************************************************************************************/ | 
|---|
| 121 | void fatfs_inode_destroy( struct vfs_inode_s * inode ); | 
|---|
| 122 |  | 
|---|
| 123 | /***************************************************************************************** | 
|---|
| 124 | * This function allocates memory for a FATFS context, initialises it, | 
|---|
| 125 | * and link it to the local VFS context. | 
|---|
| 126 | ***************************************************************************************** | 
|---|
| 127 | * @ inode   : local pointer on VFS context. | 
|---|
| 128 | * @ return 0 if success / return ENOMEM if error. | 
|---|
| 129 | ****************************************************************************************/ | 
|---|
| 130 | error_t fatfs_ctx_create( struct vfs_ctx_s * ctx ); | 
|---|
| 131 |  | 
|---|
| 132 | /***************************************************************************************** | 
|---|
| 133 | * This function releases memory allocated for a FATFS context. | 
|---|
| 134 | ***************************************************************************************** | 
|---|
| 135 | * @ ctx   : local pointer on VFS context. | 
|---|
| 136 | ****************************************************************************************/ | 
|---|
| 137 | void fatfs_ctx_destroy( struct vfs_ctx_s * ctx ); | 
|---|
| 138 |  | 
|---|
| 139 | /***************************************************************************************** | 
|---|
| 140 | * This function moves a page from the mapper to the FATFS file system. | 
|---|
| 141 | * It must be called by a thread running in cluster containing the mapper. | 
|---|
| 142 | * The pointer on the mapper and the page index in file are supposed to be registered | 
|---|
| 143 | * in the page descriptor. | 
|---|
| 144 | ***************************************************************************************** | 
|---|
| 145 | * @ page    : local pointer on page descriptor. | 
|---|
| 146 | * @ return 0 if success / return EIO if error. | 
|---|
| 147 | ****************************************************************************************/ | 
|---|
| 148 | error_t fatfs_write_page( struct page_s * page ); | 
|---|
| 149 |  | 
|---|
| 150 | /***************************************************************************************** | 
|---|
| 151 | * This function moves a page from the FATFS file system on device to the mapper. | 
|---|
| 152 | * It must be called by a thread running in cluster containing the mapper. | 
|---|
| 153 | * The pointer on the mapper and the page index in file are supposed to be registered | 
|---|
| 154 | * in the page descriptor. | 
|---|
| 155 | ***************************************************************************************** | 
|---|
| 156 | * @ page    : local pointer on page descriptor. | 
|---|
| 157 | * @ return 0 if success / return EIO if error. | 
|---|
| 158 | ****************************************************************************************/ | 
|---|
| 159 | error_t fatfs_read_page( struct page_s * page ); | 
|---|
| 160 |  | 
|---|
| 161 |  | 
|---|
| 162 | #endif  /* _FATFS_H_ */ | 
|---|