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