]> code.delx.au - refind/blob - filesystems/fsw_core.c
Added filesystem drivers.
[refind] / filesystems / fsw_core.c
1 /* $Id: fsw_core.c 29125 2010-05-06 09:43:05Z vboxsync $ */
2 /** @file
3 * fsw_core.c - Core file system wrapper abstraction layer code.
4 */
5
6 /*-
7 * This code is based on:
8 *
9 * Copyright (c) 2006 Christoph Pfisterer
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are
13 * met:
14 *
15 * * Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 *
18 * * Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the
21 * distribution.
22 *
23 * * Neither the name of Christoph Pfisterer nor the names of the
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include "fsw_core.h"
41
42
43 // functions
44
45 static void fsw_blockcache_free(struct fsw_volume *vol);
46
47 #define MAX_CACHE_LEVEL (5)
48
49
50 /**
51 * Mount a volume with a given file system driver. This function is called by the
52 * host driver to make a volume accessible. The file system driver to use is specified
53 * by a pointer to its dispatch table. The file system driver will look at the
54 * data on the volume to determine if it can read the format. If the volume is found
55 * unsuitable, FSW_UNSUPPORTED is returned.
56 *
57 * If this function returns FSW_SUCCESS, *vol_out points at a valid volume data
58 * structure. The caller must release it later by calling fsw_unmount.
59 *
60 * If this function returns an error status, the caller only needs to clean up its
61 * own buffers that may have been allocated through the read_block interface.
62 */
63
64 fsw_status_t fsw_mount(void *host_data,
65 struct fsw_host_table *host_table,
66 struct fsw_fstype_table *fstype_table,
67 struct fsw_volume **vol_out)
68 {
69 fsw_status_t status;
70 struct fsw_volume *vol;
71
72 // allocate memory for the structure
73 status = fsw_alloc_zero(fstype_table->volume_struct_size, (void **)&vol);
74 if (status)
75 return status;
76
77 // initialize fields
78 vol->phys_blocksize = 512;
79 vol->log_blocksize = 512;
80 vol->label.type = FSW_STRING_TYPE_EMPTY;
81 vol->host_data = host_data;
82 vol->host_table = host_table;
83 vol->fstype_table = fstype_table;
84 vol->host_string_type = host_table->native_string_type;
85
86 // let the fs driver mount the file system
87 status = vol->fstype_table->volume_mount(vol);
88 if (status)
89 goto errorexit;
90
91 // TODO: anything else?
92
93 *vol_out = vol;
94 return FSW_SUCCESS;
95
96 errorexit:
97 fsw_unmount(vol);
98 return status;
99 }
100
101 /**
102 * Unmount a volume by releasing all memory associated with it. This function is
103 * called by the host driver when a volume is no longer needed. It is also called
104 * by the core after a failed mount to clean up any allocated memory.
105 *
106 * Note that all dnodes must have been released before calling this function.
107 */
108
109 void fsw_unmount(struct fsw_volume *vol)
110 {
111 if (vol->root)
112 fsw_dnode_release(vol->root);
113 // TODO: check that no other dnodes are still around
114
115 vol->fstype_table->volume_free(vol);
116
117 fsw_blockcache_free(vol);
118 fsw_strfree(&vol->label);
119 fsw_free(vol);
120 }
121
122 /**
123 * Get in-depth information on the volume. This function can be called by the host
124 * driver to get additional information on the volume.
125 */
126
127 fsw_status_t fsw_volume_stat(struct fsw_volume *vol, struct fsw_volume_stat *sb)
128 {
129 return vol->fstype_table->volume_stat(vol, sb);
130 }
131
132 /**
133 * Set the physical and logical block sizes of the volume. This functions is called by
134 * the file system driver to announce the block sizes it wants to use for accessing
135 * the disk (physical) and for addressing file contents (logical).
136 * Usually both sizes will be the same but there may be file systems that need to access
137 * metadata at a smaller block size than the allocation unit for files.
138 *
139 * Calling this function causes the block cache to be dropped. All pointers returned
140 * from fsw_block_get become invalid. This function should only be called while
141 * mounting the file system, not as a part of file access operations.
142 *
143 * Both sizes are measured in bytes, must be powers of 2, and must not be smaller
144 * than 512 bytes. The logical block size cannot be smaller than the physical block size.
145 */
146
147 void fsw_set_blocksize(struct fsw_volume *vol, fsw_u32 phys_blocksize, fsw_u32 log_blocksize)
148 {
149 // TODO: Check the sizes. Both must be powers of 2. log_blocksize must not be smaller than
150 // phys_blocksize.
151
152 // drop core block cache if present
153 fsw_blockcache_free(vol);
154
155 // signal host driver to drop caches etc.
156 vol->host_table->change_blocksize(vol,
157 vol->phys_blocksize, vol->log_blocksize,
158 phys_blocksize, log_blocksize);
159
160 vol->phys_blocksize = phys_blocksize;
161 vol->log_blocksize = log_blocksize;
162 }
163
164 /**
165 * Get a block of data from the disk. This function is called by the file system driver
166 * or by core functions. It calls through to the host driver's device access routine.
167 * Given a physical block number, it reads the block into memory (or fetches it from the
168 * block cache) and returns the address of the memory buffer. The caller should provide
169 * an indication of how important the block is in the cache_level parameter. Blocks with
170 * a low level are purged first. Some suggestions for cache levels:
171 *
172 * - 0: File data
173 * - 1: Directory data, symlink data
174 * - 2: File system metadata
175 * - 3..5: File system metadata with a high rate of access
176 *
177 * If this function returns successfully, the returned data pointer is valid until the
178 * caller calls fsw_block_release.
179 */
180
181 fsw_status_t fsw_block_get(struct VOLSTRUCTNAME *vol, fsw_u32 phys_bno, fsw_u32 cache_level, void **buffer_out)
182 {
183 fsw_status_t status;
184 fsw_u32 i, discard_level, new_bcache_size;
185 struct fsw_blockcache *new_bcache;
186
187 // TODO: allow the host driver to do its own caching; just call through if
188 // the appropriate function pointers are set
189
190 if (cache_level > MAX_CACHE_LEVEL)
191 cache_level = MAX_CACHE_LEVEL;
192
193 // check block cache
194 for (i = 0; i < vol->bcache_size; i++) {
195 if (vol->bcache[i].phys_bno == phys_bno) {
196 // cache hit!
197 if (vol->bcache[i].cache_level < cache_level)
198 vol->bcache[i].cache_level = cache_level; // promote the entry
199 vol->bcache[i].refcount++;
200 *buffer_out = vol->bcache[i].data;
201 return FSW_SUCCESS;
202 }
203 }
204
205 // find a free entry in the cache table
206 for (i = 0; i < vol->bcache_size; i++) {
207 if (vol->bcache[i].phys_bno == (fsw_u32)FSW_INVALID_BNO)
208 break;
209 }
210 if (i >= vol->bcache_size) {
211 for (discard_level = 0; discard_level <= MAX_CACHE_LEVEL; discard_level++) {
212 for (i = 0; i < vol->bcache_size; i++) {
213 if (vol->bcache[i].refcount == 0 && vol->bcache[i].cache_level <= discard_level)
214 break;
215 }
216 if (i < vol->bcache_size)
217 break;
218 }
219 }
220 if (i >= vol->bcache_size) {
221 // enlarge / create the cache
222 if (vol->bcache_size < 16)
223 new_bcache_size = 16;
224 else
225 new_bcache_size = vol->bcache_size << 1;
226 status = fsw_alloc(new_bcache_size * sizeof(struct fsw_blockcache), &new_bcache);
227 if (status)
228 return status;
229 if (vol->bcache_size > 0)
230 fsw_memcpy(new_bcache, vol->bcache, vol->bcache_size * sizeof(struct fsw_blockcache));
231 for (i = vol->bcache_size; i < new_bcache_size; i++) {
232 new_bcache[i].refcount = 0;
233 new_bcache[i].cache_level = 0;
234 new_bcache[i].phys_bno = (fsw_u32)FSW_INVALID_BNO;
235 new_bcache[i].data = NULL;
236 }
237 i = vol->bcache_size;
238
239 // switch caches
240 if (vol->bcache != NULL)
241 fsw_free(vol->bcache);
242 vol->bcache = new_bcache;
243 vol->bcache_size = new_bcache_size;
244 }
245 vol->bcache[i].phys_bno = (fsw_u32)FSW_INVALID_BNO;
246
247 // read the data
248 if (vol->bcache[i].data == NULL) {
249 status = fsw_alloc(vol->phys_blocksize, &vol->bcache[i].data);
250 if (status)
251 return status;
252 }
253 status = vol->host_table->read_block(vol, phys_bno, vol->bcache[i].data);
254 if (status)
255 return status;
256
257 vol->bcache[i].phys_bno = phys_bno;
258 vol->bcache[i].cache_level = cache_level;
259 vol->bcache[i].refcount = 1;
260 *buffer_out = vol->bcache[i].data;
261 return FSW_SUCCESS;
262 }
263
264 /**
265 * Releases a disk block. This function must be called to release disk blocks returned
266 * from fsw_block_get.
267 */
268
269 void fsw_block_release(struct VOLSTRUCTNAME *vol, fsw_u32 phys_bno, void *buffer)
270 {
271 fsw_u32 i;
272
273 // TODO: allow the host driver to do its own caching; just call through if
274 // the appropriate function pointers are set
275
276 // update block cache
277 for (i = 0; i < vol->bcache_size; i++) {
278 if (vol->bcache[i].phys_bno == phys_bno && vol->bcache[i].refcount > 0)
279 vol->bcache[i].refcount--;
280 }
281 }
282
283 /**
284 * Release the block cache. Called internally when changing block sizes and when
285 * unmounting the volume. It frees all data occupied by the generic block cache.
286 */
287
288 static void fsw_blockcache_free(struct fsw_volume *vol)
289 {
290 fsw_u32 i;
291
292 for (i = 0; i < vol->bcache_size; i++) {
293 if (vol->bcache[i].data != NULL)
294 fsw_free(vol->bcache[i].data);
295 }
296 if (vol->bcache != NULL) {
297 fsw_free(vol->bcache);
298 vol->bcache = NULL;
299 }
300 vol->bcache_size = 0;
301 }
302
303 /**
304 * Add a new dnode to the list of known dnodes. This internal function is used when a
305 * dnode is created to add it to the dnode list that is used to search for existing
306 * dnodes by id.
307 */
308
309 static void fsw_dnode_register(struct fsw_volume *vol, struct fsw_dnode *dno)
310 {
311 dno->next = vol->dnode_head;
312 if (vol->dnode_head != NULL)
313 vol->dnode_head->prev = dno;
314 dno->prev = NULL;
315 vol->dnode_head = dno;
316 }
317
318 /**
319 * Create a dnode representing the root directory. This function is called by the file system
320 * driver while mounting the file system. The root directory is special because it has no parent
321 * dnode, its name is defined to be empty, and its type is also fixed. Otherwise, this functions
322 * behaves in the same way as fsw_dnode_create.
323 */
324
325 fsw_status_t fsw_dnode_create_root(struct fsw_volume *vol, fsw_u32 dnode_id, struct fsw_dnode **dno_out)
326 {
327 fsw_status_t status;
328 struct fsw_dnode *dno;
329
330 // allocate memory for the structure
331 status = fsw_alloc_zero(vol->fstype_table->dnode_struct_size, (void **)&dno);
332 if (status)
333 return status;
334
335 // fill the structure
336 dno->vol = vol;
337 dno->parent = NULL;
338 dno->dnode_id = dnode_id;
339 dno->type = FSW_DNODE_TYPE_DIR;
340 dno->refcount = 1;
341 dno->name.type = FSW_STRING_TYPE_EMPTY;
342 // TODO: instead, call a function to create an empty string in the native string type
343
344 fsw_dnode_register(vol, dno);
345
346 *dno_out = dno;
347 return FSW_SUCCESS;
348 }
349
350 /**
351 * Create a new dnode representing a file system object. This function is called by
352 * the file system driver in response to directory lookup or read requests. Note that
353 * if there already is a dnode with the given dnode_id on record, then no new object
354 * is created. Instead, the existing dnode is returned and its reference count
355 * increased. All other parameters are ignored in this case.
356 *
357 * The type passed into this function may be FSW_DNODE_TYPE_UNKNOWN. It is sufficient
358 * to fill the type field during the dnode_fill call.
359 *
360 * The name parameter must describe a string with the object's name. A copy will be
361 * stored in the dnode structure for future reference. The name will not be used to
362 * shortcut directory lookups, but may be used to reconstruct paths.
363 *
364 * If the function returns successfully, *dno_out contains a pointer to the dnode
365 * that must be released by the caller with fsw_dnode_release.
366 */
367
368 fsw_status_t fsw_dnode_create(struct fsw_dnode *parent_dno, fsw_u32 dnode_id, int type,
369 struct fsw_string *name, struct fsw_dnode **dno_out)
370 {
371 fsw_status_t status;
372 struct fsw_volume *vol = parent_dno->vol;
373 struct fsw_dnode *dno;
374
375 // check if we already have a dnode with the same id
376 for (dno = vol->dnode_head; dno; dno = dno->next) {
377 if (dno->dnode_id == dnode_id) {
378 fsw_dnode_retain(dno);
379 *dno_out = dno;
380 return FSW_SUCCESS;
381 }
382 }
383
384 // allocate memory for the structure
385 status = fsw_alloc_zero(vol->fstype_table->dnode_struct_size, (void **)&dno);
386 if (status)
387 return status;
388
389 // fill the structure
390 dno->vol = vol;
391 dno->parent = parent_dno;
392 fsw_dnode_retain(dno->parent);
393 dno->dnode_id = dnode_id;
394 dno->type = type;
395 dno->refcount = 1;
396 status = fsw_strdup_coerce(&dno->name, vol->host_table->native_string_type, name);
397 if (status) {
398 fsw_free(dno);
399 return status;
400 }
401
402 fsw_dnode_register(vol, dno);
403
404 *dno_out = dno;
405 return FSW_SUCCESS;
406 }
407
408 /**
409 * Increases the reference count of a dnode. This must be balanced with
410 * fsw_dnode_release calls. Note that some dnode functions return a retained
411 * dnode pointer to their caller.
412 */
413
414 void fsw_dnode_retain(struct fsw_dnode *dno)
415 {
416 dno->refcount++;
417 }
418
419 /**
420 * Release a dnode pointer, deallocating it if this was the last reference.
421 * This function decrements the reference counter of the dnode. If the counter
422 * reaches zero, the dnode is freed. Since the parent dnode is released
423 * during that process, this function may cause it to be freed, too.
424 */
425
426 void fsw_dnode_release(struct fsw_dnode *dno)
427 {
428 struct fsw_volume *vol = dno->vol;
429 struct fsw_dnode *parent_dno;
430
431 dno->refcount--;
432
433 if (dno->refcount == 0) {
434 parent_dno = dno->parent;
435
436 // de-register from volume's list
437 if (dno->next)
438 dno->next->prev = dno->prev;
439 if (dno->prev)
440 dno->prev->next = dno->next;
441 if (vol->dnode_head == dno)
442 vol->dnode_head = dno->next;
443
444 // run fstype-specific cleanup
445 vol->fstype_table->dnode_free(vol, dno);
446
447 fsw_strfree(&dno->name);
448 fsw_free(dno);
449
450 // release our pointer to the parent, possibly deallocating it, too
451 if (parent_dno)
452 fsw_dnode_release(parent_dno);
453 }
454 }
455
456 /**
457 * Get full information about a dnode from disk. This function is called by the host
458 * driver as well as by the core functions. Some file systems defer reading full
459 * information on a dnode until it is actually needed (i.e. separation between
460 * directory and inode information). This function makes sure that all information
461 * is available in the dnode structure. The following fields may not have a correct
462 * value until fsw_dnode_fill has been called:
463 *
464 * type, size
465 */
466
467 fsw_status_t fsw_dnode_fill(struct fsw_dnode *dno)
468 {
469 // TODO: check a flag right here, call fstype's dnode_fill only once per dnode
470
471 return dno->vol->fstype_table->dnode_fill(dno->vol, dno);
472 }
473
474 /**
475 * Get extended information about a dnode. This function can be called by the host
476 * driver to get a full compliment of information about a dnode in addition to the
477 * fields of the fsw_dnode structure itself.
478 *
479 * Some data requires host-specific conversion to be useful (i.e. timestamps) and
480 * will be passed to callback functions instead of being written into the structure.
481 * These callbacks must be filled in by the caller.
482 */
483
484 fsw_status_t fsw_dnode_stat(struct fsw_dnode *dno, struct fsw_dnode_stat *sb)
485 {
486 fsw_status_t status;
487
488 status = fsw_dnode_fill(dno);
489 if (status)
490 return status;
491
492 sb->used_bytes = 0;
493 status = dno->vol->fstype_table->dnode_stat(dno->vol, dno, sb);
494 if (!status && !sb->used_bytes)
495 sb->used_bytes = FSW_U64_DIV(dno->size + dno->vol->log_blocksize - 1, dno->vol->log_blocksize);
496 return status;
497 }
498
499 /**
500 * Lookup a directory entry by name. This function is called by the host driver.
501 * Given a directory dnode and a file name, it looks up the named entry in the
502 * directory.
503 *
504 * If the dnode is not a directory, the call will fail. The caller is responsible for
505 * resolving symbolic links before calling this function.
506 *
507 * If the function returns FSW_SUCCESS, *child_dno_out points to the requested directory
508 * entry. The caller must call fsw_dnode_release on it.
509 */
510
511 fsw_status_t fsw_dnode_lookup(struct fsw_dnode *dno,
512 struct fsw_string *lookup_name, struct fsw_dnode **child_dno_out)
513 {
514 fsw_status_t status;
515
516 status = fsw_dnode_fill(dno);
517 if (status)
518 return status;
519 if (dno->type != FSW_DNODE_TYPE_DIR)
520 return FSW_UNSUPPORTED;
521
522 return dno->vol->fstype_table->dir_lookup(dno->vol, dno, lookup_name, child_dno_out);
523 }
524
525 /**
526 * Find a file system object by path. This function is called by the host driver.
527 * Given a directory dnode and a relative or absolute path, it walks the directory
528 * tree until it finds the target dnode. If an intermediate node turns out to be
529 * a symlink, it is resolved automatically. If the target node is a symlink, it
530 * is not resolved.
531 *
532 * If the function returns FSW_SUCCESS, *child_dno_out points to the requested directory
533 * entry. The caller must call fsw_dnode_release on it.
534 */
535
536 fsw_status_t fsw_dnode_lookup_path(struct fsw_dnode *dno,
537 struct fsw_string *lookup_path, char separator,
538 struct fsw_dnode **child_dno_out)
539 {
540 fsw_status_t status;
541 struct fsw_volume *vol = dno->vol;
542 struct fsw_dnode *child_dno = NULL;
543 struct fsw_string lookup_name;
544 struct fsw_string remaining_path;
545 int root_if_empty;
546
547 remaining_path = *lookup_path;
548 fsw_dnode_retain(dno);
549
550 // loop over the path
551 for (root_if_empty = 1; fsw_strlen(&remaining_path) > 0; root_if_empty = 0) {
552 // parse next path component
553 fsw_strsplit(&lookup_name, &remaining_path, separator);
554
555 FSW_MSG_DEBUG((FSW_MSGSTR("fsw_dnode_lookup_path: split into %d '%s' and %d '%s'\n"),
556 lookup_name.len, lookup_name.data,
557 remaining_path.len, remaining_path.data));
558
559 if (fsw_strlen(&lookup_name) == 0) { // empty path component
560 if (root_if_empty)
561 child_dno = vol->root;
562 else
563 child_dno = dno;
564 fsw_dnode_retain(child_dno);
565
566 } else {
567 // do an actual directory lookup
568
569 // ensure we have full information
570 status = fsw_dnode_fill(dno);
571 if (status)
572 goto errorexit;
573
574 // resolve symlink if necessary
575 if (dno->type == FSW_DNODE_TYPE_SYMLINK) {
576 status = fsw_dnode_resolve(dno, &child_dno);
577 if (status)
578 goto errorexit;
579
580 // symlink target becomes the new dno
581 fsw_dnode_release(dno);
582 dno = child_dno; // is already retained
583 child_dno = NULL;
584
585 // ensure we have full information
586 status = fsw_dnode_fill(dno);
587 if (status)
588 goto errorexit;
589 }
590
591 // make sure we operate on a directory
592 if (dno->type != FSW_DNODE_TYPE_DIR) {
593 return FSW_UNSUPPORTED;
594 goto errorexit;
595 }
596
597 // check special paths
598 if (fsw_streq_cstr(&lookup_name, ".")) { // self directory
599 child_dno = dno;
600 fsw_dnode_retain(child_dno);
601
602 } else if (fsw_streq_cstr(&lookup_name, "..")) { // parent directory
603 if (dno->parent == NULL) {
604 // We cannot go up from the root directory. Caution: Certain apps like the EFI shell
605 // rely on this behaviour!
606 status = FSW_NOT_FOUND;
607 goto errorexit;
608 }
609 child_dno = dno->parent;
610 fsw_dnode_retain(child_dno);
611
612 } else {
613 // do an actual lookup
614 status = vol->fstype_table->dir_lookup(vol, dno, &lookup_name, &child_dno);
615 if (status)
616 goto errorexit;
617 }
618 }
619
620 // child_dno becomes the new dno
621 fsw_dnode_release(dno);
622 dno = child_dno; // is already retained
623 child_dno = NULL;
624
625 FSW_MSG_DEBUG((FSW_MSGSTR("fsw_dnode_lookup_path: now at inode %d\n"), dno->dnode_id));
626 }
627
628 *child_dno_out = dno;
629 return FSW_SUCCESS;
630
631 errorexit:
632 FSW_MSG_DEBUG((FSW_MSGSTR("fsw_dnode_lookup_path: leaving with error %d\n"), status));
633 fsw_dnode_release(dno);
634 if (child_dno != NULL)
635 fsw_dnode_release(child_dno);
636 return status;
637 }
638
639 /**
640 * Get the next directory item in sequential order. This function is called by the
641 * host driver to read the complete contents of a directory in sequential (file system
642 * defined) order. Calling this function returns the next entry. Iteration state is
643 * kept by a shandle on the directory's dnode. The caller must set up the shandle
644 * when starting the iteration.
645 *
646 * When the end of the directory is reached, this function returns FSW_NOT_FOUND.
647 * If the function returns FSW_SUCCESS, *child_dno_out points to the next directory
648 * entry. The caller must call fsw_dnode_release on it.
649 */
650
651 fsw_status_t fsw_dnode_dir_read(struct fsw_shandle *shand, struct fsw_dnode **child_dno_out)
652 {
653 fsw_status_t status;
654 struct fsw_dnode *dno = shand->dnode;
655 fsw_u64 saved_pos;
656
657 if (dno->type != FSW_DNODE_TYPE_DIR)
658 return FSW_UNSUPPORTED;
659
660 saved_pos = shand->pos;
661 status = dno->vol->fstype_table->dir_read(dno->vol, dno, shand, child_dno_out);
662 if (status)
663 shand->pos = saved_pos;
664 return status;
665 }
666
667 /**
668 * Read the target path of a symbolic link. This function is called by the host driver
669 * to read the "content" of a symbolic link, that is the relative or absolute path
670 * it points to.
671 *
672 * If the function returns FSW_SUCCESS, the string handle provided by the caller is
673 * filled with a string in the host's preferred encoding. The caller is responsible
674 * for calling fsw_strfree on the string.
675 */
676
677 fsw_status_t fsw_dnode_readlink(struct fsw_dnode *dno, struct fsw_string *target_name)
678 {
679 fsw_status_t status;
680
681 status = fsw_dnode_fill(dno);
682 if (status)
683 return status;
684 if (dno->type != FSW_DNODE_TYPE_SYMLINK)
685 return FSW_UNSUPPORTED;
686
687 return dno->vol->fstype_table->readlink(dno->vol, dno, target_name);
688 }
689
690 /**
691 * Read the target path of a symbolic link by accessing file data. This function can
692 * be called by the file system driver if the file system stores the target path
693 * as normal file data. This function will open an shandle, read the whole content
694 * of the file into a buffer, and build a string from that. Currently the encoding
695 * for the string is fixed as FSW_STRING_TYPE_ISO88591.
696 *
697 * If the function returns FSW_SUCCESS, the string handle provided by the caller is
698 * filled with a string in the host's preferred encoding. The caller is responsible
699 * for calling fsw_strfree on the string.
700 */
701
702 fsw_status_t fsw_dnode_readlink_data(struct fsw_dnode *dno, struct fsw_string *link_target)
703 {
704 fsw_status_t status;
705 struct fsw_shandle shand;
706 fsw_u32 buffer_size;
707 char buffer[FSW_PATH_MAX];
708
709 struct fsw_string s;
710
711 if (dno->size > FSW_PATH_MAX)
712 return FSW_VOLUME_CORRUPTED;
713
714 s.type = FSW_STRING_TYPE_ISO88591;
715 s.size = s.len = (int)dno->size;
716 s.data = buffer;
717
718 // open shandle and read the data
719 status = fsw_shandle_open(dno, &shand);
720 if (status)
721 return status;
722 buffer_size = (fsw_u32)s.size;
723 status = fsw_shandle_read(&shand, &buffer_size, buffer);
724 fsw_shandle_close(&shand);
725 if (status)
726 return status;
727 if ((int)buffer_size < s.size)
728 return FSW_VOLUME_CORRUPTED;
729
730 status = fsw_strdup_coerce(link_target, dno->vol->host_string_type, &s);
731 return status;
732 }
733
734 /**
735 * Resolve a symbolic link. This function can be called by the host driver to make
736 * sure the a dnode is fully resolved instead of pointing at a symlink. If the dnode
737 * passed in is not a symlink, it is returned unmodified.
738 *
739 * Note that absolute paths will be resolved relative to the root directory of the
740 * volume. If the host is an operating system with its own VFS layer, it should
741 * resolve symlinks on its own.
742 *
743 * If the function returns FSW_SUCCESS, *target_dno_out points at a dnode that is
744 * not a symlink. The caller is responsible for calling fsw_dnode_release on it.
745 */
746
747 fsw_status_t fsw_dnode_resolve(struct fsw_dnode *dno, struct fsw_dnode **target_dno_out)
748 {
749 fsw_status_t status;
750 struct fsw_string target_name;
751 struct fsw_dnode *target_dno;
752
753 fsw_dnode_retain(dno);
754
755 while (1) {
756 // get full information
757 status = fsw_dnode_fill(dno);
758 if (status)
759 goto errorexit;
760 if (dno->type != FSW_DNODE_TYPE_SYMLINK) {
761 // found a non-symlink target, return it
762 *target_dno_out = dno;
763 return FSW_SUCCESS;
764 }
765 if (dno->parent == NULL) { // safety measure, cannot happen in theory
766 status = FSW_NOT_FOUND;
767 goto errorexit;
768 }
769
770 // read the link's target
771 status = fsw_dnode_readlink(dno, &target_name);
772 if (status)
773 goto errorexit;
774
775 // resolve it
776 status = fsw_dnode_lookup_path(dno->parent, &target_name, '/', &target_dno);
777 fsw_strfree(&target_name);
778 if (status)
779 goto errorexit;
780
781 // target_dno becomes the new dno
782 fsw_dnode_release(dno);
783 dno = target_dno; // is already retained
784 }
785
786 errorexit:
787 fsw_dnode_release(dno);
788 return status;
789 }
790
791 /**
792 * Set up a shandle (storage handle) to access a file's data. This function is called
793 * by the host driver and by the core when they need to access a file's data. It is also
794 * used in accessing the raw data of directories and symlinks if the file system uses
795 * the same mechanisms for storing the data of those items.
796 *
797 * The storage for the fsw_shandle structure is provided by the caller. The dnode and pos
798 * fields may be accessed, pos may also be written to to set the file pointer. The file's
799 * data size is available as shand->dnode->size.
800 *
801 * If this function returns FSW_SUCCESS, the caller must call fsw_shandle_close to release
802 * the dnode reference held by the shandle.
803 */
804
805 fsw_status_t fsw_shandle_open(struct fsw_dnode *dno, struct fsw_shandle *shand)
806 {
807 fsw_status_t status;
808 struct fsw_volume *vol = dno->vol;
809
810 // read full dnode information into memory
811 status = vol->fstype_table->dnode_fill(vol, dno);
812 if (status)
813 return status;
814
815 // setup shandle
816 fsw_dnode_retain(dno);
817
818 shand->dnode = dno;
819 shand->pos = 0;
820 shand->extent.type = FSW_EXTENT_TYPE_INVALID;
821
822 return FSW_SUCCESS;
823 }
824
825 /**
826 * Close a shandle after accessing the dnode's data. This function is called by the host
827 * driver or core functions when they are finished with accessing a file's data. It
828 * releases the dnode reference and frees any buffers associated with the shandle itself.
829 * The dnode is only released if this was the last reference using it.
830 */
831
832 void fsw_shandle_close(struct fsw_shandle *shand)
833 {
834 if (shand->extent.type == FSW_EXTENT_TYPE_BUFFER)
835 fsw_free(shand->extent.buffer);
836 fsw_dnode_release(shand->dnode);
837 }
838
839 /**
840 * Read data from a shandle (storage handle for a dnode). This function is called by the
841 * host driver or internally when data is read from a file. TODO: more
842 */
843
844 fsw_status_t fsw_shandle_read(struct fsw_shandle *shand, fsw_u32 *buffer_size_inout, void *buffer_in)
845 {
846 fsw_status_t status;
847 struct fsw_dnode *dno = shand->dnode;
848 struct fsw_volume *vol = dno->vol;
849 fsw_u8 *buffer, *block_buffer;
850 fsw_u32 buflen, copylen, pos;
851 fsw_u32 log_bno, pos_in_extent, phys_bno, pos_in_physblock;
852 fsw_u32 cache_level;
853
854 if (shand->pos >= dno->size) { // already at EOF
855 *buffer_size_inout = 0;
856 return FSW_SUCCESS;
857 }
858
859 // initialize vars
860 buffer = buffer_in;
861 buflen = *buffer_size_inout;
862 pos = (fsw_u32)shand->pos;
863 cache_level = (dno->type != FSW_DNODE_TYPE_FILE) ? 1 : 0;
864 // restrict read to file size
865 if (buflen > dno->size - pos)
866 buflen = (fsw_u32)(dno->size - pos);
867
868 while (buflen > 0) {
869 // get extent for the current logical block
870 log_bno = pos / vol->log_blocksize;
871 if (shand->extent.type == FSW_EXTENT_TYPE_INVALID ||
872 log_bno < shand->extent.log_start ||
873 log_bno >= shand->extent.log_start + shand->extent.log_count) {
874
875 if (shand->extent.type == FSW_EXTENT_TYPE_BUFFER)
876 fsw_free(shand->extent.buffer);
877
878 // ask the file system for the proper extent
879 shand->extent.log_start = log_bno;
880 status = vol->fstype_table->get_extent(vol, dno, &shand->extent);
881 if (status) {
882 shand->extent.type = FSW_EXTENT_TYPE_INVALID;
883 return status;
884 }
885 }
886
887 pos_in_extent = pos - shand->extent.log_start * vol->log_blocksize;
888
889 // dispatch by extent type
890 if (shand->extent.type == FSW_EXTENT_TYPE_PHYSBLOCK) {
891 // convert to physical block number and offset
892 phys_bno = shand->extent.phys_start + pos_in_extent / vol->phys_blocksize;
893 pos_in_physblock = pos_in_extent & (vol->phys_blocksize - 1);
894 copylen = vol->phys_blocksize - pos_in_physblock;
895 if (copylen > buflen)
896 copylen = buflen;
897
898 // get one physical block
899 status = fsw_block_get(vol, phys_bno, cache_level, (void **)&block_buffer);
900 if (status)
901 return status;
902
903 // copy data from it
904 fsw_memcpy(buffer, block_buffer + pos_in_physblock, copylen);
905 fsw_block_release(vol, phys_bno, block_buffer);
906
907 } else if (shand->extent.type == FSW_EXTENT_TYPE_BUFFER) {
908 copylen = shand->extent.log_count * vol->log_blocksize - pos_in_extent;
909 if (copylen > buflen)
910 copylen = buflen;
911 fsw_memcpy(buffer, (fsw_u8 *)shand->extent.buffer + pos_in_extent, copylen);
912
913 } else { // _SPARSE or _INVALID
914 copylen = shand->extent.log_count * vol->log_blocksize - pos_in_extent;
915 if (copylen > buflen)
916 copylen = buflen;
917 fsw_memzero(buffer, copylen);
918
919 }
920
921 buffer += copylen;
922 buflen -= copylen;
923 pos += copylen;
924 }
925
926 *buffer_size_inout = (fsw_u32)(pos - shand->pos);
927 shand->pos = pos;
928
929 return FSW_SUCCESS;
930 }
931
932 // EOF