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