1 | /*
|
---|
2 | * Copyright (c) 2008 Jakub Jermar
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
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
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
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
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
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
|
---|
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
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup fs
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file fat_ops.c
|
---|
35 | * @brief Implementation of VFS operations for the FAT file system server.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include "fat.h"
|
---|
39 | #include "fat_dentry.h"
|
---|
40 | #include "fat_fat.h"
|
---|
41 | #include "../../vfs/vfs.h"
|
---|
42 | #include <libfs.h>
|
---|
43 | #include <libblock.h>
|
---|
44 | #include <ipc/ipc.h>
|
---|
45 | #include <ipc/services.h>
|
---|
46 | #include <ipc/devmap.h>
|
---|
47 | #include <async.h>
|
---|
48 | #include <errno.h>
|
---|
49 | #include <string.h>
|
---|
50 | #include <byteorder.h>
|
---|
51 | #include <adt/hash_table.h>
|
---|
52 | #include <adt/list.h>
|
---|
53 | #include <assert.h>
|
---|
54 | #include <fibril_synch.h>
|
---|
55 | #include <sys/mman.h>
|
---|
56 | #include <align.h>
|
---|
57 |
|
---|
58 | #define FAT_NODE(node) ((node) ? (fat_node_t *) (node)->data : NULL)
|
---|
59 | #define FS_NODE(node) ((node) ? (node)->bp : NULL)
|
---|
60 |
|
---|
61 | /** Mutex protecting the list of cached free FAT nodes. */
|
---|
62 | static FIBRIL_MUTEX_INITIALIZE(ffn_mutex);
|
---|
63 |
|
---|
64 | /** List of cached free FAT nodes. */
|
---|
65 | static LIST_INITIALIZE(ffn_head);
|
---|
66 |
|
---|
67 | /*
|
---|
68 | * Forward declarations of FAT libfs operations.
|
---|
69 | */
|
---|
70 | static int fat_root_get(fs_node_t **, dev_handle_t);
|
---|
71 | static int fat_match(fs_node_t **, fs_node_t *, const char *);
|
---|
72 | static int fat_node_get(fs_node_t **, dev_handle_t, fs_index_t);
|
---|
73 | static int fat_node_open(fs_node_t *);
|
---|
74 | static int fat_node_put(fs_node_t *);
|
---|
75 | static int fat_create_node(fs_node_t **, dev_handle_t, int);
|
---|
76 | static int fat_destroy_node(fs_node_t *);
|
---|
77 | static int fat_link(fs_node_t *, fs_node_t *, const char *);
|
---|
78 | static int fat_unlink(fs_node_t *, fs_node_t *, const char *);
|
---|
79 | static int fat_has_children(bool *, fs_node_t *);
|
---|
80 | static fs_index_t fat_index_get(fs_node_t *);
|
---|
81 | static size_t fat_size_get(fs_node_t *);
|
---|
82 | static unsigned fat_lnkcnt_get(fs_node_t *);
|
---|
83 | static char fat_plb_get_char(unsigned);
|
---|
84 | static bool fat_is_directory(fs_node_t *);
|
---|
85 | static bool fat_is_file(fs_node_t *node);
|
---|
86 | static dev_handle_t fat_device_get(fs_node_t *node);
|
---|
87 |
|
---|
88 | /*
|
---|
89 | * Helper functions.
|
---|
90 | */
|
---|
91 | static void fat_node_initialize(fat_node_t *node)
|
---|
92 | {
|
---|
93 | fibril_mutex_initialize(&node->lock);
|
---|
94 | node->bp = NULL;
|
---|
95 | node->idx = NULL;
|
---|
96 | node->type = 0;
|
---|
97 | link_initialize(&node->ffn_link);
|
---|
98 | node->size = 0;
|
---|
99 | node->lnkcnt = 0;
|
---|
100 | node->refcnt = 0;
|
---|
101 | node->dirty = false;
|
---|
102 | }
|
---|
103 |
|
---|
104 | static int fat_node_sync(fat_node_t *node)
|
---|
105 | {
|
---|
106 | block_t *b;
|
---|
107 | fat_bs_t *bs;
|
---|
108 | fat_dentry_t *d;
|
---|
109 | uint16_t bps;
|
---|
110 | unsigned dps;
|
---|
111 | int rc;
|
---|
112 |
|
---|
113 | assert(node->dirty);
|
---|
114 |
|
---|
115 | bs = block_bb_get(node->idx->dev_handle);
|
---|
116 | bps = uint16_t_le2host(bs->bps);
|
---|
117 | dps = bps / sizeof(fat_dentry_t);
|
---|
118 |
|
---|
119 | /* Read the block that contains the dentry of interest. */
|
---|
120 | rc = _fat_block_get(&b, bs, node->idx->dev_handle, node->idx->pfc,
|
---|
121 | (node->idx->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE);
|
---|
122 | if (rc != EOK)
|
---|
123 | return rc;
|
---|
124 |
|
---|
125 | d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps);
|
---|
126 |
|
---|
127 | d->firstc = host2uint16_t_le(node->firstc);
|
---|
128 | if (node->type == FAT_FILE) {
|
---|
129 | d->size = host2uint32_t_le(node->size);
|
---|
130 | } else if (node->type == FAT_DIRECTORY) {
|
---|
131 | d->attr = FAT_ATTR_SUBDIR;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /* TODO: update other fields? (e.g time fields) */
|
---|
135 |
|
---|
136 | b->dirty = true; /* need to sync block */
|
---|
137 | rc = block_put(b);
|
---|
138 | return rc;
|
---|
139 | }
|
---|
140 |
|
---|
141 | static int fat_node_fini_by_dev_handle(dev_handle_t dev_handle)
|
---|
142 | {
|
---|
143 | link_t *lnk;
|
---|
144 | fat_node_t *nodep;
|
---|
145 | int rc;
|
---|
146 |
|
---|
147 | /*
|
---|
148 | * We are called from fat_unmounted() and assume that there are already
|
---|
149 | * no nodes belonging to this instance with non-zero refcount. Therefore
|
---|
150 | * it is sufficient to clean up only the FAT free node list.
|
---|
151 | */
|
---|
152 |
|
---|
153 | restart:
|
---|
154 | fibril_mutex_lock(&ffn_mutex);
|
---|
155 | for (lnk = ffn_head.next; lnk != &ffn_head; lnk = lnk->next) {
|
---|
156 | nodep = list_get_instance(lnk, fat_node_t, ffn_link);
|
---|
157 | if (!fibril_mutex_trylock(&nodep->lock)) {
|
---|
158 | fibril_mutex_unlock(&ffn_mutex);
|
---|
159 | goto restart;
|
---|
160 | }
|
---|
161 | if (!fibril_mutex_trylock(&nodep->idx->lock)) {
|
---|
162 | fibril_mutex_unlock(&nodep->lock);
|
---|
163 | fibril_mutex_unlock(&ffn_mutex);
|
---|
164 | goto restart;
|
---|
165 | }
|
---|
166 | if (nodep->idx->dev_handle != dev_handle) {
|
---|
167 | fibril_mutex_unlock(&nodep->idx->lock);
|
---|
168 | fibril_mutex_unlock(&nodep->lock);
|
---|
169 | continue;
|
---|
170 | }
|
---|
171 |
|
---|
172 | list_remove(&nodep->ffn_link);
|
---|
173 | fibril_mutex_unlock(&ffn_mutex);
|
---|
174 |
|
---|
175 | /*
|
---|
176 | * We can unlock the node and its index structure because we are
|
---|
177 | * the last player on this playground and VFS is preventing new
|
---|
178 | * players from entering.
|
---|
179 | */
|
---|
180 | fibril_mutex_unlock(&nodep->idx->lock);
|
---|
181 | fibril_mutex_unlock(&nodep->lock);
|
---|
182 |
|
---|
183 | if (nodep->dirty) {
|
---|
184 | rc = fat_node_sync(nodep);
|
---|
185 | if (rc != EOK)
|
---|
186 | return rc;
|
---|
187 | }
|
---|
188 | nodep->idx->nodep = NULL;
|
---|
189 | free(nodep->bp);
|
---|
190 | free(nodep);
|
---|
191 |
|
---|
192 | /* Need to restart because we changed the ffn_head list. */
|
---|
193 | goto restart;
|
---|
194 | }
|
---|
195 | fibril_mutex_unlock(&ffn_mutex);
|
---|
196 |
|
---|
197 | return EOK;
|
---|
198 | }
|
---|
199 |
|
---|
200 | static int fat_node_get_new(fat_node_t **nodepp)
|
---|
201 | {
|
---|
202 | fs_node_t *fn;
|
---|
203 | fat_node_t *nodep;
|
---|
204 | int rc;
|
---|
205 |
|
---|
206 | fibril_mutex_lock(&ffn_mutex);
|
---|
207 | if (!list_empty(&ffn_head)) {
|
---|
208 | /* Try to use a cached free node structure. */
|
---|
209 | fat_idx_t *idxp_tmp;
|
---|
210 | nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link);
|
---|
211 | if (!fibril_mutex_trylock(&nodep->lock))
|
---|
212 | goto skip_cache;
|
---|
213 | idxp_tmp = nodep->idx;
|
---|
214 | if (!fibril_mutex_trylock(&idxp_tmp->lock)) {
|
---|
215 | fibril_mutex_unlock(&nodep->lock);
|
---|
216 | goto skip_cache;
|
---|
217 | }
|
---|
218 | list_remove(&nodep->ffn_link);
|
---|
219 | fibril_mutex_unlock(&ffn_mutex);
|
---|
220 | if (nodep->dirty) {
|
---|
221 | rc = fat_node_sync(nodep);
|
---|
222 | if (rc != EOK) {
|
---|
223 | idxp_tmp->nodep = NULL;
|
---|
224 | fibril_mutex_unlock(&nodep->lock);
|
---|
225 | fibril_mutex_unlock(&idxp_tmp->lock);
|
---|
226 | free(nodep->bp);
|
---|
227 | free(nodep);
|
---|
228 | return rc;
|
---|
229 | }
|
---|
230 | }
|
---|
231 | idxp_tmp->nodep = NULL;
|
---|
232 | fibril_mutex_unlock(&nodep->lock);
|
---|
233 | fibril_mutex_unlock(&idxp_tmp->lock);
|
---|
234 | fn = FS_NODE(nodep);
|
---|
235 | } else {
|
---|
236 | skip_cache:
|
---|
237 | /* Try to allocate a new node structure. */
|
---|
238 | fibril_mutex_unlock(&ffn_mutex);
|
---|
239 | fn = (fs_node_t *)malloc(sizeof(fs_node_t));
|
---|
240 | if (!fn)
|
---|
241 | return ENOMEM;
|
---|
242 | nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
|
---|
243 | if (!nodep) {
|
---|
244 | free(fn);
|
---|
245 | return ENOMEM;
|
---|
246 | }
|
---|
247 | }
|
---|
248 | fat_node_initialize(nodep);
|
---|
249 | fs_node_initialize(fn);
|
---|
250 | fn->data = nodep;
|
---|
251 | nodep->bp = fn;
|
---|
252 |
|
---|
253 | *nodepp = nodep;
|
---|
254 | return EOK;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /** Internal version of fat_node_get().
|
---|
258 | *
|
---|
259 | * @param idxp Locked index structure.
|
---|
260 | */
|
---|
261 | static int fat_node_get_core(fat_node_t **nodepp, fat_idx_t *idxp)
|
---|
262 | {
|
---|
263 | block_t *b;
|
---|
264 | fat_bs_t *bs;
|
---|
265 | fat_dentry_t *d;
|
---|
266 | fat_node_t *nodep = NULL;
|
---|
267 | unsigned bps;
|
---|
268 | unsigned spc;
|
---|
269 | unsigned dps;
|
---|
270 | int rc;
|
---|
271 |
|
---|
272 | if (idxp->nodep) {
|
---|
273 | /*
|
---|
274 | * We are lucky.
|
---|
275 | * The node is already instantiated in memory.
|
---|
276 | */
|
---|
277 | fibril_mutex_lock(&idxp->nodep->lock);
|
---|
278 | if (!idxp->nodep->refcnt++) {
|
---|
279 | fibril_mutex_lock(&ffn_mutex);
|
---|
280 | list_remove(&idxp->nodep->ffn_link);
|
---|
281 | fibril_mutex_unlock(&ffn_mutex);
|
---|
282 | }
|
---|
283 | fibril_mutex_unlock(&idxp->nodep->lock);
|
---|
284 | *nodepp = idxp->nodep;
|
---|
285 | return EOK;
|
---|
286 | }
|
---|
287 |
|
---|
288 | /*
|
---|
289 | * We must instantiate the node from the file system.
|
---|
290 | */
|
---|
291 |
|
---|
292 | assert(idxp->pfc);
|
---|
293 |
|
---|
294 | rc = fat_node_get_new(&nodep);
|
---|
295 | if (rc != EOK)
|
---|
296 | return rc;
|
---|
297 |
|
---|
298 | bs = block_bb_get(idxp->dev_handle);
|
---|
299 | bps = uint16_t_le2host(bs->bps);
|
---|
300 | spc = bs->spc;
|
---|
301 | dps = bps / sizeof(fat_dentry_t);
|
---|
302 |
|
---|
303 | /* Read the block that contains the dentry of interest. */
|
---|
304 | rc = _fat_block_get(&b, bs, idxp->dev_handle, idxp->pfc,
|
---|
305 | (idxp->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE);
|
---|
306 | if (rc != EOK) {
|
---|
307 | (void) fat_node_put(FS_NODE(nodep));
|
---|
308 | return rc;
|
---|
309 | }
|
---|
310 |
|
---|
311 | d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
|
---|
312 | if (d->attr & FAT_ATTR_SUBDIR) {
|
---|
313 | /*
|
---|
314 | * The only directory which does not have this bit set is the
|
---|
315 | * root directory itself. The root directory node is handled
|
---|
316 | * and initialized elsewhere.
|
---|
317 | */
|
---|
318 | nodep->type = FAT_DIRECTORY;
|
---|
319 | /*
|
---|
320 | * Unfortunately, the 'size' field of the FAT dentry is not
|
---|
321 | * defined for the directory entry type. We must determine the
|
---|
322 | * size of the directory by walking the FAT.
|
---|
323 | */
|
---|
324 | uint16_t clusters;
|
---|
325 | rc = fat_clusters_get(&clusters, bs, idxp->dev_handle,
|
---|
326 | uint16_t_le2host(d->firstc));
|
---|
327 | if (rc != EOK) {
|
---|
328 | (void) fat_node_put(FS_NODE(nodep));
|
---|
329 | return rc;
|
---|
330 | }
|
---|
331 | nodep->size = bps * spc * clusters;
|
---|
332 | } else {
|
---|
333 | nodep->type = FAT_FILE;
|
---|
334 | nodep->size = uint32_t_le2host(d->size);
|
---|
335 | }
|
---|
336 | nodep->firstc = uint16_t_le2host(d->firstc);
|
---|
337 | nodep->lnkcnt = 1;
|
---|
338 | nodep->refcnt = 1;
|
---|
339 |
|
---|
340 | rc = block_put(b);
|
---|
341 | if (rc != EOK) {
|
---|
342 | (void) fat_node_put(FS_NODE(nodep));
|
---|
343 | return rc;
|
---|
344 | }
|
---|
345 |
|
---|
346 | /* Link the idx structure with the node structure. */
|
---|
347 | nodep->idx = idxp;
|
---|
348 | idxp->nodep = nodep;
|
---|
349 |
|
---|
350 | *nodepp = nodep;
|
---|
351 | return EOK;
|
---|
352 | }
|
---|
353 |
|
---|
354 | /*
|
---|
355 | * FAT libfs operations.
|
---|
356 | */
|
---|
357 |
|
---|
358 | int fat_root_get(fs_node_t **rfn, dev_handle_t dev_handle)
|
---|
359 | {
|
---|
360 | return fat_node_get(rfn, dev_handle, 0);
|
---|
361 | }
|
---|
362 |
|
---|
363 | int fat_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
|
---|
364 | {
|
---|
365 | fat_bs_t *bs;
|
---|
366 | fat_node_t *parentp = FAT_NODE(pfn);
|
---|
367 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
|
---|
368 | unsigned i, j;
|
---|
369 | unsigned bps; /* bytes per sector */
|
---|
370 | unsigned dps; /* dentries per sector */
|
---|
371 | unsigned blocks;
|
---|
372 | fat_dentry_t *d;
|
---|
373 | block_t *b;
|
---|
374 | int rc;
|
---|
375 |
|
---|
376 | fibril_mutex_lock(&parentp->idx->lock);
|
---|
377 | bs = block_bb_get(parentp->idx->dev_handle);
|
---|
378 | bps = uint16_t_le2host(bs->bps);
|
---|
379 | dps = bps / sizeof(fat_dentry_t);
|
---|
380 | blocks = parentp->size / bps;
|
---|
381 | for (i = 0; i < blocks; i++) {
|
---|
382 | rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE);
|
---|
383 | if (rc != EOK) {
|
---|
384 | fibril_mutex_unlock(&parentp->idx->lock);
|
---|
385 | return rc;
|
---|
386 | }
|
---|
387 | for (j = 0; j < dps; j++) {
|
---|
388 | d = ((fat_dentry_t *)b->data) + j;
|
---|
389 | switch (fat_classify_dentry(d)) {
|
---|
390 | case FAT_DENTRY_SKIP:
|
---|
391 | case FAT_DENTRY_FREE:
|
---|
392 | continue;
|
---|
393 | case FAT_DENTRY_LAST:
|
---|
394 | /* miss */
|
---|
395 | rc = block_put(b);
|
---|
396 | fibril_mutex_unlock(&parentp->idx->lock);
|
---|
397 | *rfn = NULL;
|
---|
398 | return rc;
|
---|
399 | default:
|
---|
400 | case FAT_DENTRY_VALID:
|
---|
401 | fat_dentry_name_get(d, name);
|
---|
402 | break;
|
---|
403 | }
|
---|
404 | if (fat_dentry_namecmp(name, component) == 0) {
|
---|
405 | /* hit */
|
---|
406 | fat_node_t *nodep;
|
---|
407 | /*
|
---|
408 | * Assume tree hierarchy for locking. We
|
---|
409 | * already have the parent and now we are going
|
---|
410 | * to lock the child. Never lock in the oposite
|
---|
411 | * order.
|
---|
412 | */
|
---|
413 | fat_idx_t *idx = fat_idx_get_by_pos(
|
---|
414 | parentp->idx->dev_handle, parentp->firstc,
|
---|
415 | i * dps + j);
|
---|
416 | fibril_mutex_unlock(&parentp->idx->lock);
|
---|
417 | if (!idx) {
|
---|
418 | /*
|
---|
419 | * Can happen if memory is low or if we
|
---|
420 | * run out of 32-bit indices.
|
---|
421 | */
|
---|
422 | rc = block_put(b);
|
---|
423 | return (rc == EOK) ? ENOMEM : rc;
|
---|
424 | }
|
---|
425 | rc = fat_node_get_core(&nodep, idx);
|
---|
426 | fibril_mutex_unlock(&idx->lock);
|
---|
427 | if (rc != EOK) {
|
---|
428 | (void) block_put(b);
|
---|
429 | return rc;
|
---|
430 | }
|
---|
431 | *rfn = FS_NODE(nodep);
|
---|
432 | rc = block_put(b);
|
---|
433 | if (rc != EOK)
|
---|
434 | (void) fat_node_put(*rfn);
|
---|
435 | return rc;
|
---|
436 | }
|
---|
437 | }
|
---|
438 | rc = block_put(b);
|
---|
439 | if (rc != EOK) {
|
---|
440 | fibril_mutex_unlock(&parentp->idx->lock);
|
---|
441 | return rc;
|
---|
442 | }
|
---|
443 | }
|
---|
444 |
|
---|
445 | fibril_mutex_unlock(&parentp->idx->lock);
|
---|
446 | *rfn = NULL;
|
---|
447 | return EOK;
|
---|
448 | }
|
---|
449 |
|
---|
450 | /** Instantiate a FAT in-core node. */
|
---|
451 | int fat_node_get(fs_node_t **rfn, dev_handle_t dev_handle, fs_index_t index)
|
---|
452 | {
|
---|
453 | fat_node_t *nodep;
|
---|
454 | fat_idx_t *idxp;
|
---|
455 | int rc;
|
---|
456 |
|
---|
457 | idxp = fat_idx_get_by_index(dev_handle, index);
|
---|
458 | if (!idxp) {
|
---|
459 | *rfn = NULL;
|
---|
460 | return EOK;
|
---|
461 | }
|
---|
462 | /* idxp->lock held */
|
---|
463 | rc = fat_node_get_core(&nodep, idxp);
|
---|
464 | fibril_mutex_unlock(&idxp->lock);
|
---|
465 | if (rc == EOK)
|
---|
466 | *rfn = FS_NODE(nodep);
|
---|
467 | return rc;
|
---|
468 | }
|
---|
469 |
|
---|
470 | int fat_node_open(fs_node_t *fn)
|
---|
471 | {
|
---|
472 | /*
|
---|
473 | * Opening a file is stateless, nothing
|
---|
474 | * to be done here.
|
---|
475 | */
|
---|
476 | return EOK;
|
---|
477 | }
|
---|
478 |
|
---|
479 | int fat_node_put(fs_node_t *fn)
|
---|
480 | {
|
---|
481 | fat_node_t *nodep = FAT_NODE(fn);
|
---|
482 | bool destroy = false;
|
---|
483 |
|
---|
484 | fibril_mutex_lock(&nodep->lock);
|
---|
485 | if (!--nodep->refcnt) {
|
---|
486 | if (nodep->idx) {
|
---|
487 | fibril_mutex_lock(&ffn_mutex);
|
---|
488 | list_append(&nodep->ffn_link, &ffn_head);
|
---|
489 | fibril_mutex_unlock(&ffn_mutex);
|
---|
490 | } else {
|
---|
491 | /*
|
---|
492 | * The node does not have any index structure associated
|
---|
493 | * with itself. This can only mean that we are releasing
|
---|
494 | * the node after a failed attempt to allocate the index
|
---|
495 | * structure for it.
|
---|
496 | */
|
---|
497 | destroy = true;
|
---|
498 | }
|
---|
499 | }
|
---|
500 | fibril_mutex_unlock(&nodep->lock);
|
---|
501 | if (destroy) {
|
---|
502 | free(nodep->bp);
|
---|
503 | free(nodep);
|
---|
504 | }
|
---|
505 | return EOK;
|
---|
506 | }
|
---|
507 |
|
---|
508 | int fat_create_node(fs_node_t **rfn, dev_handle_t dev_handle, int flags)
|
---|
509 | {
|
---|
510 | fat_idx_t *idxp;
|
---|
511 | fat_node_t *nodep;
|
---|
512 | fat_bs_t *bs;
|
---|
513 | fat_cluster_t mcl, lcl;
|
---|
514 | uint16_t bps;
|
---|
515 | int rc;
|
---|
516 |
|
---|
517 | bs = block_bb_get(dev_handle);
|
---|
518 | bps = uint16_t_le2host(bs->bps);
|
---|
519 | if (flags & L_DIRECTORY) {
|
---|
520 | /* allocate a cluster */
|
---|
521 | rc = fat_alloc_clusters(bs, dev_handle, 1, &mcl, &lcl);
|
---|
522 | if (rc != EOK)
|
---|
523 | return rc;
|
---|
524 | /* populate the new cluster with unused dentries */
|
---|
525 | rc = fat_zero_cluster(bs, dev_handle, mcl);
|
---|
526 | if (rc != EOK) {
|
---|
527 | (void) fat_free_clusters(bs, dev_handle, mcl);
|
---|
528 | return rc;
|
---|
529 | }
|
---|
530 | }
|
---|
531 |
|
---|
532 | rc = fat_node_get_new(&nodep);
|
---|
533 | if (rc != EOK) {
|
---|
534 | (void) fat_free_clusters(bs, dev_handle, mcl);
|
---|
535 | return rc;
|
---|
536 | }
|
---|
537 | rc = fat_idx_get_new(&idxp, dev_handle);
|
---|
538 | if (rc != EOK) {
|
---|
539 | (void) fat_free_clusters(bs, dev_handle, mcl);
|
---|
540 | (void) fat_node_put(FS_NODE(nodep));
|
---|
541 | return rc;
|
---|
542 | }
|
---|
543 | /* idxp->lock held */
|
---|
544 | if (flags & L_DIRECTORY) {
|
---|
545 | nodep->type = FAT_DIRECTORY;
|
---|
546 | nodep->firstc = mcl;
|
---|
547 | nodep->size = bps * bs->spc;
|
---|
548 | } else {
|
---|
549 | nodep->type = FAT_FILE;
|
---|
550 | nodep->firstc = FAT_CLST_RES0;
|
---|
551 | nodep->size = 0;
|
---|
552 | }
|
---|
553 | nodep->lnkcnt = 0; /* not linked anywhere */
|
---|
554 | nodep->refcnt = 1;
|
---|
555 | nodep->dirty = true;
|
---|
556 |
|
---|
557 | nodep->idx = idxp;
|
---|
558 | idxp->nodep = nodep;
|
---|
559 |
|
---|
560 | fibril_mutex_unlock(&idxp->lock);
|
---|
561 | *rfn = FS_NODE(nodep);
|
---|
562 | return EOK;
|
---|
563 | }
|
---|
564 |
|
---|
565 | int fat_destroy_node(fs_node_t *fn)
|
---|
566 | {
|
---|
567 | fat_node_t *nodep = FAT_NODE(fn);
|
---|
568 | fat_bs_t *bs;
|
---|
569 | bool has_children;
|
---|
570 | int rc;
|
---|
571 |
|
---|
572 | /*
|
---|
573 | * The node is not reachable from the file system. This means that the
|
---|
574 | * link count should be zero and that the index structure cannot be
|
---|
575 | * found in the position hash. Obviously, we don't need to lock the node
|
---|
576 | * nor its index structure.
|
---|
577 | */
|
---|
578 | assert(nodep->lnkcnt == 0);
|
---|
579 |
|
---|
580 | /*
|
---|
581 | * The node may not have any children.
|
---|
582 | */
|
---|
583 | rc = fat_has_children(&has_children, fn);
|
---|
584 | if (rc != EOK)
|
---|
585 | return rc;
|
---|
586 | assert(!has_children);
|
---|
587 |
|
---|
588 | bs = block_bb_get(nodep->idx->dev_handle);
|
---|
589 | if (nodep->firstc != FAT_CLST_RES0) {
|
---|
590 | assert(nodep->size);
|
---|
591 | /* Free all clusters allocated to the node. */
|
---|
592 | rc = fat_free_clusters(bs, nodep->idx->dev_handle,
|
---|
593 | nodep->firstc);
|
---|
594 | }
|
---|
595 |
|
---|
596 | fat_idx_destroy(nodep->idx);
|
---|
597 | free(nodep->bp);
|
---|
598 | free(nodep);
|
---|
599 | return rc;
|
---|
600 | }
|
---|
601 |
|
---|
602 | int fat_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
|
---|
603 | {
|
---|
604 | fat_node_t *parentp = FAT_NODE(pfn);
|
---|
605 | fat_node_t *childp = FAT_NODE(cfn);
|
---|
606 | fat_dentry_t *d;
|
---|
607 | fat_bs_t *bs;
|
---|
608 | block_t *b;
|
---|
609 | unsigned i, j;
|
---|
610 | uint16_t bps;
|
---|
611 | unsigned dps;
|
---|
612 | unsigned blocks;
|
---|
613 | fat_cluster_t mcl, lcl;
|
---|
614 | int rc;
|
---|
615 |
|
---|
616 | fibril_mutex_lock(&childp->lock);
|
---|
617 | if (childp->lnkcnt == 1) {
|
---|
618 | /*
|
---|
619 | * On FAT, we don't support multiple hard links.
|
---|
620 | */
|
---|
621 | fibril_mutex_unlock(&childp->lock);
|
---|
622 | return EMLINK;
|
---|
623 | }
|
---|
624 | assert(childp->lnkcnt == 0);
|
---|
625 | fibril_mutex_unlock(&childp->lock);
|
---|
626 |
|
---|
627 | if (!fat_dentry_name_verify(name)) {
|
---|
628 | /*
|
---|
629 | * Attempt to create unsupported name.
|
---|
630 | */
|
---|
631 | return ENOTSUP;
|
---|
632 | }
|
---|
633 |
|
---|
634 | /*
|
---|
635 | * Get us an unused parent node's dentry or grow the parent and allocate
|
---|
636 | * a new one.
|
---|
637 | */
|
---|
638 |
|
---|
639 | fibril_mutex_lock(&parentp->idx->lock);
|
---|
640 | bs = block_bb_get(parentp->idx->dev_handle);
|
---|
641 | bps = uint16_t_le2host(bs->bps);
|
---|
642 | dps = bps / sizeof(fat_dentry_t);
|
---|
643 |
|
---|
644 | blocks = parentp->size / bps;
|
---|
645 |
|
---|
646 | for (i = 0; i < blocks; i++) {
|
---|
647 | rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE);
|
---|
648 | if (rc != EOK) {
|
---|
649 | fibril_mutex_unlock(&parentp->idx->lock);
|
---|
650 | return rc;
|
---|
651 | }
|
---|
652 | for (j = 0; j < dps; j++) {
|
---|
653 | d = ((fat_dentry_t *)b->data) + j;
|
---|
654 | switch (fat_classify_dentry(d)) {
|
---|
655 | case FAT_DENTRY_SKIP:
|
---|
656 | case FAT_DENTRY_VALID:
|
---|
657 | /* skipping used and meta entries */
|
---|
658 | continue;
|
---|
659 | case FAT_DENTRY_FREE:
|
---|
660 | case FAT_DENTRY_LAST:
|
---|
661 | /* found an empty slot */
|
---|
662 | goto hit;
|
---|
663 | }
|
---|
664 | }
|
---|
665 | rc = block_put(b);
|
---|
666 | if (rc != EOK) {
|
---|
667 | fibril_mutex_unlock(&parentp->idx->lock);
|
---|
668 | return rc;
|
---|
669 | }
|
---|
670 | }
|
---|
671 | j = 0;
|
---|
672 |
|
---|
673 | /*
|
---|
674 | * We need to grow the parent in order to create a new unused dentry.
|
---|
675 | */
|
---|
676 | if (parentp->firstc == FAT_CLST_ROOT) {
|
---|
677 | /* Can't grow the root directory. */
|
---|
678 | fibril_mutex_unlock(&parentp->idx->lock);
|
---|
679 | return ENOSPC;
|
---|
680 | }
|
---|
681 | rc = fat_alloc_clusters(bs, parentp->idx->dev_handle, 1, &mcl, &lcl);
|
---|
682 | if (rc != EOK) {
|
---|
683 | fibril_mutex_unlock(&parentp->idx->lock);
|
---|
684 | return rc;
|
---|
685 | }
|
---|
686 | rc = fat_zero_cluster(bs, parentp->idx->dev_handle, mcl);
|
---|
687 | if (rc != EOK) {
|
---|
688 | (void) fat_free_clusters(bs, parentp->idx->dev_handle, mcl);
|
---|
689 | fibril_mutex_unlock(&parentp->idx->lock);
|
---|
690 | return rc;
|
---|
691 | }
|
---|
692 | rc = fat_append_clusters(bs, parentp, mcl);
|
---|
693 | if (rc != EOK) {
|
---|
694 | (void) fat_free_clusters(bs, parentp->idx->dev_handle, mcl);
|
---|
695 | fibril_mutex_unlock(&parentp->idx->lock);
|
---|
696 | return rc;
|
---|
697 | }
|
---|
698 | parentp->size += bps * bs->spc;
|
---|
699 | parentp->dirty = true; /* need to sync node */
|
---|
700 | rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE);
|
---|
701 | if (rc != EOK) {
|
---|
702 | fibril_mutex_unlock(&parentp->idx->lock);
|
---|
703 | return rc;
|
---|
704 | }
|
---|
705 | d = (fat_dentry_t *)b->data;
|
---|
706 |
|
---|
707 | hit:
|
---|
708 | /*
|
---|
709 | * At this point we only establish the link between the parent and the
|
---|
710 | * child. The dentry, except of the name and the extension, will remain
|
---|
711 | * uninitialized until the corresponding node is synced. Thus the valid
|
---|
712 | * dentry data is kept in the child node structure.
|
---|
713 | */
|
---|
714 | memset(d, 0, sizeof(fat_dentry_t));
|
---|
715 | fat_dentry_name_set(d, name);
|
---|
716 | b->dirty = true; /* need to sync block */
|
---|
717 | rc = block_put(b);
|
---|
718 | fibril_mutex_unlock(&parentp->idx->lock);
|
---|
719 | if (rc != EOK)
|
---|
720 | return rc;
|
---|
721 |
|
---|
722 | fibril_mutex_lock(&childp->idx->lock);
|
---|
723 |
|
---|
724 | /*
|
---|
725 | * If possible, create the Sub-directory Identifier Entry and the
|
---|
726 | * Sub-directory Parent Pointer Entry (i.e. "." and ".."). These entries
|
---|
727 | * are not mandatory according to Standard ECMA-107 and HelenOS VFS does
|
---|
728 | * not use them anyway, so this is rather a sign of our good will.
|
---|
729 | */
|
---|
730 | rc = fat_block_get(&b, bs, childp, 0, BLOCK_FLAGS_NONE);
|
---|
731 | if (rc != EOK) {
|
---|
732 | /*
|
---|
733 | * Rather than returning an error, simply skip the creation of
|
---|
734 | * these two entries.
|
---|
735 | */
|
---|
736 | goto skip_dots;
|
---|
737 | }
|
---|
738 | d = (fat_dentry_t *)b->data;
|
---|
739 | if (fat_classify_dentry(d) == FAT_DENTRY_LAST ||
|
---|
740 | str_cmp(d->name, FAT_NAME_DOT) == 0) {
|
---|
741 | memset(d, 0, sizeof(fat_dentry_t));
|
---|
742 | str_cpy(d->name, 8, FAT_NAME_DOT);
|
---|
743 | str_cpy(d->ext, 3, FAT_EXT_PAD);
|
---|
744 | d->attr = FAT_ATTR_SUBDIR;
|
---|
745 | d->firstc = host2uint16_t_le(childp->firstc);
|
---|
746 | /* TODO: initialize also the date/time members. */
|
---|
747 | }
|
---|
748 | d++;
|
---|
749 | if (fat_classify_dentry(d) == FAT_DENTRY_LAST ||
|
---|
750 | str_cmp(d->name, FAT_NAME_DOT_DOT) == 0) {
|
---|
751 | memset(d, 0, sizeof(fat_dentry_t));
|
---|
752 | str_cpy(d->name, 8, FAT_NAME_DOT_DOT);
|
---|
753 | str_cpy(d->ext, 3, FAT_EXT_PAD);
|
---|
754 | d->attr = FAT_ATTR_SUBDIR;
|
---|
755 | d->firstc = (parentp->firstc == FAT_CLST_ROOT) ?
|
---|
756 | host2uint16_t_le(FAT_CLST_RES0) :
|
---|
757 | host2uint16_t_le(parentp->firstc);
|
---|
758 | /* TODO: initialize also the date/time members. */
|
---|
759 | }
|
---|
760 | b->dirty = true; /* need to sync block */
|
---|
761 | /*
|
---|
762 | * Ignore the return value as we would have fallen through on error
|
---|
763 | * anyway.
|
---|
764 | */
|
---|
765 | (void) block_put(b);
|
---|
766 | skip_dots:
|
---|
767 |
|
---|
768 | childp->idx->pfc = parentp->firstc;
|
---|
769 | childp->idx->pdi = i * dps + j;
|
---|
770 | fibril_mutex_unlock(&childp->idx->lock);
|
---|
771 |
|
---|
772 | fibril_mutex_lock(&childp->lock);
|
---|
773 | childp->lnkcnt = 1;
|
---|
774 | childp->dirty = true; /* need to sync node */
|
---|
775 | fibril_mutex_unlock(&childp->lock);
|
---|
776 |
|
---|
777 | /*
|
---|
778 | * Hash in the index structure into the position hash.
|
---|
779 | */
|
---|
780 | fat_idx_hashin(childp->idx);
|
---|
781 |
|
---|
782 | return EOK;
|
---|
783 | }
|
---|
784 |
|
---|
785 | int fat_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
---|
786 | {
|
---|
787 | fat_node_t *parentp = FAT_NODE(pfn);
|
---|
788 | fat_node_t *childp = FAT_NODE(cfn);
|
---|
789 | fat_bs_t *bs;
|
---|
790 | fat_dentry_t *d;
|
---|
791 | uint16_t bps;
|
---|
792 | block_t *b;
|
---|
793 | bool has_children;
|
---|
794 | int rc;
|
---|
795 |
|
---|
796 | if (!parentp)
|
---|
797 | return EBUSY;
|
---|
798 |
|
---|
799 | rc = fat_has_children(&has_children, cfn);
|
---|
800 | if (rc != EOK)
|
---|
801 | return rc;
|
---|
802 | if (has_children)
|
---|
803 | return ENOTEMPTY;
|
---|
804 |
|
---|
805 | fibril_mutex_lock(&parentp->lock);
|
---|
806 | fibril_mutex_lock(&childp->lock);
|
---|
807 | assert(childp->lnkcnt == 1);
|
---|
808 | fibril_mutex_lock(&childp->idx->lock);
|
---|
809 | bs = block_bb_get(childp->idx->dev_handle);
|
---|
810 | bps = uint16_t_le2host(bs->bps);
|
---|
811 |
|
---|
812 | rc = _fat_block_get(&b, bs, childp->idx->dev_handle, childp->idx->pfc,
|
---|
813 | (childp->idx->pdi * sizeof(fat_dentry_t)) / bps,
|
---|
814 | BLOCK_FLAGS_NONE);
|
---|
815 | if (rc != EOK)
|
---|
816 | goto error;
|
---|
817 | d = (fat_dentry_t *)b->data +
|
---|
818 | (childp->idx->pdi % (bps / sizeof(fat_dentry_t)));
|
---|
819 | /* mark the dentry as not-currently-used */
|
---|
820 | d->name[0] = FAT_DENTRY_ERASED;
|
---|
821 | b->dirty = true; /* need to sync block */
|
---|
822 | rc = block_put(b);
|
---|
823 | if (rc != EOK)
|
---|
824 | goto error;
|
---|
825 |
|
---|
826 | /* remove the index structure from the position hash */
|
---|
827 | fat_idx_hashout(childp->idx);
|
---|
828 | /* clear position information */
|
---|
829 | childp->idx->pfc = FAT_CLST_RES0;
|
---|
830 | childp->idx->pdi = 0;
|
---|
831 | fibril_mutex_unlock(&childp->idx->lock);
|
---|
832 | childp->lnkcnt = 0;
|
---|
833 | childp->dirty = true;
|
---|
834 | fibril_mutex_unlock(&childp->lock);
|
---|
835 | fibril_mutex_unlock(&parentp->lock);
|
---|
836 |
|
---|
837 | return EOK;
|
---|
838 |
|
---|
839 | error:
|
---|
840 | fibril_mutex_unlock(&parentp->idx->lock);
|
---|
841 | fibril_mutex_unlock(&childp->lock);
|
---|
842 | fibril_mutex_unlock(&childp->idx->lock);
|
---|
843 | return rc;
|
---|
844 | }
|
---|
845 |
|
---|
846 | int fat_has_children(bool *has_children, fs_node_t *fn)
|
---|
847 | {
|
---|
848 | fat_bs_t *bs;
|
---|
849 | fat_node_t *nodep = FAT_NODE(fn);
|
---|
850 | unsigned bps;
|
---|
851 | unsigned dps;
|
---|
852 | unsigned blocks;
|
---|
853 | block_t *b;
|
---|
854 | unsigned i, j;
|
---|
855 | int rc;
|
---|
856 |
|
---|
857 | if (nodep->type != FAT_DIRECTORY) {
|
---|
858 | *has_children = false;
|
---|
859 | return EOK;
|
---|
860 | }
|
---|
861 |
|
---|
862 | fibril_mutex_lock(&nodep->idx->lock);
|
---|
863 | bs = block_bb_get(nodep->idx->dev_handle);
|
---|
864 | bps = uint16_t_le2host(bs->bps);
|
---|
865 | dps = bps / sizeof(fat_dentry_t);
|
---|
866 |
|
---|
867 | blocks = nodep->size / bps;
|
---|
868 |
|
---|
869 | for (i = 0; i < blocks; i++) {
|
---|
870 | fat_dentry_t *d;
|
---|
871 |
|
---|
872 | rc = fat_block_get(&b, bs, nodep, i, BLOCK_FLAGS_NONE);
|
---|
873 | if (rc != EOK) {
|
---|
874 | fibril_mutex_unlock(&nodep->idx->lock);
|
---|
875 | return rc;
|
---|
876 | }
|
---|
877 | for (j = 0; j < dps; j++) {
|
---|
878 | d = ((fat_dentry_t *)b->data) + j;
|
---|
879 | switch (fat_classify_dentry(d)) {
|
---|
880 | case FAT_DENTRY_SKIP:
|
---|
881 | case FAT_DENTRY_FREE:
|
---|
882 | continue;
|
---|
883 | case FAT_DENTRY_LAST:
|
---|
884 | rc = block_put(b);
|
---|
885 | fibril_mutex_unlock(&nodep->idx->lock);
|
---|
886 | *has_children = false;
|
---|
887 | return rc;
|
---|
888 | default:
|
---|
889 | case FAT_DENTRY_VALID:
|
---|
890 | rc = block_put(b);
|
---|
891 | fibril_mutex_unlock(&nodep->idx->lock);
|
---|
892 | *has_children = true;
|
---|
893 | return rc;
|
---|
894 | }
|
---|
895 | }
|
---|
896 | rc = block_put(b);
|
---|
897 | if (rc != EOK) {
|
---|
898 | fibril_mutex_unlock(&nodep->idx->lock);
|
---|
899 | return rc;
|
---|
900 | }
|
---|
901 | }
|
---|
902 |
|
---|
903 | fibril_mutex_unlock(&nodep->idx->lock);
|
---|
904 | *has_children = false;
|
---|
905 | return EOK;
|
---|
906 | }
|
---|
907 |
|
---|
908 |
|
---|
909 | fs_index_t fat_index_get(fs_node_t *fn)
|
---|
910 | {
|
---|
911 | return FAT_NODE(fn)->idx->index;
|
---|
912 | }
|
---|
913 |
|
---|
914 | size_t fat_size_get(fs_node_t *fn)
|
---|
915 | {
|
---|
916 | return FAT_NODE(fn)->size;
|
---|
917 | }
|
---|
918 |
|
---|
919 | unsigned fat_lnkcnt_get(fs_node_t *fn)
|
---|
920 | {
|
---|
921 | return FAT_NODE(fn)->lnkcnt;
|
---|
922 | }
|
---|
923 |
|
---|
924 | char fat_plb_get_char(unsigned pos)
|
---|
925 | {
|
---|
926 | return fat_reg.plb_ro[pos % PLB_SIZE];
|
---|
927 | }
|
---|
928 |
|
---|
929 | bool fat_is_directory(fs_node_t *fn)
|
---|
930 | {
|
---|
931 | return FAT_NODE(fn)->type == FAT_DIRECTORY;
|
---|
932 | }
|
---|
933 |
|
---|
934 | bool fat_is_file(fs_node_t *fn)
|
---|
935 | {
|
---|
936 | return FAT_NODE(fn)->type == FAT_FILE;
|
---|
937 | }
|
---|
938 |
|
---|
939 | dev_handle_t fat_device_get(fs_node_t *node)
|
---|
940 | {
|
---|
941 | return 0;
|
---|
942 | }
|
---|
943 |
|
---|
944 | /** libfs operations */
|
---|
945 | libfs_ops_t fat_libfs_ops = {
|
---|
946 | .root_get = fat_root_get,
|
---|
947 | .match = fat_match,
|
---|
948 | .node_get = fat_node_get,
|
---|
949 | .node_open = fat_node_open,
|
---|
950 | .node_put = fat_node_put,
|
---|
951 | .create = fat_create_node,
|
---|
952 | .destroy = fat_destroy_node,
|
---|
953 | .link = fat_link,
|
---|
954 | .unlink = fat_unlink,
|
---|
955 | .has_children = fat_has_children,
|
---|
956 | .index_get = fat_index_get,
|
---|
957 | .size_get = fat_size_get,
|
---|
958 | .lnkcnt_get = fat_lnkcnt_get,
|
---|
959 | .plb_get_char = fat_plb_get_char,
|
---|
960 | .is_directory = fat_is_directory,
|
---|
961 | .is_file = fat_is_file,
|
---|
962 | .device_get = fat_device_get
|
---|
963 | };
|
---|
964 |
|
---|
965 | /*
|
---|
966 | * VFS operations.
|
---|
967 | */
|
---|
968 |
|
---|
969 | void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
|
---|
970 | {
|
---|
971 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
|
---|
972 | enum cache_mode cmode;
|
---|
973 | fat_bs_t *bs;
|
---|
974 | uint16_t bps;
|
---|
975 | uint16_t rde;
|
---|
976 |
|
---|
977 | /* Accept the mount options */
|
---|
978 | char *opts;
|
---|
979 | int rc = async_data_write_accept((char **) &opts, true, 0, 0, 0, NULL);
|
---|
980 |
|
---|
981 | if (rc != EOK) {
|
---|
982 | ipc_answer_0(rid, rc);
|
---|
983 | return;
|
---|
984 | }
|
---|
985 |
|
---|
986 | /* Check for option enabling write through. */
|
---|
987 | if (str_cmp(opts, "wtcache") == 0)
|
---|
988 | cmode = CACHE_MODE_WT;
|
---|
989 | else
|
---|
990 | cmode = CACHE_MODE_WB;
|
---|
991 |
|
---|
992 | free(opts);
|
---|
993 |
|
---|
994 | /* initialize libblock */
|
---|
995 | rc = block_init(dev_handle, BS_SIZE);
|
---|
996 | if (rc != EOK) {
|
---|
997 | ipc_answer_0(rid, rc);
|
---|
998 | return;
|
---|
999 | }
|
---|
1000 |
|
---|
1001 | /* prepare the boot block */
|
---|
1002 | rc = block_bb_read(dev_handle, BS_BLOCK);
|
---|
1003 | if (rc != EOK) {
|
---|
1004 | block_fini(dev_handle);
|
---|
1005 | ipc_answer_0(rid, rc);
|
---|
1006 | return;
|
---|
1007 | }
|
---|
1008 |
|
---|
1009 | /* get the buffer with the boot sector */
|
---|
1010 | bs = block_bb_get(dev_handle);
|
---|
1011 |
|
---|
1012 | /* Read the number of root directory entries. */
|
---|
1013 | bps = uint16_t_le2host(bs->bps);
|
---|
1014 | rde = uint16_t_le2host(bs->root_ent_max);
|
---|
1015 |
|
---|
1016 | if (bps != BS_SIZE) {
|
---|
1017 | block_fini(dev_handle);
|
---|
1018 | ipc_answer_0(rid, ENOTSUP);
|
---|
1019 | return;
|
---|
1020 | }
|
---|
1021 |
|
---|
1022 | /* Initialize the block cache */
|
---|
1023 | rc = block_cache_init(dev_handle, bps, 0 /* XXX */, cmode);
|
---|
1024 | if (rc != EOK) {
|
---|
1025 | block_fini(dev_handle);
|
---|
1026 | ipc_answer_0(rid, rc);
|
---|
1027 | return;
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 | /* Do some simple sanity checks on the file system. */
|
---|
1031 | rc = fat_sanity_check(bs, dev_handle);
|
---|
1032 | if (rc != EOK) {
|
---|
1033 | (void) block_cache_fini(dev_handle);
|
---|
1034 | block_fini(dev_handle);
|
---|
1035 | ipc_answer_0(rid, rc);
|
---|
1036 | return;
|
---|
1037 | }
|
---|
1038 |
|
---|
1039 | rc = fat_idx_init_by_dev_handle(dev_handle);
|
---|
1040 | if (rc != EOK) {
|
---|
1041 | (void) block_cache_fini(dev_handle);
|
---|
1042 | block_fini(dev_handle);
|
---|
1043 | ipc_answer_0(rid, rc);
|
---|
1044 | return;
|
---|
1045 | }
|
---|
1046 |
|
---|
1047 | /* Initialize the root node. */
|
---|
1048 | fs_node_t *rfn = (fs_node_t *)malloc(sizeof(fs_node_t));
|
---|
1049 | if (!rfn) {
|
---|
1050 | (void) block_cache_fini(dev_handle);
|
---|
1051 | block_fini(dev_handle);
|
---|
1052 | fat_idx_fini_by_dev_handle(dev_handle);
|
---|
1053 | ipc_answer_0(rid, ENOMEM);
|
---|
1054 | return;
|
---|
1055 | }
|
---|
1056 | fs_node_initialize(rfn);
|
---|
1057 | fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
|
---|
1058 | if (!rootp) {
|
---|
1059 | free(rfn);
|
---|
1060 | (void) block_cache_fini(dev_handle);
|
---|
1061 | block_fini(dev_handle);
|
---|
1062 | fat_idx_fini_by_dev_handle(dev_handle);
|
---|
1063 | ipc_answer_0(rid, ENOMEM);
|
---|
1064 | return;
|
---|
1065 | }
|
---|
1066 | fat_node_initialize(rootp);
|
---|
1067 |
|
---|
1068 | fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
|
---|
1069 | if (!ridxp) {
|
---|
1070 | free(rfn);
|
---|
1071 | free(rootp);
|
---|
1072 | (void) block_cache_fini(dev_handle);
|
---|
1073 | block_fini(dev_handle);
|
---|
1074 | fat_idx_fini_by_dev_handle(dev_handle);
|
---|
1075 | ipc_answer_0(rid, ENOMEM);
|
---|
1076 | return;
|
---|
1077 | }
|
---|
1078 | assert(ridxp->index == 0);
|
---|
1079 | /* ridxp->lock held */
|
---|
1080 |
|
---|
1081 | rootp->type = FAT_DIRECTORY;
|
---|
1082 | rootp->firstc = FAT_CLST_ROOT;
|
---|
1083 | rootp->refcnt = 1;
|
---|
1084 | rootp->lnkcnt = 0; /* FS root is not linked */
|
---|
1085 | rootp->size = rde * sizeof(fat_dentry_t);
|
---|
1086 | rootp->idx = ridxp;
|
---|
1087 | ridxp->nodep = rootp;
|
---|
1088 | rootp->bp = rfn;
|
---|
1089 | rfn->data = rootp;
|
---|
1090 |
|
---|
1091 | fibril_mutex_unlock(&ridxp->lock);
|
---|
1092 |
|
---|
1093 | ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
|
---|
1094 | }
|
---|
1095 |
|
---|
1096 | void fat_mount(ipc_callid_t rid, ipc_call_t *request)
|
---|
1097 | {
|
---|
1098 | libfs_mount(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | void fat_unmounted(ipc_callid_t rid, ipc_call_t *request)
|
---|
1102 | {
|
---|
1103 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
|
---|
1104 | fs_node_t *fn;
|
---|
1105 | fat_node_t *nodep;
|
---|
1106 | int rc;
|
---|
1107 |
|
---|
1108 | rc = fat_root_get(&fn, dev_handle);
|
---|
1109 | if (rc != EOK) {
|
---|
1110 | ipc_answer_0(rid, rc);
|
---|
1111 | return;
|
---|
1112 | }
|
---|
1113 | nodep = FAT_NODE(fn);
|
---|
1114 |
|
---|
1115 | /*
|
---|
1116 | * We expect exactly two references on the root node. One for the
|
---|
1117 | * fat_root_get() above and one created in fat_mounted().
|
---|
1118 | */
|
---|
1119 | if (nodep->refcnt != 2) {
|
---|
1120 | (void) fat_node_put(fn);
|
---|
1121 | ipc_answer_0(rid, EBUSY);
|
---|
1122 | return;
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 | /*
|
---|
1126 | * Put the root node and force it to the FAT free node list.
|
---|
1127 | */
|
---|
1128 | (void) fat_node_put(fn);
|
---|
1129 | (void) fat_node_put(fn);
|
---|
1130 |
|
---|
1131 | /*
|
---|
1132 | * Perform cleanup of the node structures, index structures and
|
---|
1133 | * associated data. Write back this file system's dirty blocks and
|
---|
1134 | * stop using libblock for this instance.
|
---|
1135 | */
|
---|
1136 | (void) fat_node_fini_by_dev_handle(dev_handle);
|
---|
1137 | fat_idx_fini_by_dev_handle(dev_handle);
|
---|
1138 | (void) block_cache_fini(dev_handle);
|
---|
1139 | block_fini(dev_handle);
|
---|
1140 |
|
---|
1141 | ipc_answer_0(rid, EOK);
|
---|
1142 | }
|
---|
1143 |
|
---|
1144 | void fat_unmount(ipc_callid_t rid, ipc_call_t *request)
|
---|
1145 | {
|
---|
1146 | libfs_unmount(&fat_libfs_ops, rid, request);
|
---|
1147 | }
|
---|
1148 |
|
---|
1149 | void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
|
---|
1150 | {
|
---|
1151 | libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
|
---|
1152 | }
|
---|
1153 |
|
---|
1154 | void fat_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
1155 | {
|
---|
1156 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
---|
1157 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
1158 | off_t pos = (off_t)IPC_GET_ARG3(*request);
|
---|
1159 | fs_node_t *fn;
|
---|
1160 | fat_node_t *nodep;
|
---|
1161 | fat_bs_t *bs;
|
---|
1162 | uint16_t bps;
|
---|
1163 | size_t bytes;
|
---|
1164 | block_t *b;
|
---|
1165 | int rc;
|
---|
1166 |
|
---|
1167 | rc = fat_node_get(&fn, dev_handle, index);
|
---|
1168 | if (rc != EOK) {
|
---|
1169 | ipc_answer_0(rid, rc);
|
---|
1170 | return;
|
---|
1171 | }
|
---|
1172 | if (!fn) {
|
---|
1173 | ipc_answer_0(rid, ENOENT);
|
---|
1174 | return;
|
---|
1175 | }
|
---|
1176 | nodep = FAT_NODE(fn);
|
---|
1177 |
|
---|
1178 | ipc_callid_t callid;
|
---|
1179 | size_t len;
|
---|
1180 | if (!async_data_read_receive(&callid, &len)) {
|
---|
1181 | fat_node_put(fn);
|
---|
1182 | ipc_answer_0(callid, EINVAL);
|
---|
1183 | ipc_answer_0(rid, EINVAL);
|
---|
1184 | return;
|
---|
1185 | }
|
---|
1186 |
|
---|
1187 | bs = block_bb_get(dev_handle);
|
---|
1188 | bps = uint16_t_le2host(bs->bps);
|
---|
1189 |
|
---|
1190 | if (nodep->type == FAT_FILE) {
|
---|
1191 | /*
|
---|
1192 | * Our strategy for regular file reads is to read one block at
|
---|
1193 | * most and make use of the possibility to return less data than
|
---|
1194 | * requested. This keeps the code very simple.
|
---|
1195 | */
|
---|
1196 | if (pos >= nodep->size) {
|
---|
1197 | /* reading beyond the EOF */
|
---|
1198 | bytes = 0;
|
---|
1199 | (void) async_data_read_finalize(callid, NULL, 0);
|
---|
1200 | } else {
|
---|
1201 | bytes = min(len, bps - pos % bps);
|
---|
1202 | bytes = min(bytes, nodep->size - pos);
|
---|
1203 | rc = fat_block_get(&b, bs, nodep, pos / bps,
|
---|
1204 | BLOCK_FLAGS_NONE);
|
---|
1205 | if (rc != EOK) {
|
---|
1206 | fat_node_put(fn);
|
---|
1207 | ipc_answer_0(callid, rc);
|
---|
1208 | ipc_answer_0(rid, rc);
|
---|
1209 | return;
|
---|
1210 | }
|
---|
1211 | (void) async_data_read_finalize(callid, b->data + pos % bps,
|
---|
1212 | bytes);
|
---|
1213 | rc = block_put(b);
|
---|
1214 | if (rc != EOK) {
|
---|
1215 | fat_node_put(fn);
|
---|
1216 | ipc_answer_0(rid, rc);
|
---|
1217 | return;
|
---|
1218 | }
|
---|
1219 | }
|
---|
1220 | } else {
|
---|
1221 | unsigned bnum;
|
---|
1222 | off_t spos = pos;
|
---|
1223 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
|
---|
1224 | fat_dentry_t *d;
|
---|
1225 |
|
---|
1226 | assert(nodep->type == FAT_DIRECTORY);
|
---|
1227 | assert(nodep->size % bps == 0);
|
---|
1228 | assert(bps % sizeof(fat_dentry_t) == 0);
|
---|
1229 |
|
---|
1230 | /*
|
---|
1231 | * Our strategy for readdir() is to use the position pointer as
|
---|
1232 | * an index into the array of all dentries. On entry, it points
|
---|
1233 | * to the first unread dentry. If we skip any dentries, we bump
|
---|
1234 | * the position pointer accordingly.
|
---|
1235 | */
|
---|
1236 | bnum = (pos * sizeof(fat_dentry_t)) / bps;
|
---|
1237 | while (bnum < nodep->size / bps) {
|
---|
1238 | off_t o;
|
---|
1239 |
|
---|
1240 | rc = fat_block_get(&b, bs, nodep, bnum,
|
---|
1241 | BLOCK_FLAGS_NONE);
|
---|
1242 | if (rc != EOK)
|
---|
1243 | goto err;
|
---|
1244 | for (o = pos % (bps / sizeof(fat_dentry_t));
|
---|
1245 | o < bps / sizeof(fat_dentry_t);
|
---|
1246 | o++, pos++) {
|
---|
1247 | d = ((fat_dentry_t *)b->data) + o;
|
---|
1248 | switch (fat_classify_dentry(d)) {
|
---|
1249 | case FAT_DENTRY_SKIP:
|
---|
1250 | case FAT_DENTRY_FREE:
|
---|
1251 | continue;
|
---|
1252 | case FAT_DENTRY_LAST:
|
---|
1253 | rc = block_put(b);
|
---|
1254 | if (rc != EOK)
|
---|
1255 | goto err;
|
---|
1256 | goto miss;
|
---|
1257 | default:
|
---|
1258 | case FAT_DENTRY_VALID:
|
---|
1259 | fat_dentry_name_get(d, name);
|
---|
1260 | rc = block_put(b);
|
---|
1261 | if (rc != EOK)
|
---|
1262 | goto err;
|
---|
1263 | goto hit;
|
---|
1264 | }
|
---|
1265 | }
|
---|
1266 | rc = block_put(b);
|
---|
1267 | if (rc != EOK)
|
---|
1268 | goto err;
|
---|
1269 | bnum++;
|
---|
1270 | }
|
---|
1271 | miss:
|
---|
1272 | rc = fat_node_put(fn);
|
---|
1273 | ipc_answer_0(callid, rc != EOK ? rc : ENOENT);
|
---|
1274 | ipc_answer_1(rid, rc != EOK ? rc : ENOENT, 0);
|
---|
1275 | return;
|
---|
1276 |
|
---|
1277 | err:
|
---|
1278 | (void) fat_node_put(fn);
|
---|
1279 | ipc_answer_0(callid, rc);
|
---|
1280 | ipc_answer_0(rid, rc);
|
---|
1281 | return;
|
---|
1282 |
|
---|
1283 | hit:
|
---|
1284 | (void) async_data_read_finalize(callid, name, str_size(name) + 1);
|
---|
1285 | bytes = (pos - spos) + 1;
|
---|
1286 | }
|
---|
1287 |
|
---|
1288 | rc = fat_node_put(fn);
|
---|
1289 | ipc_answer_1(rid, rc, (ipcarg_t)bytes);
|
---|
1290 | }
|
---|
1291 |
|
---|
1292 | void fat_write(ipc_callid_t rid, ipc_call_t *request)
|
---|
1293 | {
|
---|
1294 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
---|
1295 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
1296 | off_t pos = (off_t)IPC_GET_ARG3(*request);
|
---|
1297 | fs_node_t *fn;
|
---|
1298 | fat_node_t *nodep;
|
---|
1299 | fat_bs_t *bs;
|
---|
1300 | size_t bytes, size;
|
---|
1301 | block_t *b;
|
---|
1302 | uint16_t bps;
|
---|
1303 | unsigned spc;
|
---|
1304 | unsigned bpc; /* bytes per cluster */
|
---|
1305 | off_t boundary;
|
---|
1306 | int flags = BLOCK_FLAGS_NONE;
|
---|
1307 | int rc;
|
---|
1308 |
|
---|
1309 | rc = fat_node_get(&fn, dev_handle, index);
|
---|
1310 | if (rc != EOK) {
|
---|
1311 | ipc_answer_0(rid, rc);
|
---|
1312 | return;
|
---|
1313 | }
|
---|
1314 | if (!fn) {
|
---|
1315 | ipc_answer_0(rid, ENOENT);
|
---|
1316 | return;
|
---|
1317 | }
|
---|
1318 | nodep = FAT_NODE(fn);
|
---|
1319 |
|
---|
1320 | ipc_callid_t callid;
|
---|
1321 | size_t len;
|
---|
1322 | if (!async_data_write_receive(&callid, &len)) {
|
---|
1323 | (void) fat_node_put(fn);
|
---|
1324 | ipc_answer_0(callid, EINVAL);
|
---|
1325 | ipc_answer_0(rid, EINVAL);
|
---|
1326 | return;
|
---|
1327 | }
|
---|
1328 |
|
---|
1329 | bs = block_bb_get(dev_handle);
|
---|
1330 | bps = uint16_t_le2host(bs->bps);
|
---|
1331 | spc = bs->spc;
|
---|
1332 | bpc = bps * spc;
|
---|
1333 |
|
---|
1334 | /*
|
---|
1335 | * In all scenarios, we will attempt to write out only one block worth
|
---|
1336 | * of data at maximum. There might be some more efficient approaches,
|
---|
1337 | * but this one greatly simplifies fat_write(). Note that we can afford
|
---|
1338 | * to do this because the client must be ready to handle the return
|
---|
1339 | * value signalizing a smaller number of bytes written.
|
---|
1340 | */
|
---|
1341 | bytes = min(len, bps - pos % bps);
|
---|
1342 | if (bytes == bps)
|
---|
1343 | flags |= BLOCK_FLAGS_NOREAD;
|
---|
1344 |
|
---|
1345 | boundary = ROUND_UP(nodep->size, bpc);
|
---|
1346 | if (pos < boundary) {
|
---|
1347 | /*
|
---|
1348 | * This is the easier case - we are either overwriting already
|
---|
1349 | * existing contents or writing behind the EOF, but still within
|
---|
1350 | * the limits of the last cluster. The node size may grow to the
|
---|
1351 | * next block size boundary.
|
---|
1352 | */
|
---|
1353 | rc = fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos);
|
---|
1354 | if (rc != EOK) {
|
---|
1355 | (void) fat_node_put(fn);
|
---|
1356 | ipc_answer_0(callid, rc);
|
---|
1357 | ipc_answer_0(rid, rc);
|
---|
1358 | return;
|
---|
1359 | }
|
---|
1360 | rc = fat_block_get(&b, bs, nodep, pos / bps, flags);
|
---|
1361 | if (rc != EOK) {
|
---|
1362 | (void) fat_node_put(fn);
|
---|
1363 | ipc_answer_0(callid, rc);
|
---|
1364 | ipc_answer_0(rid, rc);
|
---|
1365 | return;
|
---|
1366 | }
|
---|
1367 | (void) async_data_write_finalize(callid, b->data + pos % bps,
|
---|
1368 | bytes);
|
---|
1369 | b->dirty = true; /* need to sync block */
|
---|
1370 | rc = block_put(b);
|
---|
1371 | if (rc != EOK) {
|
---|
1372 | (void) fat_node_put(fn);
|
---|
1373 | ipc_answer_0(rid, rc);
|
---|
1374 | return;
|
---|
1375 | }
|
---|
1376 | if (pos + bytes > nodep->size) {
|
---|
1377 | nodep->size = pos + bytes;
|
---|
1378 | nodep->dirty = true; /* need to sync node */
|
---|
1379 | }
|
---|
1380 | size = nodep->size;
|
---|
1381 | rc = fat_node_put(fn);
|
---|
1382 | ipc_answer_2(rid, rc, bytes, nodep->size);
|
---|
1383 | return;
|
---|
1384 | } else {
|
---|
1385 | /*
|
---|
1386 | * This is the more difficult case. We must allocate new
|
---|
1387 | * clusters for the node and zero them out.
|
---|
1388 | */
|
---|
1389 | unsigned nclsts;
|
---|
1390 | fat_cluster_t mcl, lcl;
|
---|
1391 |
|
---|
1392 | nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc;
|
---|
1393 | /* create an independent chain of nclsts clusters in all FATs */
|
---|
1394 | rc = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl);
|
---|
1395 | if (rc != EOK) {
|
---|
1396 | /* could not allocate a chain of nclsts clusters */
|
---|
1397 | (void) fat_node_put(fn);
|
---|
1398 | ipc_answer_0(callid, rc);
|
---|
1399 | ipc_answer_0(rid, rc);
|
---|
1400 | return;
|
---|
1401 | }
|
---|
1402 | /* zero fill any gaps */
|
---|
1403 | rc = fat_fill_gap(bs, nodep, mcl, pos);
|
---|
1404 | if (rc != EOK) {
|
---|
1405 | (void) fat_free_clusters(bs, dev_handle, mcl);
|
---|
1406 | (void) fat_node_put(fn);
|
---|
1407 | ipc_answer_0(callid, rc);
|
---|
1408 | ipc_answer_0(rid, rc);
|
---|
1409 | return;
|
---|
1410 | }
|
---|
1411 | rc = _fat_block_get(&b, bs, dev_handle, lcl, (pos / bps) % spc,
|
---|
1412 | flags);
|
---|
1413 | if (rc != EOK) {
|
---|
1414 | (void) fat_free_clusters(bs, dev_handle, mcl);
|
---|
1415 | (void) fat_node_put(fn);
|
---|
1416 | ipc_answer_0(callid, rc);
|
---|
1417 | ipc_answer_0(rid, rc);
|
---|
1418 | return;
|
---|
1419 | }
|
---|
1420 | (void) async_data_write_finalize(callid, b->data + pos % bps,
|
---|
1421 | bytes);
|
---|
1422 | b->dirty = true; /* need to sync block */
|
---|
1423 | rc = block_put(b);
|
---|
1424 | if (rc != EOK) {
|
---|
1425 | (void) fat_free_clusters(bs, dev_handle, mcl);
|
---|
1426 | (void) fat_node_put(fn);
|
---|
1427 | ipc_answer_0(rid, rc);
|
---|
1428 | return;
|
---|
1429 | }
|
---|
1430 | /*
|
---|
1431 | * Append the cluster chain starting in mcl to the end of the
|
---|
1432 | * node's cluster chain.
|
---|
1433 | */
|
---|
1434 | rc = fat_append_clusters(bs, nodep, mcl);
|
---|
1435 | if (rc != EOK) {
|
---|
1436 | (void) fat_free_clusters(bs, dev_handle, mcl);
|
---|
1437 | (void) fat_node_put(fn);
|
---|
1438 | ipc_answer_0(rid, rc);
|
---|
1439 | return;
|
---|
1440 | }
|
---|
1441 | nodep->size = size = pos + bytes;
|
---|
1442 | nodep->dirty = true; /* need to sync node */
|
---|
1443 | rc = fat_node_put(fn);
|
---|
1444 | ipc_answer_2(rid, rc, bytes, size);
|
---|
1445 | return;
|
---|
1446 | }
|
---|
1447 | }
|
---|
1448 |
|
---|
1449 | void fat_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
1450 | {
|
---|
1451 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
---|
1452 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
1453 | size_t size = (off_t)IPC_GET_ARG3(*request);
|
---|
1454 | fs_node_t *fn;
|
---|
1455 | fat_node_t *nodep;
|
---|
1456 | fat_bs_t *bs;
|
---|
1457 | uint16_t bps;
|
---|
1458 | uint8_t spc;
|
---|
1459 | unsigned bpc; /* bytes per cluster */
|
---|
1460 | int rc;
|
---|
1461 |
|
---|
1462 | rc = fat_node_get(&fn, dev_handle, index);
|
---|
1463 | if (rc != EOK) {
|
---|
1464 | ipc_answer_0(rid, rc);
|
---|
1465 | return;
|
---|
1466 | }
|
---|
1467 | if (!fn) {
|
---|
1468 | ipc_answer_0(rid, ENOENT);
|
---|
1469 | return;
|
---|
1470 | }
|
---|
1471 | nodep = FAT_NODE(fn);
|
---|
1472 |
|
---|
1473 | bs = block_bb_get(dev_handle);
|
---|
1474 | bps = uint16_t_le2host(bs->bps);
|
---|
1475 | spc = bs->spc;
|
---|
1476 | bpc = bps * spc;
|
---|
1477 |
|
---|
1478 | if (nodep->size == size) {
|
---|
1479 | rc = EOK;
|
---|
1480 | } else if (nodep->size < size) {
|
---|
1481 | /*
|
---|
1482 | * The standard says we have the freedom to grow the node.
|
---|
1483 | * For now, we simply return an error.
|
---|
1484 | */
|
---|
1485 | rc = EINVAL;
|
---|
1486 | } else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) {
|
---|
1487 | /*
|
---|
1488 | * The node will be shrunk, but no clusters will be deallocated.
|
---|
1489 | */
|
---|
1490 | nodep->size = size;
|
---|
1491 | nodep->dirty = true; /* need to sync node */
|
---|
1492 | rc = EOK;
|
---|
1493 | } else {
|
---|
1494 | /*
|
---|
1495 | * The node will be shrunk, clusters will be deallocated.
|
---|
1496 | */
|
---|
1497 | if (size == 0) {
|
---|
1498 | rc = fat_chop_clusters(bs, nodep, FAT_CLST_RES0);
|
---|
1499 | if (rc != EOK)
|
---|
1500 | goto out;
|
---|
1501 | } else {
|
---|
1502 | fat_cluster_t lastc;
|
---|
1503 | rc = fat_cluster_walk(bs, dev_handle, nodep->firstc,
|
---|
1504 | &lastc, NULL, (size - 1) / bpc);
|
---|
1505 | if (rc != EOK)
|
---|
1506 | goto out;
|
---|
1507 | rc = fat_chop_clusters(bs, nodep, lastc);
|
---|
1508 | if (rc != EOK)
|
---|
1509 | goto out;
|
---|
1510 | }
|
---|
1511 | nodep->size = size;
|
---|
1512 | nodep->dirty = true; /* need to sync node */
|
---|
1513 | rc = EOK;
|
---|
1514 | }
|
---|
1515 | out:
|
---|
1516 | fat_node_put(fn);
|
---|
1517 | ipc_answer_0(rid, rc);
|
---|
1518 | return;
|
---|
1519 | }
|
---|
1520 |
|
---|
1521 | void fat_close(ipc_callid_t rid, ipc_call_t *request)
|
---|
1522 | {
|
---|
1523 | ipc_answer_0(rid, EOK);
|
---|
1524 | }
|
---|
1525 |
|
---|
1526 | void fat_destroy(ipc_callid_t rid, ipc_call_t *request)
|
---|
1527 | {
|
---|
1528 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
---|
1529 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
1530 | fs_node_t *fn;
|
---|
1531 | int rc;
|
---|
1532 |
|
---|
1533 | rc = fat_node_get(&fn, dev_handle, index);
|
---|
1534 | if (rc != EOK) {
|
---|
1535 | ipc_answer_0(rid, rc);
|
---|
1536 | return;
|
---|
1537 | }
|
---|
1538 | if (!fn) {
|
---|
1539 | ipc_answer_0(rid, ENOENT);
|
---|
1540 | return;
|
---|
1541 | }
|
---|
1542 |
|
---|
1543 | rc = fat_destroy_node(fn);
|
---|
1544 | ipc_answer_0(rid, rc);
|
---|
1545 | }
|
---|
1546 |
|
---|
1547 | void fat_open_node(ipc_callid_t rid, ipc_call_t *request)
|
---|
1548 | {
|
---|
1549 | libfs_open_node(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
|
---|
1550 | }
|
---|
1551 |
|
---|
1552 | void fat_stat(ipc_callid_t rid, ipc_call_t *request)
|
---|
1553 | {
|
---|
1554 | libfs_stat(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
|
---|
1555 | }
|
---|
1556 |
|
---|
1557 | void fat_sync(ipc_callid_t rid, ipc_call_t *request)
|
---|
1558 | {
|
---|
1559 | /* Dummy implementation */
|
---|
1560 | ipc_answer_0(rid, EOK);
|
---|
1561 | }
|
---|
1562 |
|
---|
1563 | /**
|
---|
1564 | * @}
|
---|
1565 | */
|
---|