Subversion Repositories HelenOS

Rev

Rev 3505 | Rev 3516 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3505 Rev 3506
1
/*
1
/*
2
 * Copyright (c) 2008 Jakub Jermar
2
 * Copyright (c) 2008 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup fs
29
/** @addtogroup fs
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
33
#ifndef FAT_FAT_H_
33
#ifndef FAT_FAT_H_
34
#define FAT_FAT_H_
34
#define FAT_FAT_H_
35
 
35
 
-
 
36
#include "fat_fat.h"
36
#include <ipc/ipc.h>
37
#include <ipc/ipc.h>
37
#include <libfs.h>
38
#include <libfs.h>
38
#include <atomic.h>
39
#include <atomic.h>
39
#include <sys/types.h>
40
#include <sys/types.h>
40
#include <bool.h>
41
#include <bool.h>
41
#include "../../vfs/vfs.h"
42
#include "../../vfs/vfs.h"
42
 
43
 
43
#ifndef dprintf
44
#ifndef dprintf
44
#define dprintf(...)    printf(__VA_ARGS__)
45
#define dprintf(...)    printf(__VA_ARGS__)
45
#endif
46
#endif
46
 
47
 
-
 
48
#define min(a, b)       ((a) < (b) ? (a) : (b))
-
 
49
 
-
 
50
#define BS_BLOCK        0
-
 
51
#define BS_SIZE         512
-
 
52
 
47
typedef struct {
53
typedef struct {
48
    uint8_t     ji[3];      /**< Jump instruction. */
54
    uint8_t     ji[3];      /**< Jump instruction. */
49
    uint8_t     oem_name[8];
55
    uint8_t     oem_name[8];
50
    /* BIOS Parameter Block */
56
    /* BIOS Parameter Block */
51
    uint16_t    bps;        /**< Bytes per sector. */
57
    uint16_t    bps;        /**< Bytes per sector. */
52
    uint8_t     spc;        /**< Sectors per cluster. */
58
    uint8_t     spc;        /**< Sectors per cluster. */
53
    uint16_t    rscnt;      /**< Reserved sector count. */
59
    uint16_t    rscnt;      /**< Reserved sector count. */
54
    uint8_t     fatcnt;     /**< Number of FATs. */
60
    uint8_t     fatcnt;     /**< Number of FATs. */
55
    uint16_t    root_ent_max;   /**< Maximum number of root directory
61
    uint16_t    root_ent_max;   /**< Maximum number of root directory
56
                         entries. */
62
                         entries. */
57
    uint16_t    totsec16;   /**< Total sectors. 16-bit version. */
63
    uint16_t    totsec16;   /**< Total sectors. 16-bit version. */
58
    uint8_t     mdesc;      /**< Media descriptor. */
64
    uint8_t     mdesc;      /**< Media descriptor. */
59
    uint16_t    sec_per_fat;    /**< Sectors per FAT12/FAT16. */
65
    uint16_t    sec_per_fat;    /**< Sectors per FAT12/FAT16. */
60
    uint16_t    sec_per_track;  /**< Sectors per track. */
66
    uint16_t    sec_per_track;  /**< Sectors per track. */
61
    uint16_t    headcnt;    /**< Number of heads. */
67
    uint16_t    headcnt;    /**< Number of heads. */
62
    uint32_t    hidden_sec; /**< Hidden sectors. */
68
    uint32_t    hidden_sec; /**< Hidden sectors. */
63
    uint32_t    totsec32;   /**< Total sectors. 32-bit version. */
69
    uint32_t    totsec32;   /**< Total sectors. 32-bit version. */
64
 
70
 
