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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ed19497 was 875edff6, checked in by Oleg Romanenko <romanenko.oleg@…>, 14 years ago

It is possible to mount FAT32 file system.
Changes:

  1. 32bit fat_cluster_t
  2. Change macros SF(bs) to add support FAT32 sectors per fat value
  3. New macros (FAT_CLST_SIZE) for determining size of FAT cluster (2 or 4 bytes)
  4. New macros (FAT_MASK) for determining necessary mask for cluster value depending on FAT type
  5. fat_get_cluster support FAT32 and correctly return cluster value
  6. Change fat_sanity_check() to add support FAT32. Wrapping explicit use of br→(value) with macros.

TODO need to wrap all explicit use of br→() with appropriated macros.

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