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