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