1 | /*
|
---|
2 | * Copyright (c) 2008 Jakub Jermar
|
---|
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 "fat_fat.h"
|
---|
37 | #include <fibril_synch.h>
|
---|
38 | #include <libfs.h>
|
---|
39 | #include <atomic.h>
|
---|
40 | #include <sys/types.h>
|
---|
41 | #include <bool.h>
|
---|
42 | #include "../../vfs/vfs.h"
|
---|
43 |
|
---|
44 | #ifndef dprintf
|
---|
45 | #define dprintf(...) printf(__VA_ARGS__)
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | #define min(a, b) ((a) < (b) ? (a) : (b))
|
---|
49 |
|
---|
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 |
|
---|
63 | #define BS_BLOCK 0
|
---|
64 | #define BS_SIZE 512
|
---|
65 |
|
---|
66 | typedef struct fat_bs {
|
---|
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. */
|
---|
72 | uint16_t rscnt; /**< Reserved sector count. */
|
---|
73 | uint8_t fatcnt; /**< Number of FATs. */
|
---|
74 | uint16_t root_ent_max; /**< Maximum number of root directory
|
---|
75 | entries. */
|
---|
76 | uint16_t totsec16; /**< Total sectors. 16-bit version. */
|
---|
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. */
|
---|
82 | uint32_t totsec32; /**< Total sectors. 32-bit version. */
|
---|
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));
|
---|
103 | struct {
|
---|
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. */
|
---|
126 | uint8_t label[11];
|
---|
127 | /** FAT type. */
|
---|
128 | uint8_t type[8];
|
---|
129 | /** Boot code. */
|
---|
130 | uint8_t boot_code[420];
|
---|
131 | /** Signature. */
|
---|
132 | uint16_t signature;
|
---|
133 | } fat32 __attribute__ ((packed));
|
---|
134 | };
|
---|
135 | } __attribute__ ((packed)) fat_bs_t;
|
---|
136 |
|
---|
137 | typedef enum {
|
---|
138 | FAT_INVALID,
|
---|
139 | FAT_DIRECTORY,
|
---|
140 | FAT_FILE
|
---|
141 | } fat_node_type_t;
|
---|
142 |
|
---|
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 | */
|
---|
170 | typedef struct {
|
---|
171 | /** Used indices (position) hash table link. */
|
---|
172 | link_t uph_link;
|
---|
173 | /** Used indices (index) hash table link. */
|
---|
174 | link_t uih_link;
|
---|
175 |
|
---|
176 | fibril_mutex_t lock;
|
---|
177 | devmap_handle_t devmap_handle;
|
---|
178 | fs_index_t index;
|
---|
179 | /**
|
---|
180 | * Parent node's first cluster.
|
---|
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;
|
---|
186 | /** Directory entry index within the parent node. */
|
---|
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 {
|
---|
194 | /** Back pointer to the FS node. */
|
---|
195 | fs_node_t *bp;
|
---|
196 |
|
---|
197 | fibril_mutex_t lock;
|
---|
198 | fat_node_type_t type;
|
---|
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;
|
---|
206 | /** FAT in-core node free list link. */
|
---|
207 | link_t ffn_link;
|
---|
208 | aoff64_t size;
|
---|
209 | unsigned lnkcnt;
|
---|
210 | unsigned refcnt;
|
---|
211 | bool dirty;
|
---|
212 |
|
---|
213 | /*
|
---|
214 | * Cache of the node's last and "current" cluster to avoid some
|
---|
215 | * unnecessary FAT walks.
|
---|
216 | */
|
---|
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;
|
---|
224 | } fat_node_t;
|
---|
225 |
|
---|
226 | extern vfs_out_ops_t fat_ops;
|
---|
227 | extern libfs_ops_t fat_libfs_ops;
|
---|
228 |
|
---|
229 | extern int fat_idx_get_new(fat_idx_t **, devmap_handle_t);
|
---|
230 | extern fat_idx_t *fat_idx_get_by_pos(devmap_handle_t, fat_cluster_t, unsigned);
|
---|
231 | extern fat_idx_t *fat_idx_get_by_index(devmap_handle_t, fs_index_t);
|
---|
232 | extern void fat_idx_destroy(fat_idx_t *);
|
---|
233 | extern void fat_idx_hashin(fat_idx_t *);
|
---|
234 | extern void fat_idx_hashout(fat_idx_t *);
|
---|
235 |
|
---|
236 | extern int fat_idx_init(void);
|
---|
237 | extern void fat_idx_fini(void);
|
---|
238 | extern int fat_idx_init_by_devmap_handle(devmap_handle_t);
|
---|
239 | extern void fat_idx_fini_by_devmap_handle(devmap_handle_t);
|
---|
240 |
|
---|
241 | #endif
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * @}
|
---|
245 | */
|
---|