1 | /*
|
---|
2 | * Copyright (c) 2008 Jakub Jermar
|
---|
3 | * Copyright (c) 2011 Oleg Romanenko
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup fs
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * @file exfat_ops.c
|
---|
36 | * @brief Implementation of VFS operations for the exFAT file system server.
|
---|
37 | */
|
---|
38 |
|
---|
39 | #include "exfat.h"
|
---|
40 | #include "exfat_fat.h"
|
---|
41 | #include "exfat_dentry.h"
|
---|
42 | #include "exfat_directory.h"
|
---|
43 | #include "../../vfs/vfs.h"
|
---|
44 | #include <libfs.h>
|
---|
45 | #include <libblock.h>
|
---|
46 | #include <ipc/services.h>
|
---|
47 | #include <ipc/devmap.h>
|
---|
48 | #include <macros.h>
|
---|
49 | #include <async.h>
|
---|
50 | #include <errno.h>
|
---|
51 | #include <str.h>
|
---|
52 | #include <byteorder.h>
|
---|
53 | #include <adt/hash_table.h>
|
---|
54 | #include <adt/list.h>
|
---|
55 | #include <assert.h>
|
---|
56 | #include <fibril_synch.h>
|
---|
57 | #include <sys/mman.h>
|
---|
58 | #include <align.h>
|
---|
59 | #include <malloc.h>
|
---|
60 | #include <stdio.h>
|
---|
61 |
|
---|
62 | #define EXFAT_NODE(node) ((node) ? (exfat_node_t *) (node)->data : NULL)
|
---|
63 | #define FS_NODE(node) ((node) ? (node)->bp : NULL)
|
---|
64 | #define DPS(bs) (BPS((bs)) / sizeof(exfat_dentry_t))
|
---|
65 |
|
---|
66 | /** Mutex protecting the list of cached free FAT nodes. */
|
---|
67 | static FIBRIL_MUTEX_INITIALIZE(ffn_mutex);
|
---|
68 |
|
---|
69 | /** List of cached free FAT nodes. */
|
---|
70 | static LIST_INITIALIZE(ffn_head);
|
---|
71 |
|
---|
72 | /*
|
---|
73 | * Forward declarations of FAT libfs operations.
|
---|
74 | */
|
---|
75 | /*
|
---|
76 | static int exfat_bitmap_get(fs_node_t **, devmap_handle_t);
|
---|
77 | static int exfat_uctable_get(fs_node_t **, devmap_handle_t);
|
---|
78 | */
|
---|
79 | static int exfat_root_get(fs_node_t **, devmap_handle_t);
|
---|
80 | static int exfat_match(fs_node_t **, fs_node_t *, const char *);
|
---|
81 | static int exfat_node_get(fs_node_t **, devmap_handle_t, fs_index_t);
|
---|
82 | static int exfat_node_open(fs_node_t *);
|
---|
83 | static int exfat_node_put(fs_node_t *);
|
---|
84 | static int exfat_create_node(fs_node_t **, devmap_handle_t, int);
|
---|
85 | static int exfat_destroy_node(fs_node_t *);
|
---|
86 | static int exfat_link(fs_node_t *, fs_node_t *, const char *);
|
---|
87 | static int exfat_unlink(fs_node_t *, fs_node_t *, const char *);
|
---|
88 | static int exfat_has_children(bool *, fs_node_t *);
|
---|
89 | static fs_index_t exfat_index_get(fs_node_t *);
|
---|
90 | static aoff64_t exfat_size_get(fs_node_t *);
|
---|
91 | static unsigned exfat_lnkcnt_get(fs_node_t *);
|
---|
92 | static char exfat_plb_get_char(unsigned);
|
---|
93 | static bool exfat_is_directory(fs_node_t *);
|
---|
94 | static bool exfat_is_file(fs_node_t *node);
|
---|
95 | static devmap_handle_t exfat_device_get(fs_node_t *node);
|
---|
96 |
|
---|
97 | /*
|
---|
98 | * Helper functions.
|
---|
99 | */
|
---|
100 | static void exfat_node_initialize(exfat_node_t *node)
|
---|
101 | {
|
---|
102 | fibril_mutex_initialize(&node->lock);
|
---|
103 | node->bp = NULL;
|
---|
104 | node->idx = NULL;
|
---|
105 | node->type = EXFAT_UNKNOW;
|
---|
106 | link_initialize(&node->ffn_link);
|
---|
107 | node->size = 0;
|
---|
108 | node->lnkcnt = 0;
|
---|
109 | node->refcnt = 0;
|
---|
110 | node->dirty = false;
|
---|
111 | node->fragmented = true;
|
---|
112 | node->lastc_cached_valid = false;
|
---|
113 | node->lastc_cached_value = 0;
|
---|
114 | node->currc_cached_valid = false;
|
---|
115 | node->currc_cached_bn = 0;
|
---|
116 | node->currc_cached_value = 0;
|
---|
117 | }
|
---|
118 |
|
---|
119 | static int exfat_node_sync(exfat_node_t *node)
|
---|
120 | {
|
---|
121 | return EOK;
|
---|
122 | }
|
---|
123 |
|
---|
124 | static int exfat_node_fini_by_devmap_handle(devmap_handle_t devmap_handle)
|
---|
125 | {
|
---|
126 | link_t *lnk;
|
---|
127 | exfat_node_t *nodep;
|
---|
128 | int rc;
|
---|
129 |
|
---|
130 | /*
|
---|
131 | * We are called from fat_unmounted() and assume that there are already
|
---|
132 | * no nodes belonging to this instance with non-zero refcount. Therefore
|
---|
133 | * it is sufficient to clean up only the FAT free node list.
|
---|
134 | */
|
---|
135 |
|
---|
136 | restart:
|
---|
137 | fibril_mutex_lock(&ffn_mutex);
|
---|
138 | for (lnk = ffn_head.next; lnk != &ffn_head; lnk = lnk->next) {
|
---|
139 | nodep = list_get_instance(lnk, exfat_node_t, ffn_link);
|
---|
140 | if (!fibril_mutex_trylock(&nodep->lock)) {
|
---|
141 | fibril_mutex_unlock(&ffn_mutex);
|
---|
142 | goto restart;
|
---|
143 | }
|
---|
144 | if (!fibril_mutex_trylock(&nodep->idx->lock)) {
|
---|
145 | fibril_mutex_unlock(&nodep->lock);
|
---|
146 | fibril_mutex_unlock(&ffn_mutex);
|
---|
147 | goto restart;
|
---|
148 | }
|
---|
149 | if (nodep->idx->devmap_handle != devmap_handle) {
|
---|
150 | fibril_mutex_unlock(&nodep->idx->lock);
|
---|
151 | fibril_mutex_unlock(&nodep->lock);
|
---|
152 | continue;
|
---|
153 | }
|
---|
154 |
|
---|
155 | list_remove(&nodep->ffn_link);
|
---|
156 | fibril_mutex_unlock(&ffn_mutex);
|
---|
157 |
|
---|
158 | /*
|
---|
159 | * We can unlock the node and its index structure because we are
|
---|
160 | * the last player on this playground and VFS is preventing new
|
---|
161 | * players from entering.
|
---|
162 | */
|
---|
163 | fibril_mutex_unlock(&nodep->idx->lock);
|
---|
164 | fibril_mutex_unlock(&nodep->lock);
|
---|
165 |
|
---|
166 | if (nodep->dirty) {
|
---|
167 | rc = exfat_node_sync(nodep);
|
---|
168 | if (rc != EOK)
|
---|
169 | return rc;
|
---|
170 | }
|
---|
171 | nodep->idx->nodep = NULL;
|
---|
172 | free(nodep->bp);
|
---|
173 | free(nodep);
|
---|
174 |
|
---|
175 | /* Need to restart because we changed the ffn_head list. */
|
---|
176 | goto restart;
|
---|
177 | }
|
---|
178 | fibril_mutex_unlock(&ffn_mutex);
|
---|
179 |
|
---|
180 | return EOK;
|
---|
181 | }
|
---|
182 |
|
---|
183 | static int exfat_node_get_new(exfat_node_t **nodepp)
|
---|
184 | {
|
---|
185 | fs_node_t *fn;
|
---|
186 | exfat_node_t *nodep;
|
---|
187 | int rc;
|
---|
188 |
|
---|
189 | fibril_mutex_lock(&ffn_mutex);
|
---|
190 | if (!list_empty(&ffn_head)) {
|
---|
191 | /* Try to use a cached free node structure. */
|
---|
192 | exfat_idx_t *idxp_tmp;
|
---|
193 | nodep = list_get_instance(ffn_head.next, exfat_node_t, ffn_link);
|
---|
194 | if (!fibril_mutex_trylock(&nodep->lock))
|
---|
195 | goto skip_cache;
|
---|
196 | idxp_tmp = nodep->idx;
|
---|
197 | if (!fibril_mutex_trylock(&idxp_tmp->lock)) {
|
---|
198 | fibril_mutex_unlock(&nodep->lock);
|
---|
199 | goto skip_cache;
|
---|
200 | }
|
---|
201 | list_remove(&nodep->ffn_link);
|
---|
202 | fibril_mutex_unlock(&ffn_mutex);
|
---|
203 | if (nodep->dirty) {
|
---|
204 | rc = exfat_node_sync(nodep);
|
---|
205 | if (rc != EOK) {
|
---|
206 | idxp_tmp->nodep = NULL;
|
---|
207 | fibril_mutex_unlock(&nodep->lock);
|
---|
208 | fibril_mutex_unlock(&idxp_tmp->lock);
|
---|
209 | free(nodep->bp);
|
---|
210 | free(nodep);
|
---|
211 | return rc;
|
---|
212 | }
|
---|
213 | }
|
---|
214 | idxp_tmp->nodep = NULL;
|
---|
215 | fibril_mutex_unlock(&nodep->lock);
|
---|
216 | fibril_mutex_unlock(&idxp_tmp->lock);
|
---|
217 | fn = FS_NODE(nodep);
|
---|
218 | } else {
|
---|
219 | skip_cache:
|
---|
220 | /* Try to allocate a new node structure. */
|
---|
221 | fibril_mutex_unlock(&ffn_mutex);
|
---|
222 | fn = (fs_node_t *)malloc(sizeof(fs_node_t));
|
---|
223 | if (!fn)
|
---|
224 | return ENOMEM;
|
---|
225 | nodep = (exfat_node_t *)malloc(sizeof(exfat_node_t));
|
---|
226 | if (!nodep) {
|
---|
227 | free(fn);
|
---|
228 | return ENOMEM;
|
---|
229 | }
|
---|
230 | }
|
---|
231 | exfat_node_initialize(nodep);
|
---|
232 | fs_node_initialize(fn);
|
---|
233 | fn->data = nodep;
|
---|
234 | nodep->bp = fn;
|
---|
235 |
|
---|
236 | *nodepp = nodep;
|
---|
237 | return EOK;
|
---|
238 | }
|
---|
239 |
|
---|
240 | static int exfat_node_get_new_by_pos(exfat_node_t **nodepp,
|
---|
241 | devmap_handle_t devmap_handle, exfat_cluster_t pfc, unsigned pdi)
|
---|
242 | {
|
---|
243 | exfat_idx_t *idxp = exfat_idx_get_by_pos(devmap_handle, pfc, pdi);
|
---|
244 | if (!idxp)
|
---|
245 | return ENOMEM;
|
---|
246 | if (exfat_node_get_new(nodepp) != EOK)
|
---|
247 | return ENOMEM;
|
---|
248 | (*nodepp)->idx = idxp;
|
---|
249 | idxp->nodep = *nodepp;
|
---|
250 | return EOK;
|
---|
251 | }
|
---|
252 |
|
---|
253 |
|
---|
254 | /** Internal version of exfat_node_get().
|
---|
255 | *
|
---|
256 | * @param idxp Locked index structure.
|
---|
257 | */
|
---|
258 | static int exfat_node_get_core(exfat_node_t **nodepp, exfat_idx_t *idxp)
|
---|
259 | {
|
---|
260 | block_t *b=NULL;
|
---|
261 | exfat_bs_t *bs;
|
---|
262 | exfat_dentry_t *d;
|
---|
263 | exfat_node_t *nodep = NULL;
|
---|
264 | int rc;
|
---|
265 |
|
---|
266 | if (idxp->nodep) {
|
---|
267 | /*
|
---|
268 | * We are lucky.
|
---|
269 | * The node is already instantiated in memory.
|
---|
270 | */
|
---|
271 | fibril_mutex_lock(&idxp->nodep->lock);
|
---|
272 | if (!idxp->nodep->refcnt++) {
|
---|
273 | fibril_mutex_lock(&ffn_mutex);
|
---|
274 | list_remove(&idxp->nodep->ffn_link);
|
---|
275 | fibril_mutex_unlock(&ffn_mutex);
|
---|
276 | }
|
---|
277 | fibril_mutex_unlock(&idxp->nodep->lock);
|
---|
278 | *nodepp = idxp->nodep;
|
---|
279 | return EOK;
|
---|
280 | }
|
---|
281 |
|
---|
282 | /*
|
---|
283 | * We must instantiate the node from the file system.
|
---|
284 | */
|
---|
285 |
|
---|
286 | assert(idxp->pfc);
|
---|
287 |
|
---|
288 | rc = exfat_node_get_new(&nodep);
|
---|
289 | if (rc != EOK)
|
---|
290 | return rc;
|
---|
291 |
|
---|
292 | bs = block_bb_get(idxp->devmap_handle);
|
---|
293 |
|
---|
294 | rc = exfat_block_get_by_clst(&b, bs, idxp->devmap_handle,
|
---|
295 | idxp->parent_fragmented, idxp->pfc, NULL,
|
---|
296 | (idxp->pdi * sizeof(exfat_dentry_t)) / BPS(bs), BLOCK_FLAGS_NONE);
|
---|
297 | if (rc != EOK) {
|
---|
298 | (void) exfat_node_put(FS_NODE(nodep));
|
---|
299 | return rc;
|
---|
300 | }
|
---|
301 |
|
---|
302 | d = ((exfat_dentry_t *)b->data) + (idxp->pdi % DPS(bs));
|
---|
303 | switch (exfat_classify_dentry(d)) {
|
---|
304 | case EXFAT_DENTRY_FILE:
|
---|
305 | nodep->type = (d->file.attr & EXFAT_ATTR_SUBDIR)?
|
---|
306 | EXFAT_DIRECTORY : EXFAT_FILE;
|
---|
307 | rc = block_put(b);
|
---|
308 | if (rc != EOK) {
|
---|
309 | (void) exfat_node_put(FS_NODE(nodep));
|
---|
310 | return rc;
|
---|
311 | }
|
---|
312 | rc = exfat_block_get_by_clst(&b, bs, idxp->devmap_handle,
|
---|
313 | idxp->parent_fragmented, idxp->pfc, NULL,
|
---|
314 | ((idxp->pdi+1) * sizeof(exfat_dentry_t)) / BPS(bs), BLOCK_FLAGS_NONE);
|
---|
315 | if (rc != EOK) {
|
---|
316 | (void) exfat_node_put(FS_NODE(nodep));
|
---|
317 | return rc;
|
---|
318 | }
|
---|
319 | d = ((exfat_dentry_t *)b->data) + ((idxp->pdi+1) % DPS(bs));
|
---|
320 |
|
---|
321 | nodep->firstc = d->stream.firstc;
|
---|
322 | nodep->size = d->stream.data_size;
|
---|
323 | nodep->fragmented = (d->stream.flags & 0x02) == 0;
|
---|
324 | break;
|
---|
325 | case EXFAT_DENTRY_BITMAP:
|
---|
326 | nodep->type = EXFAT_BITMAP;
|
---|
327 | nodep->firstc = d->bitmap.firstc;
|
---|
328 | nodep->size = d->bitmap.size;
|
---|
329 | nodep->fragmented = true;
|
---|
330 | break;
|
---|
331 | case EXFAT_DENTRY_UCTABLE:
|
---|
332 | nodep->type = EXFAT_UCTABLE;
|
---|
333 | nodep->firstc = d->uctable.firstc;
|
---|
334 | nodep->size = d->uctable.size;
|
---|
335 | nodep->fragmented = true;
|
---|
336 | break;
|
---|
337 | default:
|
---|
338 | case EXFAT_DENTRY_SKIP:
|
---|
339 | case EXFAT_DENTRY_LAST:
|
---|
340 | case EXFAT_DENTRY_FREE:
|
---|
341 | case EXFAT_DENTRY_VOLLABEL:
|
---|
342 | case EXFAT_DENTRY_GUID:
|
---|
343 | case EXFAT_DENTRY_STREAM:
|
---|
344 | case EXFAT_DENTRY_NAME:
|
---|
345 | (void) block_put(b);
|
---|
346 | (void) exfat_node_put(FS_NODE(nodep));
|
---|
347 | return ENOENT;
|
---|
348 | }
|
---|
349 |
|
---|
350 | nodep->lnkcnt = 1;
|
---|
351 | nodep->refcnt = 1;
|
---|
352 |
|
---|
353 | rc = block_put(b);
|
---|
354 | if (rc != EOK) {
|
---|
355 | (void) exfat_node_put(FS_NODE(nodep));
|
---|
356 | return rc;
|
---|
357 | }
|
---|
358 |
|
---|
359 | /* Link the idx structure with the node structure. */
|
---|
360 | nodep->idx = idxp;
|
---|
361 | idxp->nodep = nodep;
|
---|
362 |
|
---|
363 | *nodepp = nodep;
|
---|
364 | return EOK;
|
---|
365 | }
|
---|
366 |
|
---|
367 |
|
---|
368 |
|
---|
369 | /*
|
---|
370 | * EXFAT libfs operations.
|
---|
371 | */
|
---|
372 |
|
---|
373 | int exfat_root_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
|
---|
374 | {
|
---|
375 | return exfat_node_get(rfn, devmap_handle, EXFAT_ROOT_IDX);
|
---|
376 | }
|
---|
377 |
|
---|
378 | /*
|
---|
379 | int exfat_bitmap_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
|
---|
380 | {
|
---|
381 | return exfat_node_get(rfn, devmap_handle, EXFAT_BITMAP_IDX);
|
---|
382 | }
|
---|
383 | */
|
---|
384 | /*
|
---|
385 | int exfat_uctable_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
|
---|
386 | {
|
---|
387 | return exfat_node_get(rfn, devmap_handle, EXFAT_UCTABLE_IDX);
|
---|
388 | }
|
---|
389 | */
|
---|
390 |
|
---|
391 | int exfat_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
|
---|
392 | {
|
---|
393 | exfat_node_t *parentp = EXFAT_NODE(pfn);
|
---|
394 | char name[EXFAT_FILENAME_LEN+1];
|
---|
395 | exfat_file_dentry_t df;
|
---|
396 | exfat_stream_dentry_t ds;
|
---|
397 | devmap_handle_t devmap_handle;
|
---|
398 | int rc;
|
---|
399 |
|
---|
400 | fibril_mutex_lock(&parentp->idx->lock);
|
---|
401 | devmap_handle = parentp->idx->devmap_handle;
|
---|
402 | fibril_mutex_unlock(&parentp->idx->lock);
|
---|
403 |
|
---|
404 | exfat_directory_t di;
|
---|
405 | rc = exfat_directory_open(parentp, &di);
|
---|
406 | if (rc != EOK)
|
---|
407 | return rc;
|
---|
408 |
|
---|
409 | while (exfat_directory_read_file(&di, name, EXFAT_FILENAME_LEN,
|
---|
410 | &df, &ds) == EOK) {
|
---|
411 | if (stricmp(name, component) == 0) {
|
---|
412 | /* hit */
|
---|
413 | exfat_node_t *nodep;
|
---|
414 | aoff64_t o = di.pos % (BPS(di.bs) / sizeof(exfat_dentry_t));
|
---|
415 | exfat_idx_t *idx = exfat_idx_get_by_pos(devmap_handle,
|
---|
416 | parentp->firstc, di.bnum * DPS(di.bs) + o);
|
---|
417 | if (!idx) {
|
---|
418 | /*
|
---|
419 | * Can happen if memory is low or if we
|
---|
420 | * run out of 32-bit indices.
|
---|
421 | */
|
---|
422 | rc = exfat_directory_close(&di);
|
---|
423 | return (rc == EOK) ? ENOMEM : rc;
|
---|
424 | }
|
---|
425 | rc = exfat_node_get_core(&nodep, idx);
|
---|
426 | fibril_mutex_unlock(&idx->lock);
|
---|
427 | if (rc != EOK) {
|
---|
428 | (void) exfat_directory_close(&di);
|
---|
429 | return rc;
|
---|
430 | }
|
---|
431 | *rfn = FS_NODE(nodep);
|
---|
432 | rc = exfat_directory_close(&di);
|
---|
433 | if (rc != EOK)
|
---|
434 | (void) exfat_node_put(*rfn);
|
---|
435 | return rc;
|
---|
436 | } else {
|
---|
437 | rc = exfat_directory_next(&di);
|
---|
438 | if (rc != EOK)
|
---|
439 | break;
|
---|
440 | }
|
---|
441 | }
|
---|
442 | (void) exfat_directory_close(&di);
|
---|
443 | *rfn = NULL;
|
---|
444 | return EOK;
|
---|
445 | }
|
---|
446 |
|
---|
447 | /** Instantiate a exFAT in-core node. */
|
---|
448 | int exfat_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle, fs_index_t index)
|
---|
449 | {
|
---|
450 | exfat_node_t *nodep;
|
---|
451 | exfat_idx_t *idxp;
|
---|
452 | int rc;
|
---|
453 |
|
---|
454 | idxp = exfat_idx_get_by_index(devmap_handle, index);
|
---|
455 | if (!idxp) {
|
---|
456 | *rfn = NULL;
|
---|
457 | return EOK;
|
---|
458 | }
|
---|
459 | /* idxp->lock held */
|
---|
460 | rc = exfat_node_get_core(&nodep, idxp);
|
---|
461 | fibril_mutex_unlock(&idxp->lock);
|
---|
462 | if (rc == EOK)
|
---|
463 | *rfn = FS_NODE(nodep);
|
---|
464 | return rc;
|
---|
465 | }
|
---|
466 |
|
---|
467 | int exfat_node_open(fs_node_t *fn)
|
---|
468 | {
|
---|
469 | /*
|
---|
470 | * Opening a file is stateless, nothing
|
---|
471 | * to be done here.
|
---|
472 | */
|
---|
473 | return EOK;
|
---|
474 | }
|
---|
475 |
|
---|
476 | int exfat_node_put(fs_node_t *fn)
|
---|
477 | {
|
---|
478 | exfat_node_t *nodep = EXFAT_NODE(fn);
|
---|
479 | bool destroy = false;
|
---|
480 |
|
---|
481 | fibril_mutex_lock(&nodep->lock);
|
---|
482 | if (!--nodep->refcnt) {
|
---|
483 | if (nodep->idx) {
|
---|
484 | fibril_mutex_lock(&ffn_mutex);
|
---|
485 | list_append(&nodep->ffn_link, &ffn_head);
|
---|
486 | fibril_mutex_unlock(&ffn_mutex);
|
---|
487 | } else {
|
---|
488 | /*
|
---|
489 | * The node does not have any index structure associated
|
---|
490 | * with itself. This can only mean that we are releasing
|
---|
491 | * the node after a failed attempt to allocate the index
|
---|
492 | * structure for it.
|
---|
493 | */
|
---|
494 | destroy = true;
|
---|
495 | }
|
---|
496 | }
|
---|
497 | fibril_mutex_unlock(&nodep->lock);
|
---|
498 | if (destroy) {
|
---|
499 | free(nodep->bp);
|
---|
500 | free(nodep);
|
---|
501 | }
|
---|
502 | return EOK;
|
---|
503 | }
|
---|
504 |
|
---|
505 | int exfat_create_node(fs_node_t **rfn, devmap_handle_t devmap_handle, int flags)
|
---|
506 | {
|
---|
507 | /* TODO */
|
---|
508 | *rfn = NULL;
|
---|
509 | return EOK;
|
---|
510 | }
|
---|
511 |
|
---|
512 | int exfat_destroy_node(fs_node_t *fn)
|
---|
513 | {
|
---|
514 | /* TODO */
|
---|
515 | return EOK;
|
---|
516 | }
|
---|
517 |
|
---|
518 | int exfat_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
|
---|
519 | {
|
---|
520 | /* TODO */
|
---|
521 | return EOK;
|
---|
522 | }
|
---|
523 |
|
---|
524 | int exfat_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
---|
525 | {
|
---|
526 | /* TODO */
|
---|
527 | return EOK;
|
---|
528 | }
|
---|
529 |
|
---|
530 | int exfat_has_children(bool *has_children, fs_node_t *fn)
|
---|
531 | {
|
---|
532 | exfat_directory_t di;
|
---|
533 | exfat_dentry_t *d;
|
---|
534 | exfat_node_t *nodep = EXFAT_NODE(fn);
|
---|
535 | int rc;
|
---|
536 |
|
---|
537 | *has_children = false;
|
---|
538 |
|
---|
539 | if (nodep->type != EXFAT_DIRECTORY)
|
---|
540 | return EOK;
|
---|
541 |
|
---|
542 | fibril_mutex_lock(&nodep->idx->lock);
|
---|
543 |
|
---|
544 | rc = exfat_directory_open(nodep, &di);
|
---|
545 | if (rc != EOK) {
|
---|
546 | fibril_mutex_unlock(&nodep->idx->lock);
|
---|
547 | return rc;
|
---|
548 | }
|
---|
549 |
|
---|
550 | do {
|
---|
551 | rc = exfat_directory_get(&di, &d);
|
---|
552 | if (rc != EOK) {
|
---|
553 | (void) exfat_directory_close(&di);
|
---|
554 | fibril_mutex_unlock(&nodep->idx->lock);
|
---|
555 | return rc;
|
---|
556 | }
|
---|
557 | switch (exfat_classify_dentry(d)) {
|
---|
558 | case EXFAT_DENTRY_SKIP:
|
---|
559 | case EXFAT_DENTRY_FREE:
|
---|
560 | continue;
|
---|
561 | case EXFAT_DENTRY_LAST:
|
---|
562 | *has_children = false;
|
---|
563 | goto exit;
|
---|
564 | default:
|
---|
565 | *has_children = true;
|
---|
566 | goto exit;
|
---|
567 | }
|
---|
568 | } while (exfat_directory_next(&di) == EOK);
|
---|
569 |
|
---|
570 | exit:
|
---|
571 | rc = exfat_directory_close(&di);
|
---|
572 | fibril_mutex_unlock(&nodep->idx->lock);
|
---|
573 | return rc;
|
---|
574 | }
|
---|
575 |
|
---|
576 |
|
---|
577 | fs_index_t exfat_index_get(fs_node_t *fn)
|
---|
578 | {
|
---|
579 | return EXFAT_NODE(fn)->idx->index;
|
---|
580 | }
|
---|
581 |
|
---|
582 | aoff64_t exfat_size_get(fs_node_t *fn)
|
---|
583 | {
|
---|
584 | return EXFAT_NODE(fn)->size;
|
---|
585 | }
|
---|
586 |
|
---|
587 | unsigned exfat_lnkcnt_get(fs_node_t *fn)
|
---|
588 | {
|
---|
589 | return EXFAT_NODE(fn)->lnkcnt;
|
---|
590 | }
|
---|
591 |
|
---|
592 | char exfat_plb_get_char(unsigned pos)
|
---|
593 | {
|
---|
594 | return exfat_reg.plb_ro[pos % PLB_SIZE];
|
---|
595 | }
|
---|
596 |
|
---|
597 | bool exfat_is_directory(fs_node_t *fn)
|
---|
598 | {
|
---|
599 | return EXFAT_NODE(fn)->type == EXFAT_DIRECTORY;
|
---|
600 | }
|
---|
601 |
|
---|
602 | bool exfat_is_file(fs_node_t *fn)
|
---|
603 | {
|
---|
604 | return EXFAT_NODE(fn)->type == EXFAT_FILE;
|
---|
605 | }
|
---|
606 |
|
---|
607 | devmap_handle_t exfat_device_get(fs_node_t *node)
|
---|
608 | {
|
---|
609 | return 0;
|
---|
610 | }
|
---|
611 |
|
---|
612 |
|
---|
613 | /** libfs operations */
|
---|
614 | libfs_ops_t exfat_libfs_ops = {
|
---|
615 | .root_get = exfat_root_get,
|
---|
616 | .match = exfat_match,
|
---|
617 | .node_get = exfat_node_get,
|
---|
618 | .node_open = exfat_node_open,
|
---|
619 | .node_put = exfat_node_put,
|
---|
620 | .create = exfat_create_node,
|
---|
621 | .destroy = exfat_destroy_node,
|
---|
622 | .link = exfat_link,
|
---|
623 | .unlink = exfat_unlink,
|
---|
624 | .has_children = exfat_has_children,
|
---|
625 | .index_get = exfat_index_get,
|
---|
626 | .size_get = exfat_size_get,
|
---|
627 | .lnkcnt_get = exfat_lnkcnt_get,
|
---|
628 | .plb_get_char = exfat_plb_get_char,
|
---|
629 | .is_directory = exfat_is_directory,
|
---|
630 | .is_file = exfat_is_file,
|
---|
631 | .device_get = exfat_device_get
|
---|
632 | };
|
---|
633 |
|
---|
634 |
|
---|
635 | /*
|
---|
636 | * VFS operations.
|
---|
637 | */
|
---|
638 |
|
---|
639 | /* Print debug info */
|
---|
640 | static void exfat_fsinfo(exfat_bs_t *bs, devmap_handle_t devmap_handle)
|
---|
641 | {
|
---|
642 | printf("exFAT file system mounted\n");
|
---|
643 | printf("Version: %d.%d\n", bs->version.major, bs->version.minor);
|
---|
644 | printf("Volume serial: %d\n", uint32_t_le2host(bs->volume_serial));
|
---|
645 | printf("Volume first sector: %lld\n", VOL_FS(bs));
|
---|
646 | printf("Volume sectors: %lld\n", VOL_CNT(bs));
|
---|
647 | printf("FAT first sector: %d\n", FAT_FS(bs));
|
---|
648 | printf("FAT sectors: %d\n", FAT_CNT(bs));
|
---|
649 | printf("Data first sector: %d\n", DATA_FS(bs));
|
---|
650 | printf("Data sectors: %d\n", DATA_CNT(bs));
|
---|
651 | printf("Root dir first cluster: %d\n", ROOT_FC(bs));
|
---|
652 | printf("Bytes per sector: %d\n", BPS(bs));
|
---|
653 | printf("Sectors per cluster: %d\n", SPC(bs));
|
---|
654 | printf("KBytes per cluster: %d\n", SPC(bs)*BPS(bs)/1024);
|
---|
655 |
|
---|
656 | int i, rc;
|
---|
657 | exfat_cluster_t clst;
|
---|
658 | for (i=0; i<6; i++) {
|
---|
659 | rc = fat_get_cluster(bs, devmap_handle, i, &clst);
|
---|
660 | if (rc != EOK)
|
---|
661 | return;
|
---|
662 | printf("Clst %d: %x\n", i, clst);
|
---|
663 | }
|
---|
664 | }
|
---|
665 |
|
---|
666 | void exfat_mounted(ipc_callid_t rid, ipc_call_t *request)
|
---|
667 | {
|
---|
668 | devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
669 | exfat_node_t *rootp=NULL, *bitmapp=NULL, *uctablep=NULL;
|
---|
670 | enum cache_mode cmode;
|
---|
671 | exfat_bs_t *bs;
|
---|
672 |
|
---|
673 | /* Accept the mount options */
|
---|
674 | char *opts;
|
---|
675 | int rc = async_data_write_accept((void **) &opts, true, 0, 0, 0, NULL);
|
---|
676 |
|
---|
677 | if (rc != EOK) {
|
---|
678 | async_answer_0(rid, rc);
|
---|
679 | return;
|
---|
680 | }
|
---|
681 |
|
---|
682 | /* Check for option enabling write through. */
|
---|
683 | if (str_cmp(opts, "wtcache") == 0)
|
---|
684 | cmode = CACHE_MODE_WT;
|
---|
685 | else
|
---|
686 | cmode = CACHE_MODE_WB;
|
---|
687 |
|
---|
688 | free(opts);
|
---|
689 |
|
---|
690 | /* initialize libblock */
|
---|
691 | rc = block_init(devmap_handle, BS_SIZE);
|
---|
692 | if (rc != EOK) {
|
---|
693 | async_answer_0(rid, rc);
|
---|
694 | return;
|
---|
695 | }
|
---|
696 |
|
---|
697 | /* prepare the boot block */
|
---|
698 | rc = block_bb_read(devmap_handle, BS_BLOCK);
|
---|
699 | if (rc != EOK) {
|
---|
700 | block_fini(devmap_handle);
|
---|
701 | async_answer_0(rid, rc);
|
---|
702 | return;
|
---|
703 | }
|
---|
704 |
|
---|
705 | /* get the buffer with the boot sector */
|
---|
706 | bs = block_bb_get(devmap_handle);
|
---|
707 |
|
---|
708 | /* Initialize the block cache */
|
---|
709 | rc = block_cache_init(devmap_handle, BPS(bs), 0 /* XXX */, cmode);
|
---|
710 | if (rc != EOK) {
|
---|
711 | block_fini(devmap_handle);
|
---|
712 | async_answer_0(rid, rc);
|
---|
713 | return;
|
---|
714 | }
|
---|
715 |
|
---|
716 | /* Do some simple sanity checks on the file system. */
|
---|
717 | rc = exfat_sanity_check(bs, devmap_handle);
|
---|
718 | if (rc != EOK) {
|
---|
719 | (void) block_cache_fini(devmap_handle);
|
---|
720 | block_fini(devmap_handle);
|
---|
721 | async_answer_0(rid, rc);
|
---|
722 | return;
|
---|
723 | }
|
---|
724 |
|
---|
725 | rc = exfat_idx_init_by_devmap_handle(devmap_handle);
|
---|
726 | if (rc != EOK) {
|
---|
727 | (void) block_cache_fini(devmap_handle);
|
---|
728 | block_fini(devmap_handle);
|
---|
729 | async_answer_0(rid, rc);
|
---|
730 | return;
|
---|
731 | }
|
---|
732 |
|
---|
733 | /* Initialize the root node. */
|
---|
734 | rc = exfat_node_get_new_by_pos(&rootp, devmap_handle, EXFAT_ROOT_PAR,
|
---|
735 | EXFAT_ROOT_POS);
|
---|
736 | if (rc!=EOK) {
|
---|
737 | (void) block_cache_fini(devmap_handle);
|
---|
738 | block_fini(devmap_handle);
|
---|
739 | exfat_idx_fini_by_devmap_handle(devmap_handle);
|
---|
740 | async_answer_0(rid, ENOMEM);
|
---|
741 | return;
|
---|
742 | }
|
---|
743 | assert(rootp->idx->index == EXFAT_ROOT_IDX);
|
---|
744 |
|
---|
745 | rootp->type = EXFAT_DIRECTORY;
|
---|
746 | rootp->firstc = ROOT_FC(bs);
|
---|
747 | rootp->fragmented = true;
|
---|
748 | rootp->refcnt = 1;
|
---|
749 | rootp->lnkcnt = 0; /* FS root is not linked */
|
---|
750 |
|
---|
751 | uint32_t clusters;
|
---|
752 | rc = fat_clusters_get(&clusters, bs, devmap_handle, rootp->firstc);
|
---|
753 | if (rc != EOK) {
|
---|
754 | free(rootp);
|
---|
755 | (void) block_cache_fini(devmap_handle);
|
---|
756 | block_fini(devmap_handle);
|
---|
757 | exfat_idx_fini_by_devmap_handle(devmap_handle);
|
---|
758 | async_answer_0(rid, ENOTSUP);
|
---|
759 | return;
|
---|
760 | }
|
---|
761 | rootp->size = BPS(bs) * SPC(bs) * clusters;
|
---|
762 | fibril_mutex_unlock(&rootp->idx->lock);
|
---|
763 |
|
---|
764 | /* Open root directory and looking for Bitmap and UC-Table */
|
---|
765 | exfat_directory_t di;
|
---|
766 | exfat_dentry_t *de;
|
---|
767 | rc = exfat_directory_open(rootp, &di);
|
---|
768 | if (rc != EOK) {
|
---|
769 | free(rootp);
|
---|
770 | (void) block_cache_fini(devmap_handle);
|
---|
771 | block_fini(devmap_handle);
|
---|
772 | exfat_idx_fini_by_devmap_handle(devmap_handle);
|
---|
773 | async_answer_0(rid, ENOTSUP);
|
---|
774 | return;
|
---|
775 | }
|
---|
776 |
|
---|
777 | /* Initialize the bitmap node. */
|
---|
778 | rc = exfat_directory_find(&di, EXFAT_DENTRY_BITMAP, &de);
|
---|
779 | if (rc != EOK) {
|
---|
780 | free(rootp);
|
---|
781 | (void) block_cache_fini(devmap_handle);
|
---|
782 | block_fini(devmap_handle);
|
---|
783 | exfat_idx_fini_by_devmap_handle(devmap_handle);
|
---|
784 | async_answer_0(rid, ENOTSUP);
|
---|
785 | return;
|
---|
786 | }
|
---|
787 |
|
---|
788 | rc = exfat_node_get_new_by_pos(&bitmapp, devmap_handle, rootp->firstc,
|
---|
789 | di.pos);
|
---|
790 | if (rc!=EOK) {
|
---|
791 | free(rootp);
|
---|
792 | (void) block_cache_fini(devmap_handle);
|
---|
793 | block_fini(devmap_handle);
|
---|
794 | exfat_idx_fini_by_devmap_handle(devmap_handle);
|
---|
795 | async_answer_0(rid, ENOMEM);
|
---|
796 | return;
|
---|
797 | }
|
---|
798 | assert(bitmapp->idx->index == EXFAT_BITMAP_IDX);
|
---|
799 | fibril_mutex_unlock(&bitmapp->idx->lock);
|
---|
800 |
|
---|
801 | bitmapp->type = EXFAT_BITMAP;
|
---|
802 | bitmapp->firstc = de->bitmap.firstc;
|
---|
803 | bitmapp->fragmented = true;
|
---|
804 | bitmapp->idx->parent_fragmented = true;
|
---|
805 | bitmapp->refcnt = 1;
|
---|
806 | bitmapp->lnkcnt = 0;
|
---|
807 | bitmapp->size = de->bitmap.size;
|
---|
808 |
|
---|
809 | /* Initialize the uctable node. */
|
---|
810 | rc = exfat_directory_seek(&di, 0);
|
---|
811 | if (rc != EOK) {
|
---|
812 | free(rootp);
|
---|
813 | free(bitmapp);
|
---|
814 | (void) block_cache_fini(devmap_handle);
|
---|
815 | block_fini(devmap_handle);
|
---|
816 | exfat_idx_fini_by_devmap_handle(devmap_handle);
|
---|
817 | async_answer_0(rid, ENOTSUP);
|
---|
818 | return;
|
---|
819 | }
|
---|
820 |
|
---|
821 | rc = exfat_directory_find(&di, EXFAT_DENTRY_UCTABLE, &de);
|
---|
822 | if (rc != EOK) {
|
---|
823 | free(rootp);
|
---|
824 | free(bitmapp);
|
---|
825 | (void) block_cache_fini(devmap_handle);
|
---|
826 | block_fini(devmap_handle);
|
---|
827 | exfat_idx_fini_by_devmap_handle(devmap_handle);
|
---|
828 | async_answer_0(rid, ENOTSUP);
|
---|
829 | return;
|
---|
830 | }
|
---|
831 |
|
---|
832 | rc = exfat_node_get_new_by_pos(&uctablep, devmap_handle, rootp->firstc,
|
---|
833 | di.pos);
|
---|
834 | if (rc!=EOK) {
|
---|
835 | free(rootp);
|
---|
836 | free(bitmapp);
|
---|
837 | (void) block_cache_fini(devmap_handle);
|
---|
838 | block_fini(devmap_handle);
|
---|
839 | exfat_idx_fini_by_devmap_handle(devmap_handle);
|
---|
840 | async_answer_0(rid, ENOMEM);
|
---|
841 | return;
|
---|
842 | }
|
---|
843 | assert(uctablep->idx->index == EXFAT_UCTABLE_IDX);
|
---|
844 | fibril_mutex_unlock(&uctablep->idx->lock);
|
---|
845 |
|
---|
846 | uctablep->type = EXFAT_UCTABLE;
|
---|
847 | uctablep->firstc = de->uctable.firstc;
|
---|
848 | uctablep->fragmented = true;
|
---|
849 | uctablep->idx->parent_fragmented = true;
|
---|
850 | uctablep->refcnt = 1;
|
---|
851 | uctablep->lnkcnt = 0;
|
---|
852 | uctablep->size = de->uctable.size;
|
---|
853 |
|
---|
854 | rc = exfat_directory_close(&di);
|
---|
855 | if (rc!=EOK) {
|
---|
856 | free(rootp);
|
---|
857 | free(bitmapp);
|
---|
858 | free(uctablep);
|
---|
859 | (void) block_cache_fini(devmap_handle);
|
---|
860 | block_fini(devmap_handle);
|
---|
861 | exfat_idx_fini_by_devmap_handle(devmap_handle);
|
---|
862 | async_answer_0(rid, ENOMEM);
|
---|
863 | return;
|
---|
864 | }
|
---|
865 |
|
---|
866 | exfat_fsinfo(bs, devmap_handle);
|
---|
867 | printf("Root dir size: %lld\n", rootp->size);
|
---|
868 |
|
---|
869 | async_answer_3(rid, EOK, rootp->idx->index, rootp->size, rootp->lnkcnt);
|
---|
870 | }
|
---|
871 |
|
---|
872 | void exfat_mount(ipc_callid_t rid, ipc_call_t *request)
|
---|
873 | {
|
---|
874 | libfs_mount(&exfat_libfs_ops, exfat_reg.fs_handle, rid, request);
|
---|
875 | }
|
---|
876 |
|
---|
877 | void exfat_unmounted(ipc_callid_t rid, ipc_call_t *request)
|
---|
878 | {
|
---|
879 | devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
880 | fs_node_t *fn;
|
---|
881 | exfat_node_t *nodep;
|
---|
882 | int rc;
|
---|
883 |
|
---|
884 | rc = exfat_root_get(&fn, devmap_handle);
|
---|
885 | if (rc != EOK) {
|
---|
886 | async_answer_0(rid, rc);
|
---|
887 | return;
|
---|
888 | }
|
---|
889 | nodep = EXFAT_NODE(fn);
|
---|
890 |
|
---|
891 | /*
|
---|
892 | * We expect exactly two references on the root node. One for the
|
---|
893 | * fat_root_get() above and one created in fat_mounted().
|
---|
894 | */
|
---|
895 | if (nodep->refcnt != 2) {
|
---|
896 | (void) exfat_node_put(fn);
|
---|
897 | async_answer_0(rid, EBUSY);
|
---|
898 | return;
|
---|
899 | }
|
---|
900 |
|
---|
901 | /*
|
---|
902 | * Put the root node and force it to the FAT free node list.
|
---|
903 | */
|
---|
904 | (void) exfat_node_put(fn);
|
---|
905 | (void) exfat_node_put(fn);
|
---|
906 |
|
---|
907 | /*
|
---|
908 | * Perform cleanup of the node structures, index structures and
|
---|
909 | * associated data. Write back this file system's dirty blocks and
|
---|
910 | * stop using libblock for this instance.
|
---|
911 | */
|
---|
912 | (void) exfat_node_fini_by_devmap_handle(devmap_handle);
|
---|
913 | exfat_idx_fini_by_devmap_handle(devmap_handle);
|
---|
914 | (void) block_cache_fini(devmap_handle);
|
---|
915 | block_fini(devmap_handle);
|
---|
916 |
|
---|
917 | async_answer_0(rid, EOK);
|
---|
918 | }
|
---|
919 |
|
---|
920 | void exfat_unmount(ipc_callid_t rid, ipc_call_t *request)
|
---|
921 | {
|
---|
922 | libfs_unmount(&exfat_libfs_ops, rid, request);
|
---|
923 | }
|
---|
924 |
|
---|
925 | /*
|
---|
926 | int bitmap_is_allocated(exfat_bs_t *bs, devmap_handle_t devmap_handle,
|
---|
927 | exfat_cluster_t clst, bool *status)
|
---|
928 | {
|
---|
929 | fs_node_t *fn;
|
---|
930 | exfat_node_t *bitmap;
|
---|
931 | int rc;
|
---|
932 |
|
---|
933 | rc = exfat_bitmap_get(&fn, devmap_handle);
|
---|
934 | if (rc != EOK)
|
---|
935 | return rc;
|
---|
936 |
|
---|
937 | nbitmap = EXFAT_NODE(fn);
|
---|
938 |
|
---|
939 |
|
---|
940 | return EOK;
|
---|
941 | }
|
---|
942 | */
|
---|
943 |
|
---|
944 | void exfat_lookup(ipc_callid_t rid, ipc_call_t *request)
|
---|
945 | {
|
---|
946 | libfs_lookup(&exfat_libfs_ops, exfat_reg.fs_handle, rid, request);
|
---|
947 | }
|
---|
948 |
|
---|
949 | void exfat_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
950 | {
|
---|
951 | devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
952 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
953 | aoff64_t pos =
|
---|
954 | (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
|
---|
955 | fs_node_t *fn;
|
---|
956 | exfat_node_t *nodep;
|
---|
957 | exfat_bs_t *bs;
|
---|
958 | size_t bytes=0;
|
---|
959 | block_t *b;
|
---|
960 | int rc;
|
---|
961 |
|
---|
962 | rc = exfat_node_get(&fn, devmap_handle, index);
|
---|
963 | if (rc != EOK) {
|
---|
964 | async_answer_0(rid, rc);
|
---|
965 | return;
|
---|
966 | }
|
---|
967 | if (!fn) {
|
---|
968 | async_answer_0(rid, ENOENT);
|
---|
969 | return;
|
---|
970 | }
|
---|
971 | nodep = EXFAT_NODE(fn);
|
---|
972 |
|
---|
973 | ipc_callid_t callid;
|
---|
974 | size_t len;
|
---|
975 | if (!async_data_read_receive(&callid, &len)) {
|
---|
976 | exfat_node_put(fn);
|
---|
977 | async_answer_0(callid, EINVAL);
|
---|
978 | async_answer_0(rid, EINVAL);
|
---|
979 | return;
|
---|
980 | }
|
---|
981 |
|
---|
982 | bs = block_bb_get(devmap_handle);
|
---|
983 |
|
---|
984 | if (nodep->type == EXFAT_FILE) {
|
---|
985 | /*
|
---|
986 | * Our strategy for regular file reads is to read one block at
|
---|
987 | * most and make use of the possibility to return less data than
|
---|
988 | * requested. This keeps the code very simple.
|
---|
989 | */
|
---|
990 | if (pos >= nodep->size) {
|
---|
991 | /* reading beyond the EOF */
|
---|
992 | bytes = 0;
|
---|
993 | (void) async_data_read_finalize(callid, NULL, 0);
|
---|
994 | } else {
|
---|
995 | bytes = min(len, BPS(bs) - pos % BPS(bs));
|
---|
996 | bytes = min(bytes, nodep->size - pos);
|
---|
997 | rc = exfat_block_get(&b, bs, nodep, pos / BPS(bs),
|
---|
998 | BLOCK_FLAGS_NONE);
|
---|
999 | if (rc != EOK) {
|
---|
1000 | exfat_node_put(fn);
|
---|
1001 | async_answer_0(callid, rc);
|
---|
1002 | async_answer_0(rid, rc);
|
---|
1003 | return;
|
---|
1004 | }
|
---|
1005 | (void) async_data_read_finalize(callid,
|
---|
1006 | b->data + pos % BPS(bs), bytes);
|
---|
1007 | rc = block_put(b);
|
---|
1008 | if (rc != EOK) {
|
---|
1009 | exfat_node_put(fn);
|
---|
1010 | async_answer_0(rid, rc);
|
---|
1011 | return;
|
---|
1012 | }
|
---|
1013 | }
|
---|
1014 | } else {
|
---|
1015 | if (nodep->type != EXFAT_DIRECTORY) {
|
---|
1016 | async_answer_0(callid, ENOTSUP);
|
---|
1017 | async_answer_0(rid, ENOTSUP);
|
---|
1018 | return;
|
---|
1019 | }
|
---|
1020 |
|
---|
1021 | aoff64_t spos = pos;
|
---|
1022 | char name[EXFAT_FILENAME_LEN+1];
|
---|
1023 | exfat_file_dentry_t df;
|
---|
1024 | exfat_stream_dentry_t ds;
|
---|
1025 |
|
---|
1026 | assert(nodep->size % BPS(bs) == 0);
|
---|
1027 | assert(BPS(bs) % sizeof(exfat_dentry_t) == 0);
|
---|
1028 |
|
---|
1029 | exfat_directory_t di;
|
---|
1030 | rc = exfat_directory_open(nodep, &di);
|
---|
1031 | if (rc != EOK) goto err;
|
---|
1032 | rc = exfat_directory_seek(&di, pos);
|
---|
1033 | if (rc != EOK) {
|
---|
1034 | (void) exfat_directory_close(&di);
|
---|
1035 | goto err;
|
---|
1036 | }
|
---|
1037 |
|
---|
1038 | rc = exfat_directory_read_file(&di, name, EXFAT_FILENAME_LEN, &df, &ds);
|
---|
1039 | if (rc == EOK) goto hit;
|
---|
1040 | if (rc == ENOENT) goto miss;
|
---|
1041 |
|
---|
1042 | err:
|
---|
1043 | (void) exfat_node_put(fn);
|
---|
1044 | async_answer_0(callid, rc);
|
---|
1045 | async_answer_0(rid, rc);
|
---|
1046 | return;
|
---|
1047 |
|
---|
1048 | miss:
|
---|
1049 | rc = exfat_directory_close(&di);
|
---|
1050 | if (rc!=EOK)
|
---|
1051 | goto err;
|
---|
1052 | rc = exfat_node_put(fn);
|
---|
1053 | async_answer_0(callid, rc != EOK ? rc : ENOENT);
|
---|
1054 | async_answer_1(rid, rc != EOK ? rc : ENOENT, 0);
|
---|
1055 | return;
|
---|
1056 |
|
---|
1057 | hit:
|
---|
1058 | pos = di.pos;
|
---|
1059 | rc = exfat_directory_close(&di);
|
---|
1060 | if (rc!=EOK)
|
---|
1061 | goto err;
|
---|
1062 | (void) async_data_read_finalize(callid, name, str_size(name) + 1);
|
---|
1063 | bytes = (pos - spos)+1;
|
---|
1064 | }
|
---|
1065 |
|
---|
1066 | rc = exfat_node_put(fn);
|
---|
1067 | async_answer_1(rid, rc, (sysarg_t)bytes);
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | void exfat_close(ipc_callid_t rid, ipc_call_t *request)
|
---|
1071 | {
|
---|
1072 | async_answer_0(rid, EOK);
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | void exfat_open_node(ipc_callid_t rid, ipc_call_t *request)
|
---|
1076 | {
|
---|
1077 | libfs_open_node(&exfat_libfs_ops, exfat_reg.fs_handle, rid, request);
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | void exfat_stat(ipc_callid_t rid, ipc_call_t *request)
|
---|
1081 | {
|
---|
1082 | libfs_stat(&exfat_libfs_ops, exfat_reg.fs_handle, rid, request);
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 | /**
|
---|
1086 | * @}
|
---|
1087 | */
|
---|