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