[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 |
|
---|
| 30 | /** @addtogroup fs
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 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>
|
---|
[be815bc] | 40 | #include <atomic.h>
|
---|
| 41 | #include <sys/types.h>
|
---|
| 42 | #include <bool.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 |
|
---|
[0f57d0e] | 49 | #define min(a, b) ((a) < (b) ? (a) : (b))
|
---|
| 50 |
|
---|
[7a23d60] | 51 | /*
|
---|
| 52 | * Convenience macros for accessing some frequently used boot sector members.
|
---|
| 53 | */
|
---|
| 54 | #define BPS(bs) uint16_t_le2host((bs)->bps)
|
---|
| 55 | #define SPC(bs) (bs)->spc
|
---|
| 56 | #define RSCNT(bs) uint16_t_le2host((bs)->rscnt)
|
---|
| 57 | #define FATCNT(bs) (bs)->fatcnt
|
---|
[875edff6] | 58 | #define SF(bs) (uint16_t_le2host((bs)->sec_per_fat) !=0 ? \
|
---|
| 59 | uint16_t_le2host((bs)->sec_per_fat) : \
|
---|
| 60 | uint32_t_le2host(bs->fat32.sectors_per_fat))
|
---|
[7a23d60] | 61 | #define RDE(bs) uint16_t_le2host((bs)->root_ent_max)
|
---|
| 62 | #define TS(bs) (uint16_t_le2host((bs)->totsec16) != 0 ? \
|
---|
| 63 | uint16_t_le2host((bs)->totsec16) : \
|
---|
| 64 | uint32_t_le2host(bs->totsec32))
|
---|
| 65 |
|
---|
[0f57d0e] | 66 | #define BS_BLOCK 0
|
---|
| 67 | #define BS_SIZE 512
|
---|
| 68 |
|
---|
[cb682eb] | 69 | typedef struct fat_bs {
|
---|
[5af627fc] | 70 | uint8_t ji[3]; /**< Jump instruction. */
|
---|
| 71 | uint8_t oem_name[8];
|
---|
| 72 | /* BIOS Parameter Block */
|
---|
| 73 | uint16_t bps; /**< Bytes per sector. */
|
---|
| 74 | uint8_t spc; /**< Sectors per cluster. */
|
---|
[5e790e6] | 75 | uint16_t rscnt; /**< Reserved sector count. */
|
---|
[5af627fc] | 76 | uint8_t fatcnt; /**< Number of FATs. */
|
---|
| 77 | uint16_t root_ent_max; /**< Maximum number of root directory
|
---|
| 78 | entries. */
|
---|
[5e790e6] | 79 | uint16_t totsec16; /**< Total sectors. 16-bit version. */
|
---|
[5af627fc] | 80 | uint8_t mdesc; /**< Media descriptor. */
|
---|
| 81 | uint16_t sec_per_fat; /**< Sectors per FAT12/FAT16. */
|
---|
| 82 | uint16_t sec_per_track; /**< Sectors per track. */
|
---|
| 83 | uint16_t headcnt; /**< Number of heads. */
|
---|
| 84 | uint32_t hidden_sec; /**< Hidden sectors. */
|
---|
[e1e3b26] | 85 | uint32_t totsec32; /**< Total sectors. 32-bit version. */
|
---|
[5af627fc] | 86 |
|
---|
| 87 | union {
|
---|
| 88 | struct {
|
---|
| 89 | /* FAT12/FAT16 only: Extended BIOS Parameter Block */
|
---|
| 90 | /** Physical drive number. */
|
---|
| 91 | uint8_t pdn;
|
---|
| 92 | uint8_t reserved;
|
---|
| 93 | /** Extended boot signature. */
|
---|
| 94 | uint8_t ebs;
|
---|
| 95 | /** Serial number. */
|
---|
| 96 | uint32_t id;
|
---|
| 97 | /** Volume label. */
|
---|
| 98 | uint8_t label[11];
|
---|
| 99 | /** FAT type. */
|
---|
| 100 | uint8_t type[8];
|
---|
| 101 | /** Boot code. */
|
---|
| 102 | uint8_t boot_code[448];
|
---|
| 103 | /** Boot sector signature. */
|
---|
| 104 | uint16_t signature;
|
---|
| 105 | } __attribute__ ((packed));
|
---|
[ed903174] | 106 | struct {
|
---|
[5af627fc] | 107 | /* FAT32 only */
|
---|
| 108 | /** Sectors per FAT. */
|
---|
| 109 | uint32_t sectors_per_fat;
|
---|
| 110 | /** FAT flags. */
|
---|
| 111 | uint16_t flags;
|
---|
| 112 | /** Version. */
|
---|
| 113 | uint16_t version;
|
---|
| 114 | /** Cluster number of root directory. */
|
---|
| 115 | uint32_t root_cluster;
|
---|
| 116 | /** Sector number of file system information sector. */
|
---|
| 117 | uint16_t fsinfo_sec;
|
---|
| 118 | /** Sector number of boot sector copy. */
|
---|
| 119 | uint16_t bscopy_sec;
|
---|
| 120 | uint8_t reserved1[12];
|
---|
| 121 | /** Physical drive number. */
|
---|
| 122 | uint8_t pdn;
|
---|
| 123 | uint8_t reserved2;
|
---|
| 124 | /** Extended boot signature. */
|
---|
| 125 | uint8_t ebs;
|
---|
| 126 | /** Serial number. */
|
---|
| 127 | uint32_t id;
|
---|
| 128 | /** Volume label. */
|
---|
[5e790e6] | 129 | uint8_t label[11];
|
---|
[5af627fc] | 130 | /** FAT type. */
|
---|
| 131 | uint8_t type[8];
|
---|
| 132 | /** Boot code. */
|
---|
| 133 | uint8_t boot_code[420];
|
---|
| 134 | /** Signature. */
|
---|
| 135 | uint16_t signature;
|
---|
[ed903174] | 136 | } fat32 __attribute__ ((packed));
|
---|
| 137 | };
|
---|
[263e1ec] | 138 | } __attribute__ ((packed)) fat_bs_t;
|
---|
| 139 |
|
---|
[5e790e6] | 140 | typedef enum {
|
---|
[e1e3b26] | 141 | FAT_INVALID,
|
---|
[5e790e6] | 142 | FAT_DIRECTORY,
|
---|
| 143 | FAT_FILE
|
---|
| 144 | } fat_node_type_t;
|
---|
| 145 |
|
---|
[869e546] | 146 | struct fat_node;
|
---|
| 147 |
|
---|
| 148 | /** FAT index structure.
|
---|
| 149 | *
|
---|
| 150 | * This structure exists to help us to overcome certain limitations of the FAT
|
---|
| 151 | * file system design. The problem with FAT is that it is hard to find
|
---|
| 152 | * an entity which could represent a VFS index. There are two candidates:
|
---|
| 153 | *
|
---|
| 154 | * a) number of the node's first cluster
|
---|
| 155 | * b) the pair of the parent directory's first cluster and the dentry index
|
---|
| 156 | * within the parent directory
|
---|
| 157 | *
|
---|
| 158 | * We need VFS indices to be:
|
---|
| 159 | * A) unique
|
---|
| 160 | * B) stable in time, at least until the next mount
|
---|
| 161 | *
|
---|
| 162 | * Unfortunately a) does not meet the A) criterion because zero-length files
|
---|
| 163 | * will have the first cluster field cleared. And b) does not meet the B)
|
---|
| 164 | * criterion because unlink() and rename() will both free up the original
|
---|
| 165 | * dentry, which contains all the essential info about the file.
|
---|
| 166 | *
|
---|
| 167 | * Therefore, a completely opaque indices are used and the FAT server maintains
|
---|
| 168 | * a mapping between them and otherwise nice b) variant. On rename(), the VFS
|
---|
| 169 | * index stays unaltered, while the internal FAT "physical tree address"
|
---|
| 170 | * changes. The unlink case is also handled this way thanks to an in-core node
|
---|
| 171 | * pointer embedded in the index structure.
|
---|
| 172 | */
|
---|
[5e790e6] | 173 | typedef struct {
|
---|
[4797132] | 174 | /** Used indices (position) hash table link. */
|
---|
| 175 | link_t uph_link;
|
---|
| 176 | /** Used indices (index) hash table link. */
|
---|
| 177 | link_t uih_link;
|
---|
[9a5ccfb3] | 178 |
|
---|
[6ebe721] | 179 | fibril_mutex_t lock;
|
---|
[15f3c3f] | 180 | service_id_t service_id;
|
---|
[869e546] | 181 | fs_index_t index;
|
---|
| 182 | /**
|
---|
[9a5ccfb3] | 183 | * Parent node's first cluster.
|
---|
[869e546] | 184 | * Zero is used if this node is not linked, in which case nodep must
|
---|
| 185 | * contain a pointer to the in-core node structure.
|
---|
| 186 | * One is used when the parent is the root directory.
|
---|
| 187 | */
|
---|
| 188 | fat_cluster_t pfc;
|
---|
[9a5ccfb3] | 189 | /** Directory entry index within the parent node. */
|
---|
[869e546] | 190 | unsigned pdi;
|
---|
| 191 | /** Pointer to in-core node instance. */
|
---|
| 192 | struct fat_node *nodep;
|
---|
| 193 | } fat_idx_t;
|
---|
| 194 |
|
---|
| 195 | /** FAT in-core node. */
|
---|
| 196 | typedef struct fat_node {
|
---|
[b6035ba] | 197 | /** Back pointer to the FS node. */
|
---|
| 198 | fs_node_t *bp;
|
---|
| 199 |
|
---|
[6ebe721] | 200 | fibril_mutex_t lock;
|
---|
[5e790e6] | 201 | fat_node_type_t type;
|
---|
[869e546] | 202 | fat_idx_t *idx;
|
---|
| 203 | /**
|
---|
| 204 | * Node's first cluster.
|
---|
| 205 | * Zero is used for zero-length nodes.
|
---|
| 206 | * One is used to mark root directory.
|
---|
| 207 | */
|
---|
| 208 | fat_cluster_t firstc;
|
---|
[e1e3b26] | 209 | /** FAT in-core node free list link. */
|
---|
| 210 | link_t ffn_link;
|
---|
[ed903174] | 211 | aoff64_t size;
|
---|
[5e790e6] | 212 | unsigned lnkcnt;
|
---|
[e1e3b26] | 213 | unsigned refcnt;
|
---|
| 214 | bool dirty;
|
---|
[377cce8] | 215 |
|
---|
| 216 | /*
|
---|
[dba4a23] | 217 | * Cache of the node's last and "current" cluster to avoid some
|
---|
| 218 | * unnecessary FAT walks.
|
---|
[377cce8] | 219 | */
|
---|
[dba4a23] | 220 | /* Node's last cluster in FAT. */
|
---|
| 221 | bool lastc_cached_valid;
|
---|
| 222 | fat_cluster_t lastc_cached_value;
|
---|
| 223 | /* Node's "current" cluster, i.e. where the last I/O took place. */
|
---|
| 224 | bool currc_cached_valid;
|
---|
| 225 | aoff64_t currc_cached_bn;
|
---|
| 226 | fat_cluster_t currc_cached_value;
|
---|
[5e790e6] | 227 | } fat_node_t;
|
---|
| 228 |
|
---|
[efcebe1] | 229 | extern vfs_out_ops_t fat_ops;
|
---|
| 230 | extern libfs_ops_t fat_libfs_ops;
|
---|
[6364d3c] | 231 |
|
---|
[15f3c3f] | 232 | extern int fat_idx_get_new(fat_idx_t **, service_id_t);
|
---|
| 233 | extern fat_idx_t *fat_idx_get_by_pos(service_id_t, fat_cluster_t, unsigned);
|
---|
| 234 | extern fat_idx_t *fat_idx_get_by_index(service_id_t, fs_index_t);
|
---|
[48eb7a14] | 235 | extern void fat_idx_destroy(fat_idx_t *);
|
---|
[0fdd6bb] | 236 | extern void fat_idx_hashin(fat_idx_t *);
|
---|
[a31c1ccf] | 237 | extern void fat_idx_hashout(fat_idx_t *);
|
---|
[297f1197] | 238 |
|
---|
[cde485d] | 239 | extern int fat_idx_init(void);
|
---|
| 240 | extern void fat_idx_fini(void);
|
---|
[15f3c3f] | 241 | extern int fat_idx_init_by_service_id(service_id_t);
|
---|
| 242 | extern void fat_idx_fini_by_service_id(service_id_t);
|
---|
[cde485d] | 243 |
|
---|
[be815bc] | 244 | #endif
|
---|
| 245 |
|
---|
| 246 | /**
|
---|
| 247 | * @}
|
---|
| 248 | */
|
---|