65
    union {
71
    union {
66
        struct {
72
        struct {
67
            /* FAT12/FAT16 only: Extended BIOS Parameter Block */
73
            /* FAT12/FAT16 only: Extended BIOS Parameter Block */
68
            /** Physical drive number. */
74
            /** Physical drive number. */
69
            uint8_t     pdn;
75
            uint8_t     pdn;
70
            uint8_t     reserved;
76
            uint8_t     reserved;
71
            /** Extended boot signature. */
77
            /** Extended boot signature. */
72
            uint8_t     ebs;
78
            uint8_t     ebs;
73
            /** Serial number. */
79
            /** Serial number. */
74
            uint32_t    id;
80
            uint32_t    id;
75
            /** Volume label. */
81
            /** Volume label. */
76
            uint8_t     label[11];
82
            uint8_t     label[11];
77
            /** FAT type. */
83
            /** FAT type. */
78
            uint8_t     type[8];
84
            uint8_t     type[8];
79
            /** Boot code. */
85
            /** Boot code. */
80
            uint8_t     boot_code[448];
86
            uint8_t     boot_code[448];
81
            /** Boot sector signature. */
87
            /** Boot sector signature. */
82
            uint16_t    signature;
88
            uint16_t    signature;
83
        } __attribute__ ((packed));
89
        } __attribute__ ((packed));
84
        struct {
90
        struct {
85
            /* FAT32 only */
91
            /* FAT32 only */
86
            /** Sectors per FAT. */
92
            /** Sectors per FAT. */
87
            uint32_t    sectors_per_fat;
93
            uint32_t    sectors_per_fat;
88
            /** FAT flags. */
94
            /** FAT flags. */
89
            uint16_t    flags;
95
            uint16_t    flags;
90
            /** Version. */
96
            /** Version. */
91
            uint16_t    version;
97
            uint16_t    version;
92
            /** Cluster number of root directory. */
98
            /** Cluster number of root directory. */
93
            uint32_t    root_cluster;
99
            uint32_t    root_cluster;
94
            /** Sector number of file system information sector. */
100
            /** Sector number of file system information sector. */
95
            uint16_t    fsinfo_sec;
101
            uint16_t    fsinfo_sec;
96
            /** Sector number of boot sector copy. */
102
            /** Sector number of boot sector copy. */
97
            uint16_t    bscopy_sec;
103
            uint16_t    bscopy_sec;
98
            uint8_t     reserved1[12];
104
            uint8_t     reserved1[12];
99
            /** Physical drive number. */
105
            /** Physical drive number. */
100
            uint8_t     pdn;
106
            uint8_t     pdn;
101
            uint8_t     reserved2;
107
            uint8_t     reserved2;
102
            /** Extended boot signature. */
108
            /** Extended boot signature. */
103
            uint8_t     ebs;
109
            uint8_t     ebs;
104
            /** Serial number. */
110
            /** Serial number. */
105
            uint32_t    id;
111
            uint32_t    id;
106
            /** Volume label. */
112
            /** Volume label. */
107
            uint8_t     label[11];
113
            uint8_t     label[11];
108
            /** FAT type. */
114
            /** FAT type. */
109
            uint8_t     type[8];
115
            uint8_t     type[8];
110
            /** Boot code. */
116
            /** Boot code. */
111
            uint8_t     boot_code[420];
117
            uint8_t     boot_code[420];
112
            /** Signature. */
118
            /** Signature. */
113
            uint16_t    signature;
119
            uint16_t    signature;
114
        } __attribute__ ((packed));
120
        } __attribute__ ((packed));
115
    };
121
    };
116
} __attribute__ ((packed)) fat_bs_t;
122
} __attribute__ ((packed)) fat_bs_t;
117
 
123
 
118
typedef uint16_t fat_cluster_t;
124
#define FAT_BS(b)       ((fat_bs_t *)((b)->data))
119
 
125
 
120
typedef enum {
126
typedef enum {
121
    FAT_INVALID,
127
    FAT_INVALID,
122
    FAT_DIRECTORY,
128
    FAT_DIRECTORY,
123
    FAT_FILE
129
    FAT_FILE
124
} fat_node_type_t;
130
} fat_node_type_t;
125
 
131
 
126
struct fat_node;
132
struct fat_node;
127
 
133
 
128
/** FAT index structure.
134
/** FAT index structure.
129
 *
135
 *
130
 * This structure exists to help us to overcome certain limitations of the FAT
136
 * This structure exists to help us to overcome certain limitations of the FAT
131
 * file system design.  The problem with FAT is that it is hard to find
137
 * file system design.  The problem with FAT is that it is hard to find
132
 * an entity which could represent a VFS index.  There are two candidates:
138
 * an entity which could represent a VFS index.  There are two candidates:
133
 *
139
 *
134
 * a) number of the node's first cluster
140
 * a) number of the node's first cluster
135
 * b) the pair of the parent directory's first cluster and the dentry index
141
 * b) the pair of the parent directory's first cluster and the dentry index
136
 *    within the parent directory
142
 *    within the parent directory
137
 *
143
 *
138
 * We need VFS indices to be:
144
 * We need VFS indices to be:
139
 * A) unique
145
 * A) unique
140
 * B) stable in time, at least until the next mount
146
 * B) stable in time, at least until the next mount
141
 *
147
 *
142
 * Unfortunately a) does not meet the A) criterion because zero-length files
148
 * Unfortunately a) does not meet the A) criterion because zero-length files
143
 * will have the first cluster field cleared.  And b) does not meet the B)
149
 * will have the first cluster field cleared.  And b) does not meet the B)
144
 * criterion because unlink() and rename() will both free up the original
150
 * criterion because unlink() and rename() will both free up the original
145
 * dentry, which contains all the essential info about the file.
151
 * dentry, which contains all the essential info about the file.
146
 *
152
 *
147
 * Therefore, a completely opaque indices are used and the FAT server maintains
153
 * Therefore, a completely opaque indices are used and the FAT server maintains
148
 * a mapping between them and otherwise nice b) variant.  On rename(), the VFS
154
 * a mapping between them and otherwise nice b) variant.  On rename(), the VFS
149
 * index stays unaltered, while the internal FAT "physical tree address"
155
 * index stays unaltered, while the internal FAT "physical tree address"
150
 * changes.  The unlink case is also handled this way thanks to an in-core node
156
 * changes.  The unlink case is also handled this way thanks to an in-core node
151
 * pointer embedded in the index structure.
157
 * pointer embedded in the index structure.
152
 */
158
 */
