source: mainline/uspace/srv/fs/fat/fat.h@ b12ca16

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b12ca16 was b69e4c0, checked in by Jakub Jermar <jakub@…>, 14 years ago

Update the FAT32 file system info on unmount.

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