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