Rev 3513 | Rev 3519 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3513 | Rev 3516 | ||
---|---|---|---|
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 <ipc/ipc.h> |
43 | #include <ipc/ipc.h> |
44 | #include <ipc/services.h> |
44 | #include <ipc/services.h> |
45 | #include <ipc/devmap.h> |
45 | #include <ipc/devmap.h> |
46 | #include <async.h> |
46 | #include <async.h> |
47 | #include <errno.h> |
47 | #include <errno.h> |
48 | #include <string.h> |
48 | #include <string.h> |
49 | #include <byteorder.h> |
49 | #include <byteorder.h> |
50 | #include <libadt/hash_table.h> |
50 | #include <libadt/hash_table.h> |
51 | #include <libadt/list.h> |
51 | #include <libadt/list.h> |
52 | #include <assert.h> |
52 | #include <assert.h> |
53 | #include <futex.h> |
53 | #include <futex.h> |
54 | #include <sys/mman.h> |
54 | #include <sys/mman.h> |
55 | #include <align.h> |
55 | #include <align.h> |
56 | 56 | ||
57 | /** Futex protecting the list of cached free FAT nodes. */ |
57 | /** Futex protecting the list of cached free FAT nodes. */ |
58 | static futex_t ffn_futex = FUTEX_INITIALIZER; |
58 | static futex_t ffn_futex = FUTEX_INITIALIZER; |
59 | 59 | ||
60 | /** List of cached free FAT nodes. */ |
60 | /** List of cached free FAT nodes. */ |
61 | static LIST_INITIALIZE(ffn_head); |
61 | static LIST_INITIALIZE(ffn_head); |
62 | 62 | ||
63 | static int dev_phone = -1; /* FIXME */ |
63 | static int dev_phone = -1; /* FIXME */ |
64 | static void *dev_buffer = NULL; /* FIXME */ |
64 | static void *dev_buffer = NULL; /* FIXME */ |
65 | 65 | ||
66 | block_t *block_get(dev_handle_t dev_handle, off_t offset, size_t bs) |
66 | block_t *block_get(dev_handle_t dev_handle, off_t offset, size_t bs) |
67 | { |
67 | { |
68 | /* FIXME */ |
68 | /* FIXME */ |
69 | block_t *b; |
69 | block_t *b; |
70 | off_t bufpos = 0; |
70 | off_t bufpos = 0; |
71 | size_t buflen = 0; |
71 | size_t buflen = 0; |
72 | off_t pos = offset * bs; |
72 | off_t pos = offset * bs; |
73 | 73 | ||
74 | assert(dev_phone != -1); |
74 | assert(dev_phone != -1); |
75 | assert(dev_buffer); |
75 | assert(dev_buffer); |
76 | 76 | ||
77 | b = malloc(sizeof(block_t)); |
77 | b = malloc(sizeof(block_t)); |
78 | if (!b) |
78 | if (!b) |
79 | return NULL; |
79 | return NULL; |
80 | 80 | ||
81 | b->data = malloc(bs); |
81 | b->data = malloc(bs); |
82 | if (!b->data) { |
82 | if (!b->data) { |
83 | free(b); |
83 | free(b); |
84 | return NULL; |
84 | return NULL; |
85 | } |
85 | } |
86 | b->size = bs; |
86 | b->size = bs; |
87 | 87 | ||
88 | if (!libfs_blockread(dev_phone, dev_buffer, &bufpos, &buflen, &pos, |
88 | if (!libfs_blockread(dev_phone, dev_buffer, &bufpos, &buflen, &pos, |
89 | b->data, bs, bs)) { |
89 | b->data, bs, bs)) { |
90 | free(b->data); |
90 | free(b->data); |
91 | free(b); |
91 | free(b); |
92 | return NULL; |
92 | return NULL; |
93 | } |
93 | } |
94 | 94 | ||
95 | return b; |
95 | return b; |
96 | } |
96 | } |
97 | 97 | ||
98 | void block_put(block_t *block) |
98 | void block_put(block_t *block) |
99 | { |
99 | { |
100 | /* FIXME */ |
100 | /* FIXME */ |
101 | free(block->data); |
101 | free(block->data); |
102 | free(block); |
102 | free(block); |
103 | } |
103 | } |
104 | 104 | ||
105 | static void fat_node_initialize(fat_node_t *node) |
105 | static void fat_node_initialize(fat_node_t *node) |
106 | { |
106 | { |
107 | futex_initialize(&node->lock, 1); |
107 | futex_initialize(&node->lock, 1); |
108 | node->idx = NULL; |
108 | node->idx = NULL; |
109 | node->type = 0; |
109 | node->type = 0; |
110 | link_initialize(&node->ffn_link); |
110 | link_initialize(&node->ffn_link); |
111 | node->size = 0; |
111 | node->size = 0; |
112 | node->lnkcnt = 0; |
112 | node->lnkcnt = 0; |
113 | node->refcnt = 0; |
113 | node->refcnt = 0; |
114 | node->dirty = false; |
114 | node->dirty = false; |
115 | } |
115 | } |
116 | 116 | ||
117 | static void fat_node_sync(fat_node_t *node) |
117 | static void fat_node_sync(fat_node_t *node) |
118 | { |
118 | { |
119 | /* TODO */ |
119 | /* TODO */ |
120 | } |
120 | } |
121 | 121 | ||
122 | /** Internal version of fat_node_get(). |
122 | /** Internal version of fat_node_get(). |
123 | * |
123 | * |
124 | * @param idxp Locked index structure. |
124 | * @param idxp Locked index structure. |
125 | */ |
125 | */ |
126 | static void *fat_node_get_core(fat_idx_t *idxp) |
126 | static void *fat_node_get_core(fat_idx_t *idxp) |
127 | { |
127 | { |
128 | block_t *b; |
128 | block_t *bb, *b; |
129 | fat_dentry_t *d; |
129 | fat_dentry_t *d; |
130 | fat_node_t *nodep = NULL; |
130 | fat_node_t *nodep = NULL; |
131 | unsigned bps; |
131 | unsigned bps; |
132 | unsigned dps; |
132 | unsigned dps; |
133 | 133 | ||
134 | if (idxp->nodep) { |
134 | if (idxp->nodep) { |
135 | /* |
135 | /* |
136 | * We are lucky. |
136 | * We are lucky. |
137 | * The node is already instantiated in memory. |
137 | * The node is already instantiated in memory. |
138 | */ |
138 | */ |
139 | futex_down(&idxp->nodep->lock); |
139 | futex_down(&idxp->nodep->lock); |
140 | if (!idxp->nodep->refcnt++) |
140 | if (!idxp->nodep->refcnt++) |
141 | list_remove(&idxp->nodep->ffn_link); |
141 | list_remove(&idxp->nodep->ffn_link); |
142 | futex_up(&idxp->nodep->lock); |
142 | futex_up(&idxp->nodep->lock); |
143 | return idxp->nodep; |
143 | return idxp->nodep; |
144 | } |
144 | } |
145 | 145 | ||
146 | /* |
146 | /* |
147 | * We must instantiate the node from the file system. |
147 | * We must instantiate the node from the file system. |
148 | */ |
148 | */ |
149 | 149 | ||
150 | assert(idxp->pfc); |
150 | assert(idxp->pfc); |
151 | 151 | ||
152 | futex_down(&ffn_futex); |
152 | futex_down(&ffn_futex); |
153 | if (!list_empty(&ffn_head)) { |
153 | if (!list_empty(&ffn_head)) { |
154 | /* Try to use a cached free node structure. */ |
154 | /* Try to use a cached free node structure. */ |
155 | fat_idx_t *idxp_tmp; |
155 | fat_idx_t *idxp_tmp; |
156 | nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link); |
156 | nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link); |
157 | if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK) |
157 | if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK) |
158 | goto skip_cache; |
158 | goto skip_cache; |
159 | idxp_tmp = nodep->idx; |
159 | idxp_tmp = nodep->idx; |
160 | if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) { |
160 | if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) { |
161 | futex_up(&nodep->lock); |
161 | futex_up(&nodep->lock); |
162 | goto skip_cache; |
162 | goto skip_cache; |
163 | } |
163 | } |
164 | list_remove(&nodep->ffn_link); |
164 | list_remove(&nodep->ffn_link); |
165 | futex_up(&ffn_futex); |
165 | futex_up(&ffn_futex); |
166 | if (nodep->dirty) |
166 | if (nodep->dirty) |
167 | fat_node_sync(nodep); |
167 | fat_node_sync(nodep); |
168 | idxp_tmp->nodep = NULL; |
168 | idxp_tmp->nodep = NULL; |
169 | futex_up(&nodep->lock); |
169 | futex_up(&nodep->lock); |
170 | futex_up(&idxp_tmp->lock); |
170 | futex_up(&idxp_tmp->lock); |
171 | } else { |
171 | } else { |
172 | skip_cache: |
172 | skip_cache: |
173 | /* Try to allocate a new node structure. */ |
173 | /* Try to allocate a new node structure. */ |
174 | futex_up(&ffn_futex); |
174 | futex_up(&ffn_futex); |
175 | nodep = (fat_node_t *)malloc(sizeof(fat_node_t)); |
175 | nodep = (fat_node_t *)malloc(sizeof(fat_node_t)); |
176 | if (!nodep) |
176 | if (!nodep) |
177 | return NULL; |
177 | return NULL; |
178 | } |
178 | } |
179 | fat_node_initialize(nodep); |
179 | fat_node_initialize(nodep); |
180 | 180 | ||
181 | bps = fat_bps_get(idxp->dev_handle); |
181 | bb = block_get(idxp->dev_handle, BS_BLOCK, BS_SIZE); |
- | 182 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
|
182 | dps = bps / sizeof(fat_dentry_t); |
183 | dps = bps / sizeof(fat_dentry_t); |
183 | 184 | ||
184 | /* Read the block that contains the dentry of interest. */ |
185 | /* Read the block that contains the dentry of interest. */ |
185 | b = _fat_block_get(idxp->dev_handle, idxp->pfc, |
186 | b = _fat_block_get(bb->data, idxp->dev_handle, idxp->pfc, |
186 | (idxp->pdi * sizeof(fat_dentry_t)) / bps); |
187 | (idxp->pdi * sizeof(fat_dentry_t)) / bps); |
187 | assert(b); |
188 | assert(b); |
188 | 189 | ||
189 | d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps); |
190 | d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps); |
190 | if (d->attr & FAT_ATTR_SUBDIR) { |
191 | if (d->attr & FAT_ATTR_SUBDIR) { |
191 | /* |
192 | /* |
192 | * The only directory which does not have this bit set is the |
193 | * The only directory which does not have this bit set is the |
193 | * root directory itself. The root directory node is handled |
194 | * root directory itself. The root directory node is handled |
194 | * and initialized elsewhere. |
195 | * and initialized elsewhere. |
195 | */ |
196 | */ |
196 | nodep->type = FAT_DIRECTORY; |
197 | nodep->type = FAT_DIRECTORY; |
197 | /* |
198 | /* |
198 | * Unfortunately, the 'size' field of the FAT dentry is not |
199 | * Unfortunately, the 'size' field of the FAT dentry is not |
199 | * defined for the directory entry type. We must determine the |
200 | * defined for the directory entry type. We must determine the |
200 | * size of the directory by walking the FAT. |
201 | * size of the directory by walking the FAT. |
201 | */ |
202 | */ |
202 | nodep->size = bps * _fat_blcks_get(idxp->dev_handle, |
203 | nodep->size = bps * _fat_blcks_get(bb->data, idxp->dev_handle, |
203 | uint16_t_le2host(d->firstc), NULL); |
204 | uint16_t_le2host(d->firstc), NULL); |
204 | } else { |
205 | } else { |
205 | nodep->type = FAT_FILE; |
206 | nodep->type = FAT_FILE; |
206 | nodep->size = uint32_t_le2host(d->size); |
207 | nodep->size = uint32_t_le2host(d->size); |
207 | } |
208 | } |
208 | nodep->firstc = uint16_t_le2host(d->firstc); |
209 | nodep->firstc = uint16_t_le2host(d->firstc); |
209 | nodep->lnkcnt = 1; |
210 | nodep->lnkcnt = 1; |
210 | nodep->refcnt = 1; |
211 | nodep->refcnt = 1; |
211 | 212 | ||
212 | block_put(b); |
213 | block_put(b); |
- | 214 | block_put(bb); |
|
213 | 215 | ||
214 | /* Link the idx structure with the node structure. */ |
216 | /* Link the idx structure with the node structure. */ |
215 | nodep->idx = idxp; |
217 | nodep->idx = idxp; |
216 | idxp->nodep = nodep; |
218 | idxp->nodep = nodep; |
217 | 219 | ||
218 | return nodep; |
220 | return nodep; |
219 | } |
221 | } |
220 | 222 | ||
221 | /** Instantiate a FAT in-core node. */ |
223 | /** Instantiate a FAT in-core node. */ |
222 | static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index) |
224 | static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index) |
223 | { |
225 | { |
224 | void *node; |
226 | void *node; |
225 | fat_idx_t *idxp; |
227 | fat_idx_t *idxp; |
226 | 228 | ||
227 | idxp = fat_idx_get_by_index(dev_handle, index); |
229 | idxp = fat_idx_get_by_index(dev_handle, index); |
228 | if (!idxp) |
230 | if (!idxp) |
229 | return NULL; |
231 | return NULL; |
230 | /* idxp->lock held */ |
232 | /* idxp->lock held */ |
231 | node = fat_node_get_core(idxp); |
233 | node = fat_node_get_core(idxp); |
232 | futex_up(&idxp->lock); |
234 | futex_up(&idxp->lock); |
233 | return node; |
235 | return node; |
234 | } |
236 | } |
235 | 237 | ||
236 | static void fat_node_put(void *node) |
238 | static void fat_node_put(void *node) |
237 | { |
239 | { |
238 | fat_node_t *nodep = (fat_node_t *)node; |
240 | fat_node_t *nodep = (fat_node_t *)node; |
239 | 241 | ||
240 | futex_down(&nodep->lock); |
242 | futex_down(&nodep->lock); |
241 | if (!--nodep->refcnt) { |
243 | if (!--nodep->refcnt) { |
242 | futex_down(&ffn_futex); |
244 | futex_down(&ffn_futex); |
243 | list_append(&nodep->ffn_link, &ffn_head); |
245 | list_append(&nodep->ffn_link, &ffn_head); |
244 | futex_up(&ffn_futex); |
246 | futex_up(&ffn_futex); |
245 | } |
247 | } |
246 | futex_up(&nodep->lock); |
248 | futex_up(&nodep->lock); |
247 | } |
249 | } |
248 | 250 | ||
249 | static void *fat_create(int flags) |
251 | static void *fat_create(int flags) |
250 | { |
252 | { |
251 | return NULL; /* not supported at the moment */ |
253 | return NULL; /* not supported at the moment */ |
252 | } |
254 | } |
253 | 255 | ||
254 | static int fat_destroy(void *node) |
256 | static int fat_destroy(void *node) |
255 | { |
257 | { |
256 | return ENOTSUP; /* not supported at the moment */ |
258 | return ENOTSUP; /* not supported at the moment */ |
257 | } |
259 | } |
258 | 260 | ||
259 | static bool fat_link(void *prnt, void *chld, const char *name) |
261 | static bool fat_link(void *prnt, void *chld, const char *name) |
260 | { |
262 | { |
261 | return false; /* not supported at the moment */ |
263 | return false; /* not supported at the moment */ |
262 | } |
264 | } |
263 | 265 | ||
264 | static int fat_unlink(void *prnt, void *chld) |
266 | static int fat_unlink(void *prnt, void *chld) |
265 | { |
267 | { |
266 | return ENOTSUP; /* not supported at the moment */ |
268 | return ENOTSUP; /* not supported at the moment */ |
267 | } |
269 | } |
268 | 270 | ||
269 | static void *fat_match(void *prnt, const char *component) |
271 | static void *fat_match(void *prnt, const char *component) |
270 | { |
272 | { |
271 | fat_node_t *parentp = (fat_node_t *)prnt; |
273 | fat_node_t *parentp = (fat_node_t *)prnt; |
272 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
274 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
273 | unsigned i, j; |
275 | unsigned i, j; |
274 | unsigned bps; /* bytes per sector */ |
276 | unsigned bps; /* bytes per sector */ |
275 | unsigned dps; /* dentries per sector */ |
277 | unsigned dps; /* dentries per sector */ |
276 | unsigned blocks; |
278 | unsigned blocks; |
277 | fat_dentry_t *d; |
279 | fat_dentry_t *d; |
278 | block_t *b; |
280 | block_t *bb, *b; |
279 | 281 | ||
280 | futex_down(&parentp->idx->lock); |
282 | futex_down(&parentp->idx->lock); |
281 | bps = fat_bps_get(parentp->idx->dev_handle); |
283 | bb = block_get(parentp->idx->dev_handle, BS_BLOCK, BS_SIZE); |
- | 284 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
|
282 | dps = bps / sizeof(fat_dentry_t); |
285 | dps = bps / sizeof(fat_dentry_t); |
283 | blocks = parentp->size / bps + (parentp->size % bps != 0); |
286 | blocks = parentp->size / bps + (parentp->size % bps != 0); |
284 | for (i = 0; i < blocks; i++) { |
287 | for (i = 0; i < blocks; i++) { |
285 | unsigned dentries; |
288 | unsigned dentries; |
286 | 289 | ||
287 | b = fat_block_get(parentp, i); |
290 | b = fat_block_get(bb->data, parentp, i); |
288 | dentries = (i == blocks - 1) ? |
291 | dentries = (i == blocks - 1) ? |
289 | parentp->size % sizeof(fat_dentry_t) : |
292 | parentp->size % sizeof(fat_dentry_t) : |
290 | dps; |
293 | dps; |
291 | for (j = 0; j < dentries; j++) { |
294 | for (j = 0; j < dentries; j++) { |
292 | d = ((fat_dentry_t *)b->data) + j; |
295 | d = ((fat_dentry_t *)b->data) + j; |
293 | switch (fat_classify_dentry(d)) { |
296 | switch (fat_classify_dentry(d)) { |
294 | case FAT_DENTRY_SKIP: |
297 | case FAT_DENTRY_SKIP: |
295 | continue; |
298 | continue; |
296 | case FAT_DENTRY_LAST: |
299 | case FAT_DENTRY_LAST: |
297 | block_put(b); |
300 | block_put(b); |
- | 301 | block_put(bb); |
|
298 | futex_up(&parentp->idx->lock); |
302 | futex_up(&parentp->idx->lock); |
299 | return NULL; |
303 | return NULL; |
300 | default: |
304 | default: |
301 | case FAT_DENTRY_VALID: |
305 | case FAT_DENTRY_VALID: |
302 | dentry_name_canonify(d, name); |
306 | dentry_name_canonify(d, name); |
303 | break; |
307 | break; |
304 | } |
308 | } |
305 | if (stricmp(name, component) == 0) { |
309 | if (stricmp(name, component) == 0) { |
306 | /* hit */ |
310 | /* hit */ |
307 | void *node; |
311 | void *node; |
308 | /* |
312 | /* |
309 | * Assume tree hierarchy for locking. We |
313 | * Assume tree hierarchy for locking. We |
310 | * already have the parent and now we are going |
314 | * already have the parent and now we are going |
311 | * to lock the child. Never lock in the oposite |
315 | * to lock the child. Never lock in the oposite |
312 | * order. |
316 | * order. |
313 | */ |
317 | */ |
314 | fat_idx_t *idx = fat_idx_get_by_pos( |
318 | fat_idx_t *idx = fat_idx_get_by_pos( |
315 | parentp->idx->dev_handle, parentp->firstc, |
319 | parentp->idx->dev_handle, parentp->firstc, |
316 | i * dps + j); |
320 | i * dps + j); |
317 | futex_up(&parentp->idx->lock); |
321 | futex_up(&parentp->idx->lock); |
318 | if (!idx) { |
322 | if (!idx) { |
319 | /* |
323 | /* |
320 | * Can happen if memory is low or if we |
324 | * Can happen if memory is low or if we |
321 | * run out of 32-bit indices. |
325 | * run out of 32-bit indices. |
322 | */ |
326 | */ |
323 | block_put(b); |
327 | block_put(b); |
- | 328 | block_put(bb); |
|
324 | return NULL; |
329 | return NULL; |
325 | } |
330 | } |
326 | node = fat_node_get_core(idx); |
331 | node = fat_node_get_core(idx); |
327 | futex_up(&idx->lock); |
332 | futex_up(&idx->lock); |
328 | block_put(b); |
333 | block_put(b); |
- | 334 | block_put(bb); |
|
329 | return node; |
335 | return node; |
330 | } |
336 | } |
331 | } |
337 | } |
332 | block_put(b); |
338 | block_put(b); |
333 | } |
339 | } |
- | 340 | block_put(bb); |
|
- | 341 | ||
334 | futex_up(&parentp->idx->lock); |
342 | futex_up(&parentp->idx->lock); |
335 | return NULL; |
343 | return NULL; |
336 | } |
344 | } |
337 | 345 | ||
338 | static fs_index_t fat_index_get(void *node) |
346 | static fs_index_t fat_index_get(void *node) |
339 | { |
347 | { |
340 | fat_node_t *fnodep = (fat_node_t *)node; |
348 | fat_node_t *fnodep = (fat_node_t *)node; |
341 | if (!fnodep) |
349 | if (!fnodep) |
342 | return 0; |
350 | return 0; |
343 | return fnodep->idx->index; |
351 | return fnodep->idx->index; |
344 | } |
352 | } |
345 | 353 | ||
346 | static size_t fat_size_get(void *node) |
354 | static size_t fat_size_get(void *node) |
347 | { |
355 | { |
348 | return ((fat_node_t *)node)->size; |
356 | return ((fat_node_t *)node)->size; |
349 | } |
357 | } |
350 | 358 | ||
351 | static unsigned fat_lnkcnt_get(void *node) |
359 | static unsigned fat_lnkcnt_get(void *node) |
352 | { |
360 | { |
353 | return ((fat_node_t *)node)->lnkcnt; |
361 | return ((fat_node_t *)node)->lnkcnt; |
354 | } |
362 | } |
355 | 363 | ||
356 | static bool fat_has_children(void *node) |
364 | static bool fat_has_children(void *node) |
357 | { |
365 | { |
358 | fat_node_t *nodep = (fat_node_t *)node; |
366 | fat_node_t *nodep = (fat_node_t *)node; |
359 | unsigned bps; |
367 | unsigned bps; |
360 | unsigned dps; |
368 | unsigned dps; |
361 | unsigned blocks; |
369 | unsigned blocks; |
362 | block_t *b; |
370 | block_t *bb, *b; |
363 | unsigned i, j; |
371 | unsigned i, j; |
364 | 372 | ||
365 | if (nodep->type != FAT_DIRECTORY) |
373 | if (nodep->type != FAT_DIRECTORY) |
366 | return false; |
374 | return false; |
367 | 375 | ||
368 | futex_down(&nodep->idx->lock); |
376 | futex_down(&nodep->idx->lock); |
369 | bps = fat_bps_get(nodep->idx->dev_handle); |
377 | bb = block_get(nodep->idx->dev_handle, BS_BLOCK, BS_SIZE); |
- | 378 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
|
370 | dps = bps / sizeof(fat_dentry_t); |
379 | dps = bps / sizeof(fat_dentry_t); |
371 | 380 | ||
372 | blocks = nodep->size / bps + (nodep->size % bps != 0); |
381 | blocks = nodep->size / bps + (nodep->size % bps != 0); |
373 | 382 | ||
374 | for (i = 0; i < blocks; i++) { |
383 | for (i = 0; i < blocks; i++) { |
375 | unsigned dentries; |
384 | unsigned dentries; |
376 | fat_dentry_t *d; |
385 | fat_dentry_t *d; |
377 | 386 | ||
378 | b = fat_block_get(nodep, i); |
387 | b = fat_block_get(bb->data, nodep, i); |
379 | dentries = (i == blocks - 1) ? |
388 | dentries = (i == blocks - 1) ? |
380 | nodep->size % sizeof(fat_dentry_t) : |
389 | nodep->size % sizeof(fat_dentry_t) : |
381 | dps; |
390 | dps; |
382 | for (j = 0; j < dentries; j++) { |
391 | for (j = 0; j < dentries; j++) { |
383 | d = ((fat_dentry_t *)b->data) + j; |
392 | d = ((fat_dentry_t *)b->data) + j; |
384 | switch (fat_classify_dentry(d)) { |
393 | switch (fat_classify_dentry(d)) { |
385 | case FAT_DENTRY_SKIP: |
394 | case FAT_DENTRY_SKIP: |
386 | continue; |
395 | continue; |
387 | case FAT_DENTRY_LAST: |
396 | case FAT_DENTRY_LAST: |
388 | block_put(b); |
397 | block_put(b); |
- | 398 | block_put(bb); |
|
389 | futex_up(&nodep->idx->lock); |
399 | futex_up(&nodep->idx->lock); |
390 | return false; |
400 | return false; |
391 | default: |
401 | default: |
392 | case FAT_DENTRY_VALID: |
402 | case FAT_DENTRY_VALID: |
393 | block_put(b); |
403 | block_put(b); |
- | 404 | block_put(bb); |
|
394 | futex_up(&nodep->idx->lock); |
405 | futex_up(&nodep->idx->lock); |
395 | return true; |
406 | return true; |
396 | } |
407 | } |
397 | block_put(b); |
408 | block_put(b); |
- | 409 | block_put(bb); |
|
398 | futex_up(&nodep->idx->lock); |
410 | futex_up(&nodep->idx->lock); |
399 | return true; |
411 | return true; |
400 | } |
412 | } |
401 | block_put(b); |
413 | block_put(b); |
402 | } |
414 | } |
- | 415 | block_put(bb); |
|
403 | 416 | ||
404 | futex_up(&nodep->idx->lock); |
417 | futex_up(&nodep->idx->lock); |
405 | return false; |
418 | return false; |
406 | } |
419 | } |
407 | 420 | ||
408 | static void *fat_root_get(dev_handle_t dev_handle) |
421 | static void *fat_root_get(dev_handle_t dev_handle) |
409 | { |
422 | { |
410 | return fat_node_get(dev_handle, 0); |
423 | return fat_node_get(dev_handle, 0); |
411 | } |
424 | } |
412 | 425 | ||
413 | static char fat_plb_get_char(unsigned pos) |
426 | static char fat_plb_get_char(unsigned pos) |
414 | { |
427 | { |
415 | return fat_reg.plb_ro[pos % PLB_SIZE]; |
428 | return fat_reg.plb_ro[pos % PLB_SIZE]; |
416 | } |
429 | } |
417 | 430 | ||
418 | static bool fat_is_directory(void *node) |
431 | static bool fat_is_directory(void *node) |
419 | { |
432 | { |
420 | return ((fat_node_t *)node)->type == FAT_DIRECTORY; |
433 | return ((fat_node_t *)node)->type == FAT_DIRECTORY; |
421 | } |
434 | } |
422 | 435 | ||
423 | static bool fat_is_file(void *node) |
436 | static bool fat_is_file(void *node) |
424 | { |
437 | { |
425 | return ((fat_node_t *)node)->type == FAT_FILE; |
438 | return ((fat_node_t *)node)->type == FAT_FILE; |
426 | } |
439 | } |
427 | 440 | ||
428 | /** libfs operations */ |
441 | /** libfs operations */ |
429 | libfs_ops_t fat_libfs_ops = { |
442 | libfs_ops_t fat_libfs_ops = { |
430 | .match = fat_match, |
443 | .match = fat_match, |
431 | .node_get = fat_node_get, |
444 | .node_get = fat_node_get, |
432 | .node_put = fat_node_put, |
445 | .node_put = fat_node_put, |
433 | .create = fat_create, |
446 | .create = fat_create, |
434 | .destroy = fat_destroy, |
447 | .destroy = fat_destroy, |
435 | .link = fat_link, |
448 | .link = fat_link, |
436 | .unlink = fat_unlink, |
449 | .unlink = fat_unlink, |
437 | .index_get = fat_index_get, |
450 | .index_get = fat_index_get, |
438 | .size_get = fat_size_get, |
451 | .size_get = fat_size_get, |
439 | .lnkcnt_get = fat_lnkcnt_get, |
452 | .lnkcnt_get = fat_lnkcnt_get, |
440 | .has_children = fat_has_children, |
453 | .has_children = fat_has_children, |
441 | .root_get = fat_root_get, |
454 | .root_get = fat_root_get, |
442 | .plb_get_char = fat_plb_get_char, |
455 | .plb_get_char = fat_plb_get_char, |
443 | .is_directory = fat_is_directory, |
456 | .is_directory = fat_is_directory, |
444 | .is_file = fat_is_file |
457 | .is_file = fat_is_file |
445 | }; |
458 | }; |
446 | 459 | ||
447 | void fat_mounted(ipc_callid_t rid, ipc_call_t *request) |
460 | void fat_mounted(ipc_callid_t rid, ipc_call_t *request) |
448 | { |
461 | { |
449 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
462 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
450 | block_t *bb; |
463 | block_t *bb; |
451 | uint16_t bps; |
464 | uint16_t bps; |
452 | uint16_t rde; |
465 | uint16_t rde; |
453 | int rc; |
466 | int rc; |
454 | 467 | ||
455 | /* |
468 | /* |
456 | * For now, we don't bother to remember dev_handle, dev_phone or |
469 | * For now, we don't bother to remember dev_handle, dev_phone or |
457 | * dev_buffer in some data structure. We use global variables because we |
470 | * dev_buffer in some data structure. We use global variables because we |
458 | * know there will be at most one mount on this file system. |
471 | * know there will be at most one mount on this file system. |
459 | * Of course, this is a huge TODO item. |
472 | * Of course, this is a huge TODO item. |
460 | */ |
473 | */ |
461 | dev_buffer = mmap(NULL, BS_SIZE, PROTO_READ | PROTO_WRITE, |
474 | dev_buffer = mmap(NULL, BS_SIZE, PROTO_READ | PROTO_WRITE, |
462 | MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); |
475 | MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); |
463 | 476 | ||
464 | if (!dev_buffer) { |
477 | if (!dev_buffer) { |
465 | ipc_answer_0(rid, ENOMEM); |
478 | ipc_answer_0(rid, ENOMEM); |
466 | return; |
479 | return; |
467 | } |
480 | } |
468 | 481 | ||
469 | dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, |
482 | dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, |
470 | DEVMAP_CONNECT_TO_DEVICE, dev_handle); |
483 | DEVMAP_CONNECT_TO_DEVICE, dev_handle); |
471 | 484 | ||
472 | if (dev_phone < 0) { |
485 | if (dev_phone < 0) { |
473 | munmap(dev_buffer, BS_SIZE); |
486 | munmap(dev_buffer, BS_SIZE); |
474 | ipc_answer_0(rid, dev_phone); |
487 | ipc_answer_0(rid, dev_phone); |
475 | return; |
488 | return; |
476 | } |
489 | } |
477 | 490 | ||
478 | rc = ipc_share_out_start(dev_phone, dev_buffer, |
491 | rc = ipc_share_out_start(dev_phone, dev_buffer, |
479 | AS_AREA_READ | AS_AREA_WRITE); |
492 | AS_AREA_READ | AS_AREA_WRITE); |
480 | if (rc != EOK) { |
493 | if (rc != EOK) { |
481 | munmap(dev_buffer, BS_SIZE); |
494 | munmap(dev_buffer, BS_SIZE); |
482 | ipc_answer_0(rid, rc); |
495 | ipc_answer_0(rid, rc); |
483 | return; |
496 | return; |
484 | } |
497 | } |
485 | 498 | ||
486 | /* Read the number of root directory entries. */ |
499 | /* Read the number of root directory entries. */ |
487 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
500 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
488 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
501 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
489 | rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max); |
502 | rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max); |
490 | block_put(bb); |
503 | block_put(bb); |
491 | 504 | ||
492 | if (bps != BS_SIZE) { |
505 | if (bps != BS_SIZE) { |
493 | munmap(dev_buffer, BS_SIZE); |
506 | munmap(dev_buffer, BS_SIZE); |
494 | ipc_answer_0(rid, ENOTSUP); |
507 | ipc_answer_0(rid, ENOTSUP); |
495 | return; |
508 | return; |
496 | } |
509 | } |
497 | 510 | ||
498 | rc = fat_idx_init_by_dev_handle(dev_handle); |
511 | rc = fat_idx_init_by_dev_handle(dev_handle); |
499 | if (rc != EOK) { |
512 | if (rc != EOK) { |
500 | munmap(dev_buffer, BS_SIZE); |
513 | munmap(dev_buffer, BS_SIZE); |
501 | ipc_answer_0(rid, rc); |
514 | ipc_answer_0(rid, rc); |
502 | return; |
515 | return; |
503 | } |
516 | } |
504 | 517 | ||
505 | /* Initialize the root node. */ |
518 | /* Initialize the root node. */ |
506 | fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t)); |
519 | fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t)); |
507 | if (!rootp) { |
520 | if (!rootp) { |
508 | munmap(dev_buffer, BS_SIZE); |
521 | munmap(dev_buffer, BS_SIZE); |
509 | fat_idx_fini_by_dev_handle(dev_handle); |
522 | fat_idx_fini_by_dev_handle(dev_handle); |
510 | ipc_answer_0(rid, ENOMEM); |
523 | ipc_answer_0(rid, ENOMEM); |
511 | return; |
524 | return; |
512 | } |
525 | } |
513 | fat_node_initialize(rootp); |
526 | fat_node_initialize(rootp); |
514 | 527 | ||
515 | fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0); |
528 | fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0); |
516 | if (!ridxp) { |
529 | if (!ridxp) { |
517 | munmap(dev_buffer, BS_SIZE); |
530 | munmap(dev_buffer, BS_SIZE); |
518 | free(rootp); |
531 | free(rootp); |
519 | fat_idx_fini_by_dev_handle(dev_handle); |
532 | fat_idx_fini_by_dev_handle(dev_handle); |
520 | ipc_answer_0(rid, ENOMEM); |
533 | ipc_answer_0(rid, ENOMEM); |
521 | return; |
534 | return; |
522 | } |
535 | } |
523 | assert(ridxp->index == 0); |
536 | assert(ridxp->index == 0); |
524 | /* ridxp->lock held */ |
537 | /* ridxp->lock held */ |
525 | 538 | ||
526 | rootp->type = FAT_DIRECTORY; |
539 | rootp->type = FAT_DIRECTORY; |
527 | rootp->firstc = FAT_CLST_ROOT; |
540 | rootp->firstc = FAT_CLST_ROOT; |
528 | rootp->refcnt = 1; |
541 | rootp->refcnt = 1; |
529 | rootp->lnkcnt = 0; /* FS root is not linked */ |
542 | rootp->lnkcnt = 0; /* FS root is not linked */ |
530 | rootp->size = rde * sizeof(fat_dentry_t); |
543 | rootp->size = rde * sizeof(fat_dentry_t); |
531 | rootp->idx = ridxp; |
544 | rootp->idx = ridxp; |
532 | ridxp->nodep = rootp; |
545 | ridxp->nodep = rootp; |
533 | 546 | ||
534 | futex_up(&ridxp->lock); |
547 | futex_up(&ridxp->lock); |
535 | 548 | ||
536 | ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt); |
549 | ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt); |
537 | } |
550 | } |
538 | 551 | ||
539 | void fat_mount(ipc_callid_t rid, ipc_call_t *request) |
552 | void fat_mount(ipc_callid_t rid, ipc_call_t *request) |
540 | { |
553 | { |
541 | ipc_answer_0(rid, ENOTSUP); |
554 | ipc_answer_0(rid, ENOTSUP); |
542 | } |
555 | } |
543 | 556 | ||
544 | void fat_lookup(ipc_callid_t rid, ipc_call_t *request) |
557 | void fat_lookup(ipc_callid_t rid, ipc_call_t *request) |
545 | { |
558 | { |
546 | libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request); |
559 | libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request); |
547 | } |
560 | } |
548 | 561 | ||
549 | void fat_read(ipc_callid_t rid, ipc_call_t *request) |
562 | void fat_read(ipc_callid_t rid, ipc_call_t *request) |
550 | { |
563 | { |
551 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
564 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
552 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
565 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
553 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
566 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
554 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
567 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
555 | uint16_t bps = fat_bps_get(dev_handle); |
568 | uint16_t bps; |
556 | size_t bytes; |
569 | size_t bytes; |
557 | block_t *b; |
570 | block_t *bb, *b; |
558 | 571 | ||
559 | if (!nodep) { |
572 | if (!nodep) { |
560 | ipc_answer_0(rid, ENOENT); |
573 | ipc_answer_0(rid, ENOENT); |
561 | return; |
574 | return; |
562 | } |
575 | } |
563 | 576 | ||
564 | ipc_callid_t callid; |
577 | ipc_callid_t callid; |
565 | size_t len; |
578 | size_t len; |
566 | if (!ipc_data_read_receive(&callid, &len)) { |
579 | if (!ipc_data_read_receive(&callid, &len)) { |
567 | fat_node_put(nodep); |
580 | fat_node_put(nodep); |
568 | ipc_answer_0(callid, EINVAL); |
581 | ipc_answer_0(callid, EINVAL); |
569 | ipc_answer_0(rid, EINVAL); |
582 | ipc_answer_0(rid, EINVAL); |
570 | return; |
583 | return; |
571 | } |
584 | } |
572 | 585 | ||
- | 586 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
|
- | 587 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
|
- | 588 | ||
573 | if (nodep->type == FAT_FILE) { |
589 | if (nodep->type == FAT_FILE) { |
574 | /* |
590 | /* |
575 | * Our strategy for regular file reads is to read one block at |
591 | * Our strategy for regular file reads is to read one block at |
576 | * most and make use of the possibility to return less data than |
592 | * most and make use of the possibility to return less data than |
577 | * requested. This keeps the code very simple. |
593 | * requested. This keeps the code very simple. |
578 | */ |
594 | */ |
579 | bytes = min(len, bps - pos % bps); |
595 | bytes = min(len, bps - pos % bps); |
580 | b = fat_block_get(nodep, pos / bps); |
596 | b = fat_block_get(bb->data, nodep, pos / bps); |
581 | (void) ipc_data_read_finalize(callid, b->data + pos % bps, |
597 | (void) ipc_data_read_finalize(callid, b->data + pos % bps, |
582 | bytes); |
598 | bytes); |
583 | block_put(b); |
599 | block_put(b); |
584 | } else { |
600 | } else { |
585 | unsigned bnum; |
601 | unsigned bnum; |
586 | off_t spos = pos; |
602 | off_t spos = pos; |
587 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
603 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
588 | fat_dentry_t *d; |
604 | fat_dentry_t *d; |
589 | 605 | ||
590 | assert(nodep->type == FAT_DIRECTORY); |
606 | assert(nodep->type == FAT_DIRECTORY); |
591 | assert(nodep->size % bps == 0); |
607 | assert(nodep->size % bps == 0); |
592 | assert(bps % sizeof(fat_dentry_t) == 0); |
608 | assert(bps % sizeof(fat_dentry_t) == 0); |
593 | 609 | ||
594 | /* |
610 | /* |
595 | * Our strategy for readdir() is to use the position pointer as |
611 | * Our strategy for readdir() is to use the position pointer as |
596 | * an index into the array of all dentries. On entry, it points |
612 | * an index into the array of all dentries. On entry, it points |
597 | * to the first unread dentry. If we skip any dentries, we bump |
613 | * to the first unread dentry. If we skip any dentries, we bump |
598 | * the position pointer accordingly. |
614 | * the position pointer accordingly. |
599 | */ |
615 | */ |
600 | bnum = (pos * sizeof(fat_dentry_t)) / bps; |
616 | bnum = (pos * sizeof(fat_dentry_t)) / bps; |
601 | while (bnum < nodep->size / bps) { |
617 | while (bnum < nodep->size / bps) { |
602 | off_t o; |
618 | off_t o; |
603 | 619 | ||
604 | b = fat_block_get(nodep, bnum); |
620 | b = fat_block_get(bb->data, nodep, bnum); |
605 | for (o = pos % (bps / sizeof(fat_dentry_t)); |
621 | for (o = pos % (bps / sizeof(fat_dentry_t)); |
606 | o < bps / sizeof(fat_dentry_t); |
622 | o < bps / sizeof(fat_dentry_t); |
607 | o++, pos++) { |
623 | o++, pos++) { |
608 | d = ((fat_dentry_t *)b->data) + o; |
624 | d = ((fat_dentry_t *)b->data) + o; |
609 | switch (fat_classify_dentry(d)) { |
625 | switch (fat_classify_dentry(d)) { |
610 | case FAT_DENTRY_SKIP: |
626 | case FAT_DENTRY_SKIP: |
611 | continue; |
627 | continue; |
612 | case FAT_DENTRY_LAST: |
628 | case FAT_DENTRY_LAST: |
613 | block_put(b); |
629 | block_put(b); |
614 | goto miss; |
630 | goto miss; |
615 | default: |
631 | default: |
616 | case FAT_DENTRY_VALID: |
632 | case FAT_DENTRY_VALID: |
617 | dentry_name_canonify(d, name); |
633 | dentry_name_canonify(d, name); |
618 | block_put(b); |
634 | block_put(b); |
619 | goto hit; |
635 | goto hit; |
620 | } |
636 | } |
621 | } |
637 | } |
622 | block_put(b); |
638 | block_put(b); |
623 | bnum++; |
639 | bnum++; |
624 | } |
640 | } |
625 | miss: |
641 | miss: |
626 | fat_node_put(nodep); |
642 | fat_node_put(nodep); |
- | 643 | block_put(bb); |
|
627 | ipc_answer_0(callid, ENOENT); |
644 | ipc_answer_0(callid, ENOENT); |
628 | ipc_answer_1(rid, ENOENT, 0); |
645 | ipc_answer_1(rid, ENOENT, 0); |
629 | return; |
646 | return; |
630 | hit: |
647 | hit: |
631 | (void) ipc_data_read_finalize(callid, name, strlen(name) + 1); |
648 | (void) ipc_data_read_finalize(callid, name, strlen(name) + 1); |
632 | bytes = (pos - spos) + 1; |
649 | bytes = (pos - spos) + 1; |
633 | } |
650 | } |
634 | 651 | ||
635 | fat_node_put(nodep); |
652 | fat_node_put(nodep); |
- | 653 | block_put(bb); |
|
636 | ipc_answer_1(rid, EOK, (ipcarg_t)bytes); |
654 | ipc_answer_1(rid, EOK, (ipcarg_t)bytes); |
637 | } |
655 | } |
638 | 656 | ||
639 | void fat_write(ipc_callid_t rid, ipc_call_t *request) |
657 | void fat_write(ipc_callid_t rid, ipc_call_t *request) |
640 | { |
658 | { |
641 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
659 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
642 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
660 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
643 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
661 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
644 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
662 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
645 | size_t bytes; |
663 | size_t bytes; |
646 | block_t *b, *bb; |
664 | block_t *b, *bb; |
647 | uint16_t bps; |
665 | uint16_t bps; |
648 | unsigned spc; |
666 | unsigned spc; |
649 | off_t boundary; |
667 | off_t boundary; |
650 | 668 | ||
651 | if (!nodep) { |
669 | if (!nodep) { |
652 | ipc_answer_0(rid, ENOENT); |
670 | ipc_answer_0(rid, ENOENT); |
653 | return; |
671 | return; |
654 | } |
672 | } |
655 | 673 | ||
656 | /* XXX remove me when you are ready */ |
674 | /* XXX remove me when you are ready */ |
657 | { |
675 | { |
658 | ipc_answer_0(rid, ENOTSUP); |
676 | ipc_answer_0(rid, ENOTSUP); |
659 | fat_node_put(nodep); |
677 | fat_node_put(nodep); |
660 | return; |
678 | return; |
661 | } |
679 | } |
662 | 680 | ||
663 | ipc_callid_t callid; |
681 | ipc_callid_t callid; |
664 | size_t len; |
682 | size_t len; |
665 | if (!ipc_data_write_receive(&callid, &len)) { |
683 | if (!ipc_data_write_receive(&callid, &len)) { |
666 | fat_node_put(nodep); |
684 | fat_node_put(nodep); |
667 | ipc_answer_0(callid, EINVAL); |
685 | ipc_answer_0(callid, EINVAL); |
668 | ipc_answer_0(rid, EINVAL); |
686 | ipc_answer_0(rid, EINVAL); |
669 | return; |
687 | return; |
670 | } |
688 | } |
671 | 689 | ||
672 | /* |
690 | /* |
673 | * In all scenarios, we will attempt to write out only one block worth |
691 | * In all scenarios, we will attempt to write out only one block worth |
674 | * of data at maximum. There might be some more efficient approaches, |
692 | * of data at maximum. There might be some more efficient approaches, |
675 | * but this one greatly simplifies fat_write(). Note that we can afford |
693 | * but this one greatly simplifies fat_write(). Note that we can afford |
676 | * to do this because the client must be ready to handle the return |
694 | * to do this because the client must be ready to handle the return |
677 | * value signalizing a smaller number of bytes written. |
695 | * value signalizing a smaller number of bytes written. |
678 | */ |
696 | */ |
679 | bytes = min(len, bps - pos % bps); |
697 | bytes = min(len, bps - pos % bps); |
680 | 698 | ||
681 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
699 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
682 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
700 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
683 | spc = FAT_BS(bb)->spc; |
701 | spc = FAT_BS(bb)->spc; |
684 | block_put(bb); |
- | |
685 | 702 | ||
686 | boundary = ROUND_UP(nodep->size, bps * spc); |
703 | boundary = ROUND_UP(nodep->size, bps * spc); |
687 | if (pos < boundary) { |
704 | if (pos < boundary) { |
688 | /* |
705 | /* |
689 | * This is the easier case - we are either overwriting already |
706 | * This is the easier case - we are either overwriting already |
690 | * existing contents or writing behind the EOF, but still within |
707 | * existing contents or writing behind the EOF, but still within |
691 | * the limits of the last cluster. The node size may grow to the |
708 | * the limits of the last cluster. The node size may grow to the |
692 | * next block size boundary. |
709 | * next block size boundary. |
693 | */ |
710 | */ |
694 | fat_fill_gap(nodep, FAT_CLST_RES0, pos); |
711 | fat_fill_gap(bb->data, nodep, FAT_CLST_RES0, pos); |
695 | b = fat_block_get(nodep, pos / bps); |
712 | b = fat_block_get(bb->data, nodep, pos / bps); |
696 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
713 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
697 | bytes); |
714 | bytes); |
698 | b->dirty = true; /* need to sync block */ |
715 | b->dirty = true; /* need to sync block */ |
699 | block_put(b); |
716 | block_put(b); |
700 | if (pos + bytes > nodep->size) { |
717 | if (pos + bytes > nodep->size) { |
701 | nodep->size = pos + bytes; |
718 | nodep->size = pos + bytes; |
702 | nodep->dirty = true; /* need to sync node */ |
719 | nodep->dirty = true; /* need to sync node */ |
703 | } |
720 | } |
704 | fat_node_put(nodep); |
721 | fat_node_put(nodep); |
- | 722 | block_put(bb); |
|
705 | ipc_answer_1(rid, EOK, bytes); |
723 | ipc_answer_1(rid, EOK, bytes); |
706 | return; |
724 | return; |
707 | } else { |
725 | } else { |
708 | /* |
726 | /* |
709 | * This is the more difficult case. We must allocate new |
727 | * This is the more difficult case. We must allocate new |
710 | * clusters for the node and zero them out. |
728 | * clusters for the node and zero them out. |
711 | */ |
729 | */ |
712 | int status; |
730 | int status; |
713 | unsigned nclsts; |
731 | unsigned nclsts; |
714 | fat_cluster_t mcl, lcl; |
732 | fat_cluster_t mcl, lcl; |
715 | 733 | ||
716 | nclsts = (ROUND_UP(pos + bytes, bps * spc) - boundary) / |
734 | nclsts = (ROUND_UP(pos + bytes, bps * spc) - boundary) / |
717 | bps * spc; |
735 | bps * spc; |
718 | /* create an independent chain of nclsts clusters in all FATs */ |
736 | /* create an independent chain of nclsts clusters in all FATs */ |
719 | status = fat_alloc_clusters(dev_handle, nclsts, &mcl, &lcl); |
737 | status = fat_alloc_clusters(bb->data, dev_handle, nclsts, &mcl, |
- | 738 | &lcl); |
|
720 | if (status != EOK) { |
739 | if (status != EOK) { |
721 | /* could not allocate a chain of nclsts clusters */ |
740 | /* could not allocate a chain of nclsts clusters */ |
722 | fat_node_put(nodep); |
741 | fat_node_put(nodep); |
- | 742 | block_put(bb); |
|
723 | ipc_answer_0(callid, status); |
743 | ipc_answer_0(callid, status); |
724 | ipc_answer_0(rid, status); |
744 | ipc_answer_0(rid, status); |
725 | return; |
745 | return; |
726 | } |
746 | } |
727 | /* zero fill any gaps */ |
747 | /* zero fill any gaps */ |
728 | fat_fill_gap(nodep, mcl, pos); |
748 | fat_fill_gap(bb->data, nodep, mcl, pos); |
729 | b = _fat_block_get(dev_handle, lcl, (pos / bps) % spc); |
749 | b = _fat_block_get(bb->data, dev_handle, lcl, |
- | 750 | (pos / bps) % spc); |
|
730 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
751 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
731 | bytes); |
752 | bytes); |
732 | b->dirty = true; /* need to sync block */ |
753 | b->dirty = true; /* need to sync block */ |
733 | block_put(b); |
754 | block_put(b); |
734 | /* |
755 | /* |
735 | * Append the cluster chain starting in mcl to the end of the |
756 | * Append the cluster chain starting in mcl to the end of the |
736 | * node's cluster chain. |
757 | * node's cluster chain. |
737 | */ |
758 | */ |
738 | fat_append_clusters(nodep, mcl); |
759 | fat_append_clusters(bb->data, nodep, mcl); |
739 | nodep->size = pos + bytes; |
760 | nodep->size = pos + bytes; |
740 | nodep->dirty = true; /* need to sync node */ |
761 | nodep->dirty = true; /* need to sync node */ |
741 | fat_node_put(nodep); |
762 | fat_node_put(nodep); |
- | 763 | block_put(bb); |
|
742 | ipc_answer_1(rid, EOK, bytes); |
764 | ipc_answer_1(rid, EOK, bytes); |
743 | return; |
765 | return; |
744 | } |
766 | } |
745 | } |
767 | } |
746 | 768 | ||
747 | /** |
769 | /** |
748 | * @} |
770 | * @} |
749 | */ |
771 | */ |
750 | 772 |