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

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

Probing for ExFAT, Ext4 and CDFS.

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