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