[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"
|
---|
[be815bc] | 37 | #include <ipc/ipc.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 |
|
---|
| 50 | #define BS_BLOCK 0
|
---|
| 51 | #define BS_SIZE 512
|
---|
| 52 |
|
---|
[5af627fc] | 53 | typedef struct {
|
---|
| 54 | uint8_t ji[3]; /**< Jump instruction. */
|
---|
| 55 | uint8_t oem_name[8];
|
---|
| 56 | /* BIOS Parameter Block */
|
---|
| 57 | uint16_t bps; /**< Bytes per sector. */
|
---|
| 58 | uint8_t spc; /**< Sectors per cluster. */
|
---|
[5e790e6] | 59 | uint16_t rscnt; /**< Reserved sector count. */
|
---|
[5af627fc] | 60 | uint8_t fatcnt; /**< Number of FATs. */
|
---|
| 61 | uint16_t root_ent_max; /**< Maximum number of root directory
|
---|
| 62 | entries. */
|
---|
[5e790e6] | 63 | uint16_t totsec16; /**< Total sectors. 16-bit version. */
|
---|
[5af627fc] | 64 | uint8_t mdesc; /**< Media descriptor. */
|
---|
| 65 | uint16_t sec_per_fat; /**< Sectors per FAT12/FAT16. */
|
---|
| 66 | uint16_t sec_per_track; /**< Sectors per track. */
|
---|
| 67 | uint16_t headcnt; /**< Number of heads. */
|
---|
| 68 | uint32_t hidden_sec; /**< Hidden sectors. */
|
---|
[e1e3b26] | 69 | uint32_t totsec32; /**< Total sectors. 32-bit version. */
|
---|
[5af627fc] | 70 |
|
---|
| 71 | union {
|
---|
| 72 | struct {
|
---|
| 73 | /* FAT12/FAT16 only: Extended BIOS Parameter Block */
|
---|
| 74 | /** Physical drive number. */
|
---|
| 75 | uint8_t pdn;
|
---|
| 76 | uint8_t reserved;
|
---|
| 77 | /** Extended boot signature. */
|
---|
| 78 | uint8_t ebs;
|
---|
| 79 | /** Serial number. */
|
---|
| 80 | uint32_t id;
|
---|
| 81 | /** Volume label. */
|
---|
| 82 | uint8_t label[11];
|
---|
| 83 | /** FAT type. */
|
---|
| 84 | uint8_t type[8];
|
---|
| 85 | /** Boot code. */
|
---|
| 86 | uint8_t boot_code[448];
|
---|
| 87 | /** Boot sector signature. */
|
---|
| 88 | uint16_t signature;
|
---|
| 89 | } __attribute__ ((packed));
|
---|
| 90 | struct {
|
---|
| 91 | /* FAT32 only */
|
---|
| 92 | /** Sectors per FAT. */
|
---|
| 93 | uint32_t sectors_per_fat;
|
---|
| 94 | /** FAT flags. */
|
---|
| 95 | uint16_t flags;
|
---|
| 96 | /** Version. */
|
---|
| 97 | uint16_t version;
|
---|
| 98 | /** Cluster number of root directory. */
|
---|
| 99 | uint32_t root_cluster;
|
---|
| 100 | /** Sector number of file system information sector. */
|
---|
| 101 | uint16_t fsinfo_sec;
|
---|
| 102 | /** Sector number of boot sector copy. */
|
---|
| 103 | uint16_t bscopy_sec;
|
---|
| 104 | uint8_t reserved1[12];
|
---|
| 105 | /** Physical drive number. */
|
---|
| 106 | uint8_t pdn;
|
---|
| 107 | uint8_t reserved2;
|
---|
| 108 | /** Extended boot signature. */
|
---|
| 109 | uint8_t ebs;
|
---|
| 110 | /** Serial number. */
|
---|
| 111 | uint32_t id;
|
---|
| 112 | /** Volume label. */
|
---|
[5e790e6] | 113 | uint8_t label[11];
|
---|
[5af627fc] | 114 | /** FAT type. */
|
---|
| 115 | uint8_t type[8];
|
---|
| 116 | /** Boot code. */
|
---|
| 117 | uint8_t boot_code[420];
|
---|
| 118 | /** Signature. */
|
---|
| 119 | uint16_t signature;
|
---|
| 120 | } __attribute__ ((packed));
|
---|
| 121 | };
|
---|
[263e1ec] | 122 | } __attribute__ ((packed)) fat_bs_t;
|
---|
| 123 |
|
---|
[0f57d0e] | 124 | #define FAT_BS(b) ((fat_bs_t *)((b)->data))
|
---|
[869e546] | 125 |
|
---|
[5e790e6] | 126 | typedef enum {
|
---|
[e1e3b26] | 127 | FAT_INVALID,
|
---|
[5e790e6] | 128 | FAT_DIRECTORY,
|
---|
| 129 | FAT_FILE
|
---|
| 130 | } fat_node_type_t;
|
---|
| 131 |
|
---|
[869e546] | 132 | struct fat_node;
|
---|
| 133 |
|
---|
| 134 | /** FAT index structure.
|
---|
| 135 | *
|
---|
| 136 | * This structure exists to help us to overcome certain limitations of the FAT
|
---|
| 137 | * file system design. The problem with FAT is that it is hard to find
|
---|
| 138 | * an entity which could represent a VFS index. There are two candidates:
|
---|
| 139 | *
|
---|
| 140 | * a) number of the node's first cluster
|
---|
| 141 | * b) the pair of the parent directory's first cluster and the dentry index
|
---|
| 142 | * within the parent directory
|
---|
| 143 | *
|
---|
| 144 | * We need VFS indices to be:
|
---|
| 145 | * A) unique
|
---|
| 146 | * B) stable in time, at least until the next mount
|
---|
| 147 | *
|
---|
| 148 | * Unfortunately a) does not meet the A) criterion because zero-length files
|
---|
| 149 | * will have the first cluster field cleared. And b) does not meet the B)
|
---|
| 150 | * criterion because unlink() and rename() will both free up the original
|
---|
| 151 | * dentry, which contains all the essential info about the file.
|
---|
| 152 | *
|
---|
| 153 | * Therefore, a completely opaque indices are used and the FAT server maintains
|
---|
| 154 | * a mapping between them and otherwise nice b) variant. On rename(), the VFS
|
---|
| 155 | * index stays unaltered, while the internal FAT "physical tree address"
|
---|
| 156 | * changes. The unlink case is also handled this way thanks to an in-core node
|
---|
| 157 | * pointer embedded in the index structure.
|
---|
| 158 | */
|
---|
[5e790e6] | 159 | typedef struct {
|
---|
[4797132] | 160 | /** Used indices (position) hash table link. */
|
---|
| 161 | link_t uph_link;
|
---|
| 162 | /** Used indices (index) hash table link. */
|
---|
| 163 | link_t uih_link;
|
---|
[9a5ccfb3] | 164 |
|
---|
[add5835] | 165 | futex_t lock;
|
---|
[869e546] | 166 | dev_handle_t dev_handle;
|
---|
| 167 | fs_index_t index;
|
---|
| 168 | /**
|
---|
[9a5ccfb3] | 169 | * Parent node's first cluster.
|
---|
[869e546] | 170 | * Zero is used if this node is not linked, in which case nodep must
|
---|
| 171 | * contain a pointer to the in-core node structure.
|
---|
| 172 | * One is used when the parent is the root directory.
|
---|
| 173 | */
|
---|
| 174 | fat_cluster_t pfc;
|
---|
[9a5ccfb3] | 175 | /** Directory entry index within the parent node. */
|
---|
[869e546] | 176 | unsigned pdi;
|
---|
| 177 | /** Pointer to in-core node instance. */
|
---|
| 178 | struct fat_node *nodep;
|
---|
| 179 | } fat_idx_t;
|
---|
| 180 |
|
---|
| 181 | /** FAT in-core node. */
|
---|
| 182 | typedef struct fat_node {
|
---|
[add5835] | 183 | futex_t lock;
|
---|
[5e790e6] | 184 | fat_node_type_t type;
|
---|
[869e546] | 185 | fat_idx_t *idx;
|
---|
| 186 | /**
|
---|
| 187 | * Node's first cluster.
|
---|
| 188 | * Zero is used for zero-length nodes.
|
---|
| 189 | * One is used to mark root directory.
|
---|
| 190 | */
|
---|
| 191 | fat_cluster_t firstc;
|
---|
[e1e3b26] | 192 | /** FAT in-core node free list link. */
|
---|
| 193 | link_t ffn_link;
|
---|
[5e790e6] | 194 | size_t size;
|
---|
| 195 | unsigned lnkcnt;
|
---|
[e1e3b26] | 196 | unsigned refcnt;
|
---|
| 197 | bool dirty;
|
---|
[5e790e6] | 198 | } fat_node_t;
|
---|
| 199 |
|
---|
[0f57d0e] | 200 | /* TODO move somewhere else */
|
---|
| 201 | typedef struct block {
|
---|
| 202 | void *data;
|
---|
| 203 | size_t size;
|
---|
| 204 | bool dirty;
|
---|
| 205 | } block_t;
|
---|
| 206 |
|
---|
| 207 | extern block_t *block_get(dev_handle_t, off_t, size_t);
|
---|
| 208 | extern void block_put(block_t *);
|
---|
| 209 |
|
---|
[efd4a72] | 210 | extern fs_reg_t fat_reg;
|
---|
[be815bc] | 211 |
|
---|
[cde485d] | 212 | extern void fat_mounted(ipc_callid_t, ipc_call_t *);
|
---|
| 213 | extern void fat_mount(ipc_callid_t, ipc_call_t *);
|
---|
[6364d3c] | 214 | extern void fat_lookup(ipc_callid_t, ipc_call_t *);
|
---|
[4bf40f6] | 215 | extern void fat_read(ipc_callid_t, ipc_call_t *);
|
---|
[c947dda] | 216 | extern void fat_write(ipc_callid_t, ipc_call_t *);
|
---|
[6364d3c] | 217 |
|
---|
[4797132] | 218 | extern fat_idx_t *fat_idx_get_by_pos(dev_handle_t, fat_cluster_t, unsigned);
|
---|
| 219 | extern fat_idx_t *fat_idx_get_by_index(dev_handle_t, fs_index_t);
|
---|
[297f1197] | 220 |
|
---|
[cde485d] | 221 | extern int fat_idx_init(void);
|
---|
| 222 | extern void fat_idx_fini(void);
|
---|
| 223 | extern int fat_idx_init_by_dev_handle(dev_handle_t);
|
---|
| 224 | extern void fat_idx_fini_by_dev_handle(dev_handle_t);
|
---|
| 225 |
|
---|
[be815bc] | 226 | #endif
|
---|
| 227 |
|
---|
| 228 | /**
|
---|
| 229 | * @}
|
---|
| 230 | */
|
---|