source: mainline/uspace/srv/fs/fat/fat.h@ 0ceeac3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0ceeac3 was 904b1bc, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Fix remaining ccheck issues.

  • Property mode set to 100644
File size: 7.9 KB
Line 
1/*
2 * Copyright (c) 2008 Jakub Jermar
3 * Copyright (c) 2011 Oleg Romanenko
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
37#include "fat_fat.h"
38#include <fibril_synch.h>
39#include <libfs.h>
40#include <atomic.h>
41#include <stdint.h>
42#include <stdbool.h>
43#include <macros.h>
44#include "../../vfs/vfs.h"
45
46#ifndef dprintf
47#define dprintf(...) printf(__VA_ARGS__)
48#endif
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
58#define SF(bs) (uint16_t_le2host((bs)->sec_per_fat) ? \
59 uint16_t_le2host((bs)->sec_per_fat) : \
60 uint32_t_le2host(bs->fat32.sectors_per_fat))
61
62#define RDE(bs) uint16_t_le2host((bs)->root_ent_max)
63
64#define TS(bs) (uint16_t_le2host((bs)->totsec16) ? \
65 uint16_t_le2host((bs)->totsec16) : \
66 uint32_t_le2host(bs->totsec32))
67
68#define BS_BLOCK 0
69#define BS_SIZE 512
70
71typedef struct fat_bs {
72 /** Jump instruction */
73 uint8_t ji[3];
74 uint8_t oem_name[8];
75
76 /* BIOS Parameter Block */
77
78 /** Bytes per sector */
79 uint16_t bps;
80 /** Sectors per cluster */
81 uint8_t spc;
82 /** Reserved sector count */
83 uint16_t rscnt;
84 /** Number of FATs */
85 uint8_t fatcnt;
86 /** Maximum number of root directory entries */
87 uint16_t root_ent_max;
88 /** Total sectors. 16-bit version */
89 uint16_t totsec16;
90 /** Media descriptor */
91 uint8_t mdesc;
92 /** Sectors per FAT12/FAT16 */
93 uint16_t sec_per_fat;
94 /** Sectors per track */
95 uint16_t sec_per_track;
96 /** Number of heads */
97 uint16_t headcnt;
98 /** Hidden sectors */
99 uint32_t hidden_sec;
100 /** Total sectors. 32-bit version */
101 uint32_t totsec32;
102
103 union {
104 struct {
105 /* FAT12/FAT16 only: Extended BIOS Parameter Block */
106 /** Physical drive number. */
107 uint8_t pdn;
108 uint8_t reserved;
109 /** Extended boot signature. */
110 uint8_t ebs;
111 /** Serial number. */
112 uint32_t id;
113 /** Volume label. */
114 uint8_t label[11];
115 /** FAT type. */
116 uint8_t type[8];
117 /** Boot code. */
118 uint8_t boot_code[448];
119 /** Boot sector signature. */
120 uint16_t signature;
121 } __attribute__((packed));
122 struct {
123 /* FAT32 only */
124 /** Sectors per FAT. */
125 uint32_t sectors_per_fat;
126 /** FAT flags. */
127 uint16_t flags;
128 /** Version. */
129 uint16_t version;
130 /** Cluster number of root directory. */
131 uint32_t root_cluster;
132 /** Sector number of file system information sector. */
133 uint16_t fsinfo_sec;
134 /** Sector number of boot sector copy. */
135 uint16_t bscopy_sec;
136 uint8_t reserved1[12];
137 /** Physical drive number. */
138 uint8_t pdn;
139 uint8_t reserved2;
140 /** Extended boot signature. */
141 uint8_t ebs;
142 /** Serial number. */
143 uint32_t id;
144 /** Volume label. */
145 uint8_t label[11];
146 /** FAT type. */
147 uint8_t type[8];
148 /** Boot code. */
149 uint8_t boot_code[420];
150 /** Signature. */
151 uint16_t signature;
152 } __attribute__((packed)) fat32;
153 };
154} __attribute__((packed)) fat_bs_t;
155
156#define FAT32_FSINFO_SIG1 "RRaA"
157#define FAT32_FSINFO_SIG2 "rrAa"
158#define FAT32_FSINFO_SIG3 "\x00\x00\x55\xaa"
159
160typedef struct {
161 uint8_t sig1[4];
162 uint8_t res1[480];
163 uint8_t sig2[4];
164 uint32_t free_clusters;
165 uint32_t last_allocated_cluster;
166 uint8_t res2[12];
167 uint8_t sig3[4];
168} __attribute__((packed)) fat32_fsinfo_t;
169
170typedef enum {
171 FAT_INVALID,
172 FAT_DIRECTORY,
173 FAT_FILE
174} fat_node_type_t;
175
176struct fat_node;
177
178/** FAT index structure.
179 *
180 * This structure exists to help us to overcome certain limitations of the FAT
181 * file system design. The problem with FAT is that it is hard to find
182 * an entity which could represent a VFS index. There are two candidates:
183 *
184 * a) number of the node's first cluster
185 * b) the pair of the parent directory's first cluster and the dentry index
186 * within the parent directory
187 *
188 * We need VFS indices to be:
189 * A) unique
190 * B) stable in time, at least until the next mount
191 *
192 * Unfortunately a) does not meet the A) criterion because zero-length files
193 * will have the first cluster field cleared. And b) does not meet the B)
194 * criterion because unlink() and rename() will both free up the original
195 * dentry, which contains all the essential info about the file.
196 *
197 * Therefore, a completely opaque indices are used and the FAT server maintains
198 * a mapping between them and otherwise nice b) variant. On rename(), the VFS
199 * index stays unaltered, while the internal FAT "physical tree address"
200 * changes. The unlink case is also handled this way thanks to an in-core node
201 * pointer embedded in the index structure.
202 */
203typedef struct {
204 /** Used indices (position) hash table link. */
205 ht_link_t uph_link;
206 /** Used indices (index) hash table link. */
207 ht_link_t uih_link;
208
209 fibril_mutex_t lock;
210 service_id_t service_id;
211 fs_index_t index;
212 /**
213 * Parent node's first cluster.
214 * Zero is used if this node is not linked, in which case nodep must
215 * contain a pointer to the in-core node structure.
216 * One is used when the parent is the root directory.
217 */
218 fat_cluster_t pfc;
219 /** Directory entry index within the parent node. */
220 unsigned pdi;
221 /** Pointer to in-core node instance. */
222 struct fat_node *nodep;
223} fat_idx_t;
224
225/** FAT in-core node. */
226typedef struct fat_node {
227 /** Back pointer to the FS node. */
228 fs_node_t *bp;
229
230 fibril_mutex_t lock;
231 fat_node_type_t type;
232 fat_idx_t *idx;
233 /**
234 * Node's first cluster.
235 * Zero is used for zero-length nodes.
236 * One is used to mark root directory.
237 */
238 fat_cluster_t firstc;
239 /** FAT in-core node free list link. */
240 link_t ffn_link;
241 aoff64_t size;
242 unsigned lnkcnt;
243 unsigned refcnt;
244 bool dirty;
245
246 /*
247 * Cache of the node's last and "current" cluster to avoid some
248 * unnecessary FAT walks.
249 */
250 /* Node's last cluster in FAT. */
251 bool lastc_cached_valid;
252 fat_cluster_t lastc_cached_value;
253 /* Node's "current" cluster, i.e. where the last I/O took place. */
254 bool currc_cached_valid;
255 aoff64_t currc_cached_bn;
256 fat_cluster_t currc_cached_value;
257} fat_node_t;
258
259typedef struct {
260 bool lfn_enabled;
261} fat_instance_t;
262
263extern vfs_out_ops_t fat_ops;
264extern libfs_ops_t fat_libfs_ops;
265
266extern errno_t fat_idx_get_new(fat_idx_t **, service_id_t);
267extern fat_idx_t *fat_idx_get_by_pos(service_id_t, fat_cluster_t, unsigned);
268extern fat_idx_t *fat_idx_get_by_index(service_id_t, fs_index_t);
269extern void fat_idx_destroy(fat_idx_t *);
270extern void fat_idx_hashin(fat_idx_t *);
271extern void fat_idx_hashout(fat_idx_t *);
272
273extern errno_t fat_idx_init(void);
274extern void fat_idx_fini(void);
275extern errno_t fat_idx_init_by_service_id(service_id_t);
276extern void fat_idx_fini_by_service_id(service_id_t);
277
278#endif
279
280/**
281 * @}
282 */
Note: See TracBrowser for help on using the repository browser.