fat.h

00001 /*
00002  * Copyright (c) 2008 Jakub Jermar
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00033 #ifndef FAT_FAT_H_
00034 #define FAT_FAT_H_
00035 
00036 #include "fat_fat.h"
00037 #include <fibril_synch.h>
00038 #include <libfs.h>
00039 #include <atomic.h>
00040 #include <sys/types.h>
00041 #include <bool.h>
00042 #include "../../vfs/vfs.h"
00043 
00044 #ifndef dprintf
00045 #define dprintf(...)    printf(__VA_ARGS__)
00046 #endif
00047 
00048 #define min(a, b)               ((a) < (b) ? (a) : (b))
00049 
00050 /*
00051  * Convenience macros for accessing some frequently used boot sector members.
00052  */
00053 #define BPS(bs)         uint16_t_le2host((bs)->bps)
00054 #define SPC(bs)         (bs)->spc
00055 #define RSCNT(bs)       uint16_t_le2host((bs)->rscnt)
00056 #define FATCNT(bs)      (bs)->fatcnt
00057 #define SF(bs)          uint16_t_le2host((bs)->sec_per_fat)
00058 #define RDE(bs)         uint16_t_le2host((bs)->root_ent_max)
00059 #define TS(bs)          (uint16_t_le2host((bs)->totsec16) != 0 ? \
00060                         uint16_t_le2host((bs)->totsec16) : \
00061                         uint32_t_le2host(bs->totsec32))
00062 
00063 #define BS_BLOCK                0
00064 #define BS_SIZE                 512
00065 
00066 typedef struct fat_bs {
00067         uint8_t         ji[3];          
00068         uint8_t         oem_name[8];
00069         /* BIOS Parameter Block */
00070         uint16_t        bps;            
00071         uint8_t         spc;            
00072         uint16_t        rscnt;          
00073         uint8_t         fatcnt;         
00074         uint16_t        root_ent_max;   
00076         uint16_t        totsec16;       
00077         uint8_t         mdesc;          
00078         uint16_t        sec_per_fat;    
00079         uint16_t        sec_per_track;  
00080         uint16_t        headcnt;        
00081         uint32_t        hidden_sec;     
00082         uint32_t        totsec32;       
00084         union {
00085                 struct {
00086                         /* FAT12/FAT16 only: Extended BIOS Parameter Block */
00088                         uint8_t         pdn;
00089                         uint8_t         reserved;
00091                         uint8_t         ebs;
00093                         uint32_t        id;
00095                         uint8_t         label[11];
00097                         uint8_t         type[8];
00099                         uint8_t         boot_code[448];
00101                         uint16_t        signature;
00102                 } __attribute__ ((packed));
00103                 struct {
00104                         /* FAT32 only */
00106                         uint32_t        sectors_per_fat;
00108                         uint16_t        flags;
00110                         uint16_t        version;
00112                         uint32_t        root_cluster;
00114                         uint16_t        fsinfo_sec;
00116                         uint16_t        bscopy_sec;
00117                         uint8_t         reserved1[12];
00119                         uint8_t         pdn;
00120                         uint8_t         reserved2;
00122                         uint8_t         ebs;
00124                         uint32_t        id;
00126                         uint8_t         label[11];
00128                         uint8_t         type[8];
00130                         uint8_t         boot_code[420];
00132                         uint16_t        signature;
00133                 } fat32 __attribute__ ((packed));
00134         };
00135 } __attribute__ ((packed)) fat_bs_t;
00136 
00137 typedef enum {
00138         FAT_INVALID,
00139         FAT_DIRECTORY,
00140         FAT_FILE
00141 } fat_node_type_t;
00142 
00143 struct fat_node;
00144 
00170 typedef struct {
00172         link_t          uph_link;
00174         link_t          uih_link;
00175 
00176         fibril_mutex_t  lock;
00177         devmap_handle_t devmap_handle;
00178         fs_index_t      index;
00185         fat_cluster_t   pfc;
00187         unsigned        pdi;
00189         struct fat_node *nodep;
00190 } fat_idx_t;
00191 
00193 typedef struct fat_node {
00195         fs_node_t               *bp;
00196         
00197         fibril_mutex_t          lock;
00198         fat_node_type_t         type;
00199         fat_idx_t               *idx;
00205         fat_cluster_t           firstc;
00207         link_t                  ffn_link;
00208         aoff64_t                size;
00209         unsigned                lnkcnt;
00210         unsigned                refcnt;
00211         bool                    dirty;
00212 
00213         /*
00214          * Cache of the node's last and "current" cluster to avoid some
00215          * unnecessary FAT walks.
00216          */
00217         /* Node's last cluster in FAT. */
00218         bool            lastc_cached_valid;
00219         fat_cluster_t   lastc_cached_value;
00220         /* Node's "current" cluster, i.e. where the last I/O took place. */
00221         bool            currc_cached_valid;
00222         aoff64_t        currc_cached_bn;
00223         fat_cluster_t   currc_cached_value;
00224 } fat_node_t;
00225 
00226 extern fs_reg_t fat_reg;
00227 
00228 extern void fat_mounted(ipc_callid_t, ipc_call_t *);
00229 extern void fat_mount(ipc_callid_t, ipc_call_t *);
00230 extern void fat_unmounted(ipc_callid_t, ipc_call_t *);
00231 extern void fat_unmount(ipc_callid_t, ipc_call_t *);
00232 extern void fat_lookup(ipc_callid_t, ipc_call_t *);
00233 extern void fat_read(ipc_callid_t, ipc_call_t *);
00234 extern void fat_write(ipc_callid_t, ipc_call_t *);
00235 extern void fat_truncate(ipc_callid_t, ipc_call_t *);
00236 extern void fat_stat(ipc_callid_t, ipc_call_t *);
00237 extern void fat_close(ipc_callid_t, ipc_call_t *);
00238 extern void fat_destroy(ipc_callid_t, ipc_call_t *);
00239 extern void fat_open_node(ipc_callid_t, ipc_call_t *);
00240 extern void fat_stat(ipc_callid_t, ipc_call_t *);
00241 extern void fat_sync(ipc_callid_t, ipc_call_t *);
00242 
00243 extern int fat_idx_get_new(fat_idx_t **, devmap_handle_t);
00244 extern fat_idx_t *fat_idx_get_by_pos(devmap_handle_t, fat_cluster_t, unsigned);
00245 extern fat_idx_t *fat_idx_get_by_index(devmap_handle_t, fs_index_t);
00246 extern void fat_idx_destroy(fat_idx_t *);
00247 extern void fat_idx_hashin(fat_idx_t *);
00248 extern void fat_idx_hashout(fat_idx_t *);
00249 
00250 extern int fat_idx_init(void);
00251 extern void fat_idx_fini(void);
00252 extern int fat_idx_init_by_devmap_handle(devmap_handle_t);
00253 extern void fat_idx_fini_by_devmap_handle(devmap_handle_t);
00254 
00255 #endif
00256 

Generated on Thu Jun 2 07:45:42 2011 for HelenOS/USB by  doxygen 1.4.7