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