source: mainline/uspace/srv/fs/cdfs/cdfs_ops.c

Last change on this file was 0db0df2, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 months ago

Hash table improvements

Implement hash_table_foreach macro, analogous to list_foreach.

Remove superfluous argument to hash_table_find_next().
(If the user needs to recheck the part of the list already
checked by hash_table_find(), they can just rerun that function.)

Add hash argument to hash_table_ops_t::key_equal.
The big change here is that users with big keys can store the hash
value alongside key in their entries, and for the low low cost of
sizeof(size_t) bytes eliminate a bunch of expensive key comparisons.

Also added a hash function for strings and arbitrary data.
Found this one by asking ChatGPT, because the latency of accesses
to my book collection is currently a couple of hours.

+ Some drive-by unused #include removal.

  • Property mode set to 100644
File size: 30.9 KB
RevLine 
[a26895d]1/*
[7ae01d5]2 * Copyright (c) 2024 Jiri Svoboda
[a26895d]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
[b1834a01]30/** @addtogroup cdfs
[a26895d]31 * @{
32 */
33
34/**
35 * @file cdfs_ops.c
36 * @brief Implementation of VFS operations for the cdfs file system
37 * server.
38 */
39
[062d900]40#include "cdfs_ops.h"
[3e6a98c5]41#include <stdbool.h>
[0003e0f5]42#include <adt/list.h>
[a26895d]43#include <adt/hash_table.h>
[062d900]44#include <adt/hash.h>
[a26895d]45#include <mem.h>
46#include <loc.h>
47#include <libfs.h>
48#include <errno.h>
[f73b291]49#include <block.h>
[3abf70c7]50#include <scsi/mmc.h>
[38d150e]51#include <stdlib.h>
[a26895d]52#include <str.h>
53#include <byteorder.h>
54#include <macros.h>
[1c88835]55#include <unaligned.h>
56
[a26895d]57#include "cdfs.h"
58#include "cdfs_endian.h"
59
60/** Standard CD-ROM block size */
61#define BLOCK_SIZE 2048
62
[062d900]63#define NODE_CACHE_SIZE 200
[a26895d]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
[8e77b12f]73enum {
74 CDFS_NAME_CURDIR = '\x00',
75 CDFS_NAME_PARENTDIR = '\x01'
76};
77
[a26895d]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];
[a35b458]95
[a26895d]96 uint8_t hour[2];
97 uint8_t min[2];
98 uint8_t sec[2];
99 uint8_t msec[2];
[a35b458]100
[a26895d]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;
[a35b458]108
[a26895d]109 uint8_t hour;
110 uint8_t min;
111 uint8_t sec;
[a35b458]112
[a26895d]113 uint8_t offset;
114} __attribute__((packed)) cdfs_timestamp_t;
115
116typedef enum {
117 DIR_FLAG_DIRECTORY = 2
118} cdfs_dir_flag_t;
119
[0cebbac]120/** Directory record */
[a26895d]121typedef struct {
122 uint8_t length;
123 uint8_t ea_length;
[a35b458]124
[a26895d]125 uint32_t_lb lba;
126 uint32_t_lb size;
[a35b458]127
[a26895d]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;
[a35b458]133
[a26895d]134 uint8_t name_length;
135 uint8_t name[];
136} __attribute__((packed)) cdfs_dir_t;
137
[0cebbac]138/** Directory record for the root directory */
139typedef struct {
140 uint8_t length;
141 uint8_t ea_length;
[a35b458]142
[0cebbac]143 uint32_t_lb lba;
144 uint32_t_lb size;
[a35b458]145
[0cebbac]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;
[a35b458]151
[0cebbac]152 uint8_t name_length;
153 uint8_t name[1];
154} __attribute__((packed)) cdfs_root_dir_t;
155
[a26895d]156typedef struct {
[993d608]157 uint8_t flags; /* reserved in primary */
[a35b458]158
[a26895d]159 uint8_t system_ident[32];
160 uint8_t ident[32];
[a35b458]161
[a26895d]162 uint64_t res1;
163 uint32_t_lb lba_size;
[a35b458]164
[993d608]165 uint8_t esc_seq[32]; /* reserved in primary */
[a26895d]166 uint16_t_lb set_size;
167 uint16_t_lb sequence_nr;
[a35b458]168
[a26895d]169 uint16_t_lb block_size;
170 uint32_t_lb path_table_size;
[a35b458]171
[a26895d]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;
[a35b458]176
[0cebbac]177 cdfs_root_dir_t root_dir;
178 uint8_t pad0;
[a35b458]179
[0cebbac]180 uint8_t set_ident[128];
181 uint8_t publisher_ident[128];
182 uint8_t preparer_ident[128];
183 uint8_t app_ident[128];
[a35b458]184
[0cebbac]185 uint8_t copyright_file_ident[37];
186 uint8_t abstract_file_ident[37];
187 uint8_t biblio_file_ident[37];
[a35b458]188
[0cebbac]189 cdfs_datetime_t creation;
190 cdfs_datetime_t modification;
191 cdfs_datetime_t expiration;
192 cdfs_datetime_t effective;
[a35b458]193
[0cebbac]194 uint8_t fs_version;
[993d608]195} __attribute__((packed)) cdfs_vol_desc_prisec_t;
[a26895d]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;
[993d608]203 cdfs_vol_desc_prisec_t prisec;
[a26895d]204 } data;
205} __attribute__((packed)) cdfs_vol_desc_t;
206
[993d608]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
[a26895d]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
[0003e0f5]228typedef struct {
[993d608]229 link_t link; /**< Link to list of all instances */
[0003e0f5]230 service_id_t service_id; /**< Service ID of block device */
[993d608]231 cdfs_enc_t enc; /**< Filesystem string encoding */
[9c96634]232 char *vol_ident; /**< Volume identifier */
[0003e0f5]233} cdfs_t;
234
[a26895d]235typedef struct {
236 fs_node_t *fs_node; /**< FS node */
237 fs_index_t index; /**< Node index */
[0003e0f5]238 cdfs_t *fs; /**< File system */
[a35b458]239
[062d900]240 ht_link_t nh_link; /**< Nodes hash table link */
[a26895d]241 cdfs_dentry_type_t type; /**< Dentry type */
[a35b458]242
[a26895d]243 unsigned int lnkcnt; /**< Link count */
244 uint32_t size; /**< File size if type is CDFS_FILE */
[a35b458]245
[a26895d]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
[993d608]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
[0003e0f5]267/** List of all instances */
268static LIST_INITIALIZE(cdfs_instances);
269
[a26895d]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
[1b20da0]279/*
[062d900]280 * Hash table support functions.
281 */
282
283typedef struct {
284 service_id_t service_id;
[3bacee1]285 fs_index_t index;
[062d900]286} ht_key_t;
287
[5e801dc]288static size_t nodes_key_hash(const void *k)
[a26895d]289{
[5e801dc]290 const ht_key_t *key = k;
[062d900]291 return hash_combine(key->service_id, key->index);
[a26895d]292}
293
[062d900]294static size_t nodes_hash(const ht_link_t *item)
[a26895d]295{
[062d900]296 cdfs_node_t *node = hash_table_get_inst(item, cdfs_node_t, nh_link);
[0003e0f5]297 return hash_combine(node->fs->service_id, node->index);
[062d900]298}
299
[0db0df2]300static bool nodes_key_equal(const void *k, size_t hash, const ht_link_t *item)
[062d900]301{
302 cdfs_node_t *node = hash_table_get_inst(item, cdfs_node_t, nh_link);
[5e801dc]303 const ht_key_t *key = k;
[a35b458]304
[0003e0f5]305 return key->service_id == node->fs->service_id && key->index == node->index;
[a26895d]306}
307
[062d900]308static void nodes_remove_callback(ht_link_t *item)
[a26895d]309{
[062d900]310 cdfs_node_t *node = hash_table_get_inst(item, cdfs_node_t, nh_link);
[a35b458]311
[251d4dd]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 }
[a26895d]319 }
[a35b458]320
[a26895d]321 free(node->fs_node);
322 free(node);
323}
324
325/** Nodes hash table operations */
[61eb2ce2]326static const hash_table_ops_t nodes_ops = {
[a26895d]327 .hash = nodes_hash,
[062d900]328 .key_hash = nodes_key_hash,
329 .key_equal = nodes_key_equal,
[4e00f87]330 .equal = NULL,
[a26895d]331 .remove_callback = nodes_remove_callback
332};
333
[b7fd2a0]334static errno_t cdfs_node_get(fs_node_t **rfn, service_id_t service_id,
[a26895d]335 fs_index_t index)
336{
[062d900]337 ht_key_t key = {
338 .index = index,
339 .service_id = service_id
[a26895d]340 };
[a35b458]341
[062d900]342 ht_link_t *link = hash_table_find(&nodes, &key);
[a26895d]343 if (link) {
344 cdfs_node_t *node =
[062d900]345 hash_table_get_inst(link, cdfs_node_t, nh_link);
[a35b458]346
[a26895d]347 *rfn = FS_NODE(node);
348 } else
349 *rfn = NULL;
[a35b458]350
[a26895d]351 return EOK;
352}
353
[b7fd2a0]354static errno_t cdfs_root_get(fs_node_t **rfn, service_id_t service_id)
[a26895d]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;
[0003e0f5]363 node->fs = NULL;
[a26895d]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;
[a35b458]370
[a26895d]371 list_initialize(&node->cs_list);
372}
373
[b7fd2a0]374static errno_t create_node(fs_node_t **rfn, cdfs_t *fs, int lflag,
[a26895d]375 fs_index_t index)
376{
377 assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
[a35b458]378
[a26895d]379 cdfs_node_t *node = malloc(sizeof(cdfs_node_t));
380 if (!node)
381 return ENOMEM;
[a35b458]382
[a26895d]383 cdfs_node_initialize(node);
[a35b458]384
[a26895d]385 node->fs_node = malloc(sizeof(fs_node_t));
386 if (!node->fs_node) {
387 free(node);
388 return ENOMEM;
389 }
[a35b458]390
[a26895d]391 fs_node_initialize(node->fs_node);
392 node->fs_node->data = node;
[a35b458]393
[a26895d]394 fs_node_t *rootfn;
[b7fd2a0]395 errno_t rc = cdfs_root_get(&rootfn, fs->service_id);
[a35b458]396
[a26895d]397 assert(rc == EOK);
[a35b458]398
[a26895d]399 if (!rootfn)
400 node->index = CDFS_SOME_ROOT;
401 else
402 node->index = index;
[a35b458]403
[0003e0f5]404 node->fs = fs;
[a35b458]405
[a26895d]406 if (lflag & L_DIRECTORY)
407 node->type = CDFS_DIRECTORY;
408 else
409 node->type = CDFS_FILE;
[a35b458]410
[a26895d]411 /* Insert the new node into the nodes hash table. */
[062d900]412 hash_table_insert(&nodes, &node->nh_link);
[a35b458]413
[a26895d]414 *rfn = FS_NODE(node);
415 nodes_cached++;
[a35b458]416
[a26895d]417 return EOK;
418}
419
[b7fd2a0]420static errno_t link_node(fs_node_t *pfn, fs_node_t *fn, const char *name)
[a26895d]421{
422 cdfs_node_t *parent = CDFS_NODE(pfn);
423 cdfs_node_t *node = CDFS_NODE(fn);
[a35b458]424
[a26895d]425 assert(parent->type == CDFS_DIRECTORY);
[a35b458]426
[a26895d]427 /* Check for duplicate entries */
[feeac0d]428 list_foreach(parent->cs_list, link, cdfs_dentry_t, dentry) {
[a26895d]429 if (str_cmp(dentry->name, name) == 0)
430 return EEXIST;
431 }
[a35b458]432
[a26895d]433 /* Allocate and initialize the dentry */
434 cdfs_dentry_t *dentry = malloc(sizeof(cdfs_dentry_t));
435 if (!dentry)
436 return ENOMEM;
[a35b458]437
[a26895d]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 }
[a35b458]444
[a26895d]445 link_initialize(&dentry->link);
446 dentry->index = node->index;
[a35b458]447
[a26895d]448 node->lnkcnt++;
449 list_append(&dentry->link, &parent->cs_list);
[a35b458]450
[a26895d]451 return EOK;
452}
453
[993d608]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{
[b7fd2a0]463 errno_t rc;
[993d608]464 char *str;
465 uint16_t *buf;
[a35b458]466
[993d608]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;
[a35b458]479
[993d608]480 size_t i;
481 for (i = 0; i < dsize / sizeof(uint16_t); i++) {
[1c88835]482 buf[i] = uint16_t_be2host(
483 ((unaligned_uint16_t *)data)[i]);
[993d608]484 }
[a35b458]485
[993d608]486 size_t dstr_size = dsize / sizeof(uint16_t) * 4 + 1;
487 str = malloc(dstr_size);
488 if (str == NULL)
489 return NULL;
[a35b458]490
[993d608]491 rc = utf16_to_str(str, dstr_size, buf);
492 free(buf);
[a35b458]493
[993d608]494 if (rc != EOK)
495 return NULL;
496 break;
497 default:
498 assert(false);
499 str = NULL;
500 }
[a35b458]501
[993d608]502 return str;
503}
504
[d4b63fa]505/** Decode file name.
506 *
507 * @param data File name buffer
508 * @param dsize Fine name buffer size
[9c96634]509 * @param enc Encoding
[d4b63fa]510 * @param dtype Directory entry type
511 * @return Decoded file name (allocated string)
512 */
[993d608]513static char *cdfs_decode_name(void *data, size_t dsize, cdfs_enc_t enc,
[d4b63fa]514 cdfs_dentry_type_t dtype)
515{
516 char *name;
517 char *dot;
518 char *scolon;
[a35b458]519
[993d608]520 name = cdfs_decode_str(data, dsize, enc);
[d4b63fa]521 if (name == NULL)
522 return NULL;
[a35b458]523
[d4b63fa]524 if (dtype == CDFS_DIRECTORY)
525 return name;
[a35b458]526
[d4b63fa]527 dot = str_chr(name, '.');
[a35b458]528
[993d608]529 if (dot != NULL) {
530 scolon = str_chr(dot, ';');
531 if (scolon != NULL) {
532 /* Trim version part */
533 *scolon = '\0';
534 }
[a35b458]535
[993d608]536 /* If the extension is an empty string, trim the dot separator. */
537 if (dot[1] == '\0')
538 *dot = '\0';
539 }
[a35b458]540
[d4b63fa]541 return name;
542}
543
[9c96634]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;
[a35b458]555
[9c96634]556 ident = cdfs_decode_str(data, dsize, enc);
557 if (ident == NULL)
558 return NULL;
[a35b458]559
[9c96634]560 /* Trim trailing spaces */
561 i = str_size(ident);
562 while (i > 0 && ident[i - 1] == ' ')
563 --i;
564 ident[i] = '\0';
[a35b458]565
[9c96634]566 return ident;
567}
568
[b7fd2a0]569static errno_t cdfs_readdir(cdfs_t *fs, fs_node_t *fs_node)
[a26895d]570{
571 cdfs_node_t *node = CDFS_NODE(fs_node);
572 assert(node);
[a35b458]573
[a26895d]574 if (node->processed)
[a8c7a6d]575 return EOK;
[a35b458]576
[a26895d]577 uint32_t blocks = node->size / BLOCK_SIZE;
578 if ((node->size % BLOCK_SIZE) != 0)
579 blocks++;
[a35b458]580
[a26895d]581 for (uint32_t i = 0; i < blocks; i++) {
582 block_t *block;
[b7fd2a0]583 errno_t rc = block_get(&block, fs->service_id, node->lba + i, BLOCK_FLAGS_NONE);
[a26895d]584 if (rc != EOK)
[a8c7a6d]585 return rc;
[a35b458]586
[8e77b12f]587 cdfs_dir_t *dir;
[a35b458]588
[8e77b12f]589 for (size_t offset = 0; offset < BLOCK_SIZE;
[a26895d]590 offset += dir->length) {
591 dir = (cdfs_dir_t *) (block->data + offset);
[8e77b12f]592 if (dir->length == 0)
593 break;
594 if (offset + dir->length > BLOCK_SIZE) {
595 /* XXX Incorrect FS structure */
596 break;
597 }
[a35b458]598
[a26895d]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;
[a35b458]604
[8e77b12f]605 /* Skip special entries */
[a35b458]606
[8e77b12f]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;
[a35b458]613
[a26895d]614 // FIXME: hack - indexing by dentry byte offset on disc
[a35b458]615
[a26895d]616 fs_node_t *fn;
[b7fd2a0]617 errno_t rc = create_node(&fn, fs, dentry_type,
[a26895d]618 (node->lba + i) * BLOCK_SIZE + offset);
[a8c7a6d]619 if (rc != EOK)
620 return rc;
621
622 assert(fn != NULL);
[a35b458]623
[a26895d]624 cdfs_node_t *cur = CDFS_NODE(fn);
625 cur->lba = uint32_lb(dir->lba);
626 cur->size = uint32_lb(dir->size);
[a35b458]627
[d4b63fa]628 char *name = cdfs_decode_name(dir->name,
[993d608]629 dir->name_length, node->fs->enc, dentry_type);
[a26895d]630 if (name == NULL)
[a8c7a6d]631 return EIO;
[a35b458]632
[a26895d]633 // FIXME: check return value
[a35b458]634
[a26895d]635 link_node(fs_node, fn, name);
636 free(name);
[a35b458]637
[a26895d]638 if (dentry_type == CDFS_FILE)
639 cur->processed = true;
640 }
[a35b458]641
[a26895d]642 block_put(block);
643 }
[a35b458]644
[a26895d]645 node->processed = true;
[a8c7a6d]646 return EOK;
[a26895d]647}
648
[0003e0f5]649static fs_node_t *get_uncached_node(cdfs_t *fs, fs_index_t index)
[a26895d]650{
651 cdfs_lba_t lba = index / BLOCK_SIZE;
652 size_t offset = index % BLOCK_SIZE;
[a35b458]653
[a26895d]654 block_t *block;
[b7fd2a0]655 errno_t rc = block_get(&block, fs->service_id, lba, BLOCK_FLAGS_NONE);
[a26895d]656 if (rc != EOK)
657 return NULL;
[a35b458]658
[a26895d]659 cdfs_dir_t *dir = (cdfs_dir_t *) (block->data + offset);
[a35b458]660
[a26895d]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;
[a35b458]666
[a26895d]667 fs_node_t *fn;
[0003e0f5]668 rc = create_node(&fn, fs, dentry_type, index);
[a26895d]669 if ((rc != EOK) || (fn == NULL))
670 return NULL;
[a35b458]671
[a26895d]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;
[a35b458]676
[a26895d]677 if (dentry_type == CDFS_FILE)
678 node->processed = true;
[a35b458]679
[a26895d]680 block_put(block);
681 return fn;
682}
683
[0003e0f5]684static fs_node_t *get_cached_node(cdfs_t *fs, fs_index_t index)
[a26895d]685{
[062d900]686 ht_key_t key = {
687 .index = index,
[0003e0f5]688 .service_id = fs->service_id
[a26895d]689 };
[a35b458]690
[062d900]691 ht_link_t *link = hash_table_find(&nodes, &key);
[a26895d]692 if (link) {
693 cdfs_node_t *node =
[062d900]694 hash_table_get_inst(link, cdfs_node_t, nh_link);
[a26895d]695 return FS_NODE(node);
696 }
[a35b458]697
[0003e0f5]698 return get_uncached_node(fs, index);
[a26895d]699}
700
[b7fd2a0]701static errno_t cdfs_match(fs_node_t **fn, fs_node_t *pfn, const char *component)
[a26895d]702{
703 cdfs_node_t *parent = CDFS_NODE(pfn);
[a35b458]704
[a26895d]705 if (!parent->processed) {
[b7fd2a0]706 errno_t rc = cdfs_readdir(parent->fs, pfn);
[a26895d]707 if (rc != EOK)
708 return rc;
709 }
[a35b458]710
[feeac0d]711 list_foreach(parent->cs_list, link, cdfs_dentry_t, dentry) {
[a26895d]712 if (str_cmp(dentry->name, component) == 0) {
[0003e0f5]713 *fn = get_cached_node(parent->fs, dentry->index);
[a26895d]714 return EOK;
715 }
716 }
[a35b458]717
[a26895d]718 *fn = NULL;
719 return EOK;
720}
721
[b7fd2a0]722static errno_t cdfs_node_open(fs_node_t *fn)
[a26895d]723{
724 cdfs_node_t *node = CDFS_NODE(fn);
[a35b458]725
[a26895d]726 if (!node->processed)
[0003e0f5]727 cdfs_readdir(node->fs, fn);
[a35b458]728
[a26895d]729 node->opened++;
730 return EOK;
731}
732
[b7fd2a0]733static errno_t cdfs_node_put(fs_node_t *fn)
[a26895d]734{
735 /* Nothing to do */
736 return EOK;
737}
738
[b7fd2a0]739static errno_t cdfs_create_node(fs_node_t **fn, service_id_t service_id, int lflag)
[a26895d]740{
741 /* Read-only */
742 return ENOTSUP;
743}
744
[b7fd2a0]745static errno_t cdfs_destroy_node(fs_node_t *fn)
[a26895d]746{
747 /* Read-only */
748 return ENOTSUP;
749}
750
[b7fd2a0]751static errno_t cdfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *name)
[a26895d]752{
753 /* Read-only */
754 return ENOTSUP;
755}
756
[b7fd2a0]757static errno_t cdfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *name)
[a26895d]758{
759 /* Read-only */
760 return ENOTSUP;
761}
762
[b7fd2a0]763static errno_t cdfs_has_children(bool *has_children, fs_node_t *fn)
[a26895d]764{
765 cdfs_node_t *node = CDFS_NODE(fn);
[a35b458]766
[a26895d]767 if ((node->type == CDFS_DIRECTORY) && (!node->processed))
[0003e0f5]768 cdfs_readdir(node->fs, fn);
[a35b458]769
[a26895d]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
[b33870b]804static service_id_t cdfs_service_get(fs_node_t *fn)
[a26895d]805{
806 return 0;
807}
808
[b7fd2a0]809static errno_t cdfs_size_block(service_id_t service_id, uint32_t *size)
[a727fb0]810{
[3dd148d]811 *size = BLOCK_SIZE;
[a35b458]812
[1b20da0]813 return EOK;
[a727fb0]814}
815
[b7fd2a0]816static errno_t cdfs_total_block_count(service_id_t service_id, uint64_t *count)
[781408e]817{
818 *count = 0;
[a35b458]819
[781408e]820 return EOK;
821}
822
[b7fd2a0]823static errno_t cdfs_free_block_count(service_id_t service_id, uint64_t *count)
[781408e]824{
825 *count = 0;
[a35b458]826
[781408e]827 return EOK;
828}
829
[a26895d]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,
[a727fb0]846 .service_get = cdfs_service_get,
[781408e]847 .size_block = cdfs_size_block,
848 .total_block_count = cdfs_total_block_count,
849 .free_block_count = cdfs_free_block_count
[a26895d]850};
851
[993d608]852/** Verify that escape sequence corresonds to one of the allowed encoding
[7c3fb9b]853 * escape sequences allowed for Joliet.
854 */
[b7fd2a0]855static errno_t cdfs_verify_joliet_esc_seq(uint8_t *seq)
[993d608]856{
857 size_t i, j, k;
858 bool match;
[a35b458]859
[993d608]860 i = 0;
861 while (i + ucs2_esc_seq_len <= 32) {
862 if (seq[i] == 0)
863 break;
[a35b458]864
[993d608]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 }
[a35b458]874
[993d608]875 if (!match)
876 return EINVAL;
[a35b458]877
[993d608]878 i += ucs2_esc_seq_len;
879 }
[a35b458]880
[993d608]881 while (i < 32) {
882 if (seq[i] != 0)
883 return EINVAL;
884 ++i;
885 }
[a35b458]886
[993d608]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
[9c96634]896 * @param vol_ident Place to store pointer to volume identifier
[993d608]897 * @return EOK if found, ENOENT if not
898 */
[b7fd2a0]899static errno_t cdfs_find_joliet_svd(service_id_t sid, cdfs_lba_t altroot,
[9c96634]900 uint32_t *rlba, uint32_t *rsize, char **vol_ident)
[993d608]901{
902 cdfs_lba_t bi;
903
904 for (bi = altroot + 17; ; bi++) {
905 block_t *block;
[b7fd2a0]906 errno_t rc = block_get(&block, sid, bi, BLOCK_FLAGS_NONE);
[993d608]907 if (rc != EOK)
908 break;
[a35b458]909
[993d608]910 cdfs_vol_desc_t *vol_desc = (cdfs_vol_desc_t *) block->data;
[a35b458]911
[993d608]912 if (vol_desc->type == VOL_DESC_SET_TERMINATOR) {
913 block_put(block);
914 break;
915 }
[a35b458]916
[993d608]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 }
[a35b458]923
[993d608]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 }
[a35b458]933
[993d608]934 uint16_t sequence_nr = uint16_lb(vol_desc->data.prisec.sequence_nr);
935 if (sequence_nr != 1) {
936 /*
[ae7d03c]937 * We only support the first disc
[993d608]938 * in multi-disc sets.
939 */
940 block_put(block);
941 continue;
942 }
[a35b458]943
[993d608]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 }
[a35b458]949
[993d608]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);
[9c96634]955
956 *vol_ident = cdfs_decode_vol_ident(vol_desc->data.prisec.ident,
957 32, enc_ucs2);
958
[993d608]959 block_put(block);
960 return EOK;
961 }
[a35b458]962
[993d608]963 return ENOENT;
964}
965
[395df52]966/** Read the volume descriptors. */
[b7fd2a0]967static errno_t iso_read_vol_desc(service_id_t sid, cdfs_lba_t altroot,
[9c96634]968 uint32_t *rlba, uint32_t *rsize, cdfs_enc_t *enc, char **vol_ident)
[a26895d]969{
970 /* First 16 blocks of isofs are empty */
971 block_t *block;
[b7fd2a0]972 errno_t rc = block_get(&block, sid, altroot + 16, BLOCK_FLAGS_NONE);
[a26895d]973 if (rc != EOK)
[94e3a03]974 return rc;
[a35b458]975
[a26895d]976 cdfs_vol_desc_t *vol_desc = (cdfs_vol_desc_t *) block->data;
[a35b458]977
[a26895d]978 /*
979 * Test for primary volume descriptor
980 * and standard compliance.
981 */
982 if ((vol_desc->type != VOL_DESC_PRIMARY) ||
[44ecf89]983 (memcmp(vol_desc->standard_ident, CDFS_STANDARD_IDENT, 5) != 0) ||
[a26895d]984 (vol_desc->version != 1)) {
985 block_put(block);
[94e3a03]986 return ENOTSUP;
[a26895d]987 }
[a35b458]988
[993d608]989 uint16_t set_size = uint16_lb(vol_desc->data.prisec.set_size);
[a26895d]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 }
[a35b458]998
[993d608]999 uint16_t sequence_nr = uint16_lb(vol_desc->data.prisec.sequence_nr);
[a26895d]1000 if (sequence_nr != 1) {
1001 /*
1002 * We only support the first disc
1003 * in multi-disc sets.
1004 */
1005 block_put(block);
[94e3a03]1006 return ENOTSUP;
[a26895d]1007 }
[a35b458]1008
[993d608]1009 uint16_t block_size = uint16_lb(vol_desc->data.prisec.block_size);
[a26895d]1010 if (block_size != BLOCK_SIZE) {
1011 block_put(block);
[94e3a03]1012 return ENOTSUP;
[a26895d]1013 }
[a35b458]1014
[a26895d]1015 // TODO: implement path table support
[a35b458]1016
[993d608]1017 /* Search for Joliet SVD */
[a35b458]1018
[993d608]1019 uint32_t jrlba;
1020 uint32_t jrsize;
[a35b458]1021
[9c96634]1022 rc = cdfs_find_joliet_svd(sid, altroot, &jrlba, &jrsize, vol_ident);
[993d608]1023 if (rc == EOK) {
1024 /* Found */
[395df52]1025 *rlba = jrlba;
1026 *rsize = jrsize;
1027 *enc = enc_ucs2;
[993d608]1028 } else {
[395df52]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;
[9c96634]1032 *vol_ident = cdfs_decode_vol_ident(vol_desc->data.prisec.ident,
1033 32, enc_ascii);
[a26895d]1034 }
[a35b458]1035
[a26895d]1036 block_put(block);
[94e3a03]1037 return EOK;
[a26895d]1038}
1039
[b7fd2a0]1040static errno_t iso_readfs(cdfs_t *fs, fs_node_t *rfn,
[395df52]1041 cdfs_lba_t altroot)
1042{
1043 cdfs_node_t *node = CDFS_NODE(rfn);
[a35b458]1044
[b7fd2a0]1045 errno_t rc = iso_read_vol_desc(fs->service_id, altroot, &node->lba,
[94e3a03]1046 &node->size, &fs->enc, &fs->vol_ident);
1047 if (rc != EOK)
1048 return rc;
[a35b458]1049
[395df52]1050 return cdfs_readdir(fs, rfn);
1051}
1052
[7c3fb9b]1053/*
1054 * Mount a session with session start offset
[a26895d]1055 *
1056 */
[0003e0f5]1057static cdfs_t *cdfs_fs_create(service_id_t sid, cdfs_lba_t altroot)
[a26895d]1058{
[0003e0f5]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;
[a35b458]1065
[0003e0f5]1066 fs->service_id = sid;
[a35b458]1067
[a26895d]1068 /* Create root node */
[b7fd2a0]1069 errno_t rc = create_node(&rfn, fs, L_DIRECTORY, cdfs_index++);
[a35b458]1070
[a26895d]1071 if ((rc != EOK) || (!rfn))
[0003e0f5]1072 goto error;
[a35b458]1073
[a26895d]1074 /* FS root is not linked */
1075 CDFS_NODE(rfn)->lnkcnt = 0;
1076 CDFS_NODE(rfn)->lba = 0;
1077 CDFS_NODE(rfn)->processed = false;
[a35b458]1078
[a26895d]1079 /* Check if there is cdfs in given session */
[94e3a03]1080 if (iso_readfs(fs, rfn, altroot) != EOK)
[0003e0f5]1081 goto error;
[a35b458]1082
[0003e0f5]1083 list_append(&fs->link, &cdfs_instances);
1084 return fs;
1085error:
1086 // XXX destroy node
1087 free(fs);
1088 return NULL;
[a26895d]1089}
1090
[b7fd2a0]1091static errno_t cdfs_fsprobe(service_id_t service_id, vfs_fs_probe_info_t *info)
[d2c8533]1092{
[9c96634]1093 char *vol_ident;
1094
[395df52]1095 /* Initialize the block layer */
[7ae01d5]1096 errno_t rc = block_init(service_id);
[395df52]1097 if (rc != EOK)
1098 return rc;
[a35b458]1099
[395df52]1100 cdfs_lba_t altroot = 0;
[a35b458]1101
[395df52]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);
[a35b458]1111
[395df52]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 }
[a35b458]1118
[395df52]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 }
[a35b458]1128
[395df52]1129 /* Read volume descriptors */
1130 uint32_t rlba;
1131 uint32_t rsize;
1132 cdfs_enc_t enc;
[94e3a03]1133 rc = iso_read_vol_desc(service_id, altroot, &rlba, &rsize, &enc,
1134 &vol_ident);
1135 if (rc != EOK) {
[395df52]1136 block_cache_fini(service_id);
1137 block_fini(service_id);
[94e3a03]1138 return rc;
[395df52]1139 }
[a35b458]1140
[9c96634]1141 str_cpy(info->label, FS_LABEL_MAXLEN + 1, vol_ident);
1142 free(vol_ident);
[a35b458]1143
[adc68de]1144 block_cache_fini(service_id);
1145 block_fini(service_id);
[395df52]1146 return EOK;
[d2c8533]1147}
1148
[b7fd2a0]1149static errno_t cdfs_mounted(service_id_t service_id, const char *opts,
[4f30222]1150 fs_index_t *index, aoff64_t *size)
[a26895d]1151{
1152 /* Initialize the block layer */
[7ae01d5]1153 errno_t rc = block_init(service_id);
[a26895d]1154 if (rc != EOK)
1155 return rc;
[a35b458]1156
[a26895d]1157 cdfs_lba_t altroot = 0;
[a35b458]1158
[a26895d]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 {
[3abf70c7]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);
[a26895d]1173 }
[a35b458]1174
[a26895d]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 }
[a35b458]1181
[a26895d]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);
[a35b458]1189
[a26895d]1190 return EEXIST;
1191 }
[a35b458]1192
[0003e0f5]1193 /* Create cdfs instance */
1194 if (cdfs_fs_create(service_id, altroot) == NULL) {
[a26895d]1195 block_cache_fini(service_id);
1196 block_fini(service_id);
[a35b458]1197
[a26895d]1198 return ENOMEM;
1199 }
[a35b458]1200
[a26895d]1201 rc = cdfs_root_get(&rootfn, service_id);
1202 assert(rc == EOK);
[a35b458]1203
[a26895d]1204 cdfs_node_t *root = CDFS_NODE(rootfn);
1205 *index = root->index;
1206 *size = root->size;
[a35b458]1207
[a26895d]1208 return EOK;
1209}
1210
[1b20da0]1211static bool rm_service_id_nodes(ht_link_t *item, void *arg)
[a26895d]1212{
[3bacee1]1213 service_id_t service_id = *(service_id_t *)arg;
[062d900]1214 cdfs_node_t *node = hash_table_get_inst(item, cdfs_node_t, nh_link);
[a35b458]1215
[0003e0f5]1216 if (node->fs->service_id == service_id) {
[062d900]1217 hash_table_remove_item(&nodes, &node->nh_link);
1218 }
[a35b458]1219
[062d900]1220 return true;
1221}
1222
[0003e0f5]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);
[9c96634]1229 free(fs->vol_ident);
[0003e0f5]1230 free(fs);
1231}
1232
1233static cdfs_t *cdfs_find_by_sid(service_id_t service_id)
[062d900]1234{
[0003e0f5]1235 list_foreach(cdfs_instances, link, cdfs_t, fs) {
1236 if (fs->service_id == service_id)
1237 return fs;
1238 }
[a35b458]1239
[0003e0f5]1240 return NULL;
[a26895d]1241}
1242
[b7fd2a0]1243static errno_t cdfs_unmounted(service_id_t service_id)
[a26895d]1244{
[0003e0f5]1245 cdfs_t *fs;
1246
1247 fs = cdfs_find_by_sid(service_id);
1248 if (fs == NULL)
1249 return ENOENT;
[a35b458]1250
[0003e0f5]1251 cdfs_fs_destroy(fs);
[a26895d]1252 return EOK;
1253}
1254
[b7fd2a0]1255static errno_t cdfs_read(service_id_t service_id, fs_index_t index, aoff64_t pos,
[a26895d]1256 size_t *rbytes)
1257{
[062d900]1258 ht_key_t key = {
1259 .index = index,
1260 .service_id = service_id
[a26895d]1261 };
[a35b458]1262
[062d900]1263 ht_link_t *link = hash_table_find(&nodes, &key);
[a26895d]1264 if (link == NULL)
1265 return ENOENT;
[a35b458]1266
[a26895d]1267 cdfs_node_t *node =
[062d900]1268 hash_table_get_inst(link, cdfs_node_t, nh_link);
[a35b458]1269
[a26895d]1270 if (!node->processed) {
[b7fd2a0]1271 errno_t rc = cdfs_readdir(node->fs, FS_NODE(node));
[a26895d]1272 if (rc != EOK)
1273 return rc;
1274 }
[a35b458]1275
[984a9ba]1276 ipc_call_t call;
[a26895d]1277 size_t len;
[984a9ba]1278 if (!async_data_read_receive(&call, &len)) {
1279 async_answer_0(&call, EINVAL);
[a26895d]1280 return EINVAL;
1281 }
[a35b458]1282
[a26895d]1283 if (node->type == CDFS_FILE) {
1284 if (pos >= node->size) {
1285 *rbytes = 0;
[984a9ba]1286 async_data_read_finalize(&call, NULL, 0);
[a26895d]1287 } else {
1288 cdfs_lba_t lba = pos / BLOCK_SIZE;
1289 size_t offset = pos % BLOCK_SIZE;
[a35b458]1290
[a26895d]1291 *rbytes = min(len, BLOCK_SIZE - offset);
1292 *rbytes = min(*rbytes, node->size - pos);
[a35b458]1293
[a26895d]1294 block_t *block;
[b7fd2a0]1295 errno_t rc = block_get(&block, service_id, node->lba + lba,
[a26895d]1296 BLOCK_FLAGS_NONE);
1297 if (rc != EOK) {
[984a9ba]1298 async_answer_0(&call, rc);
[a26895d]1299 return rc;
1300 }
[a35b458]1301
[984a9ba]1302 async_data_read_finalize(&call, block->data + offset,
[a26895d]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) {
[984a9ba]1311 async_answer_0(&call, ENOENT);
[a26895d]1312 return ENOENT;
1313 }
[a35b458]1314
[a26895d]1315 cdfs_dentry_t *dentry =
1316 list_get_instance(link, cdfs_dentry_t, link);
[a35b458]1317
[a26895d]1318 *rbytes = 1;
[984a9ba]1319 async_data_read_finalize(&call, dentry->name,
[a26895d]1320 str_size(dentry->name) + 1);
1321 }
[a35b458]1322
[a26895d]1323 return EOK;
1324}
1325
[b7fd2a0]1326static errno_t cdfs_write(service_id_t service_id, fs_index_t index, aoff64_t pos,
[a26895d]1327 size_t *wbytes, aoff64_t *nsize)
1328{
1329 /*
1330 * As cdfs is a read-only filesystem,
1331 * the operation is not supported.
1332 */
[a35b458]1333
[a26895d]1334 return ENOTSUP;
1335}
1336
[b7fd2a0]1337static errno_t cdfs_truncate(service_id_t service_id, fs_index_t index,
[a26895d]1338 aoff64_t size)
1339{
1340 /*
1341 * As cdfs is a read-only filesystem,
1342 * the operation is not supported.
1343 */
[a35b458]1344
[a26895d]1345 return ENOTSUP;
1346}
1347
[062d900]1348static bool cache_remove_closed(ht_link_t *item, void *arg)
1349{
[3bacee1]1350 size_t *premove_cnt = (size_t *)arg;
[a35b458]1351
[062d900]1352 /* Some nodes were requested to be removed from the cache. */
1353 if (0 < *premove_cnt) {
[993d608]1354 cdfs_node_t *node = hash_table_get_inst(item, cdfs_node_t, nh_link);
[062d900]1355
1356 if (!node->opened) {
1357 hash_table_remove_item(&nodes, item);
[a35b458]1358
[062d900]1359 --nodes_cached;
1360 --*premove_cnt;
1361 }
1362 }
[a35b458]1363
[062d900]1364 /* Only continue if more nodes were requested to be removed. */
1365 return 0 < *premove_cnt;
1366}
1367
[a26895d]1368static void cleanup_cache(service_id_t service_id)
1369{
1370 if (nodes_cached > NODE_CACHE_SIZE) {
[062d900]1371 size_t remove_cnt = nodes_cached - NODE_CACHE_SIZE;
[a35b458]1372
[062d900]1373 if (0 < remove_cnt)
1374 hash_table_apply(&nodes, cache_remove_closed, &remove_cnt);
[a26895d]1375 }
1376}
1377
[b7fd2a0]1378static errno_t cdfs_close(service_id_t service_id, fs_index_t index)
[a26895d]1379{
1380 /* Root node is always in memory */
1381 if (index == 0)
1382 return EOK;
[a35b458]1383
[062d900]1384 ht_key_t key = {
1385 .index = index,
1386 .service_id = service_id
[a26895d]1387 };
[a35b458]1388
[062d900]1389 ht_link_t *link = hash_table_find(&nodes, &key);
[a26895d]1390 if (link == 0)
1391 return ENOENT;
[a35b458]1392
[a26895d]1393 cdfs_node_t *node =
[062d900]1394 hash_table_get_inst(link, cdfs_node_t, nh_link);
[a35b458]1395
[a26895d]1396 assert(node->opened > 0);
[a35b458]1397
[a26895d]1398 node->opened--;
1399 cleanup_cache(service_id);
[a35b458]1400
[a26895d]1401 return EOK;
1402}
1403
[b7fd2a0]1404static errno_t cdfs_destroy(service_id_t service_id, fs_index_t index)
[a26895d]1405{
1406 /*
1407 * As cdfs is a read-only filesystem,
1408 * the operation is not supported.
1409 */
[a35b458]1410
[a26895d]1411 return ENOTSUP;
1412}
1413
[b7fd2a0]1414static errno_t cdfs_sync(service_id_t service_id, fs_index_t index)
[a26895d]1415{
1416 /*
1417 * As cdfs is a read-only filesystem,
1418 * the sync operation is a no-op.
1419 */
[a35b458]1420
[a26895d]1421 return EOK;
1422}
1423
1424vfs_out_ops_t cdfs_ops = {
[d2c8533]1425 .fsprobe = cdfs_fsprobe,
[a26895d]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{
[062d900]1441 if (!hash_table_create(&nodes, 0, 0, &nodes_ops))
[a26895d]1442 return false;
[a35b458]1443
[a26895d]1444 return true;
1445}
1446
1447/**
1448 * @}
1449 */
Note: See TracBrowser for help on using the repository browser.