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

Last change on this file since 7ae01d5 was 7ae01d5, checked in by Jiri Svoboda <jiri@…>, 14 months ago

Remove unused comm_size parameter of block_init()

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