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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1558d85 was fc22069, checked in by Martin Decky <martin@…>, 10 years ago

block devices use the same interface, therefore the API of libblock should not expose the implementation details

  • Property mode set to 100644
File size: 28.1 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
908static bool iso_readfs(cdfs_t *fs, fs_node_t *rfn,
909 cdfs_lba_t altroot)
910{
911 /* First 16 blocks of isofs are empty */
912 block_t *block;
913 int rc = block_get(&block, fs->service_id, altroot + 16, BLOCK_FLAGS_NONE);
914 if (rc != EOK)
915 return false;
916
917 cdfs_vol_desc_t *vol_desc = (cdfs_vol_desc_t *) block->data;
918
919 /*
920 * Test for primary volume descriptor
921 * and standard compliance.
922 */
923 if ((vol_desc->type != VOL_DESC_PRIMARY) ||
924 (memcmp(vol_desc->standard_ident, CDFS_STANDARD_IDENT, 5) != 0) ||
925 (vol_desc->version != 1)) {
926 block_put(block);
927 return false;
928 }
929
930 uint16_t set_size = uint16_lb(vol_desc->data.prisec.set_size);
931 if (set_size > 1) {
932 /*
933 * Technically, we don't support multi-disc sets.
934 * But one can encounter erroneously mastered
935 * images in the wild and it might actually work
936 * for the first disc in the set.
937 */
938 }
939
940 uint16_t sequence_nr = uint16_lb(vol_desc->data.prisec.sequence_nr);
941 if (sequence_nr != 1) {
942 /*
943 * We only support the first disc
944 * in multi-disc sets.
945 */
946 block_put(block);
947 return false;
948 }
949
950 uint16_t block_size = uint16_lb(vol_desc->data.prisec.block_size);
951 if (block_size != BLOCK_SIZE) {
952 block_put(block);
953 return false;
954 }
955
956 // TODO: implement path table support
957
958 cdfs_node_t *node = CDFS_NODE(rfn);
959
960 /* Search for Joliet SVD */
961
962 uint32_t jrlba;
963 uint32_t jrsize;
964
965 rc = cdfs_find_joliet_svd(fs->service_id, altroot, &jrlba, &jrsize);
966 if (rc == EOK) {
967 /* Found */
968 node->lba = jrlba;
969 node->size = jrsize;
970 fs->enc = enc_ucs2;
971 } else {
972 node->lba = uint32_lb(vol_desc->data.prisec.root_dir.lba);
973 node->size = uint32_lb(vol_desc->data.prisec.root_dir.size);
974 fs->enc = enc_ascii;
975 }
976
977 if (!cdfs_readdir(fs, rfn)) {
978 block_put(block);
979 return false;
980 }
981
982 block_put(block);
983 return true;
984}
985
986/* Mount a session with session start offset
987 *
988 */
989static cdfs_t *cdfs_fs_create(service_id_t sid, cdfs_lba_t altroot)
990{
991 cdfs_t *fs = NULL;
992 fs_node_t *rfn = NULL;
993
994 fs = calloc(1, sizeof(cdfs_t));
995 if (fs == NULL)
996 goto error;
997
998 fs->service_id = sid;
999
1000 /* Create root node */
1001 int rc = create_node(&rfn, fs, L_DIRECTORY, cdfs_index++);
1002
1003 if ((rc != EOK) || (!rfn))
1004 goto error;
1005
1006 /* FS root is not linked */
1007 CDFS_NODE(rfn)->lnkcnt = 0;
1008 CDFS_NODE(rfn)->lba = 0;
1009 CDFS_NODE(rfn)->processed = false;
1010
1011 /* Check if there is cdfs in given session */
1012 if (!iso_readfs(fs, rfn, altroot))
1013 goto error;
1014
1015 list_append(&fs->link, &cdfs_instances);
1016 return fs;
1017error:
1018 // XXX destroy node
1019 free(fs);
1020 return NULL;
1021}
1022
1023static int cdfs_mounted(service_id_t service_id, const char *opts,
1024 fs_index_t *index, aoff64_t *size, unsigned int *lnkcnt)
1025{
1026 /* Initialize the block layer */
1027 int rc = block_init(service_id, BLOCK_SIZE);
1028 if (rc != EOK)
1029 return rc;
1030
1031 cdfs_lba_t altroot = 0;
1032
1033 if (str_lcmp(opts, "altroot=", 8) == 0) {
1034 /* User-defined alternative root on a multi-session disk */
1035 if (str_uint32_t(opts + 8, NULL, 0, false, &altroot) != EOK)
1036 altroot = 0;
1037 } else {
1038 /*
1039 * Read TOC multisession information and get the start address
1040 * of the first track in the last session
1041 */
1042 scsi_toc_multisess_data_t toc;
1043
1044 rc = block_read_toc(service_id, 1, &toc, sizeof(toc));
1045 if (rc == EOK && (uint16_t_be2host(toc.toc_len) == 10))
1046 altroot = uint32_t_be2host(toc.ftrack_lsess.start_addr);
1047 }
1048
1049 /* Initialize the block cache */
1050 rc = block_cache_init(service_id, BLOCK_SIZE, 0, CACHE_MODE_WT);
1051 if (rc != EOK) {
1052 block_fini(service_id);
1053 return rc;
1054 }
1055
1056 /* Check if this device is not already mounted */
1057 fs_node_t *rootfn;
1058 rc = cdfs_root_get(&rootfn, service_id);
1059 if ((rc == EOK) && (rootfn)) {
1060 cdfs_node_put(rootfn);
1061 block_cache_fini(service_id);
1062 block_fini(service_id);
1063
1064 return EEXIST;
1065 }
1066
1067 /* Create cdfs instance */
1068 if (cdfs_fs_create(service_id, altroot) == NULL) {
1069 block_cache_fini(service_id);
1070 block_fini(service_id);
1071
1072 return ENOMEM;
1073 }
1074
1075 rc = cdfs_root_get(&rootfn, service_id);
1076 assert(rc == EOK);
1077
1078 cdfs_node_t *root = CDFS_NODE(rootfn);
1079 *index = root->index;
1080 *size = root->size;
1081 *lnkcnt = root->lnkcnt;
1082
1083 return EOK;
1084}
1085
1086static bool rm_service_id_nodes(ht_link_t *item, void *arg)
1087{
1088 service_id_t service_id = *(service_id_t*)arg;
1089 cdfs_node_t *node = hash_table_get_inst(item, cdfs_node_t, nh_link);
1090
1091 if (node->fs->service_id == service_id) {
1092 hash_table_remove_item(&nodes, &node->nh_link);
1093 }
1094
1095 return true;
1096}
1097
1098static void cdfs_fs_destroy(cdfs_t *fs)
1099{
1100 list_remove(&fs->link);
1101 hash_table_apply(&nodes, rm_service_id_nodes, &fs->service_id);
1102 block_cache_fini(fs->service_id);
1103 block_fini(fs->service_id);
1104 free(fs);
1105}
1106
1107static cdfs_t *cdfs_find_by_sid(service_id_t service_id)
1108{
1109 list_foreach(cdfs_instances, link, cdfs_t, fs) {
1110 if (fs->service_id == service_id)
1111 return fs;
1112 }
1113
1114 return NULL;
1115}
1116
1117static int cdfs_unmounted(service_id_t service_id)
1118{
1119 cdfs_t *fs;
1120
1121 fs = cdfs_find_by_sid(service_id);
1122 if (fs == NULL)
1123 return ENOENT;
1124
1125 cdfs_fs_destroy(fs);
1126 return EOK;
1127}
1128
1129static int cdfs_read(service_id_t service_id, fs_index_t index, aoff64_t pos,
1130 size_t *rbytes)
1131{
1132 ht_key_t key = {
1133 .index = index,
1134 .service_id = service_id
1135 };
1136
1137 ht_link_t *link = hash_table_find(&nodes, &key);
1138 if (link == NULL)
1139 return ENOENT;
1140
1141 cdfs_node_t *node =
1142 hash_table_get_inst(link, cdfs_node_t, nh_link);
1143
1144 if (!node->processed) {
1145 int rc = cdfs_readdir(node->fs, FS_NODE(node));
1146 if (rc != EOK)
1147 return rc;
1148 }
1149
1150 ipc_callid_t callid;
1151 size_t len;
1152 if (!async_data_read_receive(&callid, &len)) {
1153 async_answer_0(callid, EINVAL);
1154 return EINVAL;
1155 }
1156
1157 if (node->type == CDFS_FILE) {
1158 if (pos >= node->size) {
1159 *rbytes = 0;
1160 async_data_read_finalize(callid, NULL, 0);
1161 } else {
1162 cdfs_lba_t lba = pos / BLOCK_SIZE;
1163 size_t offset = pos % BLOCK_SIZE;
1164
1165 *rbytes = min(len, BLOCK_SIZE - offset);
1166 *rbytes = min(*rbytes, node->size - pos);
1167
1168 block_t *block;
1169 int rc = block_get(&block, service_id, node->lba + lba,
1170 BLOCK_FLAGS_NONE);
1171 if (rc != EOK) {
1172 async_answer_0(callid, rc);
1173 return rc;
1174 }
1175
1176 async_data_read_finalize(callid, block->data + offset,
1177 *rbytes);
1178 rc = block_put(block);
1179 if (rc != EOK)
1180 return rc;
1181 }
1182 } else {
1183 link_t *link = list_nth(&node->cs_list, pos);
1184 if (link == NULL) {
1185 async_answer_0(callid, ENOENT);
1186 return ENOENT;
1187 }
1188
1189 cdfs_dentry_t *dentry =
1190 list_get_instance(link, cdfs_dentry_t, link);
1191
1192 *rbytes = 1;
1193 async_data_read_finalize(callid, dentry->name,
1194 str_size(dentry->name) + 1);
1195 }
1196
1197 return EOK;
1198}
1199
1200static int cdfs_write(service_id_t service_id, fs_index_t index, aoff64_t pos,
1201 size_t *wbytes, aoff64_t *nsize)
1202{
1203 /*
1204 * As cdfs is a read-only filesystem,
1205 * the operation is not supported.
1206 */
1207
1208 return ENOTSUP;
1209}
1210
1211static int cdfs_truncate(service_id_t service_id, fs_index_t index,
1212 aoff64_t size)
1213{
1214 /*
1215 * As cdfs is a read-only filesystem,
1216 * the operation is not supported.
1217 */
1218
1219 return ENOTSUP;
1220}
1221
1222static bool cache_remove_closed(ht_link_t *item, void *arg)
1223{
1224 size_t *premove_cnt = (size_t*)arg;
1225
1226 /* Some nodes were requested to be removed from the cache. */
1227 if (0 < *premove_cnt) {
1228 cdfs_node_t *node = hash_table_get_inst(item, cdfs_node_t, nh_link);
1229
1230 if (!node->opened) {
1231 hash_table_remove_item(&nodes, item);
1232
1233 --nodes_cached;
1234 --*premove_cnt;
1235 }
1236 }
1237
1238 /* Only continue if more nodes were requested to be removed. */
1239 return 0 < *premove_cnt;
1240}
1241
1242static void cleanup_cache(service_id_t service_id)
1243{
1244 if (nodes_cached > NODE_CACHE_SIZE) {
1245 size_t remove_cnt = nodes_cached - NODE_CACHE_SIZE;
1246
1247 if (0 < remove_cnt)
1248 hash_table_apply(&nodes, cache_remove_closed, &remove_cnt);
1249 }
1250}
1251
1252static int cdfs_close(service_id_t service_id, fs_index_t index)
1253{
1254 /* Root node is always in memory */
1255 if (index == 0)
1256 return EOK;
1257
1258 ht_key_t key = {
1259 .index = index,
1260 .service_id = service_id
1261 };
1262
1263 ht_link_t *link = hash_table_find(&nodes, &key);
1264 if (link == 0)
1265 return ENOENT;
1266
1267 cdfs_node_t *node =
1268 hash_table_get_inst(link, cdfs_node_t, nh_link);
1269
1270 assert(node->opened > 0);
1271
1272 node->opened--;
1273 cleanup_cache(service_id);
1274
1275 return EOK;
1276}
1277
1278static int cdfs_destroy(service_id_t service_id, fs_index_t index)
1279{
1280 /*
1281 * As cdfs is a read-only filesystem,
1282 * the operation is not supported.
1283 */
1284
1285 return ENOTSUP;
1286}
1287
1288static int cdfs_sync(service_id_t service_id, fs_index_t index)
1289{
1290 /*
1291 * As cdfs is a read-only filesystem,
1292 * the sync operation is a no-op.
1293 */
1294
1295 return EOK;
1296}
1297
1298vfs_out_ops_t cdfs_ops = {
1299 .mounted = cdfs_mounted,
1300 .unmounted = cdfs_unmounted,
1301 .read = cdfs_read,
1302 .write = cdfs_write,
1303 .truncate = cdfs_truncate,
1304 .close = cdfs_close,
1305 .destroy = cdfs_destroy,
1306 .sync = cdfs_sync
1307};
1308
1309/** Initialize the cdfs server
1310 *
1311 */
1312bool cdfs_init(void)
1313{
1314 if (!hash_table_create(&nodes, 0, 0, &nodes_ops))
1315 return false;
1316
1317 return true;
1318}
1319
1320/**
1321 * @}
1322 */
Note: See TracBrowser for help on using the repository browser.