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