Subversion Repositories HelenOS

Rev

Rev 4581 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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