153
typedef struct {
159
typedef struct {
154
    /** Used indices (position) hash table link. */
160
    /** Used indices (position) hash table link. */
155
    link_t      uph_link;
161
    link_t      uph_link;
156
    /** Used indices (index) hash table link. */
162
    /** Used indices (index) hash table link. */
157
    link_t      uih_link;
163
    link_t      uih_link;
158
 
164
 
159
    futex_t     lock;
165
    futex_t     lock;
160
    dev_handle_t    dev_handle;
166
    dev_handle_t    dev_handle;
161
    fs_index_t  index;
167
    fs_index_t  index;
162
    /**
168
    /**
163
     * Parent node's first cluster.
169
     * Parent node's first cluster.
164
     * Zero is used if this node is not linked, in which case nodep must
170
     * Zero is used if this node is not linked, in which case nodep must
165
     * contain a pointer to the in-core node structure.
171
     * contain a pointer to the in-core node structure.
166
     * One is used when the parent is the root directory.
172
     * One is used when the parent is the root directory.
167
     */
173
     */
168
    fat_cluster_t   pfc;
174
    fat_cluster_t   pfc;
169
    /** Directory entry index within the parent node. */
175
    /** Directory entry index within the parent node. */
170
    unsigned    pdi;
176
    unsigned    pdi;
171
    /** Pointer to in-core node instance. */
177
    /** Pointer to in-core node instance. */
172
    struct fat_node *nodep;
178
    struct fat_node *nodep;
173
} fat_idx_t;
179
} fat_idx_t;
174
 
180
 
175
/** FAT in-core node. */
181
/** FAT in-core node. */
176
typedef struct fat_node {
182
typedef struct fat_node {
177
    futex_t         lock;
183
    futex_t         lock;
178
    fat_node_type_t     type;
184
    fat_node_type_t     type;
179
    fat_idx_t       *idx;
185
    fat_idx_t       *idx;
180
    /**
186
    /**
181
     *  Node's first cluster.
187
     *  Node's first cluster.
182
     *  Zero is used for zero-length nodes.
188
     *  Zero is used for zero-length nodes.
183
     *  One is used to mark root directory.
189
     *  One is used to mark root directory.
184
     */
190
     */
185
    fat_cluster_t       firstc;
191
    fat_cluster_t       firstc;
186
    /** FAT in-core node free list link. */
192
    /** FAT in-core node free list link. */
187
    link_t          ffn_link;
193
    link_t          ffn_link;
188
    size_t          size;
194
    size_t          size;
189
    unsigned        lnkcnt;
195
    unsigned        lnkcnt;
190
    unsigned        refcnt;
196
    unsigned        refcnt;
191
    bool            dirty;
197
    bool            dirty;
192
} fat_node_t;
198
} fat_node_t;
193
 
199
 
-
 
200
/* TODO move somewhere else */
-
 
201
typedef struct block {
-
 
202
    void *data;
-
 
203
    size_t size;
-
 
204
    bool dirty;
-
 
205
} block_t;
-
 
206
 
-
 
207
extern block_t *block_get(dev_handle_t, off_t, size_t);
-
 
208
extern void block_put(block_t *);
-
 
209
 
194
extern fs_reg_t fat_reg;
210
extern fs_reg_t fat_reg;
195
 
211
 
196
extern void fat_mounted(ipc_callid_t, ipc_call_t *);
212
extern void fat_mounted(ipc_callid_t, ipc_call_t *);
197
extern void fat_mount(ipc_callid_t, ipc_call_t *);
213
extern void fat_mount(ipc_callid_t, ipc_call_t *);
198
extern void fat_lookup(ipc_callid_t, ipc_call_t *);
214
extern void fat_lookup(ipc_callid_t, ipc_call_t *);
199
extern void fat_read(ipc_callid_t, ipc_call_t *);
215
extern void fat_read(ipc_callid_t, ipc_call_t *);
200
extern void fat_write(ipc_callid_t, ipc_call_t *);
216
extern void fat_write(ipc_callid_t, ipc_call_t *);
201
 
217
 
202
extern fat_idx_t *fat_idx_get_by_pos(dev_handle_t, fat_cluster_t, unsigned);
218
extern fat_idx_t *fat_idx_get_by_pos(dev_handle_t, fat_cluster_t, unsigned);
203
extern fat_idx_t *fat_idx_get_by_index(dev_handle_t, fs_index_t);
219
extern fat_idx_t *fat_idx_get_by_index(dev_handle_t, fs_index_t);
204
 
220
 
205
extern int fat_idx_init(void);
221
extern int fat_idx_init(void);
206
extern void fat_idx_fini(void);
222
extern void fat_idx_fini(void);
207
extern int fat_idx_init_by_dev_handle(dev_handle_t);
223
extern int fat_idx_init_by_dev_handle(dev_handle_t);
208
extern void fat_idx_fini_by_dev_handle(dev_handle_t);
224
extern void fat_idx_fini_by_dev_handle(dev_handle_t);
209
 
225
 
210
#endif
226
#endif
211
 
227
 
212
/**
228
/**
213
 * @}
229
 * @}
214
 */
230
 */
215
 
231