Rev 3638 | Rev 3700 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3638 | Rev 3660 | ||
---|---|---|---|
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 | * We don't create the '.' and '..' entries, since they are |
- | |
324 | * optional and HelenOS VFS does not need them. |
- | |
325 | */ |
323 | */ |
326 | for (i = 0; i < bs->spc; i++) { |
324 | for (i = 0; i < bs->spc; i++) { |
327 | b = _fat_block_get(bs, dev_handle, mcl, i, |
325 | b = _fat_block_get(bs, dev_handle, mcl, i, |
328 | BLOCK_FLAGS_NOREAD); |
326 | BLOCK_FLAGS_NOREAD); |
329 | /* mark all dentries as never-used */ |
327 | /* mark all dentries as never-used */ |
330 | memset(b->data, 0, bps); |
328 | memset(b->data, 0, bps); |
331 | b->dirty = false; |
329 | b->dirty = false; |
332 | block_put(b); |
330 | block_put(b); |
333 | } |
331 | } |
334 | nodep->type = FAT_DIRECTORY; |
332 | nodep->type = FAT_DIRECTORY; |
335 | nodep->firstc = mcl; |
333 | nodep->firstc = mcl; |
336 | nodep->size = bps * bs->spc; |
334 | nodep->size = bps * bs->spc; |
337 | } else { |
335 | } else { |
338 | nodep->type = FAT_FILE; |
336 | nodep->type = FAT_FILE; |
339 | nodep->firstc = FAT_CLST_RES0; |
337 | nodep->firstc = FAT_CLST_RES0; |
340 | nodep->size = 0; |
338 | nodep->size = 0; |
341 | } |
339 | } |
342 | nodep->lnkcnt = 0; /* not linked anywhere */ |
340 | nodep->lnkcnt = 0; /* not linked anywhere */ |
343 | nodep->refcnt = 1; |
341 | nodep->refcnt = 1; |
344 | nodep->dirty = true; |
342 | nodep->dirty = true; |
345 | 343 | ||
346 | nodep->idx = idxp; |
344 | nodep->idx = idxp; |
347 | idxp->nodep = nodep; |
345 | idxp->nodep = nodep; |
348 | 346 | ||
349 | futex_up(&idxp->lock); |
347 | futex_up(&idxp->lock); |
350 | return nodep; |
348 | return nodep; |
351 | } |
349 | } |
352 | 350 | ||
353 | int fat_destroy_node(void *node) |
351 | int fat_destroy_node(void *node) |
354 | { |
352 | { |
355 | fat_node_t *nodep = (fat_node_t *)node; |
353 | fat_node_t *nodep = (fat_node_t *)node; |
356 | fat_bs_t *bs; |
354 | fat_bs_t *bs; |
357 | 355 | ||
358 | /* |
356 | /* |
359 | * 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 |
360 | * 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 |
361 | * 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 |
362 | * nor its index structure. |
360 | * nor its index structure. |
363 | */ |
361 | */ |
364 | assert(nodep->lnkcnt == 0); |
362 | assert(nodep->lnkcnt == 0); |
365 | 363 | ||
366 | /* |
364 | /* |
367 | * The node may not have any children. |
365 | * The node may not have any children. |
368 | */ |
366 | */ |
369 | assert(fat_has_children(node) == false); |
367 | assert(fat_has_children(node) == false); |
370 | 368 | ||
371 | bs = block_bb_get(nodep->idx->dev_handle); |
369 | bs = block_bb_get(nodep->idx->dev_handle); |
372 | if (nodep->firstc != FAT_CLST_RES0) { |
370 | if (nodep->firstc != FAT_CLST_RES0) { |
373 | assert(nodep->size); |
371 | assert(nodep->size); |
374 | /* Free all clusters allocated to the node. */ |
372 | /* Free all clusters allocated to the node. */ |
375 | fat_free_clusters(bs, nodep->idx->dev_handle, nodep->firstc); |
373 | fat_free_clusters(bs, nodep->idx->dev_handle, nodep->firstc); |
376 | } |
374 | } |
377 | 375 | ||
378 | fat_idx_destroy(nodep->idx); |
376 | fat_idx_destroy(nodep->idx); |
379 | free(nodep); |
377 | free(nodep); |
380 | return EOK; |
378 | return EOK; |
381 | } |
379 | } |
382 | 380 | ||
383 | int fat_link(void *prnt, void *chld, const char *name) |
381 | int fat_link(void *prnt, void *chld, const char *name) |
384 | { |
382 | { |
385 | fat_node_t *parentp = (fat_node_t *)prnt; |
383 | fat_node_t *parentp = (fat_node_t *)prnt; |
386 | fat_node_t *childp = (fat_node_t *)chld; |
384 | fat_node_t *childp = (fat_node_t *)chld; |
387 | fat_dentry_t *d; |
385 | fat_dentry_t *d; |
388 | fat_bs_t *bs; |
386 | fat_bs_t *bs; |
389 | block_t *b; |
387 | block_t *b; |
390 | int i, j; |
388 | int i, j; |
391 | uint16_t bps; |
389 | uint16_t bps; |
392 | unsigned dps; |
390 | unsigned dps; |
393 | unsigned blocks; |
391 | unsigned blocks; |
394 | 392 | ||
395 | futex_down(&childp->lock); |
393 | futex_down(&childp->lock); |
396 | if (childp->lnkcnt == 1) { |
394 | if (childp->lnkcnt == 1) { |
397 | /* |
395 | /* |
398 | * On FAT, we don't support multiple hard links. |
396 | * On FAT, we don't support multiple hard links. |
399 | */ |
397 | */ |
400 | futex_up(&childp->lock); |
398 | futex_up(&childp->lock); |
401 | return EMLINK; |
399 | return EMLINK; |
402 | } |
400 | } |
403 | assert(childp->lnkcnt == 0); |
401 | assert(childp->lnkcnt == 0); |
404 | futex_up(&childp->lock); |
402 | futex_up(&childp->lock); |
405 | 403 | ||
406 | if (!fat_dentry_name_verify(name)) { |
404 | if (!fat_dentry_name_verify(name)) { |
407 | /* |
405 | /* |
408 | * Attempt to create unsupported name. |
406 | * Attempt to create unsupported name. |
409 | */ |
407 | */ |
410 | return ENOTSUP; |
408 | return ENOTSUP; |
411 | } |
409 | } |
412 | 410 | ||
413 | /* |
411 | /* |
414 | * Get us an unused parent node's dentry or grow the parent and allocate |
412 | * Get us an unused parent node's dentry or grow the parent and allocate |
415 | * a new one. |
413 | * a new one. |
416 | */ |
414 | */ |
417 | 415 | ||
418 | futex_down(&parentp->idx->lock); |
416 | futex_down(&parentp->idx->lock); |
419 | bs = block_bb_get(parentp->idx->dev_handle); |
417 | bs = block_bb_get(parentp->idx->dev_handle); |
420 | bps = uint16_t_le2host(bs->bps); |
418 | bps = uint16_t_le2host(bs->bps); |
421 | dps = bps / sizeof(fat_dentry_t); |
419 | dps = bps / sizeof(fat_dentry_t); |
422 | 420 | ||
423 | blocks = parentp->size / bps; |
421 | blocks = parentp->size / bps; |
424 | 422 | ||
425 | for (i = 0; i < blocks; i++) { |
423 | for (i = 0; i < blocks; i++) { |
426 | b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE); |
424 | b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE); |
427 | for (j = 0; j < dps; j++) { |
425 | for (j = 0; j < dps; j++) { |
428 | d = ((fat_dentry_t *)b->data) + j; |
426 | d = ((fat_dentry_t *)b->data) + j; |
429 | switch (fat_classify_dentry(d)) { |
427 | switch (fat_classify_dentry(d)) { |
430 | case FAT_DENTRY_SKIP: |
428 | case FAT_DENTRY_SKIP: |
431 | case FAT_DENTRY_VALID: |
429 | case FAT_DENTRY_VALID: |
432 | /* skipping used and meta entries */ |
430 | /* skipping used and meta entries */ |
433 | continue; |
431 | continue; |
434 | case FAT_DENTRY_FREE: |
432 | case FAT_DENTRY_FREE: |
435 | case FAT_DENTRY_LAST: |
433 | case FAT_DENTRY_LAST: |
436 | /* found an empty slot */ |
434 | /* found an empty slot */ |
437 | goto hit; |
435 | goto hit; |
438 | } |
436 | } |
439 | } |
437 | } |
440 | block_put(b); |
438 | block_put(b); |
441 | } |
439 | } |
442 | 440 | ||
443 | /* |
441 | /* |
444 | * We need to grow the parent in order to create a new unused dentry. |
442 | * We need to grow the parent in order to create a new unused dentry. |
445 | */ |
443 | */ |
446 | futex_up(&parentp->idx->lock); |
444 | futex_up(&parentp->idx->lock); |
447 | return ENOTSUP; /* XXX */ |
445 | return ENOTSUP; /* XXX */ |
448 | 446 | ||
449 | hit: |
447 | hit: |
450 | /* |
448 | /* |
451 | * At this point we only establish the link between the parent and the |
449 | * At this point we only establish the link between the parent and the |
452 | * child. The dentry, except of the name and the extension, will remain |
450 | * child. The dentry, except of the name and the extension, will remain |
453 | * uninitialized until the the corresponding node is synced. Thus the |
451 | * uninitialized until the the corresponding node is synced. Thus the |
454 | * valid dentry data is kept in the child node structure. |
452 | * valid dentry data is kept in the child node structure. |
455 | */ |
453 | */ |
456 | memset(d, 0, sizeof(fat_dentry_t)); |
454 | memset(d, 0, sizeof(fat_dentry_t)); |
457 | fat_dentry_name_set(d, name); |
455 | fat_dentry_name_set(d, name); |
458 | b->dirty = true; /* need to sync block */ |
456 | b->dirty = true; /* need to sync block */ |
459 | block_put(b); |
457 | block_put(b); |
460 | futex_up(&parentp->idx->lock); |
458 | futex_up(&parentp->idx->lock); |
461 | 459 | ||
462 | futex_down(&childp->idx->lock); |
460 | futex_down(&childp->idx->lock); |
- | 461 | ||
- | 462 | /* |
|
- | 463 | * If possible, create the Sub-directory Identifier Entry and the |
|
- | 464 | * Sub-directory Parent Pointer Entry (i.e. "." and ".."). These entries |
|
- | 465 | * 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. |
|
- | 467 | */ |
|
- | 468 | b = fat_block_get(bs, childp, 0, BLOCK_FLAGS_NONE); |
|
- | 469 | d = (fat_dentry_t *)b->data; |
|
- | 470 | if (fat_classify_dentry(d) == FAT_DENTRY_LAST || |
|
- | 471 | strcmp(d->name, FAT_NAME_DOT) == 0) { |
|
- | 472 | memset(d, 0, sizeof(fat_dentry_t)); |
|
- | 473 | strcpy(d->name, FAT_NAME_DOT); |
|
- | 474 | strcpy(d->ext, FAT_EXT_PAD); |
|
- | 475 | d->attr = FAT_ATTR_SUBDIR; |
|
- | 476 | d->firstc = host2uint16_t_le(childp->firstc); |
|
- | 477 | /* TODO: initialize also the date/time members. */ |
|
- | 478 | } |
|
- | 479 | d++; |
|
- | 480 | if (fat_classify_dentry(d) == FAT_DENTRY_LAST || |
|
- | 481 | strcmp(d->name, FAT_NAME_DOT_DOT) == 0) { |
|
- | 482 | memset(d, 0, sizeof(fat_dentry_t)); |
|
- | 483 | strcpy(d->name, FAT_NAME_DOT_DOT); |
|
- | 484 | strcpy(d->ext, FAT_EXT_PAD); |
|
- | 485 | d->attr = FAT_ATTR_SUBDIR; |
|
- | 486 | d->firstc = (parentp->firstc == FAT_CLST_ROOT) ? |
|
- | 487 | host2uint16_t_le(FAT_CLST_RES0) : |
|
- | 488 | host2uint16_t_le(parentp->firstc); |
|
- | 489 | /* TODO: initialize also the date/time members. */ |
|
- | 490 | } |
|
- | 491 | b->dirty = true; /* need to sync block */ |
|
- | 492 | block_put(b); |
|
- | 493 | ||
463 | childp->idx->pfc = parentp->firstc; |
494 | childp->idx->pfc = parentp->firstc; |
464 | childp->idx->pdi = i * dps + j; |
495 | childp->idx->pdi = i * dps + j; |
465 | futex_up(&childp->idx->lock); |
496 | futex_up(&childp->idx->lock); |
466 | 497 | ||
467 | futex_down(&childp->lock); |
498 | futex_down(&childp->lock); |
468 | childp->lnkcnt = 1; |
499 | childp->lnkcnt = 1; |
469 | childp->dirty = true; /* need to sync node */ |
500 | childp->dirty = true; /* need to sync node */ |
470 | futex_up(&childp->lock); |
501 | futex_up(&childp->lock); |
471 | 502 | ||
472 | /* |
503 | /* |
473 | * Hash in the index structure into the position hash. |
504 | * Hash in the index structure into the position hash. |
474 | */ |
505 | */ |
475 | fat_idx_hashin(childp->idx); |
506 | fat_idx_hashin(childp->idx); |
476 | 507 | ||
477 | return EOK; |
508 | return EOK; |
478 | } |
509 | } |
479 | 510 | ||
480 | int fat_unlink(void *prnt, void *chld) |
511 | int fat_unlink(void *prnt, void *chld) |
481 | { |
512 | { |
482 | fat_node_t *parentp = (fat_node_t *)prnt; |
513 | fat_node_t *parentp = (fat_node_t *)prnt; |
483 | fat_node_t *childp = (fat_node_t *)chld; |
514 | fat_node_t *childp = (fat_node_t *)chld; |
484 | fat_bs_t *bs; |
515 | fat_bs_t *bs; |
485 | fat_dentry_t *d; |
516 | fat_dentry_t *d; |
486 | uint16_t bps; |
517 | uint16_t bps; |
487 | block_t *b; |
518 | block_t *b; |
488 | 519 | ||
489 | futex_down(&parentp->lock); |
520 | futex_down(&parentp->lock); |
490 | futex_down(&childp->lock); |
521 | futex_down(&childp->lock); |
491 | assert(childp->lnkcnt == 1); |
522 | assert(childp->lnkcnt == 1); |
492 | futex_down(&childp->idx->lock); |
523 | futex_down(&childp->idx->lock); |
493 | bs = block_bb_get(childp->idx->dev_handle); |
524 | bs = block_bb_get(childp->idx->dev_handle); |
494 | bps = uint16_t_le2host(bs->bps); |
525 | bps = uint16_t_le2host(bs->bps); |
495 | 526 | ||
496 | b = _fat_block_get(bs, childp->idx->dev_handle, childp->idx->pfc, |
527 | b = _fat_block_get(bs, childp->idx->dev_handle, childp->idx->pfc, |
497 | (childp->idx->pdi * sizeof(fat_dentry_t)) / bps, |
528 | (childp->idx->pdi * sizeof(fat_dentry_t)) / bps, |
498 | BLOCK_FLAGS_NONE); |
529 | BLOCK_FLAGS_NONE); |
499 | d = (fat_dentry_t *)b->data + |
530 | d = (fat_dentry_t *)b->data + |
500 | (childp->idx->pdi % (bps / sizeof(fat_dentry_t))); |
531 | (childp->idx->pdi % (bps / sizeof(fat_dentry_t))); |
501 | /* mark the dentry as not-currently-used */ |
532 | /* mark the dentry as not-currently-used */ |
502 | d->name[0] = FAT_DENTRY_ERASED; |
533 | d->name[0] = FAT_DENTRY_ERASED; |
503 | b->dirty = true; /* need to sync block */ |
534 | b->dirty = true; /* need to sync block */ |
504 | block_put(b); |
535 | block_put(b); |
505 | 536 | ||
506 | /* remove the index structure from the position hash */ |
537 | /* remove the index structure from the position hash */ |
507 | fat_idx_hashout(childp->idx); |
538 | fat_idx_hashout(childp->idx); |
508 | /* clear position information */ |
539 | /* clear position information */ |
509 | childp->idx->pfc = FAT_CLST_RES0; |
540 | childp->idx->pfc = FAT_CLST_RES0; |
510 | childp->idx->pdi = 0; |
541 | childp->idx->pdi = 0; |
511 | futex_up(&childp->idx->lock); |
542 | futex_up(&childp->idx->lock); |
512 | childp->lnkcnt = 0; |
543 | childp->lnkcnt = 0; |
513 | childp->dirty = true; |
544 | childp->dirty = true; |
514 | futex_up(&childp->lock); |
545 | futex_up(&childp->lock); |
515 | futex_up(&parentp->lock); |
546 | futex_up(&parentp->lock); |
516 | 547 | ||
517 | return EOK; |
548 | return EOK; |
518 | } |
549 | } |
519 | 550 | ||
520 | void *fat_match(void *prnt, const char *component) |
551 | void *fat_match(void *prnt, const char *component) |
521 | { |
552 | { |
522 | fat_bs_t *bs; |
553 | fat_bs_t *bs; |
523 | fat_node_t *parentp = (fat_node_t *)prnt; |
554 | fat_node_t *parentp = (fat_node_t *)prnt; |
524 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
555 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
525 | unsigned i, j; |
556 | unsigned i, j; |
526 | unsigned bps; /* bytes per sector */ |
557 | unsigned bps; /* bytes per sector */ |
527 | unsigned dps; /* dentries per sector */ |
558 | unsigned dps; /* dentries per sector */ |
528 | unsigned blocks; |
559 | unsigned blocks; |
529 | fat_dentry_t *d; |
560 | fat_dentry_t *d; |
530 | block_t *b; |
561 | block_t *b; |
531 | 562 | ||
532 | futex_down(&parentp->idx->lock); |
563 | futex_down(&parentp->idx->lock); |
533 | bs = block_bb_get(parentp->idx->dev_handle); |
564 | bs = block_bb_get(parentp->idx->dev_handle); |
534 | bps = uint16_t_le2host(bs->bps); |
565 | bps = uint16_t_le2host(bs->bps); |
535 | dps = bps / sizeof(fat_dentry_t); |
566 | dps = bps / sizeof(fat_dentry_t); |
536 | blocks = parentp->size / bps; |
567 | blocks = parentp->size / bps; |
537 | for (i = 0; i < blocks; i++) { |
568 | for (i = 0; i < blocks; i++) { |
538 | b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE); |
569 | b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE); |
539 | for (j = 0; j < dps; j++) { |
570 | for (j = 0; j < dps; j++) { |
540 | d = ((fat_dentry_t *)b->data) + j; |
571 | d = ((fat_dentry_t *)b->data) + j; |
541 | switch (fat_classify_dentry(d)) { |
572 | switch (fat_classify_dentry(d)) { |
542 | case FAT_DENTRY_SKIP: |
573 | case FAT_DENTRY_SKIP: |
543 | case FAT_DENTRY_FREE: |
574 | case FAT_DENTRY_FREE: |
544 | continue; |
575 | continue; |
545 | case FAT_DENTRY_LAST: |
576 | case FAT_DENTRY_LAST: |
546 | block_put(b); |
577 | block_put(b); |
547 | futex_up(&parentp->idx->lock); |
578 | futex_up(&parentp->idx->lock); |
548 | return NULL; |
579 | return NULL; |
549 | default: |
580 | default: |
550 | case FAT_DENTRY_VALID: |
581 | case FAT_DENTRY_VALID: |
551 | fat_dentry_name_get(d, name); |
582 | fat_dentry_name_get(d, name); |
552 | break; |
583 | break; |
553 | } |
584 | } |
554 | if (fat_dentry_namecmp(name, component) == 0) { |
585 | if (fat_dentry_namecmp(name, component) == 0) { |
555 | /* hit */ |
586 | /* hit */ |
556 | void *node; |
587 | void *node; |
557 | /* |
588 | /* |
558 | * Assume tree hierarchy for locking. We |
589 | * Assume tree hierarchy for locking. We |
559 | * already have the parent and now we are going |
590 | * already have the parent and now we are going |
560 | * to lock the child. Never lock in the oposite |
591 | * to lock the child. Never lock in the oposite |
561 | * order. |
592 | * order. |
562 | */ |
593 | */ |
563 | fat_idx_t *idx = fat_idx_get_by_pos( |
594 | fat_idx_t *idx = fat_idx_get_by_pos( |
564 | parentp->idx->dev_handle, parentp->firstc, |
595 | parentp->idx->dev_handle, parentp->firstc, |
565 | i * dps + j); |
596 | i * dps + j); |
566 | futex_up(&parentp->idx->lock); |
597 | futex_up(&parentp->idx->lock); |
567 | if (!idx) { |
598 | if (!idx) { |
568 | /* |
599 | /* |
569 | * Can happen if memory is low or if we |
600 | * Can happen if memory is low or if we |
570 | * run out of 32-bit indices. |
601 | * run out of 32-bit indices. |
571 | */ |
602 | */ |
572 | block_put(b); |
603 | block_put(b); |
573 | return NULL; |
604 | return NULL; |
574 | } |
605 | } |
575 | node = fat_node_get_core(idx); |
606 | node = fat_node_get_core(idx); |
576 | futex_up(&idx->lock); |
607 | futex_up(&idx->lock); |
577 | block_put(b); |
608 | block_put(b); |
578 | return node; |
609 | return node; |
579 | } |
610 | } |
580 | } |
611 | } |
581 | block_put(b); |
612 | block_put(b); |
582 | } |
613 | } |
583 | 614 | ||
584 | futex_up(&parentp->idx->lock); |
615 | futex_up(&parentp->idx->lock); |
585 | return NULL; |
616 | return NULL; |
586 | } |
617 | } |
587 | 618 | ||
588 | fs_index_t fat_index_get(void *node) |
619 | fs_index_t fat_index_get(void *node) |
589 | { |
620 | { |
590 | fat_node_t *fnodep = (fat_node_t *)node; |
621 | fat_node_t *fnodep = (fat_node_t *)node; |
591 | if (!fnodep) |
622 | if (!fnodep) |
592 | return 0; |
623 | return 0; |
593 | return fnodep->idx->index; |
624 | return fnodep->idx->index; |
594 | } |
625 | } |
595 | 626 | ||
596 | size_t fat_size_get(void *node) |
627 | size_t fat_size_get(void *node) |
597 | { |
628 | { |
598 | return ((fat_node_t *)node)->size; |
629 | return ((fat_node_t *)node)->size; |
599 | } |
630 | } |
600 | 631 | ||
601 | unsigned fat_lnkcnt_get(void *node) |
632 | unsigned fat_lnkcnt_get(void *node) |
602 | { |
633 | { |
603 | return ((fat_node_t *)node)->lnkcnt; |
634 | return ((fat_node_t *)node)->lnkcnt; |
604 | } |
635 | } |
605 | 636 | ||
606 | bool fat_has_children(void *node) |
637 | bool fat_has_children(void *node) |
607 | { |
638 | { |
608 | fat_bs_t *bs; |
639 | fat_bs_t *bs; |
609 | fat_node_t *nodep = (fat_node_t *)node; |
640 | fat_node_t *nodep = (fat_node_t *)node; |
610 | unsigned bps; |
641 | unsigned bps; |
611 | unsigned dps; |
642 | unsigned dps; |
612 | unsigned blocks; |
643 | unsigned blocks; |
613 | block_t *b; |
644 | block_t *b; |
614 | unsigned i, j; |
645 | unsigned i, j; |
615 | 646 | ||
616 | if (nodep->type != FAT_DIRECTORY) |
647 | if (nodep->type != FAT_DIRECTORY) |
617 | return false; |
648 | return false; |
618 | 649 | ||
619 | futex_down(&nodep->idx->lock); |
650 | futex_down(&nodep->idx->lock); |
620 | bs = block_bb_get(nodep->idx->dev_handle); |
651 | bs = block_bb_get(nodep->idx->dev_handle); |
621 | bps = uint16_t_le2host(bs->bps); |
652 | bps = uint16_t_le2host(bs->bps); |
622 | dps = bps / sizeof(fat_dentry_t); |
653 | dps = bps / sizeof(fat_dentry_t); |
623 | 654 | ||
624 | blocks = nodep->size / bps; |
655 | blocks = nodep->size / bps; |
625 | 656 | ||
626 | for (i = 0; i < blocks; i++) { |
657 | for (i = 0; i < blocks; i++) { |
627 | fat_dentry_t *d; |
658 | fat_dentry_t *d; |
628 | 659 | ||
629 | b = fat_block_get(bs, nodep, i, BLOCK_FLAGS_NONE); |
660 | b = fat_block_get(bs, nodep, i, BLOCK_FLAGS_NONE); |
630 | for (j = 0; j < dps; j++) { |
661 | for (j = 0; j < dps; j++) { |
631 | d = ((fat_dentry_t *)b->data) + j; |
662 | d = ((fat_dentry_t *)b->data) + j; |
632 | switch (fat_classify_dentry(d)) { |
663 | switch (fat_classify_dentry(d)) { |
633 | case FAT_DENTRY_SKIP: |
664 | case FAT_DENTRY_SKIP: |
634 | case FAT_DENTRY_FREE: |
665 | case FAT_DENTRY_FREE: |
635 | continue; |
666 | continue; |
636 | case FAT_DENTRY_LAST: |
667 | case FAT_DENTRY_LAST: |
637 | block_put(b); |
668 | block_put(b); |
638 | futex_up(&nodep->idx->lock); |
669 | futex_up(&nodep->idx->lock); |
639 | return false; |
670 | return false; |
640 | default: |
671 | default: |
641 | case FAT_DENTRY_VALID: |
672 | case FAT_DENTRY_VALID: |
642 | block_put(b); |
673 | block_put(b); |
643 | futex_up(&nodep->idx->lock); |
674 | futex_up(&nodep->idx->lock); |
644 | return true; |
675 | return true; |
645 | } |
676 | } |
646 | block_put(b); |
677 | block_put(b); |
647 | futex_up(&nodep->idx->lock); |
678 | futex_up(&nodep->idx->lock); |
648 | return true; |
679 | return true; |
649 | } |
680 | } |
650 | block_put(b); |
681 | block_put(b); |
651 | } |
682 | } |
652 | 683 | ||
653 | futex_up(&nodep->idx->lock); |
684 | futex_up(&nodep->idx->lock); |
654 | return false; |
685 | return false; |
655 | } |
686 | } |
656 | 687 | ||
657 | void *fat_root_get(dev_handle_t dev_handle) |
688 | void *fat_root_get(dev_handle_t dev_handle) |
658 | { |
689 | { |
659 | return fat_node_get(dev_handle, 0); |
690 | return fat_node_get(dev_handle, 0); |
660 | } |
691 | } |
661 | 692 | ||
662 | char fat_plb_get_char(unsigned pos) |
693 | char fat_plb_get_char(unsigned pos) |
663 | { |
694 | { |
664 | return fat_reg.plb_ro[pos % PLB_SIZE]; |
695 | return fat_reg.plb_ro[pos % PLB_SIZE]; |
665 | } |
696 | } |
666 | 697 | ||
667 | bool fat_is_directory(void *node) |
698 | bool fat_is_directory(void *node) |
668 | { |
699 | { |
669 | return ((fat_node_t *)node)->type == FAT_DIRECTORY; |
700 | return ((fat_node_t *)node)->type == FAT_DIRECTORY; |
670 | } |
701 | } |
671 | 702 | ||
672 | bool fat_is_file(void *node) |
703 | bool fat_is_file(void *node) |
673 | { |
704 | { |
674 | return ((fat_node_t *)node)->type == FAT_FILE; |
705 | return ((fat_node_t *)node)->type == FAT_FILE; |
675 | } |
706 | } |
676 | 707 | ||
677 | /** libfs operations */ |
708 | /** libfs operations */ |
678 | libfs_ops_t fat_libfs_ops = { |
709 | libfs_ops_t fat_libfs_ops = { |
679 | .match = fat_match, |
710 | .match = fat_match, |
680 | .node_get = fat_node_get, |
711 | .node_get = fat_node_get, |
681 | .node_put = fat_node_put, |
712 | .node_put = fat_node_put, |
682 | .create = fat_create_node, |
713 | .create = fat_create_node, |
683 | .destroy = fat_destroy_node, |
714 | .destroy = fat_destroy_node, |
684 | .link = fat_link, |
715 | .link = fat_link, |
685 | .unlink = fat_unlink, |
716 | .unlink = fat_unlink, |
686 | .index_get = fat_index_get, |
717 | .index_get = fat_index_get, |
687 | .size_get = fat_size_get, |
718 | .size_get = fat_size_get, |
688 | .lnkcnt_get = fat_lnkcnt_get, |
719 | .lnkcnt_get = fat_lnkcnt_get, |
689 | .has_children = fat_has_children, |
720 | .has_children = fat_has_children, |
690 | .root_get = fat_root_get, |
721 | .root_get = fat_root_get, |
691 | .plb_get_char = fat_plb_get_char, |
722 | .plb_get_char = fat_plb_get_char, |
692 | .is_directory = fat_is_directory, |
723 | .is_directory = fat_is_directory, |
693 | .is_file = fat_is_file |
724 | .is_file = fat_is_file |
694 | }; |
725 | }; |
695 | 726 | ||
696 | /* |
727 | /* |
697 | * VFS operations. |
728 | * VFS operations. |
698 | */ |
729 | */ |
699 | 730 | ||
700 | void fat_mounted(ipc_callid_t rid, ipc_call_t *request) |
731 | void fat_mounted(ipc_callid_t rid, ipc_call_t *request) |
701 | { |
732 | { |
702 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
733 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
703 | fat_bs_t *bs; |
734 | fat_bs_t *bs; |
704 | uint16_t bps; |
735 | uint16_t bps; |
705 | uint16_t rde; |
736 | uint16_t rde; |
706 | int rc; |
737 | int rc; |
707 | 738 | ||
708 | /* initialize libblock */ |
739 | /* initialize libblock */ |
709 | rc = block_init(dev_handle, BS_SIZE); |
740 | rc = block_init(dev_handle, BS_SIZE); |
710 | if (rc != EOK) { |
741 | if (rc != EOK) { |
711 | ipc_answer_0(rid, rc); |
742 | ipc_answer_0(rid, rc); |
712 | return; |
743 | return; |
713 | } |
744 | } |
714 | 745 | ||
715 | /* prepare the boot block */ |
746 | /* prepare the boot block */ |
716 | rc = block_bb_read(dev_handle, BS_BLOCK * BS_SIZE, BS_SIZE); |
747 | rc = block_bb_read(dev_handle, BS_BLOCK * BS_SIZE, BS_SIZE); |
717 | if (rc != EOK) { |
748 | if (rc != EOK) { |
718 | block_fini(dev_handle); |
749 | block_fini(dev_handle); |
719 | ipc_answer_0(rid, rc); |
750 | ipc_answer_0(rid, rc); |
720 | return; |
751 | return; |
721 | } |
752 | } |
722 | 753 | ||
723 | /* get the buffer with the boot sector */ |
754 | /* get the buffer with the boot sector */ |
724 | bs = block_bb_get(dev_handle); |
755 | bs = block_bb_get(dev_handle); |
725 | 756 | ||
726 | /* Read the number of root directory entries. */ |
757 | /* Read the number of root directory entries. */ |
727 | bps = uint16_t_le2host(bs->bps); |
758 | bps = uint16_t_le2host(bs->bps); |
728 | rde = uint16_t_le2host(bs->root_ent_max); |
759 | rde = uint16_t_le2host(bs->root_ent_max); |
729 | 760 | ||
730 | if (bps != BS_SIZE) { |
761 | if (bps != BS_SIZE) { |
731 | block_fini(dev_handle); |
762 | block_fini(dev_handle); |
732 | ipc_answer_0(rid, ENOTSUP); |
763 | ipc_answer_0(rid, ENOTSUP); |
733 | return; |
764 | return; |
734 | } |
765 | } |
735 | 766 | ||
736 | /* Initialize the block cache */ |
767 | /* Initialize the block cache */ |
737 | rc = block_cache_init(dev_handle, bps, 0 /* XXX */); |
768 | rc = block_cache_init(dev_handle, bps, 0 /* XXX */); |
738 | if (rc != EOK) { |
769 | if (rc != EOK) { |
739 | block_fini(dev_handle); |
770 | block_fini(dev_handle); |
740 | ipc_answer_0(rid, rc); |
771 | ipc_answer_0(rid, rc); |
741 | return; |
772 | return; |
742 | } |
773 | } |
743 | 774 | ||
744 | rc = fat_idx_init_by_dev_handle(dev_handle); |
775 | rc = fat_idx_init_by_dev_handle(dev_handle); |
745 | if (rc != EOK) { |
776 | if (rc != EOK) { |
746 | block_fini(dev_handle); |
777 | block_fini(dev_handle); |
747 | ipc_answer_0(rid, rc); |
778 | ipc_answer_0(rid, rc); |
748 | return; |
779 | return; |
749 | } |
780 | } |
750 | 781 | ||
751 | /* Initialize the root node. */ |
782 | /* Initialize the root node. */ |
752 | fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t)); |
783 | fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t)); |
753 | if (!rootp) { |
784 | if (!rootp) { |
754 | block_fini(dev_handle); |
785 | block_fini(dev_handle); |
755 | fat_idx_fini_by_dev_handle(dev_handle); |
786 | fat_idx_fini_by_dev_handle(dev_handle); |
756 | ipc_answer_0(rid, ENOMEM); |
787 | ipc_answer_0(rid, ENOMEM); |
757 | return; |
788 | return; |
758 | } |
789 | } |
759 | fat_node_initialize(rootp); |
790 | fat_node_initialize(rootp); |
760 | 791 | ||
761 | fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0); |
792 | fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0); |
762 | if (!ridxp) { |
793 | if (!ridxp) { |
763 | block_fini(dev_handle); |
794 | block_fini(dev_handle); |
764 | free(rootp); |
795 | free(rootp); |
765 | fat_idx_fini_by_dev_handle(dev_handle); |
796 | fat_idx_fini_by_dev_handle(dev_handle); |
766 | ipc_answer_0(rid, ENOMEM); |
797 | ipc_answer_0(rid, ENOMEM); |
767 | return; |
798 | return; |
768 | } |
799 | } |
769 | assert(ridxp->index == 0); |
800 | assert(ridxp->index == 0); |
770 | /* ridxp->lock held */ |
801 | /* ridxp->lock held */ |
771 | 802 | ||
772 | rootp->type = FAT_DIRECTORY; |
803 | rootp->type = FAT_DIRECTORY; |
773 | rootp->firstc = FAT_CLST_ROOT; |
804 | rootp->firstc = FAT_CLST_ROOT; |
774 | rootp->refcnt = 1; |
805 | rootp->refcnt = 1; |
775 | rootp->lnkcnt = 0; /* FS root is not linked */ |
806 | rootp->lnkcnt = 0; /* FS root is not linked */ |
776 | rootp->size = rde * sizeof(fat_dentry_t); |
807 | rootp->size = rde * sizeof(fat_dentry_t); |
777 | rootp->idx = ridxp; |
808 | rootp->idx = ridxp; |
778 | ridxp->nodep = rootp; |
809 | ridxp->nodep = rootp; |
779 | 810 | ||
780 | futex_up(&ridxp->lock); |
811 | futex_up(&ridxp->lock); |
781 | 812 | ||
782 | ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt); |
813 | ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt); |
783 | } |
814 | } |
784 | 815 | ||
785 | void fat_mount(ipc_callid_t rid, ipc_call_t *request) |
816 | void fat_mount(ipc_callid_t rid, ipc_call_t *request) |
786 | { |
817 | { |
787 | ipc_answer_0(rid, ENOTSUP); |
818 | ipc_answer_0(rid, ENOTSUP); |
788 | } |
819 | } |
789 | 820 | ||
790 | void fat_lookup(ipc_callid_t rid, ipc_call_t *request) |
821 | void fat_lookup(ipc_callid_t rid, ipc_call_t *request) |
791 | { |
822 | { |
792 | libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request); |
823 | libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request); |
793 | } |
824 | } |
794 | 825 | ||
795 | void fat_read(ipc_callid_t rid, ipc_call_t *request) |
826 | void fat_read(ipc_callid_t rid, ipc_call_t *request) |
796 | { |
827 | { |
797 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
828 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
798 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
829 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
799 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
830 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
800 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
831 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
801 | fat_bs_t *bs; |
832 | fat_bs_t *bs; |
802 | uint16_t bps; |
833 | uint16_t bps; |
803 | size_t bytes; |
834 | size_t bytes; |
804 | block_t *b; |
835 | block_t *b; |
805 | 836 | ||
806 | if (!nodep) { |
837 | if (!nodep) { |
807 | ipc_answer_0(rid, ENOENT); |
838 | ipc_answer_0(rid, ENOENT); |
808 | return; |
839 | return; |
809 | } |
840 | } |
810 | 841 | ||
811 | ipc_callid_t callid; |
842 | ipc_callid_t callid; |
812 | size_t len; |
843 | size_t len; |
813 | if (!ipc_data_read_receive(&callid, &len)) { |
844 | if (!ipc_data_read_receive(&callid, &len)) { |
814 | fat_node_put(nodep); |
845 | fat_node_put(nodep); |
815 | ipc_answer_0(callid, EINVAL); |
846 | ipc_answer_0(callid, EINVAL); |
816 | ipc_answer_0(rid, EINVAL); |
847 | ipc_answer_0(rid, EINVAL); |
817 | return; |
848 | return; |
818 | } |
849 | } |
819 | 850 | ||
820 | bs = block_bb_get(dev_handle); |
851 | bs = block_bb_get(dev_handle); |
821 | bps = uint16_t_le2host(bs->bps); |
852 | bps = uint16_t_le2host(bs->bps); |
822 | 853 | ||
823 | if (nodep->type == FAT_FILE) { |
854 | if (nodep->type == FAT_FILE) { |
824 | /* |
855 | /* |
825 | * Our strategy for regular file reads is to read one block at |
856 | * Our strategy for regular file reads is to read one block at |
826 | * most and make use of the possibility to return less data than |
857 | * most and make use of the possibility to return less data than |
827 | * requested. This keeps the code very simple. |
858 | * requested. This keeps the code very simple. |
828 | */ |
859 | */ |
829 | if (pos >= nodep->size) { |
860 | if (pos >= nodep->size) { |
830 | /* reading beyond the EOF */ |
861 | /* reading beyond the EOF */ |
831 | bytes = 0; |
862 | bytes = 0; |
832 | (void) ipc_data_read_finalize(callid, NULL, 0); |
863 | (void) ipc_data_read_finalize(callid, NULL, 0); |
833 | } else { |
864 | } else { |
834 | bytes = min(len, bps - pos % bps); |
865 | bytes = min(len, bps - pos % bps); |
835 | bytes = min(bytes, nodep->size - pos); |
866 | bytes = min(bytes, nodep->size - pos); |
836 | b = fat_block_get(bs, nodep, pos / bps, |
867 | b = fat_block_get(bs, nodep, pos / bps, |
837 | BLOCK_FLAGS_NONE); |
868 | BLOCK_FLAGS_NONE); |
838 | (void) ipc_data_read_finalize(callid, b->data + pos % bps, |
869 | (void) ipc_data_read_finalize(callid, b->data + pos % bps, |
839 | bytes); |
870 | bytes); |
840 | block_put(b); |
871 | block_put(b); |
841 | } |
872 | } |
842 | } else { |
873 | } else { |
843 | unsigned bnum; |
874 | unsigned bnum; |
844 | off_t spos = pos; |
875 | off_t spos = pos; |
845 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
876 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
846 | fat_dentry_t *d; |
877 | fat_dentry_t *d; |
847 | 878 | ||
848 | assert(nodep->type == FAT_DIRECTORY); |
879 | assert(nodep->type == FAT_DIRECTORY); |
849 | assert(nodep->size % bps == 0); |
880 | assert(nodep->size % bps == 0); |
850 | assert(bps % sizeof(fat_dentry_t) == 0); |
881 | assert(bps % sizeof(fat_dentry_t) == 0); |
851 | 882 | ||
852 | /* |
883 | /* |
853 | * Our strategy for readdir() is to use the position pointer as |
884 | * Our strategy for readdir() is to use the position pointer as |
854 | * an index into the array of all dentries. On entry, it points |
885 | * an index into the array of all dentries. On entry, it points |
855 | * to the first unread dentry. If we skip any dentries, we bump |
886 | * to the first unread dentry. If we skip any dentries, we bump |
856 | * the position pointer accordingly. |
887 | * the position pointer accordingly. |
857 | */ |
888 | */ |
858 | bnum = (pos * sizeof(fat_dentry_t)) / bps; |
889 | bnum = (pos * sizeof(fat_dentry_t)) / bps; |
859 | while (bnum < nodep->size / bps) { |
890 | while (bnum < nodep->size / bps) { |
860 | off_t o; |
891 | off_t o; |
861 | 892 | ||
862 | b = fat_block_get(bs, nodep, bnum, BLOCK_FLAGS_NONE); |
893 | b = fat_block_get(bs, nodep, bnum, BLOCK_FLAGS_NONE); |
863 | for (o = pos % (bps / sizeof(fat_dentry_t)); |
894 | for (o = pos % (bps / sizeof(fat_dentry_t)); |
864 | o < bps / sizeof(fat_dentry_t); |
895 | o < bps / sizeof(fat_dentry_t); |
865 | o++, pos++) { |
896 | o++, pos++) { |
866 | d = ((fat_dentry_t *)b->data) + o; |
897 | d = ((fat_dentry_t *)b->data) + o; |
867 | switch (fat_classify_dentry(d)) { |
898 | switch (fat_classify_dentry(d)) { |
868 | case FAT_DENTRY_SKIP: |
899 | case FAT_DENTRY_SKIP: |
869 | case FAT_DENTRY_FREE: |
900 | case FAT_DENTRY_FREE: |
870 | continue; |
901 | continue; |
871 | case FAT_DENTRY_LAST: |
902 | case FAT_DENTRY_LAST: |
872 | block_put(b); |
903 | block_put(b); |
873 | goto miss; |
904 | goto miss; |
874 | default: |
905 | default: |
875 | case FAT_DENTRY_VALID: |
906 | case FAT_DENTRY_VALID: |
876 | fat_dentry_name_get(d, name); |
907 | fat_dentry_name_get(d, name); |
877 | block_put(b); |
908 | block_put(b); |
878 | goto hit; |
909 | goto hit; |
879 | } |
910 | } |
880 | } |
911 | } |
881 | block_put(b); |
912 | block_put(b); |
882 | bnum++; |
913 | bnum++; |
883 | } |
914 | } |
884 | miss: |
915 | miss: |
885 | fat_node_put(nodep); |
916 | fat_node_put(nodep); |
886 | ipc_answer_0(callid, ENOENT); |
917 | ipc_answer_0(callid, ENOENT); |
887 | ipc_answer_1(rid, ENOENT, 0); |
918 | ipc_answer_1(rid, ENOENT, 0); |
888 | return; |
919 | return; |
889 | hit: |
920 | hit: |
890 | (void) ipc_data_read_finalize(callid, name, strlen(name) + 1); |
921 | (void) ipc_data_read_finalize(callid, name, strlen(name) + 1); |
891 | bytes = (pos - spos) + 1; |
922 | bytes = (pos - spos) + 1; |
892 | } |
923 | } |
893 | 924 | ||
894 | fat_node_put(nodep); |
925 | fat_node_put(nodep); |
895 | ipc_answer_1(rid, EOK, (ipcarg_t)bytes); |
926 | ipc_answer_1(rid, EOK, (ipcarg_t)bytes); |
896 | } |
927 | } |
897 | 928 | ||
898 | void fat_write(ipc_callid_t rid, ipc_call_t *request) |
929 | void fat_write(ipc_callid_t rid, ipc_call_t *request) |
899 | { |
930 | { |
900 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
931 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
901 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
932 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
902 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
933 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
903 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
934 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
904 | fat_bs_t *bs; |
935 | fat_bs_t *bs; |
905 | size_t bytes; |
936 | size_t bytes; |
906 | block_t *b; |
937 | block_t *b; |
907 | uint16_t bps; |
938 | uint16_t bps; |
908 | unsigned spc; |
939 | unsigned spc; |
909 | unsigned bpc; /* bytes per cluster */ |
940 | unsigned bpc; /* bytes per cluster */ |
910 | off_t boundary; |
941 | off_t boundary; |
911 | int flags = BLOCK_FLAGS_NONE; |
942 | int flags = BLOCK_FLAGS_NONE; |
912 | 943 | ||
913 | if (!nodep) { |
944 | if (!nodep) { |
914 | ipc_answer_0(rid, ENOENT); |
945 | ipc_answer_0(rid, ENOENT); |
915 | return; |
946 | return; |
916 | } |
947 | } |
917 | 948 | ||
918 | ipc_callid_t callid; |
949 | ipc_callid_t callid; |
919 | size_t len; |
950 | size_t len; |
920 | if (!ipc_data_write_receive(&callid, &len)) { |
951 | if (!ipc_data_write_receive(&callid, &len)) { |
921 | fat_node_put(nodep); |
952 | fat_node_put(nodep); |
922 | ipc_answer_0(callid, EINVAL); |
953 | ipc_answer_0(callid, EINVAL); |
923 | ipc_answer_0(rid, EINVAL); |
954 | ipc_answer_0(rid, EINVAL); |
924 | return; |
955 | return; |
925 | } |
956 | } |
926 | 957 | ||
927 | bs = block_bb_get(dev_handle); |
958 | bs = block_bb_get(dev_handle); |
928 | bps = uint16_t_le2host(bs->bps); |
959 | bps = uint16_t_le2host(bs->bps); |
929 | spc = bs->spc; |
960 | spc = bs->spc; |
930 | bpc = bps * spc; |
961 | bpc = bps * spc; |
931 | 962 | ||
932 | /* |
963 | /* |
933 | * In all scenarios, we will attempt to write out only one block worth |
964 | * In all scenarios, we will attempt to write out only one block worth |
934 | * of data at maximum. There might be some more efficient approaches, |
965 | * of data at maximum. There might be some more efficient approaches, |
935 | * but this one greatly simplifies fat_write(). Note that we can afford |
966 | * but this one greatly simplifies fat_write(). Note that we can afford |
936 | * to do this because the client must be ready to handle the return |
967 | * to do this because the client must be ready to handle the return |
937 | * value signalizing a smaller number of bytes written. |
968 | * value signalizing a smaller number of bytes written. |
938 | */ |
969 | */ |
939 | bytes = min(len, bps - pos % bps); |
970 | bytes = min(len, bps - pos % bps); |
940 | if (bytes == bps) |
971 | if (bytes == bps) |
941 | flags |= BLOCK_FLAGS_NOREAD; |
972 | flags |= BLOCK_FLAGS_NOREAD; |
942 | 973 | ||
943 | boundary = ROUND_UP(nodep->size, bpc); |
974 | boundary = ROUND_UP(nodep->size, bpc); |
944 | if (pos < boundary) { |
975 | if (pos < boundary) { |
945 | /* |
976 | /* |
946 | * This is the easier case - we are either overwriting already |
977 | * This is the easier case - we are either overwriting already |
947 | * existing contents or writing behind the EOF, but still within |
978 | * existing contents or writing behind the EOF, but still within |
948 | * the limits of the last cluster. The node size may grow to the |
979 | * the limits of the last cluster. The node size may grow to the |
949 | * next block size boundary. |
980 | * next block size boundary. |
950 | */ |
981 | */ |
951 | fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos); |
982 | fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos); |
952 | b = fat_block_get(bs, nodep, pos / bps, flags); |
983 | b = fat_block_get(bs, nodep, pos / bps, flags); |
953 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
984 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
954 | bytes); |
985 | bytes); |
955 | b->dirty = true; /* need to sync block */ |
986 | b->dirty = true; /* need to sync block */ |
956 | block_put(b); |
987 | block_put(b); |
957 | if (pos + bytes > nodep->size) { |
988 | if (pos + bytes > nodep->size) { |
958 | nodep->size = pos + bytes; |
989 | nodep->size = pos + bytes; |
959 | nodep->dirty = true; /* need to sync node */ |
990 | nodep->dirty = true; /* need to sync node */ |
960 | } |
991 | } |
961 | ipc_answer_2(rid, EOK, bytes, nodep->size); |
992 | ipc_answer_2(rid, EOK, bytes, nodep->size); |
962 | fat_node_put(nodep); |
993 | fat_node_put(nodep); |
963 | return; |
994 | return; |
964 | } else { |
995 | } else { |
965 | /* |
996 | /* |
966 | * This is the more difficult case. We must allocate new |
997 | * This is the more difficult case. We must allocate new |
967 | * clusters for the node and zero them out. |
998 | * clusters for the node and zero them out. |
968 | */ |
999 | */ |
969 | int status; |
1000 | int status; |
970 | unsigned nclsts; |
1001 | unsigned nclsts; |
971 | fat_cluster_t mcl, lcl; |
1002 | fat_cluster_t mcl, lcl; |
972 | 1003 | ||
973 | nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc; |
1004 | nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc; |
974 | /* create an independent chain of nclsts clusters in all FATs */ |
1005 | /* create an independent chain of nclsts clusters in all FATs */ |
975 | status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl); |
1006 | status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl); |
976 | if (status != EOK) { |
1007 | if (status != EOK) { |
977 | /* could not allocate a chain of nclsts clusters */ |
1008 | /* could not allocate a chain of nclsts clusters */ |
978 | fat_node_put(nodep); |
1009 | fat_node_put(nodep); |
979 | ipc_answer_0(callid, status); |
1010 | ipc_answer_0(callid, status); |
980 | ipc_answer_0(rid, status); |
1011 | ipc_answer_0(rid, status); |
981 | return; |
1012 | return; |
982 | } |
1013 | } |
983 | /* zero fill any gaps */ |
1014 | /* zero fill any gaps */ |
984 | fat_fill_gap(bs, nodep, mcl, pos); |
1015 | fat_fill_gap(bs, nodep, mcl, pos); |
985 | b = _fat_block_get(bs, dev_handle, lcl, (pos / bps) % spc, |
1016 | b = _fat_block_get(bs, dev_handle, lcl, (pos / bps) % spc, |
986 | flags); |
1017 | flags); |
987 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
1018 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
988 | bytes); |
1019 | bytes); |
989 | b->dirty = true; /* need to sync block */ |
1020 | b->dirty = true; /* need to sync block */ |
990 | block_put(b); |
1021 | block_put(b); |
991 | /* |
1022 | /* |
992 | * Append the cluster chain starting in mcl to the end of the |
1023 | * Append the cluster chain starting in mcl to the end of the |
993 | * node's cluster chain. |
1024 | * node's cluster chain. |
994 | */ |
1025 | */ |
995 | fat_append_clusters(bs, nodep, mcl); |
1026 | fat_append_clusters(bs, nodep, mcl); |
996 | nodep->size = pos + bytes; |
1027 | nodep->size = pos + bytes; |
997 | nodep->dirty = true; /* need to sync node */ |
1028 | nodep->dirty = true; /* need to sync node */ |
998 | ipc_answer_2(rid, EOK, bytes, nodep->size); |
1029 | ipc_answer_2(rid, EOK, bytes, nodep->size); |
999 | fat_node_put(nodep); |
1030 | fat_node_put(nodep); |
1000 | return; |
1031 | return; |
1001 | } |
1032 | } |
1002 | } |
1033 | } |
1003 | 1034 | ||
1004 | void fat_truncate(ipc_callid_t rid, ipc_call_t *request) |
1035 | void fat_truncate(ipc_callid_t rid, ipc_call_t *request) |
1005 | { |
1036 | { |
1006 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
1037 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
1007 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
1038 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
1008 | size_t size = (off_t)IPC_GET_ARG3(*request); |
1039 | size_t size = (off_t)IPC_GET_ARG3(*request); |
1009 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
1040 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
1010 | fat_bs_t *bs; |
1041 | fat_bs_t *bs; |
1011 | uint16_t bps; |
1042 | uint16_t bps; |
1012 | uint8_t spc; |
1043 | uint8_t spc; |
1013 | unsigned bpc; /* bytes per cluster */ |
1044 | unsigned bpc; /* bytes per cluster */ |
1014 | int rc; |
1045 | int rc; |
1015 | 1046 | ||
1016 | if (!nodep) { |
1047 | if (!nodep) { |
1017 | ipc_answer_0(rid, ENOENT); |
1048 | ipc_answer_0(rid, ENOENT); |
1018 | return; |
1049 | return; |
1019 | } |
1050 | } |
1020 | 1051 | ||
1021 | bs = block_bb_get(dev_handle); |
1052 | bs = block_bb_get(dev_handle); |
1022 | bps = uint16_t_le2host(bs->bps); |
1053 | bps = uint16_t_le2host(bs->bps); |
1023 | spc = bs->spc; |
1054 | spc = bs->spc; |
1024 | bpc = bps * spc; |
1055 | bpc = bps * spc; |
1025 | 1056 | ||
1026 | if (nodep->size == size) { |
1057 | if (nodep->size == size) { |
1027 | rc = EOK; |
1058 | rc = EOK; |
1028 | } else if (nodep->size < size) { |
1059 | } else if (nodep->size < size) { |
1029 | /* |
1060 | /* |
1030 | * The standard says we have the freedom to grow the node. |
1061 | * The standard says we have the freedom to grow the node. |
1031 | * For now, we simply return an error. |
1062 | * For now, we simply return an error. |
1032 | */ |
1063 | */ |
1033 | rc = EINVAL; |
1064 | rc = EINVAL; |
1034 | } else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) { |
1065 | } else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) { |
1035 | /* |
1066 | /* |
1036 | * The node will be shrunk, but no clusters will be deallocated. |
1067 | * The node will be shrunk, but no clusters will be deallocated. |
1037 | */ |
1068 | */ |
1038 | nodep->size = size; |
1069 | nodep->size = size; |
1039 | nodep->dirty = true; /* need to sync node */ |
1070 | nodep->dirty = true; /* need to sync node */ |
1040 | rc = EOK; |
1071 | rc = EOK; |
1041 | } else { |
1072 | } else { |
1042 | /* |
1073 | /* |
1043 | * The node will be shrunk, clusters will be deallocated. |
1074 | * The node will be shrunk, clusters will be deallocated. |
1044 | */ |
1075 | */ |
1045 | if (size == 0) { |
1076 | if (size == 0) { |
1046 | fat_chop_clusters(bs, nodep, FAT_CLST_RES0); |
1077 | fat_chop_clusters(bs, nodep, FAT_CLST_RES0); |
1047 | } else { |
1078 | } else { |
1048 | fat_cluster_t lastc; |
1079 | fat_cluster_t lastc; |
1049 | (void) fat_cluster_walk(bs, dev_handle, nodep->firstc, |
1080 | (void) fat_cluster_walk(bs, dev_handle, nodep->firstc, |
1050 | &lastc, (size - 1) / bpc); |
1081 | &lastc, (size - 1) / bpc); |
1051 | fat_chop_clusters(bs, nodep, lastc); |
1082 | fat_chop_clusters(bs, nodep, lastc); |
1052 | } |
1083 | } |
1053 | nodep->size = size; |
1084 | nodep->size = size; |
1054 | nodep->dirty = true; /* need to sync node */ |
1085 | nodep->dirty = true; /* need to sync node */ |
1055 | rc = EOK; |
1086 | rc = EOK; |
1056 | } |
1087 | } |
1057 | fat_node_put(nodep); |
1088 | fat_node_put(nodep); |
1058 | ipc_answer_0(rid, rc); |
1089 | ipc_answer_0(rid, rc); |
1059 | return; |
1090 | return; |
1060 | } |
1091 | } |
1061 | 1092 | ||
1062 | void fat_destroy(ipc_callid_t rid, ipc_call_t *request) |
1093 | void fat_destroy(ipc_callid_t rid, ipc_call_t *request) |
1063 | { |
1094 | { |
1064 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
1095 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
1065 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
1096 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
1066 | int rc; |
1097 | int rc; |
1067 | 1098 | ||
1068 | fat_node_t *nodep = fat_node_get(dev_handle, index); |
1099 | fat_node_t *nodep = fat_node_get(dev_handle, index); |
1069 | if (!nodep) { |
1100 | if (!nodep) { |
1070 | ipc_answer_0(rid, ENOENT); |
1101 | ipc_answer_0(rid, ENOENT); |
1071 | return; |
1102 | return; |
1072 | } |
1103 | } |
1073 | 1104 | ||
1074 | rc = fat_destroy_node(nodep); |
1105 | rc = fat_destroy_node(nodep); |
1075 | ipc_answer_0(rid, rc); |
1106 | ipc_answer_0(rid, rc); |
1076 | } |
1107 | } |
1077 | 1108 | ||
1078 | /** |
1109 | /** |
1079 | * @} |
1110 | * @} |
1080 | */ |
1111 | */ |
1081 | 1112 |