source: trunk/kernel/fs/fatfs.h @ 612

Last change on this file since 612 was 612, checked in by alain, 5 years ago

Fix several bugs in vfs.c, fatfs.c, and devfs.c to support
the <.> and <..> directory entries.

File size: 26.7 KB
Line 
1/*
2 * fatfs.h - FATFS file system API definition.
3 *
4 * Author    Mohamed Lamine Karaoui (2014,2015)
5 *           Alain Greiner (2016,2017,2018)
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_kernel_types.h>
29#include <remote_queuelock.h>
30#include <vfs.h>
31
32
33///////////////////////////////////////////////////////////////////////////////////////////
34// The FATFS File System implements a FAT32 read/write file system.
35//
36// The FATFS extensions to the generic VFS are the following:
37//
38// 1) The vfs_ctx_t "extend" field is a void* pointing on the fatfs_ctx_t structure.
39//    This structure contains various general informations such as the total
40//    number of sectors in FAT region, the number of bytes per sector, the number
41//    of sectors per cluster, the lba of FAT region, the lba of data region, or the
42//    cluster index for the root directory. It contains also an extended pointer
43//    on the FAT mapper.
44//
45// 2) The vfs_inode_t "extend" contains, for each inode,
46//    the first FAT cluster index (after cast to intptr).
47//
48// 3) The vfs_dentry_t "extend" field contains, for each dentry, the entry index
49//    in the FATFS directory (32 bytes per FATFS entry).
50///////////////////////////////////////////////////////////////////////////////////////////
51
52/*************** Partition Boot Sector Format **********************************/
53//                                     offset |  length
54#define BS_JMPBOOT                          0 ,  3
55#define BS_OEMNAME                          3 ,  8
56#define BPB_BYTSPERSEC                     11 ,  2
57#define BPB_SECPERCLUS                     13 ,  1
58#define BPB_RSVDSECCNT                     14 ,  2
59#define BPB_NUMFATS                        16 ,  1
60#define BPB_ROOTENTCNT                     17 ,  2
61#define BPB_TOTSEC16                       19 ,  2
62#define BPB_MEDIA                          21 ,  1
63#define BPB_FATSZ16                        22 ,  2
64#define BPB_SECPERTRK                      24 ,  2
65#define BPB_NUMHEADS                       26 ,  2
66#define BPB_HIDDSEC                        28 ,  4
67#define BPB_TOTSEC32                       32 ,  4
68#define BPB_PARTITION_TABLE               446 , 64
69
70// FAT 32
71#define BPB_FAT32_FATSZ32                  36 ,  4
72#define BPB_FAT32_EXTFLAGS                 40 ,  2
73#define BPB_FAT32_FSVER                    42 ,  2
74#define BPB_FAT32_ROOTCLUS                 44 ,  4
75#define BPB_FAT32_FSINFO                   48 ,  2
76#define BPB_FAT32_BKBOOTSEC                50 ,  2
77#define BS_FAT32_DRVNUM                    64 ,  1
78#define BS_FAT32_BOOTSIG                   66 ,  1
79#define BS_FAT32_VOLID                     67 ,  4
80#define BS_FAT32_VOLLAB                    71 , 11
81#define BS_FAT32_FILSYSTYPE                82 ,  8
82
83// Partitions
84#define FIRST_PARTITION_ACTIVE            446 ,  8
85#define FIRST_PARTITION_BEGIN_LBA         454 ,  4
86#define FIRST_PARTITION_SIZE              458 ,  4
87#define SECOND_PARTITION_ACTIVE           462 ,  8
88#define SECOND_PARTITION_BEGIN_LBA        470 ,  4
89#define SECOND_PARTITION_SIZE             474 ,  4
90#define THIRD_PARTITION_ACTIVE            478 ,  8
91#define THIRD_PARTITION_BEGIN_LBA         486 ,  4
92#define THIRD_PARTITION_SIZE              490 ,  4
93#define FOURTH_PARTITION_ACTIVE           494 ,  8
94#define FOURTH_PARTITION_BEGIN_LBA        502 ,  4
95#define FOURTH_PARTITION_SIZE             506 ,  4   
96/*******************************************************************************/
97
98#define MBR_SIGNATURE_POSITION            510 , 2
99#define MBR_SIGNATURE_VALUE               0xAA55 
100
101/************** FAT_FS_INFO SECTOR  ********************************************/
102#define FS_SIGNATURE_VALUE_1              0x52526141
103#define FS_SIGNATURE_VALUE_2              0x72724161
104#define FS_SIGNATURE_VALUE_3              0x000055AA 
105#define FS_SIGNATURE_POSITION_1           0   , 4 
106#define FS_SIGNATURE_POSITION_2           484 , 4
107#define FS_SIGNATURE_POSITION_3           508 , 4 
108#define FS_FREE_CLUSTERS                  488 , 4
109#define FS_FREE_CLUSTER_HINT              492 , 4
110/*******************************************************************************/
111
112#define DIR_ENTRY_SIZE          32
113                   
114#define NAME_MAX_SIZE           31
115
116/******* SFN Directory Entry Structure (32 bytes) ******************************/
117//                            offset | length
118#define DIR_NAME                   0 , 11   // dir_entry name
119#define DIR_ATTR                  11 ,  1   // attributes
120#define DIR_NTRES                 12 ,  1   // reserved for the OS       
121#define DIR_CRT_TIMES_TENTH       13 ,  1
122#define DIR_FST_CLUS_HI           20 ,  2   // cluster index 16 MSB bits
123#define DIR_WRT_TIME              22 ,  2   // time of last write
124#define DIR_WRT_DATE              24 ,  2   // date of last write
125#define DIR_FST_CLUS_LO           26 ,  2   // cluster index 16 LSB bit
126#define DIR_FILE_SIZE             28 ,  4   // dir_entry size (up to 4 Gbytes)
127/*******************************************************************************/
128
129/******* LFN Directory Entry Structure  (32 bytes) *****************************/
130//                            offset | length
131#define LDIR_ORD                   0 ,  1   // Sequence number (from 0x01 to 0x0f)   
132#define LDIR_NAME_1                1 , 10   // name broken into 3 parts
133#define LDIR_ATTR                 11 ,  1   // attributes (must be 0x0F)
134#define LDIR_TYPE                 12 ,  1   // directory type (must be 0x00)
135#define LDIR_CHKSUM               13 ,  1   // checksum of name in short dir 
136#define LDIR_NAME_2               14 , 12
137#define LDIR_RSVD                 26 ,  2   // artifact of previous fat (must be 0)
138#define LDIR_NAME_3               28 ,  4   
139/*******************************************************************************/
140
141/***********************  DIR_ATTR values  (attributes) ************************/
142#define ATTR_READ_ONLY            0x01
143#define ATTR_HIDDEN               0x02
144#define ATTR_SYSTEM               0x04
145#define ATTR_VOLUME_ID            0x08
146#define ATTR_DIRECTORY            0x10
147#define ATTR_ARCHIVE              0x20
148#define ATTR_LONG_NAME_MASK       0x0f      // READ_ONLY|HIDDEN|SYSTEM|VOLUME_ID
149/*******************************************************************************/
150
151/********************* DIR_ORD special values **********************************/
152#define FREE_ENTRY                0xE5     // this entry is free in the directory
153#define NO_MORE_ENTRY             0x00     // no more entry in the directory
154/*******************************************************************************/
155
156/******************** CLuster Index Special Values *****************************/
157#define FREE_CLUSTER              0x00000000
158#define RESERVED_CLUSTER          0x00000001
159#define BAD_CLUSTER               0x0FFFFFF7
160#define END_OF_CHAIN_CLUSTER_MIN  0x0ffffff8
161#define END_OF_CHAIN_CLUSTER_MAX  0x0fffffff
162/*******************************************************************************/
163
164/****  Forward declarations  ****/
165
166struct mapper_s;
167struct page_s;
168struct vfs_ctx_s;
169struct vfs_inode_s;
170struct vfs_dentry_s;
171
172/*****************************************************************************************
173 * This structure defines a FATFS specific context (extension to VFS context).
174 * This extension is replicated in all clusters.
175 *
176 * WARNING : Almost all fields are constant values, but the <free_cluster_hint> and
177 * <free_clusters> are shared variables. All kernel instances use the variables
178 * in cluster 0, using the <free_lock> remote busy_lock for exclusive access.
179 ****************************************************************************************/
180
181typedef struct fatfs_ctx_s
182{
183    uint32_t            fat_sectors_count;     /*! number of sectors in FAT region      */
184    uint32_t            bytes_per_sector;      /*! number of bytes per sector           */
185    uint32_t            sectors_per_cluster;   /*! number of sectors per cluster        */
186    uint32_t            fat_begin_lba;         /*! lba of FAT region                    */
187    uint32_t            cluster_begin_lba;     /*! lba of data region                   */
188    uint32_t            fs_info_lba;           /*! lba of FS_INFO sector                */
189    uint32_t            root_dir_cluster;      /*! cluster index for  root directory    */
190    xptr_t              fat_mapper_xp;         /*! extended pointer on FAT mapper       */
191    uint32_t            free_cluster_hint;     /*! start point to search free cluster   */
192    uint32_t            free_clusters;         /*! free clusters number                 */
193    remote_queuelock_t  free_lock;             /*! exclusive access to hint & number    */
194}
195fatfs_ctx_t;
196
197//////////////////////////////////////////////////////////////////////////////////////////
198//              FATFS specific extern functions 
199//////////////////////////////////////////////////////////////////////////////////////////
200
201/*****************************************************************************************
202 * This function access the FAT (File Allocation Table), stored in the FAT mapper, and
203 * returns in <searched_cluster> the FATFS cluster index for a given page of a given
204 * inode identified by the <first_cluster> and <page_id> arguments.
205 * It can be called by a thread running in any cluster, as it uses remote access
206 * primitives when the FAT mapper is remote.
207 * The FAT is actually an array of uint32_t slots. Each slot in this array contains the
208 * index of another slot in this array, to form one linked list for each file stored on
209 * device in the FATFS file system. This index in the FAT array is also the index of the
210 * FATFS cluster on the device. One FATFS cluster is supposed to contain one PPM page.
211 * For a given file, the entry point in the FAT is simply the index of the FATFS cluster
212 * containing the first page of the file. The FAT mapper being a cache, this function
213 * updates the FAT mapper from informations stored on IOC device in case of miss.
214 *****************************************************************************************
215 * @ first_cluster       : [in]  index of first FATFS cluster allocated to the file.
216 * @ page_id             : [in]  index of searched page in file.
217 * @ searched_cluster    : [out] found FATFS cluster index.
218 * @ return 0 if success / return -1 if a FAT mapper miss cannot be solved.
219 ****************************************************************************************/
220error_t fatfs_get_cluster( uint32_t   first_cluster,
221                           uint32_t   page_id,
222                           uint32_t * searched_cluster );
223
224/*****************************************************************************************
225 * This function display the content of the FATFS context.
226 ****************************************************************************************/
227void fatfs_ctx_display( void );
228
229/*****************************************************************************************
230 * This function displays the content of a part of the File Allocation Table.
231 * It loads the requested page fom device to mapper if required.
232 *****************************************************************************************
233 * @ page_id   : page index in FAT mapper (one page is 4 Kbytes).
234 * @ nentries  : number of entries (one entry is 4 bytes).
235 ****************************************************************************************/
236void fatfs_display_fat( uint32_t  page_id,
237                        uint32_t  nentries );
238
239
240//////////////////////////////////////////////////////////////////////////////////////////
241// Generic API: These functions are called by the kernel VFS,
242//              and must be implemented by all File Systems.
243//////////////////////////////////////////////////////////////////////////////////////////
244
245/*****************************************************************************************
246 * This fuction allocates memory from local cluster for a FATFS context descriptor.
247 *****************************************************************************************
248 * @ return a pointer on the created context / return NULL if failure.
249 ****************************************************************************************/
250fatfs_ctx_t * fatfs_ctx_alloc( void );
251
252/*****************************************************************************************
253 * This function access the boot device, and initialises the local FATFS context
254 * from informations contained in the boot record.
255 *****************************************************************************************
256 * @ vfs_ctx   : local pointer on VFS context for FATFS.
257 ****************************************************************************************/
258void fatfs_ctx_init( fatfs_ctx_t * fatfs_ctx );
259
260/*****************************************************************************************
261 * This function releases memory dynamically allocated for the FATFS context extension.
262 *****************************************************************************************
263 * @ vfs_ctx   : local pointer on VFS context.
264 ****************************************************************************************/
265void fatfs_ctx_destroy( fatfs_ctx_t * fatfs_ctx );
266
267/*****************************************************************************************
268 * This function implements the generic vfs_fs_add_dentry() function for the FATFS.
269 *****************************************************************************************
270 * This function updates a directory identified by the <inode> argument
271 * to add a new directory entry identified by the <dentry> argument.
272 * All modified pages in directory mapper are synchronously updated on IOC device.
273 * It must be called by a thread running in the cluster containing the inode.
274 *
275 * Implementation note : this function works in two steps:
276 * - It scan the set of 32 bytes FATFS directry entries, using two embedded loops 
277 *   to find the end of directory (NO_MORE_ENTRY marker).
278 * - Then it writes 3, 4, or 5 directory entries (depending on the name length), using
279 *   a 5 steps FSM (one state per entry to be written), updates on IOC device the
280 *   modified pages, and updates the dentry extension field, that must contain
281 *   the dentry index in FATFS directory.
282 *****************************************************************************************
283 * @ inode    : local pointer on directory inode.
284 * @ dentry   : local pointer on dentry.
285 * @ return 0 if success / return ENOENT if not found, or EIO if no access to IOC device.
286 ****************************************************************************************/
287error_t fatfs_add_dentry( struct vfs_inode_s  * inode,
288                          struct vfs_dentry_s * dentry );
289
290/*****************************************************************************************
291 * This function implements the generic vfs_fs_remove_dentry() function for the FATFS.
292 *****************************************************************************************
293 * This function updates a directory identified by the <inode> argument
294 * to remove a directory entry identified by the <dentry> argument.
295 * All modified pages in directory mapper are synchronously updated on IOC device.
296 * It must be called by a thread running in the cluster containing the inode.
297 *
298 * Implementation note: this function uses the dentry extension to directly access
299 * the NORMAL directory entry and invalidate all involved LFN entries. Then it
300 * updates the modified pages on IOC device.
301 *****************************************************************************************
302 * @ inode    : local pointer on directory inode.
303 * @ dentry   : local pointer on dentry.
304 * @ return 0 if success / return ENOENT if not found, or EIO if no access to IOC device.
305 ****************************************************************************************/
306error_t fatfs_remove_dentry( struct vfs_inode_s  * inode,
307                             struct vfs_dentry_s * dentry );
308
309/*****************************************************************************************
310 * This function implements the generic vfs_fs_get_dentry() function for the FATFS.
311 *****************************************************************************************
312 * It initialises a new child (new inode/dentry couple in Inode Tree), identified
313 * by the <child_inode_xp> argument, from the parent directory mapper, identified by the
314 * <parent_inode> argument.
315 * It scan the parent mapper to find the <name> argument.
316 * It set the "type", "size", and "extend" fields in inode descriptor.
317 * It set the " extend" field in dentry descriptor.
318 * It must be called by a thread running in the cluster containing the parent inode.
319 *****************************************************************************************
320 * @ parent_inode    : local pointer on parent inode (directory).
321 * @ name            : child name.
322 * @ child_inode_xp  : extended pointer on remote child inode (file or directory).
323 * @ return 0 if success / return ENOENT if child not found.
324 ****************************************************************************************/
325error_t fatfs_get_dentry( struct vfs_inode_s * parent_inode,
326                          char               * name,
327                          xptr_t               child_inode_xp );
328
329/*****************************************************************************************
330 * This function implements the generic vfs_fs_get_user_dir() function for the FATFS.
331 *****************************************************************************************
332 * It is called by the remote_dir_create() function to scan the mapper of a directory
333 * identified by the <inode> argument and copy up to <max_dirent> valid dentries to a
334 * local dirent array, defined by the <array> argument. The <min_dentry> argument defines
335 * the index of the first dentry to copied to the target dirent array.
336 * This function returns in the <entries> buffer the number of dentries actually written,
337 * and signals in the <done> buffer when the last valid entry has been found.
338 * If the <detailed> argument is true, a dentry/inode couple that does not exist in
339 * the Inode Tree is dynamically created, and all dirent fiels are documented in the
340 * dirent array. Otherwise, only the dentry name is documented.
341 * It must be called by a thread running in the cluster containing the directory inode.
342 *****************************************************************************************
343 * @ inode      : [in]  local pointer on directory inode.
344 * @ array      : [in]  local pointer on array of dirents.
345 * @ max_dirent : [in]  max number of slots in dirent array.
346 * @ min_dentry : [in]  index of first dentry to be copied into array.
347 * @ detailed   : [in]  dynamic inode creation if true.
348 * @ entries    : [out] number of dentries actually copied into array.
349 * @ done       : [out] Boolean true when last entry found.
350 * @ return 0 if success / return -1 if failure.
351 ****************************************************************************************/
352error_t fatfs_get_user_dir( struct vfs_inode_s * inode,
353                            struct dirent      * array, 
354                            uint32_t             max_dirent,
355                            uint32_t             min_dentry,
356                            bool_t               detailed,
357                            uint32_t           * entries,
358                            bool_t             * done );
359
360/*****************************************************************************************
361 * This function implements the generic vfs_fs_sync_inode() function for the FATFS.
362 *****************************************************************************************
363 * It updates the FATFS on the IOC device for a given inode identified by
364 * the <inode> argument. It scan all pages registered in the associated mapper,
365 * and copies from mapper to device each page marked as dirty.
366 * WARNING : The target <inode> cannot be a directory, because all modifications in a
367 * directory * are synchronously done on the IOC device by the two fatfs_add_dentry()
368 * and fatfs_remove_dentry() functions.
369 *****************************************************************************************
370 * @ inode   : local pointer on inode.
371 * @ return 0 if success / return EIO if failure during device access.
372 ****************************************************************************************/
373error_t fatfs_sync_inode( struct vfs_inode_s * inode );
374
375/*****************************************************************************************
376 * This function implements the generic vfs_fs_sync_fat() function for the FATFS.
377 *****************************************************************************************
378 * It updates the FATFS on the IOC device for the FAT itself.
379 * It scan all clusters registered in the FAT mapper, and copies from mapper to device
380 * each page marked as dirty.
381 *
382 * TODO : the current implementation check ALL pages in the FAT region, even if most
383 * pages are empty, and not copied in mapper. It is sub-optimal.
384 * - A first solution is to maintain in the FAT context two "dirty_min" and "dirty_max"
385 *  variables defining the smallest/largest dirty page index in FAT mapper...
386 *****************************************************************************************
387 * @ return 0 if success / return EIO if failure during device access.
388 ****************************************************************************************/
389error_t fatfs_sync_fat( void );
390
391/*****************************************************************************************
392 * This function implements the generic vfs_fs_sync_fsinfo() function for the FATFS.
393 *****************************************************************************************
394 * It updates the FS_INFO sector on the IOC device.
395 * It copies the <free_cluster_hint> and <free_clusters> variables from
396 * the FATFS context in cluster 0 to the FS_INFO sector on device.
397 *****************************************************************************************
398 * @ return 0 if success / return EIO if failure during device access.
399 ****************************************************************************************/
400error_t fatfs_sync_free_info( void );
401
402/*****************************************************************************************
403 * This function implements the generic vfs_fs_cluster_alloc() function for the FATFS.
404 *****************************************************************************************
405 * It access the FAT (File allocation table), stored in the FAT mapper, and returns
406 * in <searched_cluster> the FATFS cluster index of a free cluster.
407 * It can be called by a thread running in any cluster, as it uses remote access
408 * primitives when the FAT mapper is remote. It takes the "free_lock" stored in the
409 * FATFS context located in the same cluster as the FAT mapper itself, to get exclusive
410 * access to the FAT. It uses (and updates) the <free_cluster_hint> and <free_clusters>
411 * shared variables in this FATFS context.
412 * It updates the FAT mapper, and synchronously updates the FAT region on IOC device.
413 * The FAT mapper being a cache, this function updates the FAT mapper from informations
414 * stored on IOC device in case of miss.
415 *****************************************************************************************
416 * @ searched_cluster    : [out] found FATFS cluster index.
417 * @ return 0 if success / return -1 if no more free clusters on IOC device.
418 ****************************************************************************************/
419error_t fatfs_cluster_alloc( uint32_t * searched_cluster );
420
421/*****************************************************************************************
422 * This function implements the generic vfs_fs_release_inode() function for the FATFS.
423 *****************************************************************************************
424 * It releases all clusters allocated to a file/directory identified by the <inode_xp>
425 * argument. All released clusters are marked FREE_CLUSTER in the FAT mapper.
426 * This function calls the recursive function fatfs_cluster_release() to release
427 * the clusters in reverse order of the linked list (from last to first).
428 * When the FAT mapper has been updated, it calls the fatfs_sync_fat() function to
429 * synchronously update all dirty pages in the FAT mapper to the IOC device.
430 * Finally the FS-INFO sector on the IOC device is updated.
431 *****************************************************************************************
432 * @ inode_xp   : extended pointer on inode.
433 * @ return 0 if success / return EIO if failure during device access.
434 ****************************************************************************************/
435error_t fatfs_release_inode( xptr_t inode_xp );
436
437/*****************************************************************************************
438 * This function implements the generic vfs_fs_move_page() function for the FATFS.
439 *****************************************************************************************
440 * This function moves a page from/to the mapper to/from the FATFS file system on device.
441 * The page must have been previously allocated and registered in the mapper.   
442 * The page - and the mapper - can be located in another cluster than the calling thread.
443 * The pointer on the mapper and the page index in file are found in the page descriptor.
444 * It is used for both for a regular file/directory mapper, and the FAT mapper.
445 * For the FAT mapper, it access the FATFS to get the location on IOC device.
446 * For a regular file, it access the FAT mapper to get the cluster index on IOC device.
447 * It can be called by any thread running in any cluster.
448 *
449 * WARNING : For the FAT mapper, the inode field in the mapper MUST be NULL, as this
450 * is used to indicate that the corresponding mapper is the FAT mapper.
451 *****************************************************************************************
452 * @ page_xp   : extended pointer on page descriptor.
453 * @ to_mapper : true for device->mapper / false for mapper->device
454 * @ return 0 if success / return EIO if error during device access.
455 ****************************************************************************************/
456error_t fatfs_move_page( xptr_t  page_xp,
457                         bool_t  to_mapper );
458
459
460
461
462
463
464#endif  /* _FATFS_H_ */
Note: See TracBrowser for help on using the repository browser.