Rev 4357 | Rev 4359 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4357 | Rev 4358 | ||
---|---|---|---|
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 | /** |
33 | /** |
34 | * @file fat_ops.c |
34 | * @file fat_ops.c |
35 | * @brief Implementation of VFS operations for the FAT file system server. |
35 | * @brief Implementation of VFS operations for the FAT file system server. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include "fat.h" |
38 | #include "fat.h" |
39 | #include "fat_dentry.h" |
39 | #include "fat_dentry.h" |
40 | #include "fat_fat.h" |
40 | #include "fat_fat.h" |
41 | #include "../../vfs/vfs.h" |
41 | #include "../../vfs/vfs.h" |
42 | #include <libfs.h> |
42 | #include <libfs.h> |
43 | #include <libblock.h> |
43 | #include <libblock.h> |
44 | #include <ipc/ipc.h> |
44 | #include <ipc/ipc.h> |
45 | #include <ipc/services.h> |
45 | #include <ipc/services.h> |
46 | #include <ipc/devmap.h> |
46 | #include <ipc/devmap.h> |
47 | #include <async.h> |
47 | #include <async.h> |
48 | #include <errno.h> |
48 | #include <errno.h> |
49 | #include <string.h> |
49 | #include <string.h> |
50 | #include <byteorder.h> |
50 | #include <byteorder.h> |
51 | #include <libadt/hash_table.h> |
51 | #include <libadt/hash_table.h> |
52 | #include <libadt/list.h> |
52 | #include <libadt/list.h> |
53 | #include <assert.h> |
53 | #include <assert.h> |
54 | #include <futex.h> |
54 | #include <futex.h> |
55 | #include <sys/mman.h> |
55 | #include <sys/mman.h> |
56 | #include <align.h> |
56 | #include <align.h> |
57 | 57 | ||
58 | #define FAT_NODE(node) ((node) ? (fat_node_t *) (node)->data : NULL) |
58 | #define FAT_NODE(node) ((node) ? (fat_node_t *) (node)->data : NULL) |
59 | #define FS_NODE(node) ((node) ? (node)->bp : NULL) |
59 | #define FS_NODE(node) ((node) ? (node)->bp : NULL) |
60 | 60 | ||
61 | /** Futex protecting the list of cached free FAT nodes. */ |
61 | /** Futex protecting the list of cached free FAT nodes. */ |
62 | static futex_t ffn_futex = FUTEX_INITIALIZER; |
62 | static futex_t ffn_futex = FUTEX_INITIALIZER; |
63 | 63 | ||
64 | /** List of cached free FAT nodes. */ |
64 | /** List of cached free FAT nodes. */ |
65 | static LIST_INITIALIZE(ffn_head); |
65 | static LIST_INITIALIZE(ffn_head); |
66 | 66 | ||
67 | static void fat_node_initialize(fat_node_t *node) |
67 | static void fat_node_initialize(fat_node_t *node) |
68 | { |
68 | { |
69 | futex_initialize(&node->lock, 1); |
69 | futex_initialize(&node->lock, 1); |
70 | node->bp = NULL; |
70 | node->bp = NULL; |
71 | node->idx = NULL; |
71 | node->idx = NULL; |
72 | node->type = 0; |
72 | node->type = 0; |
73 | link_initialize(&node->ffn_link); |
73 | link_initialize(&node->ffn_link); |
74 | node->size = 0; |
74 | node->size = 0; |
75 | node->lnkcnt = 0; |
75 | node->lnkcnt = 0; |
76 | node->refcnt = 0; |
76 | node->refcnt = 0; |
77 | node->dirty = false; |
77 | node->dirty = false; |
78 | } |
78 | } |
79 | 79 | ||
80 | static void fat_node_sync(fat_node_t *node) |
80 | static void fat_node_sync(fat_node_t *node) |
81 | { |
81 | { |
82 | block_t *b; |
82 | block_t *b; |
83 | fat_bs_t *bs; |
83 | fat_bs_t *bs; |
84 | fat_dentry_t *d; |
84 | fat_dentry_t *d; |
85 | uint16_t bps; |
85 | uint16_t bps; |
86 | unsigned dps; |
86 | unsigned dps; |
87 | 87 | ||
88 | assert(node->dirty); |
88 | assert(node->dirty); |
89 | 89 | ||
90 | bs = block_bb_get(node->idx->dev_handle); |
90 | bs = block_bb_get(node->idx->dev_handle); |
91 | bps = uint16_t_le2host(bs->bps); |
91 | bps = uint16_t_le2host(bs->bps); |
92 | dps = bps / sizeof(fat_dentry_t); |
92 | dps = bps / sizeof(fat_dentry_t); |
93 | 93 | ||
94 | /* Read the block that contains the dentry of interest. */ |
94 | /* Read the block that contains the dentry of interest. */ |
95 | b = _fat_block_get(bs, node->idx->dev_handle, node->idx->pfc, |
95 | b = _fat_block_get(bs, node->idx->dev_handle, node->idx->pfc, |
96 | (node->idx->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE); |
96 | (node->idx->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE); |
97 | 97 | ||
98 | d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps); |
98 | d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps); |
99 | 99 | ||
100 | d->firstc = host2uint16_t_le(node->firstc); |
100 | d->firstc = host2uint16_t_le(node->firstc); |
101 | if (node->type == FAT_FILE) { |
101 | if (node->type == FAT_FILE) { |
102 | d->size = host2uint32_t_le(node->size); |
102 | d->size = host2uint32_t_le(node->size); |
103 | } else if (node->type == FAT_DIRECTORY) { |
103 | } else if (node->type == FAT_DIRECTORY) { |
104 | d->attr = FAT_ATTR_SUBDIR; |
104 | d->attr = FAT_ATTR_SUBDIR; |
105 | } |
105 | } |
106 | 106 | ||
107 | /* TODO: update other fields? (e.g time fields) */ |
107 | /* TODO: update other fields? (e.g time fields) */ |
108 | 108 | ||
109 | b->dirty = true; /* need to sync block */ |
109 | b->dirty = true; /* need to sync block */ |
110 | block_put(b); |
110 | block_put(b); |
111 | } |
111 | } |
112 | 112 | ||
113 | static fat_node_t *fat_node_get_new(void) |
113 | static fat_node_t *fat_node_get_new(void) |
114 | { |
114 | { |
115 | fs_node_t *fn; |
115 | fs_node_t *fn; |
116 | fat_node_t *nodep; |
116 | fat_node_t *nodep; |
117 | 117 | ||
118 | futex_down(&ffn_futex); |
118 | futex_down(&ffn_futex); |
119 | if (!list_empty(&ffn_head)) { |
119 | if (!list_empty(&ffn_head)) { |
120 | /* Try to use a cached free node structure. */ |
120 | /* Try to use a cached free node structure. */ |
121 | fat_idx_t *idxp_tmp; |
121 | fat_idx_t *idxp_tmp; |
122 | nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link); |
122 | nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link); |
123 | if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK) |
123 | if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK) |
124 | goto skip_cache; |
124 | goto skip_cache; |
125 | idxp_tmp = nodep->idx; |
125 | idxp_tmp = nodep->idx; |
126 | if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) { |
126 | if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) { |
127 | futex_up(&nodep->lock); |
127 | futex_up(&nodep->lock); |
128 | goto skip_cache; |
128 | goto skip_cache; |
129 | } |
129 | } |
130 | list_remove(&nodep->ffn_link); |
130 | list_remove(&nodep->ffn_link); |
131 | futex_up(&ffn_futex); |
131 | futex_up(&ffn_futex); |
132 | if (nodep->dirty) |
132 | if (nodep->dirty) |
133 | fat_node_sync(nodep); |
133 | fat_node_sync(nodep); |
134 | idxp_tmp->nodep = NULL; |
134 | idxp_tmp->nodep = NULL; |
135 | futex_up(&nodep->lock); |
135 | futex_up(&nodep->lock); |
136 | futex_up(&idxp_tmp->lock); |
136 | futex_up(&idxp_tmp->lock); |
137 | fn = FS_NODE(nodep); |
137 | fn = FS_NODE(nodep); |
138 | } else { |
138 | } else { |
139 | skip_cache: |
139 | skip_cache: |
140 | /* Try to allocate a new node structure. */ |
140 | /* Try to allocate a new node structure. */ |
141 | futex_up(&ffn_futex); |
141 | futex_up(&ffn_futex); |
142 | fn = (fs_node_t *)malloc(sizeof(fs_node_t)); |
142 | fn = (fs_node_t *)malloc(sizeof(fs_node_t)); |
143 | if (!fn) |
143 | if (!fn) |
144 | return NULL; |
144 | return NULL; |
145 | nodep = (fat_node_t *)malloc(sizeof(fat_node_t)); |
145 | nodep = (fat_node_t *)malloc(sizeof(fat_node_t)); |
146 | if (!nodep) { |
146 | if (!nodep) { |
147 | free(fn); |
147 | free(fn); |
148 | return NULL; |
148 | return NULL; |
149 | } |
149 | } |
150 | } |
150 | } |
151 | fat_node_initialize(nodep); |
151 | fat_node_initialize(nodep); |
152 | fn->data = nodep; |
152 | fn->data = nodep; |
153 | nodep->bp = fn; |
153 | nodep->bp = fn; |
154 | 154 | ||
155 | return nodep; |
155 | return nodep; |
156 | } |
156 | } |
157 | 157 | ||
158 | /** Internal version of fat_node_get(). |
158 | /** Internal version of fat_node_get(). |
159 | * |
159 | * |
160 | * @param idxp Locked index structure. |
160 | * @param idxp Locked index structure. |
161 | */ |
161 | */ |
162 | static fat_node_t *fat_node_get_core(fat_idx_t *idxp) |
162 | static fat_node_t *fat_node_get_core(fat_idx_t *idxp) |
163 | { |
163 | { |
164 | block_t *b; |
164 | block_t *b; |
165 | fat_bs_t *bs; |
165 | fat_bs_t *bs; |
166 | fat_dentry_t *d; |
166 | fat_dentry_t *d; |
167 | fat_node_t *nodep = NULL; |
167 | fat_node_t *nodep = NULL; |
168 | unsigned bps; |
168 | unsigned bps; |
169 | unsigned spc; |
169 | unsigned spc; |
170 | unsigned dps; |
170 | unsigned dps; |
171 | 171 | ||
172 | if (idxp->nodep) { |
172 | if (idxp->nodep) { |
173 | /* |
173 | /* |
174 | * We are lucky. |
174 | * We are lucky. |
175 | * The node is already instantiated in memory. |
175 | * The node is already instantiated in memory. |
176 | */ |
176 | */ |
177 | futex_down(&idxp->nodep->lock); |
177 | futex_down(&idxp->nodep->lock); |
178 | if (!idxp->nodep->refcnt++) |
178 | if (!idxp->nodep->refcnt++) |
179 | list_remove(&idxp->nodep->ffn_link); |
179 | list_remove(&idxp->nodep->ffn_link); |
180 | futex_up(&idxp->nodep->lock); |
180 | futex_up(&idxp->nodep->lock); |
181 | return idxp->nodep; |
181 | return idxp->nodep; |
182 | } |
182 | } |
183 | 183 | ||
184 | /* |
184 | /* |
185 | * We must instantiate the node from the file system. |
185 | * We must instantiate the node from the file system. |
186 | */ |
186 | */ |
187 | 187 | ||
188 | assert(idxp->pfc); |
188 | assert(idxp->pfc); |
189 | 189 | ||
190 | nodep = fat_node_get_new(); |
190 | nodep = fat_node_get_new(); |
191 | if (!nodep) |
191 | if (!nodep) |
192 | return NULL; |
192 | return NULL; |
193 | 193 | ||
194 | bs = block_bb_get(idxp->dev_handle); |
194 | bs = block_bb_get(idxp->dev_handle); |
195 | bps = uint16_t_le2host(bs->bps); |
195 | bps = uint16_t_le2host(bs->bps); |
196 | spc = bs->spc; |
196 | spc = bs->spc; |
197 | dps = bps / sizeof(fat_dentry_t); |
197 | dps = bps / sizeof(fat_dentry_t); |
198 | 198 | ||
199 | /* Read the block that contains the dentry of interest. */ |
199 | /* Read the block that contains the dentry of interest. */ |
200 | b = _fat_block_get(bs, idxp->dev_handle, idxp->pfc, |
200 | b = _fat_block_get(bs, idxp->dev_handle, idxp->pfc, |
201 | (idxp->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE); |
201 | (idxp->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE); |
202 | assert(b); |
202 | assert(b); |
203 | 203 | ||
204 | d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps); |
204 | d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps); |
205 | if (d->attr & FAT_ATTR_SUBDIR) { |
205 | if (d->attr & FAT_ATTR_SUBDIR) { |
206 | /* |
206 | /* |
207 | * The only directory which does not have this bit set is the |
207 | * The only directory which does not have this bit set is the |
208 | * root directory itself. The root directory node is handled |
208 | * root directory itself. The root directory node is handled |
209 | * and initialized elsewhere. |
209 | * and initialized elsewhere. |
210 | */ |
210 | */ |
211 | nodep->type = FAT_DIRECTORY; |
211 | nodep->type = FAT_DIRECTORY; |
212 | /* |
212 | /* |
213 | * Unfortunately, the 'size' field of the FAT dentry is not |
213 | * Unfortunately, the 'size' field of the FAT dentry is not |
214 | * defined for the directory entry type. We must determine the |
214 | * defined for the directory entry type. We must determine the |
215 | * size of the directory by walking the FAT. |
215 | * size of the directory by walking the FAT. |
216 | */ |
216 | */ |
217 | nodep->size = bps * spc * fat_clusters_get(bs, idxp->dev_handle, |
217 | nodep->size = bps * spc * fat_clusters_get(bs, idxp->dev_handle, |
218 | uint16_t_le2host(d->firstc)); |
218 | uint16_t_le2host(d->firstc)); |
219 | } else { |
219 | } else { |
220 | nodep->type = FAT_FILE; |
220 | nodep->type = FAT_FILE; |
221 | nodep->size = uint32_t_le2host(d->size); |
221 | nodep->size = uint32_t_le2host(d->size); |
222 | } |
222 | } |
223 | nodep->firstc = uint16_t_le2host(d->firstc); |
223 | nodep->firstc = uint16_t_le2host(d->firstc); |
224 | nodep->lnkcnt = 1; |
224 | nodep->lnkcnt = 1; |
225 | nodep->refcnt = 1; |
225 | nodep->refcnt = 1; |
226 | 226 | ||
227 | block_put(b); |
227 | block_put(b); |
228 | 228 | ||
229 | /* Link the idx structure with the node structure. */ |
229 | /* Link the idx structure with the node structure. */ |
230 | nodep->idx = idxp; |
230 | nodep->idx = idxp; |
231 | idxp->nodep = nodep; |
231 | idxp->nodep = nodep; |
232 | 232 | ||
233 | return nodep; |
233 | return nodep; |
234 | } |
234 | } |
235 | 235 | ||
236 | /* |
236 | /* |
237 | * Forward declarations of FAT libfs operations. |
237 | * Forward declarations of FAT libfs operations. |
238 | */ |
238 | */ |
239 | static fs_node_t *fat_node_get(dev_handle_t, fs_index_t); |
239 | static fs_node_t *fat_node_get(dev_handle_t, fs_index_t); |
240 | static void fat_node_put(fs_node_t *); |
240 | static void fat_node_put(fs_node_t *); |
241 | static fs_node_t *fat_create_node(dev_handle_t, int); |
241 | static fs_node_t *fat_create_node(dev_handle_t, int); |
242 | static int fat_destroy_node(fs_node_t *); |
242 | static int fat_destroy_node(fs_node_t *); |
243 | static int fat_link(fs_node_t *, fs_node_t *, const char *); |
243 | static int fat_link(fs_node_t *, fs_node_t *, const char *); |
244 | static int fat_unlink(fs_node_t *, fs_node_t *); |
244 | static int fat_unlink(fs_node_t *, fs_node_t *); |
245 | static fs_node_t *fat_match(fs_node_t *, const char *); |
245 | static fs_node_t *fat_match(fs_node_t *, const char *); |
246 | static fs_index_t fat_index_get(fs_node_t *); |
246 | static fs_index_t fat_index_get(fs_node_t *); |
247 | static size_t fat_size_get(fs_node_t *); |
247 | static size_t fat_size_get(fs_node_t *); |
248 | static unsigned fat_lnkcnt_get(fs_node_t *); |
248 | static unsigned fat_lnkcnt_get(fs_node_t *); |
249 | static bool fat_has_children(fs_node_t *); |
249 | static bool fat_has_children(fs_node_t *); |
250 | static fs_node_t *fat_root_get(dev_handle_t); |
250 | static fs_node_t *fat_root_get(dev_handle_t); |
251 | static char fat_plb_get_char(unsigned); |
251 | static char fat_plb_get_char(unsigned); |
252 | static bool fat_is_directory(fs_node_t *); |
252 | static bool fat_is_directory(fs_node_t *); |
253 | static bool fat_is_file(fs_node_t *node); |
253 | static bool fat_is_file(fs_node_t *node); |
254 | 254 | ||
255 | /* |
255 | /* |
256 | * FAT libfs operations. |
256 | * FAT libfs operations. |
257 | */ |
257 | */ |
258 | 258 | ||
259 | /** Instantiate a FAT in-core node. */ |
259 | /** Instantiate a FAT in-core node. */ |
260 | fs_node_t *fat_node_get(dev_handle_t dev_handle, fs_index_t index) |
260 | fs_node_t *fat_node_get(dev_handle_t dev_handle, fs_index_t index) |
261 | { |
261 | { |
262 | fat_node_t *nodep; |
262 | fat_node_t *nodep; |
263 | fat_idx_t *idxp; |
263 | fat_idx_t *idxp; |
264 | 264 | ||
265 | idxp = fat_idx_get_by_index(dev_handle, index); |
265 | idxp = fat_idx_get_by_index(dev_handle, index); |
266 | if (!idxp) |
266 | if (!idxp) |
267 | return NULL; |
267 | return NULL; |
268 | /* idxp->lock held */ |
268 | /* idxp->lock held */ |
269 | nodep = fat_node_get_core(idxp); |
269 | nodep = fat_node_get_core(idxp); |
270 | futex_up(&idxp->lock); |
270 | futex_up(&idxp->lock); |
271 | return FS_NODE(nodep); |
271 | return FS_NODE(nodep); |
272 | } |
272 | } |
273 | 273 | ||
274 | void fat_node_put(fs_node_t *fn) |
274 | void fat_node_put(fs_node_t *fn) |
275 | { |
275 | { |
276 | fat_node_t *nodep = FAT_NODE(fn); |
276 | fat_node_t *nodep = FAT_NODE(fn); |
277 | bool destroy = false; |
277 | bool destroy = false; |
278 | 278 | ||
279 | futex_down(&nodep->lock); |
279 | futex_down(&nodep->lock); |
280 | if (!--nodep->refcnt) { |
280 | if (!--nodep->refcnt) { |
281 | if (nodep->idx) { |
281 | if (nodep->idx) { |
282 | futex_down(&ffn_futex); |
282 | futex_down(&ffn_futex); |
283 | list_append(&nodep->ffn_link, &ffn_head); |
283 | list_append(&nodep->ffn_link, &ffn_head); |
284 | futex_up(&ffn_futex); |
284 | futex_up(&ffn_futex); |
285 | } else { |
285 | } else { |
286 | /* |
286 | /* |
287 | * The node does not have any index structure associated |
287 | * The node does not have any index structure associated |
288 | * with itself. This can only mean that we are releasing |
288 | * with itself. This can only mean that we are releasing |
289 | * the node after a failed attempt to allocate the index |
289 | * the node after a failed attempt to allocate the index |
290 | * structure for it. |
290 | * structure for it. |
291 | */ |
291 | */ |
292 | destroy = true; |
292 | destroy = true; |
293 | } |
293 | } |
294 | } |
294 | } |
295 | futex_up(&nodep->lock); |
295 | futex_up(&nodep->lock); |
296 | if (destroy) { |
296 | if (destroy) { |
297 | free(nodep->bp); |
297 | free(nodep->bp); |
298 | free(nodep); |
298 | free(nodep); |
299 | } |
299 | } |
300 | } |
300 | } |
301 | 301 | ||
302 | fs_node_t *fat_create_node(dev_handle_t dev_handle, int flags) |
302 | fs_node_t *fat_create_node(dev_handle_t dev_handle, int flags) |
303 | { |
303 | { |
304 | fat_idx_t *idxp; |
304 | fat_idx_t *idxp; |
305 | fat_node_t *nodep; |
305 | fat_node_t *nodep; |
306 | fat_bs_t *bs; |
306 | fat_bs_t *bs; |
307 | fat_cluster_t mcl, lcl; |
307 | fat_cluster_t mcl, lcl; |
308 | uint16_t bps; |
308 | uint16_t bps; |
309 | int rc; |
309 | int rc; |
310 | 310 | ||
311 | bs = block_bb_get(dev_handle); |
311 | bs = block_bb_get(dev_handle); |
312 | bps = uint16_t_le2host(bs->bps); |
312 | bps = uint16_t_le2host(bs->bps); |
313 | if (flags & L_DIRECTORY) { |
313 | if (flags & L_DIRECTORY) { |
314 | /* allocate a cluster */ |
314 | /* allocate a cluster */ |
315 | rc = fat_alloc_clusters(bs, dev_handle, 1, &mcl, &lcl); |
315 | rc = fat_alloc_clusters(bs, dev_handle, 1, &mcl, &lcl); |
316 | if (rc != EOK) |
316 | if (rc != EOK) |
317 | return NULL; |
317 | return NULL; |
318 | } |
318 | } |
319 | 319 | ||
320 | nodep = fat_node_get_new(); |
320 | nodep = fat_node_get_new(); |
321 | if (!nodep) { |
321 | if (!nodep) { |
322 | fat_free_clusters(bs, dev_handle, mcl); |
322 | fat_free_clusters(bs, dev_handle, mcl); |
323 | return NULL; |
323 | return NULL; |
324 | } |
324 | } |
325 | idxp = fat_idx_get_new(dev_handle); |
325 | idxp = fat_idx_get_new(dev_handle); |
326 | if (!idxp) { |
326 | if (!idxp) { |
327 | fat_free_clusters(bs, dev_handle, mcl); |
327 | fat_free_clusters(bs, dev_handle, mcl); |
328 | fat_node_put(FS_NODE(nodep)); |
328 | fat_node_put(FS_NODE(nodep)); |
329 | return NULL; |
329 | return NULL; |
330 | } |
330 | } |
331 | /* idxp->lock held */ |
331 | /* idxp->lock held */ |
332 | if (flags & L_DIRECTORY) { |
332 | if (flags & L_DIRECTORY) { |
333 | int i; |
333 | int i; |
334 | block_t *b; |
334 | block_t *b; |
335 | 335 | ||
336 | /* |
336 | /* |
337 | * Populate the new cluster with unused dentries. |
337 | * Populate the new cluster with unused dentries. |
338 | */ |
338 | */ |
339 | for (i = 0; i < bs->spc; i++) { |
339 | for (i = 0; i < bs->spc; i++) { |
340 | b = _fat_block_get(bs, dev_handle, mcl, i, |
340 | b = _fat_block_get(bs, dev_handle, mcl, i, |
341 | BLOCK_FLAGS_NOREAD); |
341 | BLOCK_FLAGS_NOREAD); |
342 | /* mark all dentries as never-used */ |
342 | /* mark all dentries as never-used */ |
343 | memset(b->data, 0, bps); |
343 | memset(b->data, 0, bps); |
344 | b->dirty = false; |
344 | b->dirty = false; |
345 | block_put(b); |
345 | block_put(b); |
346 | } |
346 | } |
347 | nodep->type = FAT_DIRECTORY; |
347 | nodep->type = FAT_DIRECTORY; |
348 | nodep->firstc = mcl; |
348 | nodep->firstc = mcl; |
349 | nodep->size = bps * bs->spc; |
349 | nodep->size = bps * bs->spc; |
350 | } else { |
350 | } else { |
351 | nodep->type = FAT_FILE; |
351 | nodep->type = FAT_FILE; |
352 | nodep->firstc = FAT_CLST_RES0; |
352 | nodep->firstc = FAT_CLST_RES0; |
353 | nodep->size = 0; |
353 | nodep->size = 0; |
354 | } |
354 | } |
355 | nodep->lnkcnt = 0; /* not linked anywhere */ |
355 | nodep->lnkcnt = 0; /* not linked anywhere */ |
356 | nodep->refcnt = 1; |
356 | nodep->refcnt = 1; |
357 | nodep->dirty = true; |
357 | nodep->dirty = true; |
358 | 358 | ||
359 | nodep->idx = idxp; |
359 | nodep->idx = idxp; |
360 | idxp->nodep = nodep; |
360 | idxp->nodep = nodep; |
361 | 361 | ||
362 | futex_up(&idxp->lock); |
362 | futex_up(&idxp->lock); |
363 | return FS_NODE(nodep); |
363 | return FS_NODE(nodep); |
364 | } |
364 | } |
365 | 365 | ||
366 | int fat_destroy_node(fs_node_t *fn) |
366 | int fat_destroy_node(fs_node_t *fn) |
367 | { |
367 | { |
368 | fat_node_t *nodep = FAT_NODE(fn); |
368 | fat_node_t *nodep = FAT_NODE(fn); |
369 | fat_bs_t *bs; |
369 | fat_bs_t *bs; |
370 | 370 | ||
371 | /* |
371 | /* |
372 | * The node is not reachable from the file system. This means that the |
372 | * The node is not reachable from the file system. This means that the |
373 | * link count should be zero and that the index structure cannot be |
373 | * link count should be zero and that the index structure cannot be |
374 | * found in the position hash. Obviously, we don't need to lock the node |
374 | * found in the position hash. Obviously, we don't need to lock the node |
375 | * nor its index structure. |
375 | * nor its index structure. |
376 | */ |
376 | */ |
377 | assert(nodep->lnkcnt == 0); |
377 | assert(nodep->lnkcnt == 0); |
378 | 378 | ||
379 | /* |
379 | /* |
380 | * The node may not have any children. |
380 | * The node may not have any children. |
381 | */ |
381 | */ |
382 | assert(fat_has_children(fn) == false); |
382 | assert(fat_has_children(fn) == false); |
383 | 383 | ||
384 | bs = block_bb_get(nodep->idx->dev_handle); |
384 | bs = block_bb_get(nodep->idx->dev_handle); |
385 | if (nodep->firstc != FAT_CLST_RES0) { |
385 | if (nodep->firstc != FAT_CLST_RES0) { |
386 | assert(nodep->size); |
386 | assert(nodep->size); |
387 | /* Free all clusters allocated to the node. */ |
387 | /* Free all clusters allocated to the node. */ |
388 | fat_free_clusters(bs, nodep->idx->dev_handle, nodep->firstc); |
388 | fat_free_clusters(bs, nodep->idx->dev_handle, nodep->firstc); |
389 | } |
389 | } |
390 | 390 | ||
391 | fat_idx_destroy(nodep->idx); |
391 | fat_idx_destroy(nodep->idx); |
392 | free(nodep->bp); |
392 | free(nodep->bp); |
393 | free(nodep); |
393 | free(nodep); |
394 | return EOK; |
394 | return EOK; |
395 | } |
395 | } |
396 | 396 | ||
397 | int fat_link(fs_node_t *pfn, fs_node_t *cfn, const char *name) |
397 | int fat_link(fs_node_t *pfn, fs_node_t *cfn, const char *name) |
398 | { |
398 | { |
399 | fat_node_t *parentp = FAT_NODE(pfn); |
399 | fat_node_t *parentp = FAT_NODE(pfn); |
400 | fat_node_t *childp = FAT_NODE(cfn); |
400 | fat_node_t *childp = FAT_NODE(cfn); |
401 | fat_dentry_t *d; |
401 | fat_dentry_t *d; |
402 | fat_bs_t *bs; |
402 | fat_bs_t *bs; |
403 | block_t *b; |
403 | block_t *b; |
404 | int i, j; |
404 | int i, j; |
405 | uint16_t bps; |
405 | uint16_t bps; |
406 | unsigned dps; |
406 | unsigned dps; |
407 | unsigned blocks; |
407 | unsigned blocks; |
408 | fat_cluster_t mcl, lcl; |
408 | fat_cluster_t mcl, lcl; |
409 | int rc; |
409 | int rc; |
410 | 410 | ||
411 | futex_down(&childp->lock); |
411 | futex_down(&childp->lock); |
412 | if (childp->lnkcnt == 1) { |
412 | if (childp->lnkcnt == 1) { |
413 | /* |
413 | /* |
414 | * On FAT, we don't support multiple hard links. |
414 | * On FAT, we don't support multiple hard links. |
415 | */ |
415 | */ |
416 | futex_up(&childp->lock); |
416 | futex_up(&childp->lock); |
417 | return EMLINK; |
417 | return EMLINK; |
418 | } |
418 | } |
419 | assert(childp->lnkcnt == 0); |
419 | assert(childp->lnkcnt == 0); |
420 | futex_up(&childp->lock); |
420 | futex_up(&childp->lock); |
421 | 421 | ||
422 | if (!fat_dentry_name_verify(name)) { |
422 | if (!fat_dentry_name_verify(name)) { |
423 | /* |
423 | /* |
424 | * Attempt to create unsupported name. |
424 | * Attempt to create unsupported name. |
425 | */ |
425 | */ |
426 | return ENOTSUP; |
426 | return ENOTSUP; |
427 | } |
427 | } |
428 | 428 | ||
429 | /* |
429 | /* |
430 | * Get us an unused parent node's dentry or grow the parent and allocate |
430 | * Get us an unused parent node's dentry or grow the parent and allocate |
431 | * a new one. |
431 | * a new one. |
432 | */ |
432 | */ |
433 | 433 | ||
434 | futex_down(&parentp->idx->lock); |
434 | futex_down(&parentp->idx->lock); |
435 | bs = block_bb_get(parentp->idx->dev_handle); |
435 | bs = block_bb_get(parentp->idx->dev_handle); |
436 | bps = uint16_t_le2host(bs->bps); |
436 | bps = uint16_t_le2host(bs->bps); |
437 | dps = bps / sizeof(fat_dentry_t); |
437 | dps = bps / sizeof(fat_dentry_t); |
438 | 438 | ||
439 | blocks = parentp->size / bps; |
439 | blocks = parentp->size / bps; |
440 | 440 | ||
441 | for (i = 0; i < blocks; i++) { |
441 | for (i = 0; i < blocks; i++) { |
442 | b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE); |
442 | b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE); |
443 | for (j = 0; j < dps; j++) { |
443 | for (j = 0; j < dps; j++) { |
444 | d = ((fat_dentry_t *)b->data) + j; |
444 | d = ((fat_dentry_t *)b->data) + j; |
445 | switch (fat_classify_dentry(d)) { |
445 | switch (fat_classify_dentry(d)) { |
446 | case FAT_DENTRY_SKIP: |
446 | case FAT_DENTRY_SKIP: |
447 | case FAT_DENTRY_VALID: |
447 | case FAT_DENTRY_VALID: |
448 | /* skipping used and meta entries */ |
448 | /* skipping used and meta entries */ |
449 | continue; |
449 | continue; |
450 | case FAT_DENTRY_FREE: |
450 | case FAT_DENTRY_FREE: |
451 | case FAT_DENTRY_LAST: |
451 | case FAT_DENTRY_LAST: |
452 | /* found an empty slot */ |
452 | /* found an empty slot */ |
453 | goto hit; |
453 | goto hit; |
454 | } |
454 | } |
455 | } |
455 | } |
456 | block_put(b); |
456 | block_put(b); |
457 | } |
457 | } |
458 | j = 0; |
458 | j = 0; |
459 | 459 | ||
460 | /* |
460 | /* |
461 | * We need to grow the parent in order to create a new unused dentry. |
461 | * We need to grow the parent in order to create a new unused dentry. |
462 | */ |
462 | */ |
463 | if (parentp->idx->pfc == FAT_CLST_ROOT) { |
463 | if (parentp->idx->pfc == FAT_CLST_ROOT) { |
464 | /* Can't grow the root directory. */ |
464 | /* Can't grow the root directory. */ |
465 | futex_up(&parentp->idx->lock); |
465 | futex_up(&parentp->idx->lock); |
466 | return ENOSPC; |
466 | return ENOSPC; |
467 | } |
467 | } |
468 | rc = fat_alloc_clusters(bs, parentp->idx->dev_handle, 1, &mcl, &lcl); |
468 | rc = fat_alloc_clusters(bs, parentp->idx->dev_handle, 1, &mcl, &lcl); |
469 | if (rc != EOK) { |
469 | if (rc != EOK) { |
470 | futex_up(&parentp->idx->lock); |
470 | futex_up(&parentp->idx->lock); |
471 | return rc; |
471 | return rc; |
472 | } |
472 | } |
473 | fat_append_clusters(bs, parentp, mcl); |
473 | fat_append_clusters(bs, parentp, mcl); |
474 | b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NOREAD); |
474 | b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NOREAD); |
475 | d = (fat_dentry_t *)b->data; |
475 | d = (fat_dentry_t *)b->data; |
476 | /* |
476 | /* |
477 | * Clear all dentries in the block except for the first one (the first |
477 | * Clear all dentries in the block except for the first one (the first |
478 | * dentry will be cleared in the next step). |
478 | * dentry will be cleared in the next step). |
479 | */ |
479 | */ |
480 | memset(d + 1, 0, bps - sizeof(fat_dentry_t)); |
480 | memset(d + 1, 0, bps - sizeof(fat_dentry_t)); |
481 | 481 | ||
482 | hit: |
482 | hit: |
483 | /* |
483 | /* |
484 | * At this point we only establish the link between the parent and the |
484 | * At this point we only establish the link between the parent and the |
485 | * child. The dentry, except of the name and the extension, will remain |
485 | * child. The dentry, except of the name and the extension, will remain |
486 | * uninitialized until the corresponding node is synced. Thus the valid |
486 | * uninitialized until the corresponding node is synced. Thus the valid |
487 | * dentry data is kept in the child node structure. |
487 | * dentry data is kept in the child node structure. |
488 | */ |
488 | */ |
489 | memset(d, 0, sizeof(fat_dentry_t)); |
489 | memset(d, 0, sizeof(fat_dentry_t)); |
490 | fat_dentry_name_set(d, name); |
490 | fat_dentry_name_set(d, name); |
491 | b->dirty = true; /* need to sync block */ |
491 | b->dirty = true; /* need to sync block */ |
492 | block_put(b); |
492 | block_put(b); |
493 | futex_up(&parentp->idx->lock); |
493 | futex_up(&parentp->idx->lock); |
494 | 494 | ||
495 | futex_down(&childp->idx->lock); |
495 | futex_down(&childp->idx->lock); |
496 | 496 | ||
497 | /* |
497 | /* |
498 | * If possible, create the Sub-directory Identifier Entry and the |
498 | * If possible, create the Sub-directory Identifier Entry and the |
499 | * Sub-directory Parent Pointer Entry (i.e. "." and ".."). These entries |
499 | * Sub-directory Parent Pointer Entry (i.e. "." and ".."). These entries |
500 | * are not mandatory according to Standard ECMA-107 and HelenOS VFS does |
500 | * are not mandatory according to Standard ECMA-107 and HelenOS VFS does |
501 | * not use them anyway, so this is rather a sign of our good will. |
501 | * not use them anyway, so this is rather a sign of our good will. |
502 | */ |
502 | */ |
503 | b = fat_block_get(bs, childp, 0, BLOCK_FLAGS_NONE); |
503 | b = fat_block_get(bs, childp, 0, BLOCK_FLAGS_NONE); |
504 | d = (fat_dentry_t *)b->data; |
504 | d = (fat_dentry_t *)b->data; |
505 | if (fat_classify_dentry(d) == FAT_DENTRY_LAST || |
505 | if (fat_classify_dentry(d) == FAT_DENTRY_LAST || |
506 | str_cmp(d->name, FAT_NAME_DOT) == 0) { |
506 | str_cmp(d->name, FAT_NAME_DOT) == 0) { |
507 | memset(d, 0, sizeof(fat_dentry_t)); |
507 | memset(d, 0, sizeof(fat_dentry_t)); |
508 | str_cpy(d->name, 8, FAT_NAME_DOT); |
508 | str_cpy(d->name, 8, FAT_NAME_DOT); |
509 | str_cpy(d->ext, 3, FAT_EXT_PAD); |
509 | str_cpy(d->ext, 3, FAT_EXT_PAD); |
510 | d->attr = FAT_ATTR_SUBDIR; |
510 | d->attr = FAT_ATTR_SUBDIR; |
511 | d->firstc = host2uint16_t_le(childp->firstc); |
511 | d->firstc = host2uint16_t_le(childp->firstc); |
512 | /* TODO: initialize also the date/time members. */ |
512 | /* TODO: initialize also the date/time members. */ |
513 | } |
513 | } |
514 | d++; |
514 | d++; |
515 | if (fat_classify_dentry(d) == FAT_DENTRY_LAST || |
515 | if (fat_classify_dentry(d) == FAT_DENTRY_LAST || |
516 | str_cmp(d->name, FAT_NAME_DOT_DOT) == 0) { |
516 | str_cmp(d->name, FAT_NAME_DOT_DOT) == 0) { |
517 | memset(d, 0, sizeof(fat_dentry_t)); |
517 | memset(d, 0, sizeof(fat_dentry_t)); |
518 | str_cpy(d->name, 8, FAT_NAME_DOT_DOT); |
518 | str_cpy(d->name, 8, FAT_NAME_DOT_DOT); |
519 | str_cpy(d->ext, 3, FAT_EXT_PAD); |
519 | str_cpy(d->ext, 3, FAT_EXT_PAD); |
520 | d->attr = FAT_ATTR_SUBDIR; |
520 | d->attr = FAT_ATTR_SUBDIR; |
521 | d->firstc = (parentp->firstc == FAT_CLST_ROOT) ? |
521 | d->firstc = (parentp->firstc == FAT_CLST_ROOT) ? |
522 | host2uint16_t_le(FAT_CLST_RES0) : |
522 | host2uint16_t_le(FAT_CLST_RES0) : |
523 | host2uint16_t_le(parentp->firstc); |
523 | host2uint16_t_le(parentp->firstc); |
524 | /* TODO: initialize also the date/time members. */ |
524 | /* TODO: initialize also the date/time members. */ |
525 | } |
525 | } |
526 | b->dirty = true; /* need to sync block */ |
526 | b->dirty = true; /* need to sync block */ |
527 | block_put(b); |
527 | block_put(b); |
528 | 528 | ||
529 | childp->idx->pfc = parentp->firstc; |
529 | childp->idx->pfc = parentp->firstc; |
530 | childp->idx->pdi = i * dps + j; |
530 | childp->idx->pdi = i * dps + j; |
531 | futex_up(&childp->idx->lock); |
531 | futex_up(&childp->idx->lock); |
532 | 532 | ||
533 | futex_down(&childp->lock); |
533 | futex_down(&childp->lock); |
534 | childp->lnkcnt = 1; |
534 | childp->lnkcnt = 1; |
535 | childp->dirty = true; /* need to sync node */ |
535 | childp->dirty = true; /* need to sync node */ |
536 | futex_up(&childp->lock); |
536 | futex_up(&childp->lock); |
537 | 537 | ||
538 | /* |
538 | /* |
539 | * Hash in the index structure into the position hash. |
539 | * Hash in the index structure into the position hash. |
540 | */ |
540 | */ |
541 | fat_idx_hashin(childp->idx); |
541 | fat_idx_hashin(childp->idx); |
542 | 542 | ||
543 | return EOK; |
543 | return EOK; |
544 | } |
544 | } |
545 | 545 | ||
546 | int fat_unlink(fs_node_t *pfn, fs_node_t *cfn) |
546 | int fat_unlink(fs_node_t *pfn, fs_node_t *cfn) |
547 | { |
547 | { |
548 | fat_node_t *parentp = FAT_NODE(pfn); |
548 | fat_node_t *parentp = FAT_NODE(pfn); |
549 | fat_node_t *childp = FAT_NODE(cfn); |
549 | fat_node_t *childp = FAT_NODE(cfn); |
550 | fat_bs_t *bs; |
550 | fat_bs_t *bs; |
551 | fat_dentry_t *d; |
551 | fat_dentry_t *d; |
552 | uint16_t bps; |
552 | uint16_t bps; |
553 | block_t *b; |
553 | block_t *b; |
554 | 554 | ||
- | 555 | if (!parentp) |
|
- | 556 | return EBUSY; |
|
- | 557 | ||
555 | futex_down(&parentp->lock); |
558 | futex_down(&parentp->lock); |
556 | futex_down(&childp->lock); |
559 | futex_down(&childp->lock); |
557 | assert(childp->lnkcnt == 1); |
560 | assert(childp->lnkcnt == 1); |
558 | futex_down(&childp->idx->lock); |
561 | futex_down(&childp->idx->lock); |
559 | bs = block_bb_get(childp->idx->dev_handle); |
562 | bs = block_bb_get(childp->idx->dev_handle); |
560 | bps = uint16_t_le2host(bs->bps); |
563 | bps = uint16_t_le2host(bs->bps); |
561 | 564 | ||
562 | b = _fat_block_get(bs, childp->idx->dev_handle, childp->idx->pfc, |
565 | b = _fat_block_get(bs, childp->idx->dev_handle, childp->idx->pfc, |
563 | (childp->idx->pdi * sizeof(fat_dentry_t)) / bps, |
566 | (childp->idx->pdi * sizeof(fat_dentry_t)) / bps, |
564 | BLOCK_FLAGS_NONE); |
567 | BLOCK_FLAGS_NONE); |
565 | d = (fat_dentry_t *)b->data + |
568 | d = (fat_dentry_t *)b->data + |
566 | (childp->idx->pdi % (bps / sizeof(fat_dentry_t))); |
569 | (childp->idx->pdi % (bps / sizeof(fat_dentry_t))); |
567 | /* mark the dentry as not-currently-used */ |
570 | /* mark the dentry as not-currently-used */ |
568 | d->name[0] = FAT_DENTRY_ERASED; |
571 | d->name[0] = FAT_DENTRY_ERASED; |
569 | b->dirty = true; /* need to sync block */ |
572 | b->dirty = true; /* need to sync block */ |
570 | block_put(b); |
573 | block_put(b); |
571 | 574 | ||
572 | /* remove the index structure from the position hash */ |
575 | /* remove the index structure from the position hash */ |
573 | fat_idx_hashout(childp->idx); |
576 | fat_idx_hashout(childp->idx); |
574 | /* clear position information */ |
577 | /* clear position information */ |
575 | childp->idx->pfc = FAT_CLST_RES0; |
578 | childp->idx->pfc = FAT_CLST_RES0; |
576 | childp->idx->pdi = 0; |
579 | childp->idx->pdi = 0; |
577 | futex_up(&childp->idx->lock); |
580 | futex_up(&childp->idx->lock); |
578 | childp->lnkcnt = 0; |
581 | childp->lnkcnt = 0; |
579 | childp->dirty = true; |
582 | childp->dirty = true; |
580 | futex_up(&childp->lock); |
583 | futex_up(&childp->lock); |
581 | futex_up(&parentp->lock); |
584 | futex_up(&parentp->lock); |
582 | 585 | ||
583 | return EOK; |
586 | return EOK; |
584 | } |
587 | } |
585 | 588 | ||
586 | fs_node_t *fat_match(fs_node_t *pfn, const char *component) |
589 | fs_node_t *fat_match(fs_node_t *pfn, const char *component) |
587 | { |
590 | { |
588 | fat_bs_t *bs; |
591 | fat_bs_t *bs; |
589 | fat_node_t *parentp = FAT_NODE(pfn); |
592 | fat_node_t *parentp = FAT_NODE(pfn); |
590 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
593 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
591 | unsigned i, j; |
594 | unsigned i, j; |
592 | unsigned bps; /* bytes per sector */ |
595 | unsigned bps; /* bytes per sector */ |
593 | unsigned dps; /* dentries per sector */ |
596 | unsigned dps; /* dentries per sector */ |
594 | unsigned blocks; |
597 | unsigned blocks; |
595 | fat_dentry_t *d; |
598 | fat_dentry_t *d; |
596 | block_t *b; |
599 | block_t *b; |
597 | 600 | ||
598 | futex_down(&parentp->idx->lock); |
601 | futex_down(&parentp->idx->lock); |
599 | bs = block_bb_get(parentp->idx->dev_handle); |
602 | bs = block_bb_get(parentp->idx->dev_handle); |
600 | bps = uint16_t_le2host(bs->bps); |
603 | bps = uint16_t_le2host(bs->bps); |
601 | dps = bps / sizeof(fat_dentry_t); |
604 | dps = bps / sizeof(fat_dentry_t); |
602 | blocks = parentp->size / bps; |
605 | blocks = parentp->size / bps; |
603 | for (i = 0; i < blocks; i++) { |
606 | for (i = 0; i < blocks; i++) { |
604 | b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE); |
607 | b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE); |
605 | for (j = 0; j < dps; j++) { |
608 | for (j = 0; j < dps; j++) { |
606 | d = ((fat_dentry_t *)b->data) + j; |
609 | d = ((fat_dentry_t *)b->data) + j; |
607 | switch (fat_classify_dentry(d)) { |
610 | switch (fat_classify_dentry(d)) { |
608 | case FAT_DENTRY_SKIP: |
611 | case FAT_DENTRY_SKIP: |
609 | case FAT_DENTRY_FREE: |
612 | case FAT_DENTRY_FREE: |
610 | continue; |
613 | continue; |
611 | case FAT_DENTRY_LAST: |
614 | case FAT_DENTRY_LAST: |
612 | block_put(b); |
615 | block_put(b); |
613 | futex_up(&parentp->idx->lock); |
616 | futex_up(&parentp->idx->lock); |
614 | return NULL; |
617 | return NULL; |
615 | default: |
618 | default: |
616 | case FAT_DENTRY_VALID: |
619 | case FAT_DENTRY_VALID: |
617 | fat_dentry_name_get(d, name); |
620 | fat_dentry_name_get(d, name); |
618 | break; |
621 | break; |
619 | } |
622 | } |
620 | if (fat_dentry_namecmp(name, component) == 0) { |
623 | if (fat_dentry_namecmp(name, component) == 0) { |
621 | /* hit */ |
624 | /* hit */ |
622 | fat_node_t *nodep; |
625 | fat_node_t *nodep; |
623 | /* |
626 | /* |
624 | * Assume tree hierarchy for locking. We |
627 | * Assume tree hierarchy for locking. We |
625 | * already have the parent and now we are going |
628 | * already have the parent and now we are going |
626 | * to lock the child. Never lock in the oposite |
629 | * to lock the child. Never lock in the oposite |
627 | * order. |
630 | * order. |
628 | */ |
631 | */ |
629 | fat_idx_t *idx = fat_idx_get_by_pos( |
632 | fat_idx_t *idx = fat_idx_get_by_pos( |
630 | parentp->idx->dev_handle, parentp->firstc, |
633 | parentp->idx->dev_handle, parentp->firstc, |
631 | i * dps + j); |
634 | i * dps + j); |
632 | futex_up(&parentp->idx->lock); |
635 | futex_up(&parentp->idx->lock); |
633 | if (!idx) { |
636 | if (!idx) { |
634 | /* |
637 | /* |
635 | * Can happen if memory is low or if we |
638 | * Can happen if memory is low or if we |
636 | * run out of 32-bit indices. |
639 | * run out of 32-bit indices. |
637 | */ |
640 | */ |
638 | block_put(b); |
641 | block_put(b); |
639 | return NULL; |
642 | return NULL; |
640 | } |
643 | } |
641 | nodep = fat_node_get_core(idx); |
644 | nodep = fat_node_get_core(idx); |
642 | futex_up(&idx->lock); |
645 | futex_up(&idx->lock); |
643 | block_put(b); |
646 | block_put(b); |
644 | return FS_NODE(nodep); |
647 | return FS_NODE(nodep); |
645 | } |
648 | } |
646 | } |
649 | } |
647 | block_put(b); |
650 | block_put(b); |
648 | } |
651 | } |
649 | 652 | ||
650 | futex_up(&parentp->idx->lock); |
653 | futex_up(&parentp->idx->lock); |
651 | return NULL; |
654 | return NULL; |
652 | } |
655 | } |
653 | 656 | ||
654 | fs_index_t fat_index_get(fs_node_t *fn) |
657 | fs_index_t fat_index_get(fs_node_t *fn) |
655 | { |
658 | { |
656 | return FAT_NODE(fn)->idx->index; |
659 | return FAT_NODE(fn)->idx->index; |
657 | } |
660 | } |
658 | 661 | ||
659 | size_t fat_size_get(fs_node_t *fn) |
662 | size_t fat_size_get(fs_node_t *fn) |
660 | { |
663 | { |
661 | return FAT_NODE(fn)->size; |
664 | return FAT_NODE(fn)->size; |
662 | } |
665 | } |
663 | 666 | ||
664 | unsigned fat_lnkcnt_get(fs_node_t *fn) |
667 | unsigned fat_lnkcnt_get(fs_node_t *fn) |
665 | { |
668 | { |
666 | return FAT_NODE(fn)->lnkcnt; |
669 | return FAT_NODE(fn)->lnkcnt; |
667 | } |
670 | } |
668 | 671 | ||
669 | bool fat_has_children(fs_node_t *fn) |
672 | bool fat_has_children(fs_node_t *fn) |
670 | { |
673 | { |
671 | fat_bs_t *bs; |
674 | fat_bs_t *bs; |
672 | fat_node_t *nodep = FAT_NODE(fn); |
675 | fat_node_t *nodep = FAT_NODE(fn); |
673 | unsigned bps; |
676 | unsigned bps; |
674 | unsigned dps; |
677 | unsigned dps; |
675 | unsigned blocks; |
678 | unsigned blocks; |
676 | block_t *b; |
679 | block_t *b; |
677 | unsigned i, j; |
680 | unsigned i, j; |
678 | 681 | ||
679 | if (nodep->type != FAT_DIRECTORY) |
682 | if (nodep->type != FAT_DIRECTORY) |
680 | return false; |
683 | return false; |
681 | 684 | ||
682 | futex_down(&nodep->idx->lock); |
685 | futex_down(&nodep->idx->lock); |
683 | bs = block_bb_get(nodep->idx->dev_handle); |
686 | bs = block_bb_get(nodep->idx->dev_handle); |
684 | bps = uint16_t_le2host(bs->bps); |
687 | bps = uint16_t_le2host(bs->bps); |
685 | dps = bps / sizeof(fat_dentry_t); |
688 | dps = bps / sizeof(fat_dentry_t); |
686 | 689 | ||
687 | blocks = nodep->size / bps; |
690 | blocks = nodep->size / bps; |
688 | 691 | ||
689 | for (i = 0; i < blocks; i++) { |
692 | for (i = 0; i < blocks; i++) { |
690 | fat_dentry_t *d; |
693 | fat_dentry_t *d; |
691 | 694 | ||
692 | b = fat_block_get(bs, nodep, i, BLOCK_FLAGS_NONE); |
695 | b = fat_block_get(bs, nodep, i, BLOCK_FLAGS_NONE); |
693 | for (j = 0; j < dps; j++) { |
696 | for (j = 0; j < dps; j++) { |
694 | d = ((fat_dentry_t *)b->data) + j; |
697 | d = ((fat_dentry_t *)b->data) + j; |
695 | switch (fat_classify_dentry(d)) { |
698 | switch (fat_classify_dentry(d)) { |
696 | case FAT_DENTRY_SKIP: |
699 | case FAT_DENTRY_SKIP: |
697 | case FAT_DENTRY_FREE: |
700 | case FAT_DENTRY_FREE: |
698 | continue; |
701 | continue; |
699 | case FAT_DENTRY_LAST: |
702 | case FAT_DENTRY_LAST: |
700 | block_put(b); |
703 | block_put(b); |
701 | futex_up(&nodep->idx->lock); |
704 | futex_up(&nodep->idx->lock); |
702 | return false; |
705 | return false; |
703 | default: |
706 | default: |
704 | case FAT_DENTRY_VALID: |
707 | case FAT_DENTRY_VALID: |
705 | block_put(b); |
708 | block_put(b); |
706 | futex_up(&nodep->idx->lock); |
709 | futex_up(&nodep->idx->lock); |
707 | return true; |
710 | return true; |
708 | } |
711 | } |
709 | block_put(b); |
712 | block_put(b); |
710 | futex_up(&nodep->idx->lock); |
713 | futex_up(&nodep->idx->lock); |
711 | return true; |
714 | return true; |
712 | } |
715 | } |
713 | block_put(b); |
716 | block_put(b); |
714 | } |
717 | } |
715 | 718 | ||
716 | futex_up(&nodep->idx->lock); |
719 | futex_up(&nodep->idx->lock); |
717 | return false; |
720 | return false; |
718 | } |
721 | } |
719 | 722 | ||
720 | fs_node_t *fat_root_get(dev_handle_t dev_handle) |
723 | fs_node_t *fat_root_get(dev_handle_t dev_handle) |
721 | { |
724 | { |
722 | return fat_node_get(dev_handle, 0); |
725 | return fat_node_get(dev_handle, 0); |
723 | } |
726 | } |
724 | 727 | ||
725 | char fat_plb_get_char(unsigned pos) |
728 | char fat_plb_get_char(unsigned pos) |
726 | { |
729 | { |
727 | return fat_reg.plb_ro[pos % PLB_SIZE]; |
730 | return fat_reg.plb_ro[pos % PLB_SIZE]; |
728 | } |
731 | } |
729 | 732 | ||
730 | bool fat_is_directory(fs_node_t *fn) |
733 | bool fat_is_directory(fs_node_t *fn) |
731 | { |
734 | { |
732 | return FAT_NODE(fn)->type == FAT_DIRECTORY; |
735 | return FAT_NODE(fn)->type == FAT_DIRECTORY; |
733 | } |
736 | } |
734 | 737 | ||
735 | bool fat_is_file(fs_node_t *fn) |
738 | bool fat_is_file(fs_node_t *fn) |
736 | { |
739 | { |
737 | return FAT_NODE(fn)->type == FAT_FILE; |
740 | return FAT_NODE(fn)->type == FAT_FILE; |
738 | } |
741 | } |
739 | 742 | ||
740 | /** libfs operations */ |
743 | /** libfs operations */ |
741 | libfs_ops_t fat_libfs_ops = { |
744 | libfs_ops_t fat_libfs_ops = { |
742 | .match = fat_match, |
745 | .match = fat_match, |
743 | .node_get = fat_node_get, |
746 | .node_get = fat_node_get, |
744 | .node_put = fat_node_put, |
747 | .node_put = fat_node_put, |
745 | .create = fat_create_node, |
748 | .create = fat_create_node, |
746 | .destroy = fat_destroy_node, |
749 | .destroy = fat_destroy_node, |
747 | .link = fat_link, |
750 | .link = fat_link, |
748 | .unlink = fat_unlink, |
751 | .unlink = fat_unlink, |
749 | .index_get = fat_index_get, |
752 | .index_get = fat_index_get, |
750 | .size_get = fat_size_get, |
753 | .size_get = fat_size_get, |
751 | .lnkcnt_get = fat_lnkcnt_get, |
754 | .lnkcnt_get = fat_lnkcnt_get, |
752 | .has_children = fat_has_children, |
755 | .has_children = fat_has_children, |
753 | .root_get = fat_root_get, |
756 | .root_get = fat_root_get, |
754 | .plb_get_char = fat_plb_get_char, |
757 | .plb_get_char = fat_plb_get_char, |
755 | .is_directory = fat_is_directory, |
758 | .is_directory = fat_is_directory, |
756 | .is_file = fat_is_file |
759 | .is_file = fat_is_file |
757 | }; |
760 | }; |
758 | 761 | ||
759 | /* |
762 | /* |
760 | * VFS operations. |
763 | * VFS operations. |
761 | */ |
764 | */ |
762 | 765 | ||
763 | void fat_mounted(ipc_callid_t rid, ipc_call_t *request) |
766 | void fat_mounted(ipc_callid_t rid, ipc_call_t *request) |
764 | { |
767 | { |
765 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
768 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
766 | fat_bs_t *bs; |
769 | fat_bs_t *bs; |
767 | uint16_t bps; |
770 | uint16_t bps; |
768 | uint16_t rde; |
771 | uint16_t rde; |
769 | int rc; |
772 | int rc; |
770 | 773 | ||
771 | /* accept the mount options */ |
774 | /* accept the mount options */ |
772 | ipc_callid_t callid; |
775 | ipc_callid_t callid; |
773 | size_t size; |
776 | size_t size; |
774 | if (!ipc_data_write_receive(&callid, &size)) { |
777 | if (!ipc_data_write_receive(&callid, &size)) { |
775 | ipc_answer_0(callid, EINVAL); |
778 | ipc_answer_0(callid, EINVAL); |
776 | ipc_answer_0(rid, EINVAL); |
779 | ipc_answer_0(rid, EINVAL); |
777 | return; |
780 | return; |
778 | } |
781 | } |
779 | char *opts = malloc(size + 1); |
782 | char *opts = malloc(size + 1); |
780 | if (!opts) { |
783 | if (!opts) { |
781 | ipc_answer_0(callid, ENOMEM); |
784 | ipc_answer_0(callid, ENOMEM); |
782 | ipc_answer_0(rid, ENOMEM); |
785 | ipc_answer_0(rid, ENOMEM); |
783 | return; |
786 | return; |
784 | } |
787 | } |
785 | ipcarg_t retval = ipc_data_write_finalize(callid, opts, size); |
788 | ipcarg_t retval = ipc_data_write_finalize(callid, opts, size); |
786 | if (retval != EOK) { |
789 | if (retval != EOK) { |
787 | ipc_answer_0(rid, retval); |
790 | ipc_answer_0(rid, retval); |
788 | free(opts); |
791 | free(opts); |
789 | return; |
792 | return; |
790 | } |
793 | } |
791 | opts[size] = '\0'; |
794 | opts[size] = '\0'; |
792 | 795 | ||
793 | /* initialize libblock */ |
796 | /* initialize libblock */ |
794 | rc = block_init(dev_handle, BS_SIZE); |
797 | rc = block_init(dev_handle, BS_SIZE); |
795 | if (rc != EOK) { |
798 | if (rc != EOK) { |
796 | ipc_answer_0(rid, rc); |
799 | ipc_answer_0(rid, rc); |
797 | return; |
800 | return; |
798 | } |
801 | } |
799 | 802 | ||
800 | /* prepare the boot block */ |
803 | /* prepare the boot block */ |
801 | rc = block_bb_read(dev_handle, BS_BLOCK * BS_SIZE, BS_SIZE); |
804 | rc = block_bb_read(dev_handle, BS_BLOCK * BS_SIZE, BS_SIZE); |
802 | if (rc != EOK) { |
805 | if (rc != EOK) { |
803 | block_fini(dev_handle); |
806 | block_fini(dev_handle); |
804 | ipc_answer_0(rid, rc); |
807 | ipc_answer_0(rid, rc); |
805 | return; |
808 | return; |
806 | } |
809 | } |
807 | 810 | ||
808 | /* get the buffer with the boot sector */ |
811 | /* get the buffer with the boot sector */ |
809 | bs = block_bb_get(dev_handle); |
812 | bs = block_bb_get(dev_handle); |
810 | 813 | ||
811 | /* Read the number of root directory entries. */ |
814 | /* Read the number of root directory entries. */ |
812 | bps = uint16_t_le2host(bs->bps); |
815 | bps = uint16_t_le2host(bs->bps); |
813 | rde = uint16_t_le2host(bs->root_ent_max); |
816 | rde = uint16_t_le2host(bs->root_ent_max); |
814 | 817 | ||
815 | if (bps != BS_SIZE) { |
818 | if (bps != BS_SIZE) { |
816 | block_fini(dev_handle); |
819 | block_fini(dev_handle); |
817 | ipc_answer_0(rid, ENOTSUP); |
820 | ipc_answer_0(rid, ENOTSUP); |
818 | return; |
821 | return; |
819 | } |
822 | } |
820 | 823 | ||
821 | /* Initialize the block cache */ |
824 | /* Initialize the block cache */ |
822 | rc = block_cache_init(dev_handle, bps, 0 /* XXX */); |
825 | rc = block_cache_init(dev_handle, bps, 0 /* XXX */); |
823 | if (rc != EOK) { |
826 | if (rc != EOK) { |
824 | block_fini(dev_handle); |
827 | block_fini(dev_handle); |
825 | ipc_answer_0(rid, rc); |
828 | ipc_answer_0(rid, rc); |
826 | return; |
829 | return; |
827 | } |
830 | } |
828 | 831 | ||
829 | rc = fat_idx_init_by_dev_handle(dev_handle); |
832 | rc = fat_idx_init_by_dev_handle(dev_handle); |
830 | if (rc != EOK) { |
833 | if (rc != EOK) { |
831 | block_fini(dev_handle); |
834 | block_fini(dev_handle); |
832 | ipc_answer_0(rid, rc); |
835 | ipc_answer_0(rid, rc); |
833 | return; |
836 | return; |
834 | } |
837 | } |
835 | 838 | ||
836 | /* Initialize the root node. */ |
839 | /* Initialize the root node. */ |
837 | fs_node_t *rfn = (fs_node_t *)malloc(sizeof(fs_node_t)); |
840 | fs_node_t *rfn = (fs_node_t *)malloc(sizeof(fs_node_t)); |
838 | if (!rfn) { |
841 | if (!rfn) { |
839 | block_fini(dev_handle); |
842 | block_fini(dev_handle); |
840 | fat_idx_fini_by_dev_handle(dev_handle); |
843 | fat_idx_fini_by_dev_handle(dev_handle); |
841 | ipc_answer_0(rid, ENOMEM); |
844 | ipc_answer_0(rid, ENOMEM); |
842 | return; |
845 | return; |
843 | } |
846 | } |
844 | fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t)); |
847 | fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t)); |
845 | if (!rootp) { |
848 | if (!rootp) { |
846 | free(rfn); |
849 | free(rfn); |
847 | block_fini(dev_handle); |
850 | block_fini(dev_handle); |
848 | fat_idx_fini_by_dev_handle(dev_handle); |
851 | fat_idx_fini_by_dev_handle(dev_handle); |
849 | ipc_answer_0(rid, ENOMEM); |
852 | ipc_answer_0(rid, ENOMEM); |
850 | return; |
853 | return; |
851 | } |
854 | } |
852 | fat_node_initialize(rootp); |
855 | fat_node_initialize(rootp); |
853 | 856 | ||
854 | fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0); |
857 | fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0); |
855 | if (!ridxp) { |
858 | if (!ridxp) { |
856 | free(rfn); |
859 | free(rfn); |
857 | free(rootp); |
860 | free(rootp); |
858 | block_fini(dev_handle); |
861 | block_fini(dev_handle); |
859 | fat_idx_fini_by_dev_handle(dev_handle); |
862 | fat_idx_fini_by_dev_handle(dev_handle); |
860 | ipc_answer_0(rid, ENOMEM); |
863 | ipc_answer_0(rid, ENOMEM); |
861 | return; |
864 | return; |
862 | } |
865 | } |
863 | assert(ridxp->index == 0); |
866 | assert(ridxp->index == 0); |
864 | /* ridxp->lock held */ |
867 | /* ridxp->lock held */ |
865 | 868 | ||
866 | rootp->type = FAT_DIRECTORY; |
869 | rootp->type = FAT_DIRECTORY; |
867 | rootp->firstc = FAT_CLST_ROOT; |
870 | rootp->firstc = FAT_CLST_ROOT; |
868 | rootp->refcnt = 1; |
871 | rootp->refcnt = 1; |
869 | rootp->lnkcnt = 0; /* FS root is not linked */ |
872 | rootp->lnkcnt = 0; /* FS root is not linked */ |
870 | rootp->size = rde * sizeof(fat_dentry_t); |
873 | rootp->size = rde * sizeof(fat_dentry_t); |
871 | rootp->idx = ridxp; |
874 | rootp->idx = ridxp; |
872 | ridxp->nodep = rootp; |
875 | ridxp->nodep = rootp; |
873 | rootp->bp = rfn; |
876 | rootp->bp = rfn; |
874 | rfn->data = rootp; |
877 | rfn->data = rootp; |
875 | 878 | ||
876 | futex_up(&ridxp->lock); |
879 | futex_up(&ridxp->lock); |
877 | 880 | ||
878 | ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt); |
881 | ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt); |
879 | } |
882 | } |
880 | 883 | ||
881 | void fat_mount(ipc_callid_t rid, ipc_call_t *request) |
884 | void fat_mount(ipc_callid_t rid, ipc_call_t *request) |
882 | { |
885 | { |
883 | ipc_answer_0(rid, ENOTSUP); |
886 | ipc_answer_0(rid, ENOTSUP); |
884 | } |
887 | } |
885 | 888 | ||
886 | void fat_lookup(ipc_callid_t rid, ipc_call_t *request) |
889 | void fat_lookup(ipc_callid_t rid, ipc_call_t *request) |
887 | { |
890 | { |
888 | libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request); |
891 | libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request); |
889 | } |
892 | } |
890 | 893 | ||
891 | void fat_read(ipc_callid_t rid, ipc_call_t *request) |
894 | void fat_read(ipc_callid_t rid, ipc_call_t *request) |
892 | { |
895 | { |
893 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
896 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
894 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
897 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
895 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
898 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
896 | fs_node_t *fn = fat_node_get(dev_handle, index); |
899 | fs_node_t *fn = fat_node_get(dev_handle, index); |
897 | fat_node_t *nodep; |
900 | fat_node_t *nodep; |
898 | fat_bs_t *bs; |
901 | fat_bs_t *bs; |
899 | uint16_t bps; |
902 | uint16_t bps; |
900 | size_t bytes; |
903 | size_t bytes; |
901 | block_t *b; |
904 | block_t *b; |
902 | 905 | ||
903 | if (!fn) { |
906 | if (!fn) { |
904 | ipc_answer_0(rid, ENOENT); |
907 | ipc_answer_0(rid, ENOENT); |
905 | return; |
908 | return; |
906 | } |
909 | } |
907 | nodep = FAT_NODE(fn); |
910 | nodep = FAT_NODE(fn); |
908 | 911 | ||
909 | ipc_callid_t callid; |
912 | ipc_callid_t callid; |
910 | size_t len; |
913 | size_t len; |
911 | if (!ipc_data_read_receive(&callid, &len)) { |
914 | if (!ipc_data_read_receive(&callid, &len)) { |
912 | fat_node_put(fn); |
915 | fat_node_put(fn); |
913 | ipc_answer_0(callid, EINVAL); |
916 | ipc_answer_0(callid, EINVAL); |
914 | ipc_answer_0(rid, EINVAL); |
917 | ipc_answer_0(rid, EINVAL); |
915 | return; |
918 | return; |
916 | } |
919 | } |
917 | 920 | ||
918 | bs = block_bb_get(dev_handle); |
921 | bs = block_bb_get(dev_handle); |
919 | bps = uint16_t_le2host(bs->bps); |
922 | bps = uint16_t_le2host(bs->bps); |
920 | 923 | ||
921 | if (nodep->type == FAT_FILE) { |
924 | if (nodep->type == FAT_FILE) { |
922 | /* |
925 | /* |
923 | * Our strategy for regular file reads is to read one block at |
926 | * Our strategy for regular file reads is to read one block at |
924 | * most and make use of the possibility to return less data than |
927 | * most and make use of the possibility to return less data than |
925 | * requested. This keeps the code very simple. |
928 | * requested. This keeps the code very simple. |
926 | */ |
929 | */ |
927 | if (pos >= nodep->size) { |
930 | if (pos >= nodep->size) { |
928 | /* reading beyond the EOF */ |
931 | /* reading beyond the EOF */ |
929 | bytes = 0; |
932 | bytes = 0; |
930 | (void) ipc_data_read_finalize(callid, NULL, 0); |
933 | (void) ipc_data_read_finalize(callid, NULL, 0); |
931 | } else { |
934 | } else { |
932 | bytes = min(len, bps - pos % bps); |
935 | bytes = min(len, bps - pos % bps); |
933 | bytes = min(bytes, nodep->size - pos); |
936 | bytes = min(bytes, nodep->size - pos); |
934 | b = fat_block_get(bs, nodep, pos / bps, |
937 | b = fat_block_get(bs, nodep, pos / bps, |
935 | BLOCK_FLAGS_NONE); |
938 | BLOCK_FLAGS_NONE); |
936 | (void) ipc_data_read_finalize(callid, b->data + pos % bps, |
939 | (void) ipc_data_read_finalize(callid, b->data + pos % bps, |
937 | bytes); |
940 | bytes); |
938 | block_put(b); |
941 | block_put(b); |
939 | } |
942 | } |
940 | } else { |
943 | } else { |
941 | unsigned bnum; |
944 | unsigned bnum; |
942 | off_t spos = pos; |
945 | off_t spos = pos; |
943 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
946 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
944 | fat_dentry_t *d; |
947 | fat_dentry_t *d; |
945 | 948 | ||
946 | assert(nodep->type == FAT_DIRECTORY); |
949 | assert(nodep->type == FAT_DIRECTORY); |
947 | assert(nodep->size % bps == 0); |
950 | assert(nodep->size % bps == 0); |
948 | assert(bps % sizeof(fat_dentry_t) == 0); |
951 | assert(bps % sizeof(fat_dentry_t) == 0); |
949 | 952 | ||
950 | /* |
953 | /* |
951 | * Our strategy for readdir() is to use the position pointer as |
954 | * Our strategy for readdir() is to use the position pointer as |
952 | * an index into the array of all dentries. On entry, it points |
955 | * an index into the array of all dentries. On entry, it points |
953 | * to the first unread dentry. If we skip any dentries, we bump |
956 | * to the first unread dentry. If we skip any dentries, we bump |
954 | * the position pointer accordingly. |
957 | * the position pointer accordingly. |
955 | */ |
958 | */ |
956 | bnum = (pos * sizeof(fat_dentry_t)) / bps; |
959 | bnum = (pos * sizeof(fat_dentry_t)) / bps; |
957 | while (bnum < nodep->size / bps) { |
960 | while (bnum < nodep->size / bps) { |
958 | off_t o; |
961 | off_t o; |
959 | 962 | ||
960 | b = fat_block_get(bs, nodep, bnum, BLOCK_FLAGS_NONE); |
963 | b = fat_block_get(bs, nodep, bnum, BLOCK_FLAGS_NONE); |
961 | for (o = pos % (bps / sizeof(fat_dentry_t)); |
964 | for (o = pos % (bps / sizeof(fat_dentry_t)); |
962 | o < bps / sizeof(fat_dentry_t); |
965 | o < bps / sizeof(fat_dentry_t); |
963 | o++, pos++) { |
966 | o++, pos++) { |
964 | d = ((fat_dentry_t *)b->data) + o; |
967 | d = ((fat_dentry_t *)b->data) + o; |
965 | switch (fat_classify_dentry(d)) { |
968 | switch (fat_classify_dentry(d)) { |
966 | case FAT_DENTRY_SKIP: |
969 | case FAT_DENTRY_SKIP: |
967 | case FAT_DENTRY_FREE: |
970 | case FAT_DENTRY_FREE: |
968 | continue; |
971 | continue; |
969 | case FAT_DENTRY_LAST: |
972 | case FAT_DENTRY_LAST: |
970 | block_put(b); |
973 | block_put(b); |
971 | goto miss; |
974 | goto miss; |
972 | default: |
975 | default: |
973 | case FAT_DENTRY_VALID: |
976 | case FAT_DENTRY_VALID: |
974 | fat_dentry_name_get(d, name); |
977 | fat_dentry_name_get(d, name); |
975 | block_put(b); |
978 | block_put(b); |
976 | goto hit; |
979 | goto hit; |
977 | } |
980 | } |
978 | } |
981 | } |
979 | block_put(b); |
982 | block_put(b); |
980 | bnum++; |
983 | bnum++; |
981 | } |
984 | } |
982 | miss: |
985 | miss: |
983 | fat_node_put(fn); |
986 | fat_node_put(fn); |
984 | ipc_answer_0(callid, ENOENT); |
987 | ipc_answer_0(callid, ENOENT); |
985 | ipc_answer_1(rid, ENOENT, 0); |
988 | ipc_answer_1(rid, ENOENT, 0); |
986 | return; |
989 | return; |
987 | hit: |
990 | hit: |
988 | (void) ipc_data_read_finalize(callid, name, str_size(name) + 1); |
991 | (void) ipc_data_read_finalize(callid, name, str_size(name) + 1); |
989 | bytes = (pos - spos) + 1; |
992 | bytes = (pos - spos) + 1; |
990 | } |
993 | } |
991 | 994 | ||
992 | fat_node_put(fn); |
995 | fat_node_put(fn); |
993 | ipc_answer_1(rid, EOK, (ipcarg_t)bytes); |
996 | ipc_answer_1(rid, EOK, (ipcarg_t)bytes); |
994 | } |
997 | } |
995 | 998 | ||
996 | void fat_write(ipc_callid_t rid, ipc_call_t *request) |
999 | void fat_write(ipc_callid_t rid, ipc_call_t *request) |
997 | { |
1000 | { |
998 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
1001 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
999 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
1002 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
1000 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
1003 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
1001 | fs_node_t *fn = fat_node_get(dev_handle, index); |
1004 | fs_node_t *fn = fat_node_get(dev_handle, index); |
1002 | fat_node_t *nodep; |
1005 | fat_node_t *nodep; |
1003 | fat_bs_t *bs; |
1006 | fat_bs_t *bs; |
1004 | size_t bytes; |
1007 | size_t bytes; |
1005 | block_t *b; |
1008 | block_t *b; |
1006 | uint16_t bps; |
1009 | uint16_t bps; |
1007 | unsigned spc; |
1010 | unsigned spc; |
1008 | unsigned bpc; /* bytes per cluster */ |
1011 | unsigned bpc; /* bytes per cluster */ |
1009 | off_t boundary; |
1012 | off_t boundary; |
1010 | int flags = BLOCK_FLAGS_NONE; |
1013 | int flags = BLOCK_FLAGS_NONE; |
1011 | 1014 | ||
1012 | if (!fn) { |
1015 | if (!fn) { |
1013 | ipc_answer_0(rid, ENOENT); |
1016 | ipc_answer_0(rid, ENOENT); |
1014 | return; |
1017 | return; |
1015 | } |
1018 | } |
1016 | nodep = FAT_NODE(fn); |
1019 | nodep = FAT_NODE(fn); |
1017 | 1020 | ||
1018 | ipc_callid_t callid; |
1021 | ipc_callid_t callid; |
1019 | size_t len; |
1022 | size_t len; |
1020 | if (!ipc_data_write_receive(&callid, &len)) { |
1023 | if (!ipc_data_write_receive(&callid, &len)) { |
1021 | fat_node_put(fn); |
1024 | fat_node_put(fn); |
1022 | ipc_answer_0(callid, EINVAL); |
1025 | ipc_answer_0(callid, EINVAL); |
1023 | ipc_answer_0(rid, EINVAL); |
1026 | ipc_answer_0(rid, EINVAL); |
1024 | return; |
1027 | return; |
1025 | } |
1028 | } |
1026 | 1029 | ||
1027 | bs = block_bb_get(dev_handle); |
1030 | bs = block_bb_get(dev_handle); |
1028 | bps = uint16_t_le2host(bs->bps); |
1031 | bps = uint16_t_le2host(bs->bps); |
1029 | spc = bs->spc; |
1032 | spc = bs->spc; |
1030 | bpc = bps * spc; |
1033 | bpc = bps * spc; |
1031 | 1034 | ||
1032 | /* |
1035 | /* |
1033 | * In all scenarios, we will attempt to write out only one block worth |
1036 | * In all scenarios, we will attempt to write out only one block worth |
1034 | * of data at maximum. There might be some more efficient approaches, |
1037 | * of data at maximum. There might be some more efficient approaches, |
1035 | * but this one greatly simplifies fat_write(). Note that we can afford |
1038 | * but this one greatly simplifies fat_write(). Note that we can afford |
1036 | * to do this because the client must be ready to handle the return |
1039 | * to do this because the client must be ready to handle the return |
1037 | * value signalizing a smaller number of bytes written. |
1040 | * value signalizing a smaller number of bytes written. |
1038 | */ |
1041 | */ |
1039 | bytes = min(len, bps - pos % bps); |
1042 | bytes = min(len, bps - pos % bps); |
1040 | if (bytes == bps) |
1043 | if (bytes == bps) |
1041 | flags |= BLOCK_FLAGS_NOREAD; |
1044 | flags |= BLOCK_FLAGS_NOREAD; |
1042 | 1045 | ||
1043 | boundary = ROUND_UP(nodep->size, bpc); |
1046 | boundary = ROUND_UP(nodep->size, bpc); |
1044 | if (pos < boundary) { |
1047 | if (pos < boundary) { |
1045 | /* |
1048 | /* |
1046 | * This is the easier case - we are either overwriting already |
1049 | * This is the easier case - we are either overwriting already |
1047 | * existing contents or writing behind the EOF, but still within |
1050 | * existing contents or writing behind the EOF, but still within |
1048 | * the limits of the last cluster. The node size may grow to the |
1051 | * the limits of the last cluster. The node size may grow to the |
1049 | * next block size boundary. |
1052 | * next block size boundary. |
1050 | */ |
1053 | */ |
1051 | fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos); |
1054 | fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos); |
1052 | b = fat_block_get(bs, nodep, pos / bps, flags); |
1055 | b = fat_block_get(bs, nodep, pos / bps, flags); |
1053 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
1056 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
1054 | bytes); |
1057 | bytes); |
1055 | b->dirty = true; /* need to sync block */ |
1058 | b->dirty = true; /* need to sync block */ |
1056 | block_put(b); |
1059 | block_put(b); |
1057 | if (pos + bytes > nodep->size) { |
1060 | if (pos + bytes > nodep->size) { |
1058 | nodep->size = pos + bytes; |
1061 | nodep->size = pos + bytes; |
1059 | nodep->dirty = true; /* need to sync node */ |
1062 | nodep->dirty = true; /* need to sync node */ |
1060 | } |
1063 | } |
1061 | ipc_answer_2(rid, EOK, bytes, nodep->size); |
1064 | ipc_answer_2(rid, EOK, bytes, nodep->size); |
1062 | fat_node_put(fn); |
1065 | fat_node_put(fn); |
1063 | return; |
1066 | return; |
1064 | } else { |
1067 | } else { |
1065 | /* |
1068 | /* |
1066 | * This is the more difficult case. We must allocate new |
1069 | * This is the more difficult case. We must allocate new |
1067 | * clusters for the node and zero them out. |
1070 | * clusters for the node and zero them out. |
1068 | */ |
1071 | */ |
1069 | int status; |
1072 | int status; |
1070 | unsigned nclsts; |
1073 | unsigned nclsts; |
1071 | fat_cluster_t mcl, lcl; |
1074 | fat_cluster_t mcl, lcl; |
1072 | 1075 | ||
1073 | nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc; |
1076 | nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc; |
1074 | /* create an independent chain of nclsts clusters in all FATs */ |
1077 | /* create an independent chain of nclsts clusters in all FATs */ |
1075 | status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl); |
1078 | status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl); |
1076 | if (status != EOK) { |
1079 | if (status != EOK) { |
1077 | /* could not allocate a chain of nclsts clusters */ |
1080 | /* could not allocate a chain of nclsts clusters */ |
1078 | fat_node_put(fn); |
1081 | fat_node_put(fn); |
1079 | ipc_answer_0(callid, status); |
1082 | ipc_answer_0(callid, status); |
1080 | ipc_answer_0(rid, status); |
1083 | ipc_answer_0(rid, status); |
1081 | return; |
1084 | return; |
1082 | } |
1085 | } |
1083 | /* zero fill any gaps */ |
1086 | /* zero fill any gaps */ |
1084 | fat_fill_gap(bs, nodep, mcl, pos); |
1087 | fat_fill_gap(bs, nodep, mcl, pos); |
1085 | b = _fat_block_get(bs, dev_handle, lcl, (pos / bps) % spc, |
1088 | b = _fat_block_get(bs, dev_handle, lcl, (pos / bps) % spc, |
1086 | flags); |
1089 | flags); |
1087 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
1090 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
1088 | bytes); |
1091 | bytes); |
1089 | b->dirty = true; /* need to sync block */ |
1092 | b->dirty = true; /* need to sync block */ |
1090 | block_put(b); |
1093 | block_put(b); |
1091 | /* |
1094 | /* |
1092 | * Append the cluster chain starting in mcl to the end of the |
1095 | * Append the cluster chain starting in mcl to the end of the |
1093 | * node's cluster chain. |
1096 | * node's cluster chain. |
1094 | */ |
1097 | */ |
1095 | fat_append_clusters(bs, nodep, mcl); |
1098 | fat_append_clusters(bs, nodep, mcl); |
1096 | nodep->size = pos + bytes; |
1099 | nodep->size = pos + bytes; |
1097 | nodep->dirty = true; /* need to sync node */ |
1100 | nodep->dirty = true; /* need to sync node */ |
1098 | ipc_answer_2(rid, EOK, bytes, nodep->size); |
1101 | ipc_answer_2(rid, EOK, bytes, nodep->size); |
1099 | fat_node_put(fn); |
1102 | fat_node_put(fn); |
1100 | return; |
1103 | return; |
1101 | } |
1104 | } |
1102 | } |
1105 | } |
1103 | 1106 | ||
1104 | void fat_truncate(ipc_callid_t rid, ipc_call_t *request) |
1107 | void fat_truncate(ipc_callid_t rid, ipc_call_t *request) |
1105 | { |
1108 | { |
1106 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
1109 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
1107 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
1110 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
1108 | size_t size = (off_t)IPC_GET_ARG3(*request); |
1111 | size_t size = (off_t)IPC_GET_ARG3(*request); |
1109 | fs_node_t *fn = fat_node_get(dev_handle, index); |
1112 | fs_node_t *fn = fat_node_get(dev_handle, index); |
1110 | fat_node_t *nodep; |
1113 | fat_node_t *nodep; |
1111 | fat_bs_t *bs; |
1114 | fat_bs_t *bs; |
1112 | uint16_t bps; |
1115 | uint16_t bps; |
1113 | uint8_t spc; |
1116 | uint8_t spc; |
1114 | unsigned bpc; /* bytes per cluster */ |
1117 | unsigned bpc; /* bytes per cluster */ |
1115 | int rc; |
1118 | int rc; |
1116 | 1119 | ||
1117 | if (!fn) { |
1120 | if (!fn) { |
1118 | ipc_answer_0(rid, ENOENT); |
1121 | ipc_answer_0(rid, ENOENT); |
1119 | return; |
1122 | return; |
1120 | } |
1123 | } |
1121 | nodep = FAT_NODE(fn); |
1124 | nodep = FAT_NODE(fn); |
1122 | 1125 | ||
1123 | bs = block_bb_get(dev_handle); |
1126 | bs = block_bb_get(dev_handle); |
1124 | bps = uint16_t_le2host(bs->bps); |
1127 | bps = uint16_t_le2host(bs->bps); |
1125 | spc = bs->spc; |
1128 | spc = bs->spc; |
1126 | bpc = bps * spc; |
1129 | bpc = bps * spc; |
1127 | 1130 | ||
1128 | if (nodep->size == size) { |
1131 | if (nodep->size == size) { |
1129 | rc = EOK; |
1132 | rc = EOK; |
1130 | } else if (nodep->size < size) { |
1133 | } else if (nodep->size < size) { |
1131 | /* |
1134 | /* |
1132 | * The standard says we have the freedom to grow the node. |
1135 | * The standard says we have the freedom to grow the node. |
1133 | * For now, we simply return an error. |
1136 | * For now, we simply return an error. |
1134 | */ |
1137 | */ |
1135 | rc = EINVAL; |
1138 | rc = EINVAL; |
1136 | } else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) { |
1139 | } else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) { |
1137 | /* |
1140 | /* |
1138 | * The node will be shrunk, but no clusters will be deallocated. |
1141 | * The node will be shrunk, but no clusters will be deallocated. |
1139 | */ |
1142 | */ |
1140 | nodep->size = size; |
1143 | nodep->size = size; |
1141 | nodep->dirty = true; /* need to sync node */ |
1144 | nodep->dirty = true; /* need to sync node */ |
1142 | rc = EOK; |
1145 | rc = EOK; |
1143 | } else { |
1146 | } else { |
1144 | /* |
1147 | /* |
1145 | * The node will be shrunk, clusters will be deallocated. |
1148 | * The node will be shrunk, clusters will be deallocated. |
1146 | */ |
1149 | */ |
1147 | if (size == 0) { |
1150 | if (size == 0) { |
1148 | fat_chop_clusters(bs, nodep, FAT_CLST_RES0); |
1151 | fat_chop_clusters(bs, nodep, FAT_CLST_RES0); |
1149 | } else { |
1152 | } else { |
1150 | fat_cluster_t lastc; |
1153 | fat_cluster_t lastc; |
1151 | (void) fat_cluster_walk(bs, dev_handle, nodep->firstc, |
1154 | (void) fat_cluster_walk(bs, dev_handle, nodep->firstc, |
1152 | &lastc, (size - 1) / bpc); |
1155 | &lastc, (size - 1) / bpc); |
1153 | fat_chop_clusters(bs, nodep, lastc); |
1156 | fat_chop_clusters(bs, nodep, lastc); |
1154 | } |
1157 | } |
1155 | nodep->size = size; |
1158 | nodep->size = size; |
1156 | nodep->dirty = true; /* need to sync node */ |
1159 | nodep->dirty = true; /* need to sync node */ |
1157 | rc = EOK; |
1160 | rc = EOK; |
1158 | } |
1161 | } |
1159 | fat_node_put(fn); |
1162 | fat_node_put(fn); |
1160 | ipc_answer_0(rid, rc); |
1163 | ipc_answer_0(rid, rc); |
1161 | return; |
1164 | return; |
1162 | } |
1165 | } |
1163 | 1166 | ||
1164 | void fat_destroy(ipc_callid_t rid, ipc_call_t *request) |
1167 | void fat_destroy(ipc_callid_t rid, ipc_call_t *request) |
1165 | { |
1168 | { |
1166 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
1169 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
1167 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
1170 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
1168 | int rc; |
1171 | int rc; |
1169 | 1172 | ||
1170 | fs_node_t *fn = fat_node_get(dev_handle, index); |
1173 | fs_node_t *fn = fat_node_get(dev_handle, index); |
1171 | if (!fn) { |
1174 | if (!fn) { |
1172 | ipc_answer_0(rid, ENOENT); |
1175 | ipc_answer_0(rid, ENOENT); |
1173 | return; |
1176 | return; |
1174 | } |
1177 | } |
1175 | 1178 | ||
1176 | rc = fat_destroy_node(fn); |
1179 | rc = fat_destroy_node(fn); |
1177 | ipc_answer_0(rid, rc); |
1180 | ipc_answer_0(rid, rc); |
1178 | } |
1181 | } |
1179 | 1182 | ||
1180 | /** |
1183 | /** |
1181 | * @} |
1184 | * @} |
1182 | */ |
1185 | */ |
1183 | 1186 |