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

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

Selected ccheck-proposed comment fixes.

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