Rev 3629 | Rev 3634 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3629 | Rev 3633 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Jakub Jermar |
2 | * Copyright (c) 2008 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup fs |
29 | /** @addtogroup fs |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file fat_ops.c |
34 | * @file fat_ops.c |
35 | * @brief Implementation of VFS operations for the FAT file system server. |
35 | * @brief Implementation of VFS operations for the FAT file system server. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include "fat.h" |
38 | #include "fat.h" |
39 | #include "fat_dentry.h" |
39 | #include "fat_dentry.h" |
40 | #include "fat_fat.h" |
40 | #include "fat_fat.h" |
41 | #include "../../vfs/vfs.h" |
41 | #include "../../vfs/vfs.h" |
42 | #include <libfs.h> |
42 | #include <libfs.h> |
43 | #include <libblock.h> |
43 | #include <libblock.h> |
44 | #include <ipc/ipc.h> |
44 | #include <ipc/ipc.h> |
45 | #include <ipc/services.h> |
45 | #include <ipc/services.h> |
46 | #include <ipc/devmap.h> |
46 | #include <ipc/devmap.h> |
47 | #include <async.h> |
47 | #include <async.h> |
48 | #include <errno.h> |
48 | #include <errno.h> |
49 | #include <string.h> |
49 | #include <string.h> |
50 | #include <byteorder.h> |
50 | #include <byteorder.h> |
51 | #include <libadt/hash_table.h> |
51 | #include <libadt/hash_table.h> |
52 | #include <libadt/list.h> |
52 | #include <libadt/list.h> |
53 | #include <assert.h> |
53 | #include <assert.h> |
54 | #include <futex.h> |
54 | #include <futex.h> |
55 | #include <sys/mman.h> |
55 | #include <sys/mman.h> |
56 | #include <align.h> |
56 | #include <align.h> |
57 | 57 | ||
58 | /** Futex protecting the list of cached free FAT nodes. */ |
58 | /** Futex protecting the list of cached free FAT nodes. */ |
59 | static futex_t ffn_futex = FUTEX_INITIALIZER; |
59 | static futex_t ffn_futex = FUTEX_INITIALIZER; |
60 | 60 | ||
61 | /** List of cached free FAT nodes. */ |
61 | /** List of cached free FAT nodes. */ |
62 | static LIST_INITIALIZE(ffn_head); |
62 | static LIST_INITIALIZE(ffn_head); |
63 | 63 | ||
64 | static void fat_node_initialize(fat_node_t *node) |
64 | static void fat_node_initialize(fat_node_t *node) |
65 | { |
65 | { |
66 | futex_initialize(&node->lock, 1); |
66 | futex_initialize(&node->lock, 1); |
67 | node->idx = NULL; |
67 | node->idx = NULL; |
68 | node->type = 0; |
68 | node->type = 0; |
69 | link_initialize(&node->ffn_link); |
69 | link_initialize(&node->ffn_link); |
70 | node->size = 0; |
70 | node->size = 0; |
71 | node->lnkcnt = 0; |
71 | node->lnkcnt = 0; |
72 | node->refcnt = 0; |
72 | node->refcnt = 0; |
73 | node->dirty = false; |
73 | node->dirty = false; |
74 | } |
74 | } |
75 | 75 | ||
76 | static void fat_node_sync(fat_node_t *node) |
76 | static void fat_node_sync(fat_node_t *node) |
77 | { |
77 | { |
78 | block_t *b; |
78 | block_t *b; |
79 | fat_bs_t *bs; |
79 | fat_bs_t *bs; |
80 | fat_dentry_t *d; |
80 | fat_dentry_t *d; |
81 | uint16_t bps; |
81 | uint16_t bps; |
82 | unsigned dps; |
82 | unsigned dps; |
83 | 83 | ||
84 | assert(node->dirty); |
84 | assert(node->dirty); |
85 | 85 | ||
86 | bs = block_bb_get(node->idx->dev_handle); |
86 | bs = block_bb_get(node->idx->dev_handle); |
87 | bps = uint16_t_le2host(bs->bps); |
87 | bps = uint16_t_le2host(bs->bps); |
88 | dps = bps / sizeof(fat_dentry_t); |
88 | dps = bps / sizeof(fat_dentry_t); |
89 | 89 | ||
90 | /* Read the block that contains the dentry of interest. */ |
90 | /* Read the block that contains the dentry of interest. */ |
91 | b = _fat_block_get(bs, node->idx->dev_handle, node->idx->pfc, |
91 | b = _fat_block_get(bs, node->idx->dev_handle, node->idx->pfc, |
92 | (node->idx->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE); |
92 | (node->idx->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE); |
93 | 93 | ||
94 | d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps); |
94 | d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps); |
95 | 95 | ||
96 | d->firstc = host2uint16_t_le(node->firstc); |
96 | d->firstc = host2uint16_t_le(node->firstc); |
97 | if (node->type == FAT_FILE) { |
97 | if (node->type == FAT_FILE) { |
98 | d->size = host2uint32_t_le(node->size); |
98 | d->size = host2uint32_t_le(node->size); |
99 | } else if (node->type == FAT_DIRECTORY) { |
99 | } else if (node->type == FAT_DIRECTORY) { |
100 | d->attr = FAT_ATTR_SUBDIR; |
100 | d->attr = FAT_ATTR_SUBDIR; |
101 | } |
101 | } |
102 | 102 | ||
103 | /* TODO: update other fields? (e.g time fields) */ |
103 | /* TODO: update other fields? (e.g time fields) */ |
104 | 104 | ||
105 | b->dirty = true; /* need to sync block */ |
105 | b->dirty = true; /* need to sync block */ |
106 | block_put(b); |
106 | block_put(b); |
107 | } |
107 | } |
108 | 108 | ||
109 | static fat_node_t *fat_node_get_new(void) |
109 | static fat_node_t *fat_node_get_new(void) |
110 | { |
110 | { |
111 | fat_node_t *nodep; |
111 | fat_node_t *nodep; |
112 | 112 | ||
113 | futex_down(&ffn_futex); |
113 | futex_down(&ffn_futex); |
114 | if (!list_empty(&ffn_head)) { |
114 | if (!list_empty(&ffn_head)) { |
115 | /* Try to use a cached free node structure. */ |
115 | /* Try to use a cached free node structure. */ |
116 | fat_idx_t *idxp_tmp; |
116 | fat_idx_t *idxp_tmp; |
117 | nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link); |
117 | nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link); |
118 | if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK) |
118 | if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK) |
119 | goto skip_cache; |
119 | goto skip_cache; |
120 | idxp_tmp = nodep->idx; |
120 | idxp_tmp = nodep->idx; |
121 | if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) { |
121 | if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) { |
122 | futex_up(&nodep->lock); |
122 | futex_up(&nodep->lock); |
123 | goto skip_cache; |
123 | goto skip_cache; |
124 | } |
124 | } |
125 | list_remove(&nodep->ffn_link); |
125 | list_remove(&nodep->ffn_link); |
126 | futex_up(&ffn_futex); |
126 | futex_up(&ffn_futex); |
127 | if (nodep->dirty) |
127 | if (nodep->dirty) |
128 | fat_node_sync(nodep); |
128 | fat_node_sync(nodep); |
129 | idxp_tmp->nodep = NULL; |
129 | idxp_tmp->nodep = NULL; |
130 | futex_up(&nodep->lock); |
130 | futex_up(&nodep->lock); |
131 | futex_up(&idxp_tmp->lock); |
131 | futex_up(&idxp_tmp->lock); |
132 | } else { |
132 | } else { |
133 | skip_cache: |
133 | skip_cache: |
134 | /* Try to allocate a new node structure. */ |
134 | /* Try to allocate a new node structure. */ |
135 | futex_up(&ffn_futex); |
135 | futex_up(&ffn_futex); |
136 | nodep = (fat_node_t *)malloc(sizeof(fat_node_t)); |
136 | nodep = (fat_node_t *)malloc(sizeof(fat_node_t)); |
137 | if (!nodep) |
137 | if (!nodep) |
138 | return NULL; |
138 | return NULL; |
139 | } |
139 | } |
140 | fat_node_initialize(nodep); |
140 | fat_node_initialize(nodep); |
141 | 141 | ||
142 | return nodep; |
142 | return nodep; |
143 | } |
143 | } |
144 | 144 | ||
145 | /** Internal version of fat_node_get(). |
145 | /** Internal version of fat_node_get(). |
146 | * |
146 | * |
147 | * @param idxp Locked index structure. |
147 | * @param idxp Locked index structure. |
148 | */ |
148 | */ |
149 | static void *fat_node_get_core(fat_idx_t *idxp) |
149 | static void *fat_node_get_core(fat_idx_t *idxp) |
150 | { |
150 | { |
151 | block_t *b; |
151 | block_t *b; |
152 | fat_bs_t *bs; |
152 | fat_bs_t *bs; |
153 | fat_dentry_t *d; |
153 | fat_dentry_t *d; |
154 | fat_node_t *nodep = NULL; |
154 | fat_node_t *nodep = NULL; |
155 | unsigned bps; |
155 | unsigned bps; |
156 | unsigned spc; |
156 | unsigned spc; |
157 | unsigned dps; |
157 | unsigned dps; |
158 | 158 | ||
159 | if (idxp->nodep) { |
159 | if (idxp->nodep) { |
160 | /* |
160 | /* |
161 | * We are lucky. |
161 | * We are lucky. |
162 | * The node is already instantiated in memory. |
162 | * The node is already instantiated in memory. |
163 | */ |
163 | */ |
164 | futex_down(&idxp->nodep->lock); |
164 | futex_down(&idxp->nodep->lock); |
165 | if (!idxp->nodep->refcnt++) |
165 | if (!idxp->nodep->refcnt++) |
166 | list_remove(&idxp->nodep->ffn_link); |
166 | list_remove(&idxp->nodep->ffn_link); |
167 | futex_up(&idxp->nodep->lock); |
167 | futex_up(&idxp->nodep->lock); |
168 | return idxp->nodep; |
168 | return idxp->nodep; |
169 | } |
169 | } |
170 | 170 | ||
171 | /* |
171 | /* |
172 | * We must instantiate the node from the file system. |
172 | * We must instantiate the node from the file system. |
173 | */ |
173 | */ |
174 | 174 | ||
175 | assert(idxp->pfc); |
175 | assert(idxp->pfc); |
176 | 176 | ||
177 | nodep = fat_node_get_new(); |
177 | nodep = fat_node_get_new(); |
178 | if (!nodep) |
178 | if (!nodep) |
179 | return NULL; |
179 | return NULL; |
180 | 180 | ||
181 | bs = block_bb_get(idxp->dev_handle); |
181 | bs = block_bb_get(idxp->dev_handle); |
182 | bps = uint16_t_le2host(bs->bps); |
182 | bps = uint16_t_le2host(bs->bps); |
183 | spc = bs->spc; |
183 | spc = bs->spc; |
184 | dps = bps / sizeof(fat_dentry_t); |
184 | dps = bps / sizeof(fat_dentry_t); |
185 | 185 | ||
186 | /* Read the block that contains the dentry of interest. */ |
186 | /* Read the block that contains the dentry of interest. */ |
187 | b = _fat_block_get(bs, idxp->dev_handle, idxp->pfc, |
187 | b = _fat_block_get(bs, idxp->dev_handle, idxp->pfc, |
188 | (idxp->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE); |
188 | (idxp->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE); |
189 | assert(b); |
189 | assert(b); |
190 | 190 | ||
191 | d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps); |
191 | d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps); |
192 | if (d->attr & FAT_ATTR_SUBDIR) { |
192 | if (d->attr & FAT_ATTR_SUBDIR) { |
193 | /* |
193 | /* |
194 | * The only directory which does not have this bit set is the |
194 | * The only directory which does not have this bit set is the |
195 | * root directory itself. The root directory node is handled |
195 | * root directory itself. The root directory node is handled |
196 | * and initialized elsewhere. |
196 | * and initialized elsewhere. |
197 | */ |
197 | */ |
198 | nodep->type = FAT_DIRECTORY; |
198 | nodep->type = FAT_DIRECTORY; |
199 | /* |
199 | /* |
200 | * Unfortunately, the 'size' field of the FAT dentry is not |
200 | * Unfortunately, the 'size' field of the FAT dentry is not |
201 | * defined for the directory entry type. We must determine the |
201 | * defined for the directory entry type. We must determine the |
202 | * size of the directory by walking the FAT. |
202 | * size of the directory by walking the FAT. |
203 | */ |
203 | */ |
204 | nodep->size = bps * spc * fat_clusters_get(bs, idxp->dev_handle, |
204 | nodep->size = bps * spc * fat_clusters_get(bs, idxp->dev_handle, |
205 | uint16_t_le2host(d->firstc)); |
205 | uint16_t_le2host(d->firstc)); |
206 | } else { |
206 | } else { |
207 | nodep->type = FAT_FILE; |
207 | nodep->type = FAT_FILE; |
208 | nodep->size = uint32_t_le2host(d->size); |
208 | nodep->size = uint32_t_le2host(d->size); |
209 | } |
209 | } |
210 | nodep->firstc = uint16_t_le2host(d->firstc); |
210 | nodep->firstc = uint16_t_le2host(d->firstc); |
211 | nodep->lnkcnt = 1; |
211 | nodep->lnkcnt = 1; |
212 | nodep->refcnt = 1; |
212 | nodep->refcnt = 1; |
213 | 213 | ||
214 | block_put(b); |
214 | block_put(b); |
215 | 215 | ||
216 | /* Link the idx structure with the node structure. */ |
216 | /* Link the idx structure with the node structure. */ |
217 | nodep->idx = idxp; |
217 | nodep->idx = idxp; |
218 | idxp->nodep = nodep; |
218 | idxp->nodep = nodep; |
219 | 219 | ||
220 | return nodep; |
220 | return nodep; |
221 | } |
221 | } |
222 | 222 | ||
223 | /* |
223 | /* |
224 | * Forward declarations of FAT libfs operations. |
224 | * Forward declarations of FAT libfs operations. |
225 | */ |
225 | */ |
226 | static void *fat_node_get(dev_handle_t, fs_index_t); |
226 | static void *fat_node_get(dev_handle_t, fs_index_t); |
227 | static void fat_node_put(void *); |
227 | static void fat_node_put(void *); |
228 | static void *fat_create_node(dev_handle_t, int); |
228 | static void *fat_create_node(dev_handle_t, int); |
229 | static int fat_destroy_node(void *); |
229 | static int fat_destroy_node(void *); |
230 | static int fat_link(void *, void *, const char *); |
230 | static int fat_link(void *, void *, const char *); |
231 | static int fat_unlink(void *, void *); |
231 | static int fat_unlink(void *, void *); |
232 | static void *fat_match(void *, const char *); |
232 | static void *fat_match(void *, const char *); |
233 | static fs_index_t fat_index_get(void *); |
233 | static fs_index_t fat_index_get(void *); |
234 | static size_t fat_size_get(void *); |
234 | static size_t fat_size_get(void *); |
235 | static unsigned fat_lnkcnt_get(void *); |
235 | static unsigned fat_lnkcnt_get(void *); |
236 | static bool fat_has_children(void *); |
236 | static bool fat_has_children(void *); |
237 | static void *fat_root_get(dev_handle_t); |
237 | static void *fat_root_get(dev_handle_t); |
238 | static char fat_plb_get_char(unsigned); |
238 | static char fat_plb_get_char(unsigned); |
239 | static bool fat_is_directory(void *); |
239 | static bool fat_is_directory(void *); |
240 | static bool fat_is_file(void *node); |
240 | static bool fat_is_file(void *node); |
241 | 241 | ||
242 | /* |
242 | /* |
243 | * FAT libfs operations. |
243 | * FAT libfs operations. |
244 | */ |
244 | */ |
245 | 245 | ||
246 | /** Instantiate a FAT in-core node. */ |
246 | /** Instantiate a FAT in-core node. */ |
247 | void *fat_node_get(dev_handle_t dev_handle, fs_index_t index) |
247 | void *fat_node_get(dev_handle_t dev_handle, fs_index_t index) |
248 | { |
248 | { |
249 | void *node; |
249 | void *node; |
250 | fat_idx_t *idxp; |
250 | fat_idx_t *idxp; |
251 | 251 | ||
252 | idxp = fat_idx_get_by_index(dev_handle, index); |
252 | idxp = fat_idx_get_by_index(dev_handle, index); |
253 | if (!idxp) |
253 | if (!idxp) |
254 | return NULL; |
254 | return NULL; |
255 | /* idxp->lock held */ |
255 | /* idxp->lock held */ |
256 | node = fat_node_get_core(idxp); |
256 | node = fat_node_get_core(idxp); |
257 | futex_up(&idxp->lock); |
257 | futex_up(&idxp->lock); |
258 | return node; |
258 | return node; |
259 | } |
259 | } |
260 | 260 | ||
261 | void fat_node_put(void *node) |
261 | void fat_node_put(void *node) |
262 | { |
262 | { |
263 | fat_node_t *nodep = (fat_node_t *)node; |
263 | fat_node_t *nodep = (fat_node_t *)node; |
264 | bool destroy = false; |
264 | bool destroy = false; |
265 | 265 | ||
266 | futex_down(&nodep->lock); |
266 | futex_down(&nodep->lock); |
267 | if (!--nodep->refcnt) { |
267 | if (!--nodep->refcnt) { |
268 | if (nodep->idx) { |
268 | if (nodep->idx) { |
269 | futex_down(&ffn_futex); |
269 | futex_down(&ffn_futex); |
270 | list_append(&nodep->ffn_link, &ffn_head); |
270 | list_append(&nodep->ffn_link, &ffn_head); |
271 | futex_up(&ffn_futex); |
271 | futex_up(&ffn_futex); |
272 | } else { |
272 | } else { |
273 | /* |
273 | /* |
274 | * The node does not have any index structure associated |
274 | * The node does not have any index structure associated |
275 | * with itself. This can only mean that we are releasing |
275 | * with itself. This can only mean that we are releasing |
276 | * the node after a failed attempt to allocate the index |
276 | * the node after a failed attempt to allocate the index |
277 | * structure for it. |
277 | * structure for it. |
278 | */ |
278 | */ |
279 | destroy = true; |
279 | destroy = true; |
280 | } |
280 | } |
281 | } |
281 | } |
282 | futex_up(&nodep->lock); |
282 | futex_up(&nodep->lock); |
283 | if (destroy) |
283 | if (destroy) |
284 | free(node); |
284 | free(node); |
285 | } |
285 | } |
286 | 286 | ||
287 | void *fat_create_node(dev_handle_t dev_handle, int flags) |
287 | void *fat_create_node(dev_handle_t dev_handle, int flags) |
288 | { |
288 | { |
289 | fat_idx_t *idxp; |
289 | fat_idx_t *idxp; |
290 | fat_node_t *nodep; |
290 | fat_node_t *nodep; |
- | 291 | fat_bs_t *bs; |
|
- | 292 | fat_cluster_t mcl, lcl; |
|
- | 293 | uint16_t bps; |
|
- | 294 | int rc; |
|
- | 295 | ||
- | 296 | bs = block_bb_get(dev_handle); |
|
- | 297 | bps = uint16_t_le2host(bs->bps); |
|
- | 298 | if (flags & L_DIRECTORY) { |
|
- | 299 | /* allocate a cluster */ |
|
- | 300 | rc = fat_alloc_clusters(bs, dev_handle, 1, &mcl, &lcl); |
|
- | 301 | if (rc != EOK) |
|
- | 302 | return NULL; |
|
- | 303 | } |
|
291 | 304 | ||
292 | nodep = fat_node_get_new(); |
305 | nodep = fat_node_get_new(); |
293 | if (!nodep) |
306 | if (!nodep) { |
- | 307 | fat_free_clusters(bs, dev_handle, mcl); |
|
294 | return NULL; |
308 | return NULL; |
- | 309 | } |
|
295 | idxp = fat_idx_get_new(dev_handle); |
310 | idxp = fat_idx_get_new(dev_handle); |
296 | if (!idxp) { |
311 | if (!idxp) { |
- | 312 | fat_free_clusters(bs, dev_handle, mcl); |
|
297 | fat_node_put(nodep); |
313 | fat_node_put(nodep); |
298 | return NULL; |
314 | return NULL; |
299 | } |
315 | } |
300 | /* idxp->lock held */ |
316 | /* idxp->lock held */ |
301 | if (flags & L_DIRECTORY) { |
317 | if (flags & L_DIRECTORY) { |
- | 318 | int i; |
|
- | 319 | block_t *b; |
|
- | 320 | ||
- | 321 | /* |
|
- | 322 | * Populate the new cluster with unused dentries. |
|
- | 323 | * We don't create the '.' and '..' entries, since they are |
|
- | 324 | * optional and HelenOS VFS does not need them. |
|
- | 325 | */ |
|
- | 326 | for (i = 0; i < bs->spc; i++) { |
|
- | 327 | b = _fat_block_get(bs, dev_handle, mcl, i, |
|
- | 328 | BLOCK_FLAGS_NOREAD); |
|
- | 329 | /* mark all dentries as never-used */ |
|
- | 330 | memset(b->data, 0, bps); |
|
- | 331 | b->dirty = false; |
|
- | 332 | block_put(b); |
|
- | 333 | } |
|
302 | nodep->type = FAT_DIRECTORY; |
334 | nodep->type = FAT_DIRECTORY; |
- | 335 | nodep->firstc = mcl; |
|
- | 336 | nodep->size = bps * bs->spc; |
|
303 | } else { |
337 | } else { |
304 | nodep->type = FAT_FILE; |
338 | nodep->type = FAT_FILE; |
305 | } |
- | |
306 | nodep->size = 0; |
- | |
307 | nodep->firstc = FAT_CLST_RES0; |
339 | nodep->firstc = FAT_CLST_RES0; |
- | 340 | nodep->size = 0; |
|
- | 341 | } |
|
308 | nodep->lnkcnt = 0; /* not linked anywhere */ |
342 | nodep->lnkcnt = 0; /* not linked anywhere */ |
309 | nodep->refcnt = 1; |
343 | nodep->refcnt = 1; |
- | 344 | nodep->dirty = true; |
|
310 | 345 | ||
311 | nodep->idx = idxp; |
346 | nodep->idx = idxp; |
312 | idxp->nodep = nodep; |
347 | idxp->nodep = nodep; |
313 | 348 | ||
314 | futex_up(&idxp->lock); |
349 | futex_up(&idxp->lock); |
315 | return nodep; |
350 | return nodep; |
316 | } |
351 | } |
317 | 352 | ||
318 | int fat_destroy_node(void *node) |
353 | int fat_destroy_node(void *node) |
319 | { |
354 | { |
320 | fat_node_t *nodep = (fat_node_t *)node; |
355 | fat_node_t *nodep = (fat_node_t *)node; |
321 | fat_bs_t *bs; |
356 | fat_bs_t *bs; |
322 | 357 | ||
323 | /* |
358 | /* |
324 | * The node is not reachable from the file system. This means that the |
359 | * The node is not reachable from the file system. This means that the |
325 | * link count should be zero and that the index structure cannot be |
360 | * link count should be zero and that the index structure cannot be |
326 | * found in the position hash. Obviously, we don't need to lock the node |
361 | * found in the position hash. Obviously, we don't need to lock the node |
327 | * nor its index structure. |
362 | * nor its index structure. |
328 | */ |
363 | */ |
329 | assert(nodep->lnkcnt == 0); |
364 | assert(nodep->lnkcnt == 0); |
330 | 365 | ||
331 | /* |
366 | /* |
332 | * The node may not have any children. |
367 | * The node may not have any children. |
333 | */ |
368 | */ |
334 | assert(fat_has_children(node) == false); |
369 | assert(fat_has_children(node) == false); |
335 | 370 | ||
336 | bs = block_bb_get(nodep->idx->dev_handle); |
371 | bs = block_bb_get(nodep->idx->dev_handle); |
337 | if (nodep->firstc != FAT_CLST_RES0) { |
372 | if (nodep->firstc != FAT_CLST_RES0) { |
338 | assert(nodep->size); |
373 | assert(nodep->size); |
339 | /* Free all clusters allocated to the node. */ |
374 | /* Free all clusters allocated to the node. */ |
340 | fat_free_clusters(bs, nodep->idx->dev_handle, nodep->firstc); |
375 | fat_free_clusters(bs, nodep->idx->dev_handle, nodep->firstc); |
341 | } |
376 | } |
342 | 377 | ||
343 | fat_idx_destroy(nodep->idx); |
378 | fat_idx_destroy(nodep->idx); |
344 | free(nodep); |
379 | free(nodep); |
345 | return EOK; |
380 | return EOK; |
346 | } |
381 | } |
347 | 382 | ||
348 | int fat_link(void *prnt, void *chld, const char *name) |
383 | int fat_link(void *prnt, void *chld, const char *name) |
349 | { |
384 | { |
350 | fat_node_t *parentp = (fat_node_t *)prnt; |
385 | fat_node_t *parentp = (fat_node_t *)prnt; |
351 | fat_node_t *childp = (fat_node_t *)chld; |
386 | fat_node_t *childp = (fat_node_t *)chld; |
352 | fat_dentry_t *d; |
387 | fat_dentry_t *d; |
353 | fat_bs_t *bs; |
388 | fat_bs_t *bs; |
354 | block_t *b; |
389 | block_t *b; |
355 | int i, j; |
390 | int i, j; |
356 | uint16_t bps; |
391 | uint16_t bps; |
357 | unsigned dps; |
392 | unsigned dps; |
358 | unsigned blocks; |
393 | unsigned blocks; |
359 | 394 | ||
360 | futex_down(&childp->lock); |
395 | futex_down(&childp->lock); |
361 | if (childp->lnkcnt == 1) { |
396 | if (childp->lnkcnt == 1) { |
362 | /* |
397 | /* |
363 | * On FAT, we don't support multiple hard links. |
398 | * On FAT, we don't support multiple hard links. |
364 | */ |
399 | */ |
365 | futex_up(&childp->lock); |
400 | futex_up(&childp->lock); |
366 | return EMLINK; |
401 | return EMLINK; |
367 | } |
402 | } |
368 | assert(childp->lnkcnt == 0); |
403 | assert(childp->lnkcnt == 0); |
369 | futex_up(&childp->lock); |
404 | futex_up(&childp->lock); |
370 | 405 | ||
371 | if (!fat_dentry_name_verify(name)) { |
406 | if (!fat_dentry_name_verify(name)) { |
372 | /* |
407 | /* |
373 | * Attempt to create unsupported name. |
408 | * Attempt to create unsupported name. |
374 | */ |
409 | */ |
375 | return ENOTSUP; |
410 | return ENOTSUP; |
376 | } |
411 | } |
377 | 412 | ||
378 | /* |
413 | /* |
379 | * Get us an unused parent node's dentry or grow the parent and allocate |
414 | * Get us an unused parent node's dentry or grow the parent and allocate |
380 | * a new one. |
415 | * a new one. |
381 | */ |
416 | */ |
382 | 417 | ||
383 | futex_down(&parentp->idx->lock); |
418 | futex_down(&parentp->idx->lock); |
384 | bs = block_bb_get(parentp->idx->dev_handle); |
419 | bs = block_bb_get(parentp->idx->dev_handle); |
385 | bps = uint16_t_le2host(bs->bps); |
420 | bps = uint16_t_le2host(bs->bps); |
386 | dps = bps / sizeof(fat_dentry_t); |
421 | dps = bps / sizeof(fat_dentry_t); |
387 | 422 | ||
388 | blocks = parentp->size / bps; |
423 | blocks = parentp->size / bps; |
389 | 424 | ||
390 | for (i = 0; i < blocks; i++) { |
425 | for (i = 0; i < blocks; i++) { |
391 | b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE); |
426 | b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE); |
392 | for (j = 0; j < dps; j++) { |
427 | for (j = 0; j < dps; j++) { |
393 | d = ((fat_dentry_t *)b->data) + j; |
428 | d = ((fat_dentry_t *)b->data) + j; |
394 | switch (fat_classify_dentry(d)) { |
429 | switch (fat_classify_dentry(d)) { |
395 | case FAT_DENTRY_SKIP: |
430 | case FAT_DENTRY_SKIP: |
396 | case FAT_DENTRY_VALID: |
431 | case FAT_DENTRY_VALID: |
397 | /* skipping used and meta entries */ |
432 | /* skipping used and meta entries */ |
398 | continue; |
433 | continue; |
399 | case FAT_DENTRY_FREE: |
434 | case FAT_DENTRY_FREE: |
400 | case FAT_DENTRY_LAST: |
435 | case FAT_DENTRY_LAST: |
401 | /* found an empty slot */ |
436 | /* found an empty slot */ |
402 | goto hit; |
437 | goto hit; |
403 | } |
438 | } |
404 | } |
439 | } |
405 | block_put(b); |
440 | block_put(b); |
406 | } |
441 | } |
407 | 442 | ||
408 | /* |
443 | /* |
409 | * We need to grow the parent in order to create a new unused dentry. |
444 | * We need to grow the parent in order to create a new unused dentry. |
410 | */ |
445 | */ |
411 | futex_up(&parentp->idx->lock); |
446 | futex_up(&parentp->idx->lock); |
412 | return ENOTSUP; /* XXX */ |
447 | return ENOTSUP; /* XXX */ |
413 | 448 | ||
414 | hit: |
449 | hit: |
415 | /* |
450 | /* |
416 | * At this point we only establish the link between the parent and the |
451 | * At this point we only establish the link between the parent and the |
417 | * child. The dentry, except of the name and the extension, will remain |
452 | * child. The dentry, except of the name and the extension, will remain |
418 | * uninitialized until the the corresponding node is synced. Thus the |
453 | * uninitialized until the the corresponding node is synced. Thus the |
419 | * valid dentry data is kept in the child node structure. |
454 | * valid dentry data is kept in the child node structure. |
420 | */ |
455 | */ |
421 | memset(d, 0, sizeof(fat_dentry_t)); |
456 | memset(d, 0, sizeof(fat_dentry_t)); |
422 | fat_dentry_name_set(d, name); |
457 | fat_dentry_name_set(d, name); |
423 | b->dirty = true; /* need to sync block */ |
458 | b->dirty = true; /* need to sync block */ |
424 | block_put(b); |
459 | block_put(b); |
425 | futex_up(&parentp->idx->lock); |
460 | futex_up(&parentp->idx->lock); |
426 | 461 | ||
427 | futex_down(&childp->idx->lock); |
462 | futex_down(&childp->idx->lock); |
428 | childp->idx->pfc = parentp->firstc; |
463 | childp->idx->pfc = parentp->firstc; |
429 | childp->idx->pdi = i * dps + j; |
464 | childp->idx->pdi = i * dps + j; |
430 | futex_up(&childp->idx->lock); |
465 | futex_up(&childp->idx->lock); |
431 | 466 | ||
432 | futex_down(&childp->lock); |
467 | futex_down(&childp->lock); |
433 | childp->lnkcnt = 1; |
468 | childp->lnkcnt = 1; |
434 | childp->dirty = true; /* need to sync node */ |
469 | childp->dirty = true; /* need to sync node */ |
435 | futex_up(&childp->lock); |
470 | futex_up(&childp->lock); |
436 | 471 | ||
437 | /* |
472 | /* |
438 | * Hash in the index structure into the position hash. |
473 | * Hash in the index structure into the position hash. |
439 | */ |
474 | */ |
440 | fat_idx_hashin(childp->idx); |
475 | fat_idx_hashin(childp->idx); |
441 | 476 | ||
442 | return EOK; |
477 | return EOK; |
443 | } |
478 | } |
444 | 479 | ||
445 | int fat_unlink(void *prnt, void *chld) |
480 | int fat_unlink(void *prnt, void *chld) |
446 | { |
481 | { |
447 | return ENOTSUP; /* not supported at the moment */ |
482 | return ENOTSUP; /* not supported at the moment */ |
448 | } |
483 | } |
449 | 484 | ||
450 | void *fat_match(void *prnt, const char *component) |
485 | void *fat_match(void *prnt, const char *component) |
451 | { |
486 | { |
452 | fat_bs_t *bs; |
487 | fat_bs_t *bs; |
453 | fat_node_t *parentp = (fat_node_t *)prnt; |
488 | fat_node_t *parentp = (fat_node_t *)prnt; |
454 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
489 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
455 | unsigned i, j; |
490 | unsigned i, j; |
456 | unsigned bps; /* bytes per sector */ |
491 | unsigned bps; /* bytes per sector */ |
457 | unsigned dps; /* dentries per sector */ |
492 | unsigned dps; /* dentries per sector */ |
458 | unsigned blocks; |
493 | unsigned blocks; |
459 | fat_dentry_t *d; |
494 | fat_dentry_t *d; |
460 | block_t *b; |
495 | block_t *b; |
461 | 496 | ||
462 | futex_down(&parentp->idx->lock); |
497 | futex_down(&parentp->idx->lock); |
463 | bs = block_bb_get(parentp->idx->dev_handle); |
498 | bs = block_bb_get(parentp->idx->dev_handle); |
464 | bps = uint16_t_le2host(bs->bps); |
499 | bps = uint16_t_le2host(bs->bps); |
465 | dps = bps / sizeof(fat_dentry_t); |
500 | dps = bps / sizeof(fat_dentry_t); |
466 | blocks = parentp->size / bps; |
501 | blocks = parentp->size / bps; |
467 | for (i = 0; i < blocks; i++) { |
502 | for (i = 0; i < blocks; i++) { |
468 | b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE); |
503 | b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE); |
469 | for (j = 0; j < dps; j++) { |
504 | for (j = 0; j < dps; j++) { |
470 | d = ((fat_dentry_t *)b->data) + j; |
505 | d = ((fat_dentry_t *)b->data) + j; |
471 | switch (fat_classify_dentry(d)) { |
506 | switch (fat_classify_dentry(d)) { |
472 | case FAT_DENTRY_SKIP: |
507 | case FAT_DENTRY_SKIP: |
473 | case FAT_DENTRY_FREE: |
508 | case FAT_DENTRY_FREE: |
474 | continue; |
509 | continue; |
475 | case FAT_DENTRY_LAST: |
510 | case FAT_DENTRY_LAST: |
476 | block_put(b); |
511 | block_put(b); |
477 | futex_up(&parentp->idx->lock); |
512 | futex_up(&parentp->idx->lock); |
478 | return NULL; |
513 | return NULL; |
479 | default: |
514 | default: |
480 | case FAT_DENTRY_VALID: |
515 | case FAT_DENTRY_VALID: |
481 | fat_dentry_name_get(d, name); |
516 | fat_dentry_name_get(d, name); |
482 | break; |
517 | break; |
483 | } |
518 | } |
484 | if (stricmp(name, component) == 0) { |
519 | if (stricmp(name, component) == 0) { |
485 | /* hit */ |
520 | /* hit */ |
486 | void *node; |
521 | void *node; |
487 | /* |
522 | /* |
488 | * Assume tree hierarchy for locking. We |
523 | * Assume tree hierarchy for locking. We |
489 | * already have the parent and now we are going |
524 | * already have the parent and now we are going |
490 | * to lock the child. Never lock in the oposite |
525 | * to lock the child. Never lock in the oposite |
491 | * order. |
526 | * order. |
492 | */ |
527 | */ |
493 | fat_idx_t *idx = fat_idx_get_by_pos( |
528 | fat_idx_t *idx = fat_idx_get_by_pos( |
494 | parentp->idx->dev_handle, parentp->firstc, |
529 | parentp->idx->dev_handle, parentp->firstc, |
495 | i * dps + j); |
530 | i * dps + j); |
496 | futex_up(&parentp->idx->lock); |
531 | futex_up(&parentp->idx->lock); |
497 | if (!idx) { |
532 | if (!idx) { |
498 | /* |
533 | /* |
499 | * Can happen if memory is low or if we |
534 | * Can happen if memory is low or if we |
500 | * run out of 32-bit indices. |
535 | * run out of 32-bit indices. |
501 | */ |
536 | */ |
502 | block_put(b); |
537 | block_put(b); |
503 | return NULL; |
538 | return NULL; |
504 | } |
539 | } |
505 | node = fat_node_get_core(idx); |
540 | node = fat_node_get_core(idx); |
506 | futex_up(&idx->lock); |
541 | futex_up(&idx->lock); |
507 | block_put(b); |
542 | block_put(b); |
508 | return node; |
543 | return node; |
509 | } |
544 | } |
510 | } |
545 | } |
511 | block_put(b); |
546 | block_put(b); |
512 | } |
547 | } |
513 | 548 | ||
514 | futex_up(&parentp->idx->lock); |
549 | futex_up(&parentp->idx->lock); |
515 | return NULL; |
550 | return NULL; |
516 | } |
551 | } |
517 | 552 | ||
518 | fs_index_t fat_index_get(void *node) |
553 | fs_index_t fat_index_get(void *node) |
519 | { |
554 | { |
520 | fat_node_t *fnodep = (fat_node_t *)node; |
555 | fat_node_t *fnodep = (fat_node_t *)node; |
521 | if (!fnodep) |
556 | if (!fnodep) |
522 | return 0; |
557 | return 0; |
523 | return fnodep->idx->index; |
558 | return fnodep->idx->index; |
524 | } |
559 | } |
525 | 560 | ||
526 | size_t fat_size_get(void *node) |
561 | size_t fat_size_get(void *node) |
527 | { |
562 | { |
528 | return ((fat_node_t *)node)->size; |
563 | return ((fat_node_t *)node)->size; |
529 | } |
564 | } |
530 | 565 | ||
531 | unsigned fat_lnkcnt_get(void *node) |
566 | unsigned fat_lnkcnt_get(void *node) |
532 | { |
567 | { |
533 | return ((fat_node_t *)node)->lnkcnt; |
568 | return ((fat_node_t *)node)->lnkcnt; |
534 | } |
569 | } |
535 | 570 | ||
536 | bool fat_has_children(void *node) |
571 | bool fat_has_children(void *node) |
537 | { |
572 | { |
538 | fat_bs_t *bs; |
573 | fat_bs_t *bs; |
539 | fat_node_t *nodep = (fat_node_t *)node; |
574 | fat_node_t *nodep = (fat_node_t *)node; |
540 | unsigned bps; |
575 | unsigned bps; |
541 | unsigned dps; |
576 | unsigned dps; |
542 | unsigned blocks; |
577 | unsigned blocks; |
543 | block_t *b; |
578 | block_t *b; |
544 | unsigned i, j; |
579 | unsigned i, j; |
545 | 580 | ||
546 | if (nodep->type != FAT_DIRECTORY) |
581 | if (nodep->type != FAT_DIRECTORY) |
547 | return false; |
582 | return false; |
548 | 583 | ||
549 | futex_down(&nodep->idx->lock); |
584 | futex_down(&nodep->idx->lock); |
550 | bs = block_bb_get(nodep->idx->dev_handle); |
585 | bs = block_bb_get(nodep->idx->dev_handle); |
551 | bps = uint16_t_le2host(bs->bps); |
586 | bps = uint16_t_le2host(bs->bps); |
552 | dps = bps / sizeof(fat_dentry_t); |
587 | dps = bps / sizeof(fat_dentry_t); |
553 | 588 | ||
554 | blocks = nodep->size / bps; |
589 | blocks = nodep->size / bps; |
555 | 590 | ||
556 | for (i = 0; i < blocks; i++) { |
591 | for (i = 0; i < blocks; i++) { |
557 | fat_dentry_t *d; |
592 | fat_dentry_t *d; |
558 | 593 | ||
559 | b = fat_block_get(bs, nodep, i, BLOCK_FLAGS_NONE); |
594 | b = fat_block_get(bs, nodep, i, BLOCK_FLAGS_NONE); |
560 | for (j = 0; j < dps; j++) { |
595 | for (j = 0; j < dps; j++) { |
561 | d = ((fat_dentry_t *)b->data) + j; |
596 | d = ((fat_dentry_t *)b->data) + j; |
562 | switch (fat_classify_dentry(d)) { |
597 | switch (fat_classify_dentry(d)) { |
563 | case FAT_DENTRY_SKIP: |
598 | case FAT_DENTRY_SKIP: |
564 | case FAT_DENTRY_FREE: |
599 | case FAT_DENTRY_FREE: |
565 | continue; |
600 | continue; |
566 | case FAT_DENTRY_LAST: |
601 | case FAT_DENTRY_LAST: |
567 | block_put(b); |
602 | block_put(b); |
568 | futex_up(&nodep->idx->lock); |
603 | futex_up(&nodep->idx->lock); |
569 | return false; |
604 | return false; |
570 | default: |
605 | default: |
571 | case FAT_DENTRY_VALID: |
606 | case FAT_DENTRY_VALID: |
572 | block_put(b); |
607 | block_put(b); |
573 | futex_up(&nodep->idx->lock); |
608 | futex_up(&nodep->idx->lock); |
574 | return true; |
609 | return true; |
575 | } |
610 | } |
576 | block_put(b); |
611 | block_put(b); |
577 | futex_up(&nodep->idx->lock); |
612 | futex_up(&nodep->idx->lock); |
578 | return true; |
613 | return true; |
579 | } |
614 | } |
580 | block_put(b); |
615 | block_put(b); |
581 | } |
616 | } |
582 | 617 | ||
583 | futex_up(&nodep->idx->lock); |
618 | futex_up(&nodep->idx->lock); |
584 | return false; |
619 | return false; |
585 | } |
620 | } |
586 | 621 | ||
587 | void *fat_root_get(dev_handle_t dev_handle) |
622 | void *fat_root_get(dev_handle_t dev_handle) |
588 | { |
623 | { |
589 | return fat_node_get(dev_handle, 0); |
624 | return fat_node_get(dev_handle, 0); |
590 | } |
625 | } |
591 | 626 | ||
592 | char fat_plb_get_char(unsigned pos) |
627 | char fat_plb_get_char(unsigned pos) |
593 | { |
628 | { |
594 | return fat_reg.plb_ro[pos % PLB_SIZE]; |
629 | return fat_reg.plb_ro[pos % PLB_SIZE]; |
595 | } |
630 | } |
596 | 631 | ||
597 | bool fat_is_directory(void *node) |
632 | bool fat_is_directory(void *node) |
598 | { |
633 | { |
599 | return ((fat_node_t *)node)->type == FAT_DIRECTORY; |
634 | return ((fat_node_t *)node)->type == FAT_DIRECTORY; |
600 | } |
635 | } |
601 | 636 | ||
602 | bool fat_is_file(void *node) |
637 | bool fat_is_file(void *node) |
603 | { |
638 | { |
604 | return ((fat_node_t *)node)->type == FAT_FILE; |
639 | return ((fat_node_t *)node)->type == FAT_FILE; |
605 | } |
640 | } |
606 | 641 | ||
607 | /** libfs operations */ |
642 | /** libfs operations */ |
608 | libfs_ops_t fat_libfs_ops = { |
643 | libfs_ops_t fat_libfs_ops = { |
609 | .match = fat_match, |
644 | .match = fat_match, |
610 | .node_get = fat_node_get, |
645 | .node_get = fat_node_get, |
611 | .node_put = fat_node_put, |
646 | .node_put = fat_node_put, |
612 | .create = fat_create_node, |
647 | .create = fat_create_node, |
613 | .destroy = fat_destroy_node, |
648 | .destroy = fat_destroy_node, |
614 | .link = fat_link, |
649 | .link = fat_link, |
615 | .unlink = fat_unlink, |
650 | .unlink = fat_unlink, |
616 | .index_get = fat_index_get, |
651 | .index_get = fat_index_get, |
617 | .size_get = fat_size_get, |
652 | .size_get = fat_size_get, |
618 | .lnkcnt_get = fat_lnkcnt_get, |
653 | .lnkcnt_get = fat_lnkcnt_get, |
619 | .has_children = fat_has_children, |
654 | .has_children = fat_has_children, |
620 | .root_get = fat_root_get, |
655 | .root_get = fat_root_get, |
621 | .plb_get_char = fat_plb_get_char, |
656 | .plb_get_char = fat_plb_get_char, |
622 | .is_directory = fat_is_directory, |
657 | .is_directory = fat_is_directory, |
623 | .is_file = fat_is_file |
658 | .is_file = fat_is_file |
624 | }; |
659 | }; |
625 | 660 | ||
626 | /* |
661 | /* |
627 | * VFS operations. |
662 | * VFS operations. |
628 | */ |
663 | */ |
629 | 664 | ||
630 | void fat_mounted(ipc_callid_t rid, ipc_call_t *request) |
665 | void fat_mounted(ipc_callid_t rid, ipc_call_t *request) |
631 | { |
666 | { |
632 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
667 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
633 | fat_bs_t *bs; |
668 | fat_bs_t *bs; |
634 | uint16_t bps; |
669 | uint16_t bps; |
635 | uint16_t rde; |
670 | uint16_t rde; |
636 | int rc; |
671 | int rc; |
637 | 672 | ||
638 | /* initialize libblock */ |
673 | /* initialize libblock */ |
639 | rc = block_init(dev_handle, BS_SIZE); |
674 | rc = block_init(dev_handle, BS_SIZE); |
640 | if (rc != EOK) { |
675 | if (rc != EOK) { |
641 | ipc_answer_0(rid, rc); |
676 | ipc_answer_0(rid, rc); |
642 | return; |
677 | return; |
643 | } |
678 | } |
644 | 679 | ||
645 | /* prepare the boot block */ |
680 | /* prepare the boot block */ |
646 | rc = block_bb_read(dev_handle, BS_BLOCK * BS_SIZE, BS_SIZE); |
681 | rc = block_bb_read(dev_handle, BS_BLOCK * BS_SIZE, BS_SIZE); |
647 | if (rc != EOK) { |
682 | if (rc != EOK) { |
648 | block_fini(dev_handle); |
683 | block_fini(dev_handle); |
649 | ipc_answer_0(rid, rc); |
684 | ipc_answer_0(rid, rc); |
650 | return; |
685 | return; |
651 | } |
686 | } |
652 | 687 | ||
653 | /* get the buffer with the boot sector */ |
688 | /* get the buffer with the boot sector */ |
654 | bs = block_bb_get(dev_handle); |
689 | bs = block_bb_get(dev_handle); |
655 | 690 | ||
656 | /* Read the number of root directory entries. */ |
691 | /* Read the number of root directory entries. */ |
657 | bps = uint16_t_le2host(bs->bps); |
692 | bps = uint16_t_le2host(bs->bps); |
658 | rde = uint16_t_le2host(bs->root_ent_max); |
693 | rde = uint16_t_le2host(bs->root_ent_max); |
659 | 694 | ||
660 | if (bps != BS_SIZE) { |
695 | if (bps != BS_SIZE) { |
661 | block_fini(dev_handle); |
696 | block_fini(dev_handle); |
662 | ipc_answer_0(rid, ENOTSUP); |
697 | ipc_answer_0(rid, ENOTSUP); |
663 | return; |
698 | return; |
664 | } |
699 | } |
665 | 700 | ||
666 | /* Initialize the block cache */ |
701 | /* Initialize the block cache */ |
667 | rc = block_cache_init(dev_handle, bps, 0 /* XXX */); |
702 | rc = block_cache_init(dev_handle, bps, 0 /* XXX */); |
668 | if (rc != EOK) { |
703 | if (rc != EOK) { |
669 | block_fini(dev_handle); |
704 | block_fini(dev_handle); |
670 | ipc_answer_0(rid, rc); |
705 | ipc_answer_0(rid, rc); |
671 | return; |
706 | return; |
672 | } |
707 | } |
673 | 708 | ||
674 | rc = fat_idx_init_by_dev_handle(dev_handle); |
709 | rc = fat_idx_init_by_dev_handle(dev_handle); |
675 | if (rc != EOK) { |
710 | if (rc != EOK) { |
676 | block_fini(dev_handle); |
711 | block_fini(dev_handle); |
677 | ipc_answer_0(rid, rc); |
712 | ipc_answer_0(rid, rc); |
678 | return; |
713 | return; |
679 | } |
714 | } |
680 | 715 | ||
681 | /* Initialize the root node. */ |
716 | /* Initialize the root node. */ |
682 | fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t)); |
717 | fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t)); |
683 | if (!rootp) { |
718 | if (!rootp) { |
684 | block_fini(dev_handle); |
719 | block_fini(dev_handle); |
685 | fat_idx_fini_by_dev_handle(dev_handle); |
720 | fat_idx_fini_by_dev_handle(dev_handle); |
686 | ipc_answer_0(rid, ENOMEM); |
721 | ipc_answer_0(rid, ENOMEM); |
687 | return; |
722 | return; |
688 | } |
723 | } |
689 | fat_node_initialize(rootp); |
724 | fat_node_initialize(rootp); |
690 | 725 | ||
691 | fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0); |
726 | fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0); |
692 | if (!ridxp) { |
727 | if (!ridxp) { |
693 | block_fini(dev_handle); |
728 | block_fini(dev_handle); |
694 | free(rootp); |
729 | free(rootp); |
695 | fat_idx_fini_by_dev_handle(dev_handle); |
730 | fat_idx_fini_by_dev_handle(dev_handle); |
696 | ipc_answer_0(rid, ENOMEM); |
731 | ipc_answer_0(rid, ENOMEM); |
697 | return; |
732 | return; |
698 | } |
733 | } |
699 | assert(ridxp->index == 0); |
734 | assert(ridxp->index == 0); |
700 | /* ridxp->lock held */ |
735 | /* ridxp->lock held */ |
701 | 736 | ||
702 | rootp->type = FAT_DIRECTORY; |
737 | rootp->type = FAT_DIRECTORY; |
703 | rootp->firstc = FAT_CLST_ROOT; |
738 | rootp->firstc = FAT_CLST_ROOT; |
704 | rootp->refcnt = 1; |
739 | rootp->refcnt = 1; |
705 | rootp->lnkcnt = 0; /* FS root is not linked */ |
740 | rootp->lnkcnt = 0; /* FS root is not linked */ |
706 | rootp->size = rde * sizeof(fat_dentry_t); |
741 | rootp->size = rde * sizeof(fat_dentry_t); |
707 | rootp->idx = ridxp; |
742 | rootp->idx = ridxp; |
708 | ridxp->nodep = rootp; |
743 | ridxp->nodep = rootp; |
709 | 744 | ||
710 | futex_up(&ridxp->lock); |
745 | futex_up(&ridxp->lock); |
711 | 746 | ||
712 | ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt); |
747 | ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt); |
713 | } |
748 | } |
714 | 749 | ||
715 | void fat_mount(ipc_callid_t rid, ipc_call_t *request) |
750 | void fat_mount(ipc_callid_t rid, ipc_call_t *request) |
716 | { |
751 | { |
717 | ipc_answer_0(rid, ENOTSUP); |
752 | ipc_answer_0(rid, ENOTSUP); |
718 | } |
753 | } |
719 | 754 | ||
720 | void fat_lookup(ipc_callid_t rid, ipc_call_t *request) |
755 | void fat_lookup(ipc_callid_t rid, ipc_call_t *request) |
721 | { |
756 | { |
722 | libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request); |
757 | libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request); |
723 | } |
758 | } |
724 | 759 | ||
725 | void fat_read(ipc_callid_t rid, ipc_call_t *request) |
760 | void fat_read(ipc_callid_t rid, ipc_call_t *request) |
726 | { |
761 | { |
727 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
762 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
728 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
763 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
729 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
764 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
730 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
765 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
731 | fat_bs_t *bs; |
766 | fat_bs_t *bs; |
732 | uint16_t bps; |
767 | uint16_t bps; |
733 | size_t bytes; |
768 | size_t bytes; |
734 | block_t *b; |
769 | block_t *b; |
735 | 770 | ||
736 | if (!nodep) { |
771 | if (!nodep) { |
737 | ipc_answer_0(rid, ENOENT); |
772 | ipc_answer_0(rid, ENOENT); |
738 | return; |
773 | return; |
739 | } |
774 | } |
740 | 775 | ||
741 | ipc_callid_t callid; |
776 | ipc_callid_t callid; |
742 | size_t len; |
777 | size_t len; |
743 | if (!ipc_data_read_receive(&callid, &len)) { |
778 | if (!ipc_data_read_receive(&callid, &len)) { |
744 | fat_node_put(nodep); |
779 | fat_node_put(nodep); |
745 | ipc_answer_0(callid, EINVAL); |
780 | ipc_answer_0(callid, EINVAL); |
746 | ipc_answer_0(rid, EINVAL); |
781 | ipc_answer_0(rid, EINVAL); |
747 | return; |
782 | return; |
748 | } |
783 | } |
749 | 784 | ||
750 | bs = block_bb_get(dev_handle); |
785 | bs = block_bb_get(dev_handle); |
751 | bps = uint16_t_le2host(bs->bps); |
786 | bps = uint16_t_le2host(bs->bps); |
752 | 787 | ||
753 | if (nodep->type == FAT_FILE) { |
788 | if (nodep->type == FAT_FILE) { |
754 | /* |
789 | /* |
755 | * Our strategy for regular file reads is to read one block at |
790 | * Our strategy for regular file reads is to read one block at |
756 | * most and make use of the possibility to return less data than |
791 | * most and make use of the possibility to return less data than |
757 | * requested. This keeps the code very simple. |
792 | * requested. This keeps the code very simple. |
758 | */ |
793 | */ |
759 | if (pos >= nodep->size) { |
794 | if (pos >= nodep->size) { |
760 | /* reading beyond the EOF */ |
795 | /* reading beyond the EOF */ |
761 | bytes = 0; |
796 | bytes = 0; |
762 | (void) ipc_data_read_finalize(callid, NULL, 0); |
797 | (void) ipc_data_read_finalize(callid, NULL, 0); |
763 | } else { |
798 | } else { |
764 | bytes = min(len, bps - pos % bps); |
799 | bytes = min(len, bps - pos % bps); |
765 | bytes = min(bytes, nodep->size - pos); |
800 | bytes = min(bytes, nodep->size - pos); |
766 | b = fat_block_get(bs, nodep, pos / bps, |
801 | b = fat_block_get(bs, nodep, pos / bps, |
767 | BLOCK_FLAGS_NONE); |
802 | BLOCK_FLAGS_NONE); |
768 | (void) ipc_data_read_finalize(callid, b->data + pos % bps, |
803 | (void) ipc_data_read_finalize(callid, b->data + pos % bps, |
769 | bytes); |
804 | bytes); |
770 | block_put(b); |
805 | block_put(b); |
771 | } |
806 | } |
772 | } else { |
807 | } else { |
773 | unsigned bnum; |
808 | unsigned bnum; |
774 | off_t spos = pos; |
809 | off_t spos = pos; |
775 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
810 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
776 | fat_dentry_t *d; |
811 | fat_dentry_t *d; |
777 | 812 | ||
778 | assert(nodep->type == FAT_DIRECTORY); |
813 | assert(nodep->type == FAT_DIRECTORY); |
779 | assert(nodep->size % bps == 0); |
814 | assert(nodep->size % bps == 0); |
780 | assert(bps % sizeof(fat_dentry_t) == 0); |
815 | assert(bps % sizeof(fat_dentry_t) == 0); |
781 | 816 | ||
782 | /* |
817 | /* |
783 | * Our strategy for readdir() is to use the position pointer as |
818 | * Our strategy for readdir() is to use the position pointer as |
784 | * an index into the array of all dentries. On entry, it points |
819 | * an index into the array of all dentries. On entry, it points |
785 | * to the first unread dentry. If we skip any dentries, we bump |
820 | * to the first unread dentry. If we skip any dentries, we bump |
786 | * the position pointer accordingly. |
821 | * the position pointer accordingly. |
787 | */ |
822 | */ |
788 | bnum = (pos * sizeof(fat_dentry_t)) / bps; |
823 | bnum = (pos * sizeof(fat_dentry_t)) / bps; |
789 | while (bnum < nodep->size / bps) { |
824 | while (bnum < nodep->size / bps) { |
790 | off_t o; |
825 | off_t o; |
791 | 826 | ||
792 | b = fat_block_get(bs, nodep, bnum, BLOCK_FLAGS_NONE); |
827 | b = fat_block_get(bs, nodep, bnum, BLOCK_FLAGS_NONE); |
793 | for (o = pos % (bps / sizeof(fat_dentry_t)); |
828 | for (o = pos % (bps / sizeof(fat_dentry_t)); |
794 | o < bps / sizeof(fat_dentry_t); |
829 | o < bps / sizeof(fat_dentry_t); |
795 | o++, pos++) { |
830 | o++, pos++) { |
796 | d = ((fat_dentry_t *)b->data) + o; |
831 | d = ((fat_dentry_t *)b->data) + o; |
797 | switch (fat_classify_dentry(d)) { |
832 | switch (fat_classify_dentry(d)) { |
798 | case FAT_DENTRY_SKIP: |
833 | case FAT_DENTRY_SKIP: |
799 | case FAT_DENTRY_FREE: |
834 | case FAT_DENTRY_FREE: |
800 | continue; |
835 | continue; |
801 | case FAT_DENTRY_LAST: |
836 | case FAT_DENTRY_LAST: |
802 | block_put(b); |
837 | block_put(b); |
803 | goto miss; |
838 | goto miss; |
804 | default: |
839 | default: |
805 | case FAT_DENTRY_VALID: |
840 | case FAT_DENTRY_VALID: |
806 | fat_dentry_name_get(d, name); |
841 | fat_dentry_name_get(d, name); |
807 | block_put(b); |
842 | block_put(b); |
808 | goto hit; |
843 | goto hit; |
809 | } |
844 | } |
810 | } |
845 | } |
811 | block_put(b); |
846 | block_put(b); |
812 | bnum++; |
847 | bnum++; |
813 | } |
848 | } |
814 | miss: |
849 | miss: |
815 | fat_node_put(nodep); |
850 | fat_node_put(nodep); |
816 | ipc_answer_0(callid, ENOENT); |
851 | ipc_answer_0(callid, ENOENT); |
817 | ipc_answer_1(rid, ENOENT, 0); |
852 | ipc_answer_1(rid, ENOENT, 0); |
818 | return; |
853 | return; |
819 | hit: |
854 | hit: |
820 | (void) ipc_data_read_finalize(callid, name, strlen(name) + 1); |
855 | (void) ipc_data_read_finalize(callid, name, strlen(name) + 1); |
821 | bytes = (pos - spos) + 1; |
856 | bytes = (pos - spos) + 1; |
822 | } |
857 | } |
823 | 858 | ||
824 | fat_node_put(nodep); |
859 | fat_node_put(nodep); |
825 | ipc_answer_1(rid, EOK, (ipcarg_t)bytes); |
860 | ipc_answer_1(rid, EOK, (ipcarg_t)bytes); |
826 | } |
861 | } |
827 | 862 | ||
828 | void fat_write(ipc_callid_t rid, ipc_call_t *request) |
863 | void fat_write(ipc_callid_t rid, ipc_call_t *request) |
829 | { |
864 | { |
830 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
865 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
831 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
866 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
832 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
867 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
833 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
868 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
834 | fat_bs_t *bs; |
869 | fat_bs_t *bs; |
835 | size_t bytes; |
870 | size_t bytes; |
836 | block_t *b; |
871 | block_t *b; |
837 | uint16_t bps; |
872 | uint16_t bps; |
838 | unsigned spc; |
873 | unsigned spc; |
839 | unsigned bpc; /* bytes per cluster */ |
874 | unsigned bpc; /* bytes per cluster */ |
840 | off_t boundary; |
875 | off_t boundary; |
841 | int flags = BLOCK_FLAGS_NONE; |
876 | int flags = BLOCK_FLAGS_NONE; |
842 | 877 | ||
843 | if (!nodep) { |
878 | if (!nodep) { |
844 | ipc_answer_0(rid, ENOENT); |
879 | ipc_answer_0(rid, ENOENT); |
845 | return; |
880 | return; |
846 | } |
881 | } |
847 | 882 | ||
848 | ipc_callid_t callid; |
883 | ipc_callid_t callid; |
849 | size_t len; |
884 | size_t len; |
850 | if (!ipc_data_write_receive(&callid, &len)) { |
885 | if (!ipc_data_write_receive(&callid, &len)) { |
851 | fat_node_put(nodep); |
886 | fat_node_put(nodep); |
852 | ipc_answer_0(callid, EINVAL); |
887 | ipc_answer_0(callid, EINVAL); |
853 | ipc_answer_0(rid, EINVAL); |
888 | ipc_answer_0(rid, EINVAL); |
854 | return; |
889 | return; |
855 | } |
890 | } |
856 | 891 | ||
857 | bs = block_bb_get(dev_handle); |
892 | bs = block_bb_get(dev_handle); |
858 | bps = uint16_t_le2host(bs->bps); |
893 | bps = uint16_t_le2host(bs->bps); |
859 | spc = bs->spc; |
894 | spc = bs->spc; |
860 | bpc = bps * spc; |
895 | bpc = bps * spc; |
861 | 896 | ||
862 | /* |
897 | /* |
863 | * In all scenarios, we will attempt to write out only one block worth |
898 | * In all scenarios, we will attempt to write out only one block worth |
864 | * of data at maximum. There might be some more efficient approaches, |
899 | * of data at maximum. There might be some more efficient approaches, |
865 | * but this one greatly simplifies fat_write(). Note that we can afford |
900 | * but this one greatly simplifies fat_write(). Note that we can afford |
866 | * to do this because the client must be ready to handle the return |
901 | * to do this because the client must be ready to handle the return |
867 | * value signalizing a smaller number of bytes written. |
902 | * value signalizing a smaller number of bytes written. |
868 | */ |
903 | */ |
869 | bytes = min(len, bps - pos % bps); |
904 | bytes = min(len, bps - pos % bps); |
870 | if (bytes == bps) |
905 | if (bytes == bps) |
871 | flags |= BLOCK_FLAGS_NOREAD; |
906 | flags |= BLOCK_FLAGS_NOREAD; |
872 | 907 | ||
873 | boundary = ROUND_UP(nodep->size, bpc); |
908 | boundary = ROUND_UP(nodep->size, bpc); |
874 | if (pos < boundary) { |
909 | if (pos < boundary) { |
875 | /* |
910 | /* |
876 | * This is the easier case - we are either overwriting already |
911 | * This is the easier case - we are either overwriting already |
877 | * existing contents or writing behind the EOF, but still within |
912 | * existing contents or writing behind the EOF, but still within |
878 | * the limits of the last cluster. The node size may grow to the |
913 | * the limits of the last cluster. The node size may grow to the |
879 | * next block size boundary. |
914 | * next block size boundary. |
880 | */ |
915 | */ |
881 | fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos); |
916 | fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos); |
882 | b = fat_block_get(bs, nodep, pos / bps, flags); |
917 | b = fat_block_get(bs, nodep, pos / bps, flags); |
883 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
918 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
884 | bytes); |
919 | bytes); |
885 | b->dirty = true; /* need to sync block */ |
920 | b->dirty = true; /* need to sync block */ |
886 | block_put(b); |
921 | block_put(b); |
887 | if (pos + bytes > nodep->size) { |
922 | if (pos + bytes > nodep->size) { |
888 | nodep->size = pos + bytes; |
923 | nodep->size = pos + bytes; |
889 | nodep->dirty = true; /* need to sync node */ |
924 | nodep->dirty = true; /* need to sync node */ |
890 | } |
925 | } |
891 | ipc_answer_2(rid, EOK, bytes, nodep->size); |
926 | ipc_answer_2(rid, EOK, bytes, nodep->size); |
892 | fat_node_put(nodep); |
927 | fat_node_put(nodep); |
893 | return; |
928 | return; |
894 | } else { |
929 | } else { |
895 | /* |
930 | /* |
896 | * This is the more difficult case. We must allocate new |
931 | * This is the more difficult case. We must allocate new |
897 | * clusters for the node and zero them out. |
932 | * clusters for the node and zero them out. |
898 | */ |
933 | */ |
899 | int status; |
934 | int status; |
900 | unsigned nclsts; |
935 | unsigned nclsts; |
901 | fat_cluster_t mcl, lcl; |
936 | fat_cluster_t mcl, lcl; |
902 | 937 | ||
903 | nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc; |
938 | nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc; |
904 | /* create an independent chain of nclsts clusters in all FATs */ |
939 | /* create an independent chain of nclsts clusters in all FATs */ |
905 | status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl); |
940 | status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl); |
906 | if (status != EOK) { |
941 | if (status != EOK) { |
907 | /* could not allocate a chain of nclsts clusters */ |
942 | /* could not allocate a chain of nclsts clusters */ |
908 | fat_node_put(nodep); |
943 | fat_node_put(nodep); |
909 | ipc_answer_0(callid, status); |
944 | ipc_answer_0(callid, status); |
910 | ipc_answer_0(rid, status); |
945 | ipc_answer_0(rid, status); |
911 | return; |
946 | return; |
912 | } |
947 | } |
913 | /* zero fill any gaps */ |
948 | /* zero fill any gaps */ |
914 | fat_fill_gap(bs, nodep, mcl, pos); |
949 | fat_fill_gap(bs, nodep, mcl, pos); |
915 | b = _fat_block_get(bs, dev_handle, lcl, (pos / bps) % spc, |
950 | b = _fat_block_get(bs, dev_handle, lcl, (pos / bps) % spc, |
916 | flags); |
951 | flags); |
917 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
952 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
918 | bytes); |
953 | bytes); |
919 | b->dirty = true; /* need to sync block */ |
954 | b->dirty = true; /* need to sync block */ |
920 | block_put(b); |
955 | block_put(b); |
921 | /* |
956 | /* |
922 | * Append the cluster chain starting in mcl to the end of the |
957 | * Append the cluster chain starting in mcl to the end of the |
923 | * node's cluster chain. |
958 | * node's cluster chain. |
924 | */ |
959 | */ |
925 | fat_append_clusters(bs, nodep, mcl); |
960 | fat_append_clusters(bs, nodep, mcl); |
926 | nodep->size = pos + bytes; |
961 | nodep->size = pos + bytes; |
927 | nodep->dirty = true; /* need to sync node */ |
962 | nodep->dirty = true; /* need to sync node */ |
928 | ipc_answer_2(rid, EOK, bytes, nodep->size); |
963 | ipc_answer_2(rid, EOK, bytes, nodep->size); |
929 | fat_node_put(nodep); |
964 | fat_node_put(nodep); |
930 | return; |
965 | return; |
931 | } |
966 | } |
932 | } |
967 | } |
933 | 968 | ||
934 | void fat_truncate(ipc_callid_t rid, ipc_call_t *request) |
969 | void fat_truncate(ipc_callid_t rid, ipc_call_t *request) |
935 | { |
970 | { |
936 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
971 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
937 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
972 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
938 | size_t size = (off_t)IPC_GET_ARG3(*request); |
973 | size_t size = (off_t)IPC_GET_ARG3(*request); |
939 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
974 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
940 | fat_bs_t *bs; |
975 | fat_bs_t *bs; |
941 | uint16_t bps; |
976 | uint16_t bps; |
942 | uint8_t spc; |
977 | uint8_t spc; |
943 | unsigned bpc; /* bytes per cluster */ |
978 | unsigned bpc; /* bytes per cluster */ |
944 | int rc; |
979 | int rc; |
945 | 980 | ||
946 | if (!nodep) { |
981 | if (!nodep) { |
947 | ipc_answer_0(rid, ENOENT); |
982 | ipc_answer_0(rid, ENOENT); |
948 | return; |
983 | return; |
949 | } |
984 | } |
950 | 985 | ||
951 | bs = block_bb_get(dev_handle); |
986 | bs = block_bb_get(dev_handle); |
952 | bps = uint16_t_le2host(bs->bps); |
987 | bps = uint16_t_le2host(bs->bps); |
953 | spc = bs->spc; |
988 | spc = bs->spc; |
954 | bpc = bps * spc; |
989 | bpc = bps * spc; |
955 | 990 | ||
956 | if (nodep->size == size) { |
991 | if (nodep->size == size) { |
957 | rc = EOK; |
992 | rc = EOK; |
958 | } else if (nodep->size < size) { |
993 | } else if (nodep->size < size) { |
959 | /* |
994 | /* |
960 | * The standard says we have the freedom to grow the node. |
995 | * The standard says we have the freedom to grow the node. |
961 | * For now, we simply return an error. |
996 | * For now, we simply return an error. |
962 | */ |
997 | */ |
963 | rc = EINVAL; |
998 | rc = EINVAL; |
964 | } else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) { |
999 | } else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) { |
965 | /* |
1000 | /* |
966 | * The node will be shrunk, but no clusters will be deallocated. |
1001 | * The node will be shrunk, but no clusters will be deallocated. |
967 | */ |
1002 | */ |
968 | nodep->size = size; |
1003 | nodep->size = size; |
969 | nodep->dirty = true; /* need to sync node */ |
1004 | nodep->dirty = true; /* need to sync node */ |
970 | rc = EOK; |
1005 | rc = EOK; |
971 | } else { |
1006 | } else { |
972 | /* |
1007 | /* |
973 | * The node will be shrunk, clusters will be deallocated. |
1008 | * The node will be shrunk, clusters will be deallocated. |
974 | */ |
1009 | */ |
975 | if (size == 0) { |
1010 | if (size == 0) { |
976 | fat_chop_clusters(bs, nodep, FAT_CLST_RES0); |
1011 | fat_chop_clusters(bs, nodep, FAT_CLST_RES0); |
977 | } else { |
1012 | } else { |
978 | fat_cluster_t lastc; |
1013 | fat_cluster_t lastc; |
979 | (void) fat_cluster_walk(bs, dev_handle, nodep->firstc, |
1014 | (void) fat_cluster_walk(bs, dev_handle, nodep->firstc, |
980 | &lastc, (size - 1) / bpc); |
1015 | &lastc, (size - 1) / bpc); |
981 | fat_chop_clusters(bs, nodep, lastc); |
1016 | fat_chop_clusters(bs, nodep, lastc); |
982 | } |
1017 | } |
983 | nodep->size = size; |
1018 | nodep->size = size; |
984 | nodep->dirty = true; /* need to sync node */ |
1019 | nodep->dirty = true; /* need to sync node */ |
985 | rc = EOK; |
1020 | rc = EOK; |
986 | } |
1021 | } |
987 | fat_node_put(nodep); |
1022 | fat_node_put(nodep); |
988 | ipc_answer_0(rid, rc); |
1023 | ipc_answer_0(rid, rc); |
989 | return; |
1024 | return; |
990 | } |
1025 | } |
991 | 1026 | ||
992 | void fat_destroy(ipc_callid_t rid, ipc_call_t *request) |
1027 | void fat_destroy(ipc_callid_t rid, ipc_call_t *request) |
993 | { |
1028 | { |
994 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
1029 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
995 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
1030 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
996 | int rc; |
1031 | int rc; |
997 | 1032 | ||
998 | fat_node_t *nodep = fat_node_get(dev_handle, index); |
1033 | fat_node_t *nodep = fat_node_get(dev_handle, index); |
999 | if (!nodep) { |
1034 | if (!nodep) { |
1000 | ipc_answer_0(rid, ENOENT); |
1035 | ipc_answer_0(rid, ENOENT); |
1001 | return; |
1036 | return; |
1002 | } |
1037 | } |
1003 | 1038 | ||
1004 | rc = fat_destroy_node(nodep); |
1039 | rc = fat_destroy_node(nodep); |
1005 | ipc_answer_0(rid, rc); |
1040 | ipc_answer_0(rid, rc); |
1006 | } |
1041 | } |
1007 | 1042 | ||
1008 | /** |
1043 | /** |
1009 | * @} |
1044 | * @} |
1010 | */ |
1045 | */ |
1011 | 1046 |