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

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

Fix CDFS unaligned memory access when decoding Joliet file names.

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