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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4f30222 was 4f30222, checked in by Jakub Jermar <jakub@…>, 8 years ago

VFS_OUT_MOUNTED does not need to return the link count

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