source: mainline/uspace/srv/fs/fat/fat.h@ 64d2b10

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

Rename dev_handle_t to devmap_handle_t and make it explicitly clear that
dev_handle_t is a handle understood by devmap.

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