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