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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a7e04d16 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
RevLine 
[be815bc]1/*
[5e790e6]2 * Copyright (c) 2008 Jakub Jermar
[be815bc]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
[0f57d0e]36#include "fat_fat.h"
[be815bc]37#include <ipc/ipc.h>
[1e4cada]38#include <fibril_synch.h>
[efd4a72]39#include <libfs.h>
[be815bc]40#include <atomic.h>
41#include <sys/types.h>
42#include <bool.h>
[5e790e6]43#include "../../vfs/vfs.h"
[be815bc]44
[b7b6753]45#ifndef dprintf
[be815bc]46#define dprintf(...) printf(__VA_ARGS__)
[b7b6753]47#endif
[be815bc]48
[0f57d0e]49#define min(a, b) ((a) < (b) ? (a) : (b))
50
[7a23d60]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
[0f57d0e]64#define BS_BLOCK 0
65#define BS_SIZE 512
66
[cb682eb]67typedef struct fat_bs {
[5af627fc]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. */
[5e790e6]73 uint16_t rscnt; /**< Reserved sector count. */
[5af627fc]74 uint8_t fatcnt; /**< Number of FATs. */
75 uint16_t root_ent_max; /**< Maximum number of root directory
76 entries. */
[5e790e6]77 uint16_t totsec16; /**< Total sectors. 16-bit version. */
[5af627fc]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. */
[e1e3b26]83 uint32_t totsec32; /**< Total sectors. 32-bit version. */
[5af627fc]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));
[ed903174]104 struct {
[5af627fc]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. */
[5e790e6]127 uint8_t label[11];
[5af627fc]128 /** FAT type. */
129 uint8_t type[8];
130 /** Boot code. */
131 uint8_t boot_code[420];
132 /** Signature. */
133 uint16_t signature;
[ed903174]134 } fat32 __attribute__ ((packed));
135 };
[263e1ec]136} __attribute__ ((packed)) fat_bs_t;
137
[5e790e6]138typedef enum {
[e1e3b26]139 FAT_INVALID,
[5e790e6]140 FAT_DIRECTORY,
141 FAT_FILE
142} fat_node_type_t;
143
[869e546]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 */
[5e790e6]171typedef struct {
[4797132]172 /** Used indices (position) hash table link. */
173 link_t uph_link;
174 /** Used indices (index) hash table link. */
175 link_t uih_link;
[9a5ccfb3]176
[6ebe721]177 fibril_mutex_t lock;
[991f645]178 devmap_handle_t devmap_handle;
[869e546]179 fs_index_t index;
180 /**
[9a5ccfb3]181 * Parent node's first cluster.
[869e546]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;
[9a5ccfb3]187 /** Directory entry index within the parent node. */
[869e546]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 {
[b6035ba]195 /** Back pointer to the FS node. */
196 fs_node_t *bp;
197
[6ebe721]198 fibril_mutex_t lock;
[5e790e6]199 fat_node_type_t type;
[869e546]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;
[e1e3b26]207 /** FAT in-core node free list link. */
208 link_t ffn_link;
[ed903174]209 aoff64_t size;
[5e790e6]210 unsigned lnkcnt;
[e1e3b26]211 unsigned refcnt;
212 bool dirty;
[377cce8]213
214 /*
[dba4a23]215 * Cache of the node's last and "current" cluster to avoid some
216 * unnecessary FAT walks.
[377cce8]217 */
[dba4a23]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;
[5e790e6]225} fat_node_t;
226
[efd4a72]227extern fs_reg_t fat_reg;
[be815bc]228
[cde485d]229extern void fat_mounted(ipc_callid_t, ipc_call_t *);
230extern void fat_mount(ipc_callid_t, ipc_call_t *);
[3c11713]231extern void fat_unmounted(ipc_callid_t, ipc_call_t *);
232extern void fat_unmount(ipc_callid_t, ipc_call_t *);
[6364d3c]233extern void fat_lookup(ipc_callid_t, ipc_call_t *);
[4bf40f6]234extern void fat_read(ipc_callid_t, ipc_call_t *);
[c947dda]235extern void fat_write(ipc_callid_t, ipc_call_t *);
[6c71a1f]236extern void fat_truncate(ipc_callid_t, ipc_call_t *);
[852b801]237extern void fat_stat(ipc_callid_t, ipc_call_t *);
[c20aa06]238extern void fat_close(ipc_callid_t, ipc_call_t *);
[50e5b25]239extern void fat_destroy(ipc_callid_t, ipc_call_t *);
[c20aa06]240extern void fat_open_node(ipc_callid_t, ipc_call_t *);
[852b801]241extern void fat_stat(ipc_callid_t, ipc_call_t *);
[c20aa06]242extern void fat_sync(ipc_callid_t, ipc_call_t *);
[6364d3c]243
[991f645]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);
[48eb7a14]247extern void fat_idx_destroy(fat_idx_t *);
[0fdd6bb]248extern void fat_idx_hashin(fat_idx_t *);
[a31c1ccf]249extern void fat_idx_hashout(fat_idx_t *);
[297f1197]250
[cde485d]251extern int fat_idx_init(void);
252extern void fat_idx_fini(void);
[991f645]253extern int fat_idx_init_by_devmap_handle(devmap_handle_t);
254extern void fat_idx_fini_by_devmap_handle(devmap_handle_t);
[cde485d]255
[be815bc]256#endif
257
258/**
259 * @}
260 */
Note: See TracBrowser for help on using the repository browser.