[be815bc] | 1 | /*
|
---|
[5e790e6] | 2 | * Copyright (c) 2008 Jakub Jermar
|
---|
[be815bc] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup fs
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | #ifndef FAT_FAT_H_
|
---|
| 34 | #define FAT_FAT_H_
|
---|
| 35 |
|
---|
[0f57d0e] | 36 | #include "fat_fat.h"
|
---|
[1e4cada] | 37 | #include <fibril_synch.h>
|
---|
[efd4a72] | 38 | #include <libfs.h>
|
---|
[be815bc] | 39 | #include <atomic.h>
|
---|
| 40 | #include <sys/types.h>
|
---|
| 41 | #include <bool.h>
|
---|
[5e790e6] | 42 | #include "../../vfs/vfs.h"
|
---|
[be815bc] | 43 |
|
---|
[b7b6753] | 44 | #ifndef dprintf
|
---|
[be815bc] | 45 | #define dprintf(...) printf(__VA_ARGS__)
|
---|
[b7b6753] | 46 | #endif
|
---|
[be815bc] | 47 |
|
---|
[0f57d0e] | 48 | #define min(a, b) ((a) < (b) ? (a) : (b))
|
---|
| 49 |
|
---|
[7a23d60] | 50 | /*
|
---|
| 51 | * Convenience macros for accessing some frequently used boot sector members.
|
---|
| 52 | */
|
---|
| 53 | #define BPS(bs) uint16_t_le2host((bs)->bps)
|
---|
| 54 | #define SPC(bs) (bs)->spc
|
---|
| 55 | #define RSCNT(bs) uint16_t_le2host((bs)->rscnt)
|
---|
| 56 | #define FATCNT(bs) (bs)->fatcnt
|
---|
| 57 | #define SF(bs) uint16_t_le2host((bs)->sec_per_fat)
|
---|
| 58 | #define RDE(bs) uint16_t_le2host((bs)->root_ent_max)
|
---|
| 59 | #define TS(bs) (uint16_t_le2host((bs)->totsec16) != 0 ? \
|
---|
| 60 | uint16_t_le2host((bs)->totsec16) : \
|
---|
| 61 | uint32_t_le2host(bs->totsec32))
|
---|
| 62 |
|
---|
[0f57d0e] | 63 | #define BS_BLOCK 0
|
---|
| 64 | #define BS_SIZE 512
|
---|
| 65 |
|
---|
[cb682eb] | 66 | typedef struct fat_bs {
|
---|
[5af627fc] | 67 | uint8_t ji[3]; /**< Jump instruction. */
|
---|
| 68 | uint8_t oem_name[8];
|
---|
| 69 | /* BIOS Parameter Block */
|
---|
| 70 | uint16_t bps; /**< Bytes per sector. */
|
---|
| 71 | uint8_t spc; /**< Sectors per cluster. */
|
---|
[5e790e6] | 72 | uint16_t rscnt; /**< Reserved sector count. */
|
---|
[5af627fc] | 73 | uint8_t fatcnt; /**< Number of FATs. */
|
---|
| 74 | uint16_t root_ent_max; /**< Maximum number of root directory
|
---|
| 75 | entries. */
|
---|
[5e790e6] | 76 | uint16_t totsec16; /**< Total sectors. 16-bit version. */
|
---|
[5af627fc] | 77 | uint8_t mdesc; /**< Media descriptor. */
|
---|
| 78 | uint16_t sec_per_fat; /**< Sectors per FAT12/FAT16. */
|
---|
| 79 | uint16_t sec_per_track; /**< Sectors per track. */
|
---|
| 80 | uint16_t headcnt; /**< Number of heads. */
|
---|
| 81 | uint32_t hidden_sec; /**< Hidden sectors. */
|
---|
[e1e3b26] | 82 | uint32_t totsec32; /**< Total sectors. 32-bit version. */
|
---|
[5af627fc] | 83 |
|
---|
| 84 | union {
|
---|
| 85 | struct {
|
---|
| 86 | /* FAT12/FAT16 only: Extended BIOS Parameter Block */
|
---|
| 87 | /** Physical drive number. */
|
---|
| 88 | uint8_t pdn;
|
---|
| 89 | uint8_t reserved;
|
---|
| 90 | /** Extended boot signature. */
|
---|
| 91 | uint8_t ebs;
|
---|
| 92 | /** Serial number. */
|
---|
| 93 | uint32_t id;
|
---|
| 94 | /** Volume label. */
|
---|
| 95 | uint8_t label[11];
|
---|
| 96 | /** FAT type. */
|
---|
| 97 | uint8_t type[8];
|
---|
| 98 | /** Boot code. */
|
---|
| 99 | uint8_t boot_code[448];
|
---|
| 100 | /** Boot sector signature. */
|
---|
| 101 | uint16_t signature;
|
---|
| 102 | } __attribute__ ((packed));
|
---|
[ed903174] | 103 | struct {
|
---|
[5af627fc] | 104 | /* FAT32 only */
|
---|
| 105 | /** Sectors per FAT. */
|
---|
| 106 | uint32_t sectors_per_fat;
|
---|
| 107 | /** FAT flags. */
|
---|
| 108 | uint16_t flags;
|
---|
| 109 | /** Version. */
|
---|
| 110 | uint16_t version;
|
---|
| 111 | /** Cluster number of root directory. */
|
---|
| 112 | uint32_t root_cluster;
|
---|
| 113 | /** Sector number of file system information sector. */
|
---|
| 114 | uint16_t fsinfo_sec;
|
---|
| 115 | /** Sector number of boot sector copy. */
|
---|
| 116 | uint16_t bscopy_sec;
|
---|
| 117 | uint8_t reserved1[12];
|
---|
| 118 | /** Physical drive number. */
|
---|
| 119 | uint8_t pdn;
|
---|
| 120 | uint8_t reserved2;
|
---|
| 121 | /** Extended boot signature. */
|
---|
| 122 | uint8_t ebs;
|
---|
| 123 | /** Serial number. */
|
---|
| 124 | uint32_t id;
|
---|
| 125 | /** Volume label. */
|
---|
[5e790e6] | 126 | uint8_t label[11];
|
---|
[5af627fc] | 127 | /** FAT type. */
|
---|
| 128 | uint8_t type[8];
|
---|
| 129 | /** Boot code. */
|
---|
| 130 | uint8_t boot_code[420];
|
---|
| 131 | /** Signature. */
|
---|
| 132 | uint16_t signature;
|
---|
[ed903174] | 133 | } fat32 __attribute__ ((packed));
|
---|
| 134 | };
|
---|
[263e1ec] | 135 | } __attribute__ ((packed)) fat_bs_t;
|
---|
| 136 |
|
---|
[5e790e6] | 137 | typedef enum {
|
---|
[e1e3b26] | 138 | FAT_INVALID,
|
---|
[5e790e6] | 139 | FAT_DIRECTORY,
|
---|
| 140 | FAT_FILE
|
---|
| 141 | } fat_node_type_t;
|
---|
| 142 |
|
---|
[869e546] | 143 | struct fat_node;
|
---|
| 144 |
|
---|
| 145 | /** FAT index structure.
|
---|
| 146 | *
|
---|
| 147 | * This structure exists to help us to overcome certain limitations of the FAT
|
---|
| 148 | * file system design. The problem with FAT is that it is hard to find
|
---|
| 149 | * an entity which could represent a VFS index. There are two candidates:
|
---|
| 150 | *
|
---|
| 151 | * a) number of the node's first cluster
|
---|
| 152 | * b) the pair of the parent directory's first cluster and the dentry index
|
---|
| 153 | * within the parent directory
|
---|
| 154 | *
|
---|
| 155 | * We need VFS indices to be:
|
---|
| 156 | * A) unique
|
---|
| 157 | * B) stable in time, at least until the next mount
|
---|
| 158 | *
|
---|
| 159 | * Unfortunately a) does not meet the A) criterion because zero-length files
|
---|
| 160 | * will have the first cluster field cleared. And b) does not meet the B)
|
---|
| 161 | * criterion because unlink() and rename() will both free up the original
|
---|
| 162 | * dentry, which contains all the essential info about the file.
|
---|
| 163 | *
|
---|
| 164 | * Therefore, a completely opaque indices are used and the FAT server maintains
|
---|
| 165 | * a mapping between them and otherwise nice b) variant. On rename(), the VFS
|
---|
| 166 | * index stays unaltered, while the internal FAT "physical tree address"
|
---|
| 167 | * changes. The unlink case is also handled this way thanks to an in-core node
|
---|
| 168 | * pointer embedded in the index structure.
|
---|
| 169 | */
|
---|
[5e790e6] | 170 | typedef struct {
|
---|
[4797132] | 171 | /** Used indices (position) hash table link. */
|
---|
| 172 | link_t uph_link;
|
---|
| 173 | /** Used indices (index) hash table link. */
|
---|
| 174 | link_t uih_link;
|
---|
[9a5ccfb3] | 175 |
|
---|
[6ebe721] | 176 | fibril_mutex_t lock;
|
---|
[991f645] | 177 | devmap_handle_t devmap_handle;
|
---|
[869e546] | 178 | fs_index_t index;
|
---|
| 179 | /**
|
---|
[9a5ccfb3] | 180 | * Parent node's first cluster.
|
---|
[869e546] | 181 | * Zero is used if this node is not linked, in which case nodep must
|
---|
| 182 | * contain a pointer to the in-core node structure.
|
---|
| 183 | * One is used when the parent is the root directory.
|
---|
| 184 | */
|
---|
| 185 | fat_cluster_t pfc;
|
---|
[9a5ccfb3] | 186 | /** Directory entry index within the parent node. */
|
---|
[869e546] | 187 | unsigned pdi;
|
---|
| 188 | /** Pointer to in-core node instance. */
|
---|
| 189 | struct fat_node *nodep;
|
---|
| 190 | } fat_idx_t;
|
---|
| 191 |
|
---|
| 192 | /** FAT in-core node. */
|
---|
| 193 | typedef struct fat_node {
|
---|
[b6035ba] | 194 | /** Back pointer to the FS node. */
|
---|
| 195 | fs_node_t *bp;
|
---|
| 196 |
|
---|
[6ebe721] | 197 | fibril_mutex_t lock;
|
---|
[5e790e6] | 198 | fat_node_type_t type;
|
---|
[869e546] | 199 | fat_idx_t *idx;
|
---|
| 200 | /**
|
---|
| 201 | * Node's first cluster.
|
---|
| 202 | * Zero is used for zero-length nodes.
|
---|
| 203 | * One is used to mark root directory.
|
---|
| 204 | */
|
---|
| 205 | fat_cluster_t firstc;
|
---|
[e1e3b26] | 206 | /** FAT in-core node free list link. */
|
---|
| 207 | link_t ffn_link;
|
---|
[ed903174] | 208 | aoff64_t size;
|
---|
[5e790e6] | 209 | unsigned lnkcnt;
|
---|
[e1e3b26] | 210 | unsigned refcnt;
|
---|
| 211 | bool dirty;
|
---|
[377cce8] | 212 |
|
---|
| 213 | /*
|
---|
[dba4a23] | 214 | * Cache of the node's last and "current" cluster to avoid some
|
---|
| 215 | * unnecessary FAT walks.
|
---|
[377cce8] | 216 | */
|
---|
[dba4a23] | 217 | /* Node's last cluster in FAT. */
|
---|
| 218 | bool lastc_cached_valid;
|
---|
| 219 | fat_cluster_t lastc_cached_value;
|
---|
| 220 | /* Node's "current" cluster, i.e. where the last I/O took place. */
|
---|
| 221 | bool currc_cached_valid;
|
---|
| 222 | aoff64_t currc_cached_bn;
|
---|
| 223 | fat_cluster_t currc_cached_value;
|
---|
[5e790e6] | 224 | } fat_node_t;
|
---|
| 225 |
|
---|
[efd4a72] | 226 | extern fs_reg_t fat_reg;
|
---|
[be815bc] | 227 |
|
---|
[cde485d] | 228 | extern void fat_mounted(ipc_callid_t, ipc_call_t *);
|
---|
| 229 | extern void fat_mount(ipc_callid_t, ipc_call_t *);
|
---|
[3c11713] | 230 | extern void fat_unmounted(ipc_callid_t, ipc_call_t *);
|
---|
| 231 | extern void fat_unmount(ipc_callid_t, ipc_call_t *);
|
---|
[6364d3c] | 232 | extern void fat_lookup(ipc_callid_t, ipc_call_t *);
|
---|
[4bf40f6] | 233 | extern void fat_read(ipc_callid_t, ipc_call_t *);
|
---|
[c947dda] | 234 | extern void fat_write(ipc_callid_t, ipc_call_t *);
|
---|
[6c71a1f] | 235 | extern void fat_truncate(ipc_callid_t, ipc_call_t *);
|
---|
[852b801] | 236 | extern void fat_stat(ipc_callid_t, ipc_call_t *);
|
---|
[c20aa06] | 237 | extern void fat_close(ipc_callid_t, ipc_call_t *);
|
---|
[50e5b25] | 238 | extern void fat_destroy(ipc_callid_t, ipc_call_t *);
|
---|
[c20aa06] | 239 | extern void fat_open_node(ipc_callid_t, ipc_call_t *);
|
---|
[852b801] | 240 | extern void fat_stat(ipc_callid_t, ipc_call_t *);
|
---|
[c20aa06] | 241 | extern void fat_sync(ipc_callid_t, ipc_call_t *);
|
---|
[6364d3c] | 242 |
|
---|
[991f645] | 243 | extern int fat_idx_get_new(fat_idx_t **, devmap_handle_t);
|
---|
| 244 | extern fat_idx_t *fat_idx_get_by_pos(devmap_handle_t, fat_cluster_t, unsigned);
|
---|
| 245 | extern fat_idx_t *fat_idx_get_by_index(devmap_handle_t, fs_index_t);
|
---|
[48eb7a14] | 246 | extern void fat_idx_destroy(fat_idx_t *);
|
---|
[0fdd6bb] | 247 | extern void fat_idx_hashin(fat_idx_t *);
|
---|
[a31c1ccf] | 248 | extern void fat_idx_hashout(fat_idx_t *);
|
---|
[297f1197] | 249 |
|
---|
[cde485d] | 250 | extern int fat_idx_init(void);
|
---|
| 251 | extern void fat_idx_fini(void);
|
---|
[991f645] | 252 | extern int fat_idx_init_by_devmap_handle(devmap_handle_t);
|
---|
| 253 | extern void fat_idx_fini_by_devmap_handle(devmap_handle_t);
|
---|
[cde485d] | 254 |
|
---|
[be815bc] | 255 | #endif
|
---|
| 256 |
|
---|
| 257 | /**
|
---|
| 258 | * @}
|
---|
| 259 | */
|
---|