source: mainline/uspace/srv/fs/cdfs/cdfs_ops.c@ c67195c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c67195c was 993d608, checked in by Jiri Svoboda <jiri@…>, 11 years ago

Add Joliet extension support to CDFS.

  • Property mode set to 100644
File size: 28.0 KB
Line 
1/*
2 * Copyright (c) 2011 Martin Decky
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 cdfs_ops.c
35 * @brief Implementation of VFS operations for the cdfs file system
36 * server.
37 */
38
39#include "cdfs_ops.h"
40#include <stdbool.h>
41#include <adt/list.h>
42#include <adt/hash_table.h>
43#include <adt/hash.h>
44#include <malloc.h>
45#include <mem.h>
46#include <loc.h>
47#include <libfs.h>
48#include <errno.h>
49#include <block.h>
50#include <str.h>
51#include <byteorder.h>
52#include <macros.h>
53#include "cdfs.h"
54#include "cdfs_endian.h"
55
56/** Standard CD-ROM block size */
57#define BLOCK_SIZE 2048
58
59#define NODE_CACHE_SIZE 200
60
61/** All root nodes have index 0 */
62#define CDFS_SOME_ROOT 0
63
64#define CDFS_NODE(node) ((node) ? (cdfs_node_t *)(node)->data : NULL)
65#define FS_NODE(node) ((node) ? (node)->fs_node : NULL)
66
67#define CDFS_STANDARD_IDENT "CD001"
68
69enum {
70 CDFS_NAME_CURDIR = '\x00',
71 CDFS_NAME_PARENTDIR = '\x01'
72};
73
74typedef enum {
75 VOL_DESC_BOOT = 0,
76 VOL_DESC_PRIMARY = 1,
77 VOL_DESC_SUPPLEMENTARY = 2,
78 VOL_DESC_VOL_PARTITION = 3,
79 VOL_DESC_SET_TERMINATOR = 255
80} vol_desc_type_t;
81
82typedef struct {
83 uint8_t system_ident[32];
84 uint8_t ident[32];
85} __attribute__((packed)) cdfs_vol_desc_boot_t;
86
87typedef struct {
88 uint8_t year[4];
89 uint8_t mon[2];
90 uint8_t day[2];
91
92 uint8_t hour[2];
93 uint8_t min[2];
94 uint8_t sec[2];
95 uint8_t msec[2];
96
97 uint8_t offset;
98} __attribute__((packed)) cdfs_datetime_t;
99
100typedef struct {
101 uint8_t year; /**< Since 1900 */
102 uint8_t mon;
103 uint8_t day;
104
105 uint8_t hour;
106 uint8_t min;
107 uint8_t sec;
108
109 uint8_t offset;
110} __attribute__((packed)) cdfs_timestamp_t;
111
112typedef enum {
113 DIR_FLAG_DIRECTORY = 2
114} cdfs_dir_flag_t;
115
116typedef struct {
117 uint8_t length;
118 uint8_t ea_length;
119
120 uint32_t_lb lba;
121 uint32_t_lb size;
122
123 cdfs_timestamp_t timestamp;
124 uint8_t flags;
125 uint8_t unit_size;
126 uint8_t gap_size;
127 uint16_t_lb sequence_nr;
128
129 uint8_t name_length;
130 uint8_t name[];
131} __attribute__((packed)) cdfs_dir_t;
132
133typedef struct {
134 uint8_t flags; /* reserved in primary */
135
136 uint8_t system_ident[32];
137 uint8_t ident[32];
138
139 uint64_t res1;
140 uint32_t_lb lba_size;
141
142 uint8_t esc_seq[32]; /* reserved in primary */
143 uint16_t_lb set_size;
144 uint16_t_lb sequence_nr;
145
146 uint16_t_lb block_size;
147 uint32_t_lb path_table_size;
148
149 uint32_t path_table_lsb;
150 uint32_t opt_path_table_lsb;
151 uint32_t path_table_msb;
152 uint32_t opt_path_table_msb;
153
154 cdfs_dir_t root_dir;
155 uint8_t pad0;
156
157 uint8_t set_ident[128];
158 uint8_t publisher_ident[128];
159 uint8_t preparer_ident[128];
160 uint8_t app_ident[128];
161
162 uint8_t copyright_file_ident[37];
163 uint8_t abstract_file_ident[37];
164 uint8_t biblio_file_ident[37];
165
166 cdfs_datetime_t creation;
167 cdfs_datetime_t modification;
168 cdfs_datetime_t expiration;
169 cdfs_datetime_t effective;
170
171 uint8_t fs_version;
172} __attribute__((packed)) cdfs_vol_desc_prisec_t;
173
174typedef struct {
175 uint8_t type;
176 uint8_t standard_ident[5];
177 uint8_t version;
178 union {
179 cdfs_vol_desc_boot_t boot;
180 cdfs_vol_desc_prisec_t prisec;
181 } data;
182} __attribute__((packed)) cdfs_vol_desc_t;
183
184typedef enum {
185 /** ASCII character set / encoding (base ISO 9660) */
186 enc_ascii,
187 /** UCS-2 character set / encoding (Joliet) */
188 enc_ucs2
189} cdfs_enc_t;
190
191typedef enum {
192 CDFS_NONE,
193 CDFS_FILE,
194 CDFS_DIRECTORY
195} cdfs_dentry_type_t;
196
197typedef struct {
198 link_t link; /**< Siblings list link */
199 fs_index_t index; /**< Node index */
200 char *name; /**< Dentry name */
201} cdfs_dentry_t;
202
203typedef uint32_t cdfs_lba_t;
204
205typedef struct {
206 link_t link; /**< Link to list of all instances */
207 service_id_t service_id; /**< Service ID of block device */
208 cdfs_enc_t enc; /**< Filesystem string encoding */
209} cdfs_t;
210
211typedef struct {
212 fs_node_t *fs_node; /**< FS node */
213 fs_index_t index; /**< Node index */
214 cdfs_t *fs; /**< File system */
215
216 ht_link_t nh_link; /**< Nodes hash table link */
217 cdfs_dentry_type_t type; /**< Dentry type */
218
219 unsigned int lnkcnt; /**< Link count */
220 uint32_t size; /**< File size if type is CDFS_FILE */
221
222 list_t cs_list; /**< Child's siblings list */
223 cdfs_lba_t lba; /**< LBA of data on disk */
224 bool processed; /**< If all children have been read */
225 unsigned int opened; /**< Opened count */
226} cdfs_node_t;
227
228/** String encoding */
229enum {
230 /** ASCII - standard ISO 9660 */
231 ucs2_esc_seq_no = 3,
232 /** USC-2 - Joliet */
233 ucs2_esc_seq_len = 3
234};
235
236/** Joliet SVD UCS-2 escape sequences */
237static uint8_t ucs2_esc_seq[ucs2_esc_seq_no][ucs2_esc_seq_len] = {
238 { 0x25, 0x2f, 0x40 },
239 { 0x25, 0x2f, 0x43 },
240 { 0x25, 0x2f, 0x45 }
241};
242
243/** List of all instances */
244static LIST_INITIALIZE(cdfs_instances);
245
246/** Shared index of nodes */
247static fs_index_t cdfs_index = 1;
248
249/** Number of currently cached nodes */
250static size_t nodes_cached = 0;
251
252/** Hash table of all cdfs nodes */
253static hash_table_t nodes;
254
255/*
256 * Hash table support functions.
257 */
258
259typedef struct {
260 service_id_t service_id;
261 fs_index_t index;
262} ht_key_t;
263
264static size_t nodes_key_hash(void *k)
265{
266 ht_key_t *key = (ht_key_t*)k;
267 return hash_combine(key->service_id, key->index);
268}
269
270static size_t nodes_hash(const ht_link_t *item)
271{
272 cdfs_node_t *node = hash_table_get_inst(item, cdfs_node_t, nh_link);
273 return hash_combine(node->fs->service_id, node->index);
274}
275
276static bool nodes_key_equal(void *k, const ht_link_t *item)
277{
278 cdfs_node_t *node = hash_table_get_inst(item, cdfs_node_t, nh_link);
279 ht_key_t *key = (ht_key_t*)k;
280
281 return key->service_id == node->fs->service_id && key->index == node->index;
282}
283
284static void nodes_remove_callback(ht_link_t *item)
285{
286 cdfs_node_t *node = hash_table_get_inst(item, cdfs_node_t, nh_link);
287
288 if (node->type == CDFS_DIRECTORY) {
289 link_t *link;
290 while ((link = list_first(&node->cs_list)) != NULL) {
291 cdfs_dentry_t *dentry = list_get_instance(link, cdfs_dentry_t, link);
292 list_remove(&dentry->link);
293 free(dentry);
294 }
295 }
296
297 free(node->fs_node);
298 free(node);
299}
300
301/** Nodes hash table operations */
302static hash_table_ops_t nodes_ops = {
303 .hash = nodes_hash,
304 .key_hash = nodes_key_hash,
305 .key_equal = nodes_key_equal,
306 .equal = NULL,
307 .remove_callback = nodes_remove_callback
308};
309
310static int cdfs_node_get(fs_node_t **rfn, service_id_t service_id,
311 fs_index_t index)
312{
313 ht_key_t key = {
314 .index = index,
315 .service_id = service_id
316 };
317
318 ht_link_t *link = hash_table_find(&nodes, &key);
319 if (link) {
320 cdfs_node_t *node =
321 hash_table_get_inst(link, cdfs_node_t, nh_link);
322
323 *rfn = FS_NODE(node);
324 } else
325 *rfn = NULL;
326
327 return EOK;
328}
329
330static int cdfs_root_get(fs_node_t **rfn, service_id_t service_id)
331{
332 return cdfs_node_get(rfn, service_id, CDFS_SOME_ROOT);
333}
334
335static void cdfs_node_initialize(cdfs_node_t *node)
336{
337 node->fs_node = NULL;
338 node->index = 0;
339 node->fs = NULL;
340 node->type = CDFS_NONE;
341 node->lnkcnt = 0;
342 node->size = 0;
343 node->lba = 0;
344 node->processed = false;
345 node->opened = 0;
346
347 list_initialize(&node->cs_list);
348}
349
350static int create_node(fs_node_t **rfn, cdfs_t *fs, int lflag,
351 fs_index_t index)
352{
353 assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
354
355 cdfs_node_t *node = malloc(sizeof(cdfs_node_t));
356 if (!node)
357 return ENOMEM;
358
359 cdfs_node_initialize(node);
360
361 node->fs_node = malloc(sizeof(fs_node_t));
362 if (!node->fs_node) {
363 free(node);
364 return ENOMEM;
365 }
366
367 fs_node_initialize(node->fs_node);
368 node->fs_node->data = node;
369
370 fs_node_t *rootfn;
371 int rc = cdfs_root_get(&rootfn, fs->service_id);
372
373 assert(rc == EOK);
374
375 if (!rootfn)
376 node->index = CDFS_SOME_ROOT;
377 else
378 node->index = index;
379
380 node->fs = fs;
381
382 if (lflag & L_DIRECTORY)
383 node->type = CDFS_DIRECTORY;
384 else
385 node->type = CDFS_FILE;
386
387 /* Insert the new node into the nodes hash table. */
388 hash_table_insert(&nodes, &node->nh_link);
389
390 *rfn = FS_NODE(node);
391 nodes_cached++;
392
393 return EOK;
394}
395
396static int link_node(fs_node_t *pfn, fs_node_t *fn, const char *name)
397{
398 cdfs_node_t *parent = CDFS_NODE(pfn);
399 cdfs_node_t *node = CDFS_NODE(fn);
400
401 assert(parent->type == CDFS_DIRECTORY);
402
403 /* Check for duplicate entries */
404 list_foreach(parent->cs_list, link, cdfs_dentry_t, dentry) {
405 if (str_cmp(dentry->name, name) == 0)
406 return EEXIST;
407 }
408
409 /* Allocate and initialize the dentry */
410 cdfs_dentry_t *dentry = malloc(sizeof(cdfs_dentry_t));
411 if (!dentry)
412 return ENOMEM;
413
414 /* Populate and link the new dentry */
415 dentry->name = str_dup(name);
416 if (dentry->name == NULL) {
417 free(dentry);
418 return ENOMEM;
419 }
420
421 link_initialize(&dentry->link);
422 dentry->index = node->index;
423
424 node->lnkcnt++;
425 list_append(&dentry->link, &parent->cs_list);
426
427 return EOK;
428}
429
430/** Decode CDFS string.
431 *
432 * @param data Pointer to string data
433 * @param dsize Size of data in bytes
434 * @param enc String encoding
435 * @return Decoded string
436 */
437static char *cdfs_decode_str(void *data, size_t dsize, cdfs_enc_t enc)
438{
439 int rc;
440 char *str;
441 uint16_t *buf;
442
443 switch (enc) {
444 case enc_ascii:
445 str = malloc(dsize + 1);
446 if (str == NULL)
447 return NULL;
448 memcpy(str, data, dsize);
449 str[dsize] = '\0';
450 break;
451 case enc_ucs2:
452 buf = calloc(dsize + 2, 1);
453 if (buf == NULL)
454 return NULL;
455
456 size_t i;
457 for (i = 0; i < dsize / sizeof(uint16_t); i++) {
458 buf[i] = uint16_t_be2host(((uint16_t *)data)[i]);
459 }
460
461 size_t dstr_size = dsize / sizeof(uint16_t) * 4 + 1;
462 str = malloc(dstr_size);
463 if (str == NULL)
464 return NULL;
465
466 rc = utf16_to_str(str, dstr_size, buf);
467 free(buf);
468
469 if (rc != EOK)
470 return NULL;
471 break;
472 default:
473 assert(false);
474 str = NULL;
475 }
476
477 return str;
478}
479
480/** Decode file name.
481 *
482 * @param data File name buffer
483 * @param dsize Fine name buffer size
484 * @param dtype Directory entry type
485 * @return Decoded file name (allocated string)
486 */
487static char *cdfs_decode_name(void *data, size_t dsize, cdfs_enc_t enc,
488 cdfs_dentry_type_t dtype)
489{
490 char *name;
491 char *dot;
492 char *scolon;
493
494 name = cdfs_decode_str(data, dsize, enc);
495 if (name == NULL)
496 return NULL;
497
498 if (dtype == CDFS_DIRECTORY)
499 return name;
500
501 dot = str_chr(name, '.');
502
503 if (dot != NULL) {
504 scolon = str_chr(dot, ';');
505 if (scolon != NULL) {
506 /* Trim version part */
507 *scolon = '\0';
508 }
509
510 /* If the extension is an empty string, trim the dot separator. */
511 if (dot[1] == '\0')
512 *dot = '\0';
513 }
514
515 return name;
516}
517
518static bool cdfs_readdir(cdfs_t *fs, fs_node_t *fs_node)
519{
520 cdfs_node_t *node = CDFS_NODE(fs_node);
521 assert(node);
522
523 if (node->processed)
524 return true;
525
526 uint32_t blocks = node->size / BLOCK_SIZE;
527 if ((node->size % BLOCK_SIZE) != 0)
528 blocks++;
529
530 for (uint32_t i = 0; i < blocks; i++) {
531 block_t *block;
532 int rc = block_get(&block, fs->service_id, node->lba + i, BLOCK_FLAGS_NONE);
533 if (rc != EOK)
534 return false;
535
536 cdfs_dir_t *dir;
537
538 for (size_t offset = 0; offset < BLOCK_SIZE;
539 offset += dir->length) {
540 dir = (cdfs_dir_t *) (block->data + offset);
541 if (dir->length == 0)
542 break;
543 if (offset + dir->length > BLOCK_SIZE) {
544 /* XXX Incorrect FS structure */
545 break;
546 }
547
548 cdfs_dentry_type_t dentry_type;
549 if (dir->flags & DIR_FLAG_DIRECTORY)
550 dentry_type = CDFS_DIRECTORY;
551 else
552 dentry_type = CDFS_FILE;
553
554 /* Skip special entries */
555
556 if (dir->name_length == 1 &&
557 dir->name[0] == CDFS_NAME_CURDIR)
558 continue;
559 if (dir->name_length == 1 &&
560 dir->name[0] == CDFS_NAME_PARENTDIR)
561 continue;
562
563 // FIXME: hack - indexing by dentry byte offset on disc
564
565 fs_node_t *fn;
566 int rc = create_node(&fn, fs, dentry_type,
567 (node->lba + i) * BLOCK_SIZE + offset);
568 if ((rc != EOK) || (fn == NULL))
569 return false;
570
571 cdfs_node_t *cur = CDFS_NODE(fn);
572 cur->lba = uint32_lb(dir->lba);
573 cur->size = uint32_lb(dir->size);
574
575 char *name = cdfs_decode_name(dir->name,
576 dir->name_length, node->fs->enc, dentry_type);
577 if (name == NULL)
578 return false;
579
580 // FIXME: check return value
581
582 link_node(fs_node, fn, name);
583 free(name);
584
585 if (dentry_type == CDFS_FILE)
586 cur->processed = true;
587 }
588
589 block_put(block);
590 }
591
592 node->processed = true;
593 return true;
594}
595
596static fs_node_t *get_uncached_node(cdfs_t *fs, fs_index_t index)
597{
598 cdfs_lba_t lba = index / BLOCK_SIZE;
599 size_t offset = index % BLOCK_SIZE;
600
601 block_t *block;
602 int rc = block_get(&block, fs->service_id, lba, BLOCK_FLAGS_NONE);
603 if (rc != EOK)
604 return NULL;
605
606 cdfs_dir_t *dir = (cdfs_dir_t *) (block->data + offset);
607
608 cdfs_dentry_type_t dentry_type;
609 if (dir->flags & DIR_FLAG_DIRECTORY)
610 dentry_type = CDFS_DIRECTORY;
611 else
612 dentry_type = CDFS_FILE;
613
614 fs_node_t *fn;
615 rc = create_node(&fn, fs, dentry_type, index);
616 if ((rc != EOK) || (fn == NULL))
617 return NULL;
618
619 cdfs_node_t *node = CDFS_NODE(fn);
620 node->lba = uint32_lb(dir->lba);
621 node->size = uint32_lb(dir->size);
622 node->lnkcnt = 1;
623
624 if (dentry_type == CDFS_FILE)
625 node->processed = true;
626
627 block_put(block);
628 return fn;
629}
630
631static fs_node_t *get_cached_node(cdfs_t *fs, fs_index_t index)
632{
633 ht_key_t key = {
634 .index = index,
635 .service_id = fs->service_id
636 };
637
638 ht_link_t *link = hash_table_find(&nodes, &key);
639 if (link) {
640 cdfs_node_t *node =
641 hash_table_get_inst(link, cdfs_node_t, nh_link);
642 return FS_NODE(node);
643 }
644
645 return get_uncached_node(fs, index);
646}
647
648static int cdfs_match(fs_node_t **fn, fs_node_t *pfn, const char *component)
649{
650 cdfs_node_t *parent = CDFS_NODE(pfn);
651
652 if (!parent->processed) {
653 int rc = cdfs_readdir(parent->fs, pfn);
654 if (rc != EOK)
655 return rc;
656 }
657
658 list_foreach(parent->cs_list, link, cdfs_dentry_t, dentry) {
659 if (str_cmp(dentry->name, component) == 0) {
660 *fn = get_cached_node(parent->fs, dentry->index);
661 return EOK;
662 }
663 }
664
665 *fn = NULL;
666 return EOK;
667}
668
669static int cdfs_node_open(fs_node_t *fn)
670{
671 cdfs_node_t *node = CDFS_NODE(fn);
672
673 if (!node->processed)
674 cdfs_readdir(node->fs, fn);
675
676 node->opened++;
677 return EOK;
678}
679
680static int cdfs_node_put(fs_node_t *fn)
681{
682 /* Nothing to do */
683 return EOK;
684}
685
686static int cdfs_create_node(fs_node_t **fn, service_id_t service_id, int lflag)
687{
688 /* Read-only */
689 return ENOTSUP;
690}
691
692static int cdfs_destroy_node(fs_node_t *fn)
693{
694 /* Read-only */
695 return ENOTSUP;
696}
697
698static int cdfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *name)
699{
700 /* Read-only */
701 return ENOTSUP;
702}
703
704static int cdfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *name)
705{
706 /* Read-only */
707 return ENOTSUP;
708}
709
710static int cdfs_has_children(bool *has_children, fs_node_t *fn)
711{
712 cdfs_node_t *node = CDFS_NODE(fn);
713
714 if ((node->type == CDFS_DIRECTORY) && (!node->processed))
715 cdfs_readdir(node->fs, fn);
716
717 *has_children = !list_empty(&node->cs_list);
718 return EOK;
719}
720
721static fs_index_t cdfs_index_get(fs_node_t *fn)
722{
723 cdfs_node_t *node = CDFS_NODE(fn);
724 return node->index;
725}
726
727static aoff64_t cdfs_size_get(fs_node_t *fn)
728{
729 cdfs_node_t *node = CDFS_NODE(fn);
730 return node->size;
731}
732
733static unsigned int cdfs_lnkcnt_get(fs_node_t *fn)
734{
735 cdfs_node_t *node = CDFS_NODE(fn);
736 return node->lnkcnt;
737}
738
739static bool cdfs_is_directory(fs_node_t *fn)
740{
741 cdfs_node_t *node = CDFS_NODE(fn);
742 return (node->type == CDFS_DIRECTORY);
743}
744
745static bool cdfs_is_file(fs_node_t *fn)
746{
747 cdfs_node_t *node = CDFS_NODE(fn);
748 return (node->type == CDFS_FILE);
749}
750
751static service_id_t cdfs_service_get(fs_node_t *fn)
752{
753 return 0;
754}
755
756static int cdfs_size_block(service_id_t service_id, uint32_t *size)
757{
758 *size = BLOCK_SIZE;
759
760 return EOK;
761}
762
763static int cdfs_total_block_count(service_id_t service_id, uint64_t *count)
764{
765 *count = 0;
766
767 return EOK;
768}
769
770static int cdfs_free_block_count(service_id_t service_id, uint64_t *count)
771{
772 *count = 0;
773
774 return EOK;
775}
776
777libfs_ops_t cdfs_libfs_ops = {
778 .root_get = cdfs_root_get,
779 .match = cdfs_match,
780 .node_get = cdfs_node_get,
781 .node_open = cdfs_node_open,
782 .node_put = cdfs_node_put,
783 .create = cdfs_create_node,
784 .destroy = cdfs_destroy_node,
785 .link = cdfs_link_node,
786 .unlink = cdfs_unlink_node,
787 .has_children = cdfs_has_children,
788 .index_get = cdfs_index_get,
789 .size_get = cdfs_size_get,
790 .lnkcnt_get = cdfs_lnkcnt_get,
791 .is_directory = cdfs_is_directory,
792 .is_file = cdfs_is_file,
793 .service_get = cdfs_service_get,
794 .size_block = cdfs_size_block,
795 .total_block_count = cdfs_total_block_count,
796 .free_block_count = cdfs_free_block_count
797};
798
799/** Verify that escape sequence corresonds to one of the allowed encoding
800 * escape sequences allowed for Joliet. */
801static int cdfs_verify_joliet_esc_seq(uint8_t *seq)
802{
803 size_t i, j, k;
804 bool match;
805
806 i = 0;
807 while (i + ucs2_esc_seq_len <= 32) {
808 if (seq[i] == 0)
809 break;
810
811 for (j = 0; j < ucs2_esc_seq_no; j++) {
812 match = true;
813 for (k = 0; k < ucs2_esc_seq_len; k++)
814 if (seq[i + k] != ucs2_esc_seq[j][k])
815 match = false;
816 if (match) {
817 break;
818 }
819 }
820
821 if (!match)
822 return EINVAL;
823
824 i += ucs2_esc_seq_len;
825 }
826
827 while (i < 32) {
828 if (seq[i] != 0)
829 return EINVAL;
830 ++i;
831 }
832
833 return EOK;
834}
835
836/** Find Joliet supplementary volume descriptor.
837 *
838 * @param sid Block device service ID
839 * @param altroot First filesystem block
840 * @param rlba Place to store LBA of root dir
841 * @param rsize Place to store size of root dir
842 * @return EOK if found, ENOENT if not
843 */
844static int cdfs_find_joliet_svd(service_id_t sid, cdfs_lba_t altroot,
845 uint32_t *rlba, uint32_t *rsize)
846{
847 cdfs_lba_t bi;
848
849 for (bi = altroot + 17; ; bi++) {
850 block_t *block;
851 int rc = block_get(&block, sid, bi, BLOCK_FLAGS_NONE);
852 if (rc != EOK)
853 break;
854
855 cdfs_vol_desc_t *vol_desc = (cdfs_vol_desc_t *) block->data;
856
857 if (vol_desc->type == VOL_DESC_SET_TERMINATOR) {
858 block_put(block);
859 break;
860 }
861
862 if ((vol_desc->type != VOL_DESC_SUPPLEMENTARY) ||
863 (memcmp(vol_desc->standard_ident, CDFS_STANDARD_IDENT, 5) != 0) ||
864 (vol_desc->version != 1)) {
865 block_put(block);
866 continue;
867 }
868
869 uint16_t set_size = uint16_lb(vol_desc->data.prisec.set_size);
870 if (set_size > 1) {
871 /*
872 * Technically, we don't support multi-disc sets.
873 * But one can encounter erroneously mastered
874 * images in the wild and it might actually work
875 * for the first disc in the set.
876 */
877 }
878
879 uint16_t sequence_nr = uint16_lb(vol_desc->data.prisec.sequence_nr);
880 if (sequence_nr != 1) {
881 /*
882 * We only support the first disc
883 * in multi-disc sets.
884 */
885 block_put(block);
886 continue;
887 }
888
889 uint16_t block_size = uint16_lb(vol_desc->data.prisec.block_size);
890 if (block_size != BLOCK_SIZE) {
891 block_put(block);
892 continue;
893 }
894
895 rc = cdfs_verify_joliet_esc_seq(vol_desc->data.prisec.esc_seq);
896 if (rc != EOK)
897 continue;
898 *rlba = uint32_lb(vol_desc->data.prisec.root_dir.lba);
899 *rsize = uint32_lb(vol_desc->data.prisec.root_dir.size);
900 block_put(block);
901 return EOK;
902 }
903
904 return ENOENT;
905}
906
907static bool iso_readfs(cdfs_t *fs, fs_node_t *rfn,
908 cdfs_lba_t altroot)
909{
910 /* First 16 blocks of isofs are empty */
911 block_t *block;
912 int rc = block_get(&block, fs->service_id, altroot + 16, BLOCK_FLAGS_NONE);
913 if (rc != EOK)
914 return false;
915
916 cdfs_vol_desc_t *vol_desc = (cdfs_vol_desc_t *) block->data;
917
918 /*
919 * Test for primary volume descriptor
920 * and standard compliance.
921 */
922 if ((vol_desc->type != VOL_DESC_PRIMARY) ||
923 (memcmp(vol_desc->standard_ident, CDFS_STANDARD_IDENT, 5) != 0) ||
924 (vol_desc->version != 1)) {
925 block_put(block);
926 return false;
927 }
928
929 uint16_t set_size = uint16_lb(vol_desc->data.prisec.set_size);
930 if (set_size > 1) {
931 /*
932 * Technically, we don't support multi-disc sets.
933 * But one can encounter erroneously mastered
934 * images in the wild and it might actually work
935 * for the first disc in the set.
936 */
937 }
938
939 uint16_t sequence_nr = uint16_lb(vol_desc->data.prisec.sequence_nr);
940 if (sequence_nr != 1) {
941 /*
942 * We only support the first disc
943 * in multi-disc sets.
944 */
945 block_put(block);
946 return false;
947 }
948
949 uint16_t block_size = uint16_lb(vol_desc->data.prisec.block_size);
950 if (block_size != BLOCK_SIZE) {
951 block_put(block);
952 return false;
953 }
954
955 // TODO: implement path table support
956
957 cdfs_node_t *node = CDFS_NODE(rfn);
958
959 /* Search for Joliet SVD */
960
961 uint32_t jrlba;
962 uint32_t jrsize;
963
964 rc = cdfs_find_joliet_svd(fs->service_id, altroot, &jrlba, &jrsize);
965 if (rc == EOK) {
966 /* Found */
967 node->lba = jrlba;
968 node->size = jrsize;
969 fs->enc = enc_ucs2;
970 } else {
971 node->lba = uint32_lb(vol_desc->data.prisec.root_dir.lba);
972 node->size = uint32_lb(vol_desc->data.prisec.root_dir.size);
973 fs->enc = enc_ascii;
974 }
975
976 if (!cdfs_readdir(fs, rfn)) {
977 block_put(block);
978 return false;
979 }
980
981 block_put(block);
982 return true;
983}
984
985/* Mount a session with session start offset
986 *
987 */
988static cdfs_t *cdfs_fs_create(service_id_t sid, cdfs_lba_t altroot)
989{
990 cdfs_t *fs = NULL;
991 fs_node_t *rfn = NULL;
992
993 fs = calloc(1, sizeof(cdfs_t));
994 if (fs == NULL)
995 goto error;
996
997 fs->service_id = sid;
998
999 /* Create root node */
1000 int rc = create_node(&rfn, fs, L_DIRECTORY, cdfs_index++);
1001
1002 if ((rc != EOK) || (!rfn))
1003 goto error;
1004
1005 /* FS root is not linked */
1006 CDFS_NODE(rfn)->lnkcnt = 0;
1007 CDFS_NODE(rfn)->lba = 0;
1008 CDFS_NODE(rfn)->processed = false;
1009
1010 /* Check if there is cdfs in given session */
1011 if (!iso_readfs(fs, rfn, altroot))
1012 goto error;
1013
1014 list_append(&fs->link, &cdfs_instances);
1015 return fs;
1016error:
1017 // XXX destroy node
1018 free(fs);
1019 return NULL;
1020}
1021
1022static int cdfs_mounted(service_id_t service_id, const char *opts,
1023 fs_index_t *index, aoff64_t *size, unsigned int *lnkcnt)
1024{
1025 /* Initialize the block layer */
1026 int rc = block_init(EXCHANGE_SERIALIZE, service_id, BLOCK_SIZE);
1027 if (rc != EOK)
1028 return rc;
1029
1030 cdfs_lba_t altroot = 0;
1031
1032 if (str_lcmp(opts, "altroot=", 8) == 0) {
1033 /* User-defined alternative root on a multi-session disk */
1034 if (str_uint32_t(opts + 8, NULL, 0, false, &altroot) != EOK)
1035 altroot = 0;
1036 } else {
1037 /* Read TOC and find the last session */
1038 toc_block_t *toc = block_get_toc(service_id, 1);
1039 if ((toc != NULL) && (uint16_t_be2host(toc->size) == 10)) {
1040 altroot = uint32_t_be2host(toc->first_lba);
1041 free(toc);
1042 }
1043 }
1044
1045 /* Initialize the block cache */
1046 rc = block_cache_init(service_id, BLOCK_SIZE, 0, CACHE_MODE_WT);
1047 if (rc != EOK) {
1048 block_fini(service_id);
1049 return rc;
1050 }
1051
1052 /* Check if this device is not already mounted */
1053 fs_node_t *rootfn;
1054 rc = cdfs_root_get(&rootfn, service_id);
1055 if ((rc == EOK) && (rootfn)) {
1056 cdfs_node_put(rootfn);
1057 block_cache_fini(service_id);
1058 block_fini(service_id);
1059
1060 return EEXIST;
1061 }
1062
1063 /* Create cdfs instance */
1064 if (cdfs_fs_create(service_id, altroot) == NULL) {
1065 block_cache_fini(service_id);
1066 block_fini(service_id);
1067
1068 return ENOMEM;
1069 }
1070
1071 rc = cdfs_root_get(&rootfn, service_id);
1072 assert(rc == EOK);
1073
1074 cdfs_node_t *root = CDFS_NODE(rootfn);
1075 *index = root->index;
1076 *size = root->size;
1077 *lnkcnt = root->lnkcnt;
1078
1079 return EOK;
1080}
1081
1082static bool rm_service_id_nodes(ht_link_t *item, void *arg)
1083{
1084 service_id_t service_id = *(service_id_t*)arg;
1085 cdfs_node_t *node = hash_table_get_inst(item, cdfs_node_t, nh_link);
1086
1087 if (node->fs->service_id == service_id) {
1088 hash_table_remove_item(&nodes, &node->nh_link);
1089 }
1090
1091 return true;
1092}
1093
1094static void cdfs_fs_destroy(cdfs_t *fs)
1095{
1096 list_remove(&fs->link);
1097 hash_table_apply(&nodes, rm_service_id_nodes, &fs->service_id);
1098 block_cache_fini(fs->service_id);
1099 block_fini(fs->service_id);
1100 free(fs);
1101}
1102
1103static cdfs_t *cdfs_find_by_sid(service_id_t service_id)
1104{
1105 list_foreach(cdfs_instances, link, cdfs_t, fs) {
1106 if (fs->service_id == service_id)
1107 return fs;
1108 }
1109
1110 return NULL;
1111}
1112
1113static int cdfs_unmounted(service_id_t service_id)
1114{
1115 cdfs_t *fs;
1116
1117 fs = cdfs_find_by_sid(service_id);
1118 if (fs == NULL)
1119 return ENOENT;
1120
1121 cdfs_fs_destroy(fs);
1122 return EOK;
1123}
1124
1125static int cdfs_read(service_id_t service_id, fs_index_t index, aoff64_t pos,
1126 size_t *rbytes)
1127{
1128 ht_key_t key = {
1129 .index = index,
1130 .service_id = service_id
1131 };
1132
1133 ht_link_t *link = hash_table_find(&nodes, &key);
1134 if (link == NULL)
1135 return ENOENT;
1136
1137 cdfs_node_t *node =
1138 hash_table_get_inst(link, cdfs_node_t, nh_link);
1139
1140 if (!node->processed) {
1141 int rc = cdfs_readdir(node->fs, FS_NODE(node));
1142 if (rc != EOK)
1143 return rc;
1144 }
1145
1146 ipc_callid_t callid;
1147 size_t len;
1148 if (!async_data_read_receive(&callid, &len)) {
1149 async_answer_0(callid, EINVAL);
1150 return EINVAL;
1151 }
1152
1153 if (node->type == CDFS_FILE) {
1154 if (pos >= node->size) {
1155 *rbytes = 0;
1156 async_data_read_finalize(callid, NULL, 0);
1157 } else {
1158 cdfs_lba_t lba = pos / BLOCK_SIZE;
1159 size_t offset = pos % BLOCK_SIZE;
1160
1161 *rbytes = min(len, BLOCK_SIZE - offset);
1162 *rbytes = min(*rbytes, node->size - pos);
1163
1164 block_t *block;
1165 int rc = block_get(&block, service_id, node->lba + lba,
1166 BLOCK_FLAGS_NONE);
1167 if (rc != EOK) {
1168 async_answer_0(callid, rc);
1169 return rc;
1170 }
1171
1172 async_data_read_finalize(callid, block->data + offset,
1173 *rbytes);
1174 rc = block_put(block);
1175 if (rc != EOK)
1176 return rc;
1177 }
1178 } else {
1179 link_t *link = list_nth(&node->cs_list, pos);
1180 if (link == NULL) {
1181 async_answer_0(callid, ENOENT);
1182 return ENOENT;
1183 }
1184
1185 cdfs_dentry_t *dentry =
1186 list_get_instance(link, cdfs_dentry_t, link);
1187
1188 *rbytes = 1;
1189 async_data_read_finalize(callid, dentry->name,
1190 str_size(dentry->name) + 1);
1191 }
1192
1193 return EOK;
1194}
1195
1196static int cdfs_write(service_id_t service_id, fs_index_t index, aoff64_t pos,
1197 size_t *wbytes, aoff64_t *nsize)
1198{
1199 /*
1200 * As cdfs is a read-only filesystem,
1201 * the operation is not supported.
1202 */
1203
1204 return ENOTSUP;
1205}
1206
1207static int cdfs_truncate(service_id_t service_id, fs_index_t index,
1208 aoff64_t size)
1209{
1210 /*
1211 * As cdfs is a read-only filesystem,
1212 * the operation is not supported.
1213 */
1214
1215 return ENOTSUP;
1216}
1217
1218static bool cache_remove_closed(ht_link_t *item, void *arg)
1219{
1220 size_t *premove_cnt = (size_t*)arg;
1221
1222 /* Some nodes were requested to be removed from the cache. */
1223 if (0 < *premove_cnt) {
1224 cdfs_node_t *node = hash_table_get_inst(item, cdfs_node_t, nh_link);
1225
1226 if (!node->opened) {
1227 hash_table_remove_item(&nodes, item);
1228
1229 --nodes_cached;
1230 --*premove_cnt;
1231 }
1232 }
1233
1234 /* Only continue if more nodes were requested to be removed. */
1235 return 0 < *premove_cnt;
1236}
1237
1238static void cleanup_cache(service_id_t service_id)
1239{
1240 if (nodes_cached > NODE_CACHE_SIZE) {
1241 size_t remove_cnt = nodes_cached - NODE_CACHE_SIZE;
1242
1243 if (0 < remove_cnt)
1244 hash_table_apply(&nodes, cache_remove_closed, &remove_cnt);
1245 }
1246}
1247
1248static int cdfs_close(service_id_t service_id, fs_index_t index)
1249{
1250 /* Root node is always in memory */
1251 if (index == 0)
1252 return EOK;
1253
1254 ht_key_t key = {
1255 .index = index,
1256 .service_id = service_id
1257 };
1258
1259 ht_link_t *link = hash_table_find(&nodes, &key);
1260 if (link == 0)
1261 return ENOENT;
1262
1263 cdfs_node_t *node =
1264 hash_table_get_inst(link, cdfs_node_t, nh_link);
1265
1266 assert(node->opened > 0);
1267
1268 node->opened--;
1269 cleanup_cache(service_id);
1270
1271 return EOK;
1272}
1273
1274static int cdfs_destroy(service_id_t service_id, fs_index_t index)
1275{
1276 /*
1277 * As cdfs is a read-only filesystem,
1278 * the operation is not supported.
1279 */
1280
1281 return ENOTSUP;
1282}
1283
1284static int cdfs_sync(service_id_t service_id, fs_index_t index)
1285{
1286 /*
1287 * As cdfs is a read-only filesystem,
1288 * the sync operation is a no-op.
1289 */
1290
1291 return EOK;
1292}
1293
1294vfs_out_ops_t cdfs_ops = {
1295 .mounted = cdfs_mounted,
1296 .unmounted = cdfs_unmounted,
1297 .read = cdfs_read,
1298 .write = cdfs_write,
1299 .truncate = cdfs_truncate,
1300 .close = cdfs_close,
1301 .destroy = cdfs_destroy,
1302 .sync = cdfs_sync
1303};
1304
1305/** Initialize the cdfs server
1306 *
1307 */
1308bool cdfs_init(void)
1309{
1310 if (!hash_table_create(&nodes, 0, 0, &nodes_ops))
1311 return false;
1312
1313 return true;
1314}
1315
1316/**
1317 * @}
1318 */
Note: See TracBrowser for help on using the repository browser.