source: mainline/uspace/srv/fs/minixfs/mfs_ops.c@ ff2e72e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ff2e72e was ff2e72e, checked in by Maurizio Lombardi <m.lombardi85@…>, 15 years ago

remove old debug messages

  • Property mode set to 100644
File size: 19.9 KB
Line 
1/*
2 * Copyright (c) 2011 Maurizio Lombardi
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#include <stdlib.h>
34#include <fibril_synch.h>
35#include <align.h>
36#include "mfs.h"
37#include "mfs_utils.h"
38
39static bool check_magic_number(uint16_t magic, bool *native,
40 mfs_version_t *version, bool *longfilenames);
41static int mfs_node_core_get(fs_node_t **rfn, struct mfs_instance *inst,
42 fs_index_t index);
43
44static int mfs_node_put(fs_node_t *fsnode);
45static int mfs_node_open(fs_node_t *fsnode);
46static fs_index_t mfs_index_get(fs_node_t *fsnode);
47static unsigned mfs_lnkcnt_get(fs_node_t *fsnode);
48static char mfs_plb_get_char(unsigned pos);
49static bool mfs_is_directory(fs_node_t *fsnode);
50static bool mfs_is_file(fs_node_t *fsnode);
51static int mfs_has_children(bool *has_children, fs_node_t *fsnode);
52static int mfs_root_get(fs_node_t **rfn, devmap_handle_t handle);
53static devmap_handle_t mfs_device_get(fs_node_t *fsnode);
54static aoff64_t mfs_size_get(fs_node_t *node);
55static int mfs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component);
56static int mfs_create_node(fs_node_t **rfn, devmap_handle_t handle, int flags);
57static int mfs_link(fs_node_t *pfn, fs_node_t *cfn, const char *name);
58
59static int mfs_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle,
60 fs_index_t index);
61
62
63static LIST_INITIALIZE(inst_list);
64static FIBRIL_MUTEX_INITIALIZE(inst_list_mutex);
65
66libfs_ops_t mfs_libfs_ops = {
67 .size_get = mfs_size_get,
68 .root_get = mfs_root_get,
69 .device_get = mfs_device_get,
70 .is_directory = mfs_is_directory,
71 .is_file = mfs_is_file,
72 .node_get = mfs_node_get,
73 .node_put = mfs_node_put,
74 .node_open = mfs_node_open,
75 .index_get = mfs_index_get,
76 .match = mfs_match,
77 .create = mfs_create_node,
78 .link = mfs_link,
79 .plb_get_char = mfs_plb_get_char,
80 .has_children = mfs_has_children,
81 .lnkcnt_get = mfs_lnkcnt_get
82};
83
84void mfs_mounted(ipc_callid_t rid, ipc_call_t *request)
85{
86 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
87 enum cache_mode cmode;
88 struct mfs_superblock *sb;
89 struct mfs3_superblock *sb3;
90 struct mfs_sb_info *sbi;
91 struct mfs_instance *instance;
92 bool native, longnames;
93 mfs_version_t version;
94 uint16_t magic;
95
96 /* Accept the mount options */
97 char *opts;
98 int rc = async_data_write_accept((void **) &opts, true, 0, 0, 0, NULL);
99
100 if (rc != EOK) {
101 mfsdebug("Can't accept async data write\n");
102 async_answer_0(rid, rc);
103 return;
104 }
105
106 /* Check for option enabling write through. */
107 if (str_cmp(opts, "wtcache") == 0)
108 cmode = CACHE_MODE_WT;
109 else
110 cmode = CACHE_MODE_WB;
111
112 free(opts);
113
114 /* initialize libblock */
115 rc = block_init(devmap_handle, 1024);
116 if (rc != EOK) {
117 mfsdebug("libblock initialization failed\n");
118 async_answer_0(rid, rc);
119 return;
120 }
121
122 /*Allocate space for generic MFS superblock*/
123 sbi = malloc(sizeof(*sbi));
124
125 if (!sbi) {
126 async_answer_0(rid, ENOMEM);
127 return;
128 }
129
130 /*Allocate space for filesystem instance*/
131 instance = malloc(sizeof(*instance));
132
133 if (!instance) {
134 async_answer_0(rid, ENOMEM);
135 return;
136 }
137
138 sb = malloc(MFS_SUPERBLOCK_SIZE);
139
140 if (!sb) {
141 async_answer_0(rid, ENOMEM);
142 return;
143 }
144
145 /* Read the superblock */
146 rc = block_read_direct(devmap_handle, MFS_SUPERBLOCK << 1, 1, sb);
147 if (rc != EOK) {
148 block_fini(devmap_handle);
149 async_answer_0(rid, rc);
150 return;
151 }
152
153 sb3 = (struct mfs3_superblock *) sb;
154
155 if (check_magic_number(sb->s_magic, &native, &version, &longnames)) {
156 /*This is a V1 or V2 Minix filesystem*/
157 magic = sb->s_magic;
158 goto recognized;
159 }
160
161 if (!check_magic_number(sb3->s_magic, &native, &version, &longnames)) {
162 mfsdebug("magic number not recognized\n");
163 block_fini(devmap_handle);
164 async_answer_0(rid, ENOTSUP);
165 return;
166 }
167
168 /*This is a V3 Minix filesystem*/
169
170 magic = sb3->s_magic;
171
172recognized:
173
174 mfsdebug("magic number recognized = %04x\n", magic);
175
176 /*Fill superblock info structure*/
177
178 sbi->fs_version = version;
179 sbi->long_names = longnames;
180 sbi->native = native;
181 sbi->magic = magic;
182 sbi->isearch = 0;
183 sbi->zsearch = 0;
184
185 if (version == MFS_VERSION_V3) {
186 sbi->ninodes = conv32(native, sb3->s_ninodes);
187 sbi->ibmap_blocks = conv16(native, sb3->s_ibmap_blocks);
188 sbi->zbmap_blocks = conv16(native, sb3->s_zbmap_blocks);
189 sbi->firstdatazone = conv16(native, sb3->s_first_data_zone);
190 sbi->log2_zone_size = conv16(native, sb3->s_log2_zone_size);
191 sbi->max_file_size = conv32(native, sb3->s_max_file_size);
192 sbi->nzones = conv32(native, sb3->s_nzones);
193 sbi->block_size = conv16(native, sb3->s_block_size);
194 sbi->ino_per_block = V3_INODES_PER_BLOCK(sbi->block_size);
195 sbi->dirsize = MFS3_DIRSIZE;
196 sbi->max_name_len = MFS3_MAX_NAME_LEN;
197 } else {
198 sbi->ninodes = conv16(native, sb->s_ninodes);
199 sbi->ibmap_blocks = conv16(native, sb->s_ibmap_blocks);
200 sbi->zbmap_blocks = conv16(native, sb->s_zbmap_blocks);
201 sbi->firstdatazone = conv16(native, sb->s_first_data_zone);
202 sbi->log2_zone_size = conv16(native, sb->s_log2_zone_size);
203 sbi->max_file_size = conv32(native, sb->s_max_file_size);
204 sbi->block_size = MFS_BLOCKSIZE;
205 if (version == MFS_VERSION_V2) {
206 sbi->nzones = conv32(native, sb->s_nzones2);
207 sbi->ino_per_block = V2_INODES_PER_BLOCK;
208 } else {
209 sbi->nzones = conv16(native, sb->s_nzones);
210 sbi->ino_per_block = V1_INODES_PER_BLOCK;
211 }
212 sbi->dirsize = longnames ? MFSL_DIRSIZE : MFS_DIRSIZE;
213 sbi->max_name_len = longnames ? MFS_L_MAX_NAME_LEN :
214 MFS_MAX_NAME_LEN;
215 }
216 sbi->itable_off = 2 + sbi->ibmap_blocks + sbi->zbmap_blocks;
217
218 free(sb);
219
220 rc = block_cache_init(devmap_handle, sbi->block_size, 0, cmode);
221
222 if (rc != EOK) {
223 block_fini(devmap_handle);
224 async_answer_0(rid, EINVAL);
225 mfsdebug("block cache initialization failed\n");
226 return;
227 }
228
229 /*Initialize the instance structure and add it to the list*/
230 link_initialize(&instance->link);
231 instance->handle = devmap_handle;
232 instance->sbi = sbi;
233
234 fibril_mutex_lock(&inst_list_mutex);
235 list_append(&instance->link, &inst_list);
236 fibril_mutex_unlock(&inst_list_mutex);
237
238 mfsdebug("mount successful\n");
239
240 async_answer_0(rid, EOK);
241}
242
243void mfs_mount(ipc_callid_t rid, ipc_call_t *request)
244{
245 libfs_mount(&mfs_libfs_ops, mfs_reg.fs_handle, rid, request);
246}
247
248devmap_handle_t mfs_device_get(fs_node_t *fsnode)
249{
250 struct mfs_node *node = fsnode->data;
251 return node->instance->handle;
252}
253
254static int mfs_create_node(fs_node_t **rfn, devmap_handle_t handle, int flags)
255{
256 int r;
257 struct mfs_instance *inst;
258 struct mfs_node *mnode;
259 fs_node_t *fsnode;
260 uint32_t inum;
261
262 mfsdebug("create_node()\n");
263
264 r = mfs_instance_get(handle, &inst);
265 on_error(r, return r);
266
267 if (flags & L_DIRECTORY) {
268 /*Not yet supported*/
269 return ENOTSUP;
270 }
271
272 /*Alloc a new inode*/
273 r = mfs_alloc_bit(inst, &inum, BMAP_INODE);
274 on_error(r, return r);
275
276 struct mfs_ino_info *ino_i;
277
278 ino_i = malloc(sizeof(*ino_i));
279 if (!ino_i) {
280 r = ENOMEM;
281 goto out_err;
282 }
283
284 mnode = malloc(sizeof(*mnode));
285 if (!mnode) {
286 r = ENOMEM;
287 goto out_err_1;
288 }
289
290 fsnode = malloc(sizeof(fs_node_t));
291 if (!fsnode) {
292 r = ENOMEM;
293 goto out_err_2;
294 }
295
296 ino_i->i_mode = S_IFREG;
297 ino_i->i_nlinks = 1;
298 ino_i->i_uid = 0;
299 ino_i->i_gid = 0;
300 ino_i->i_size = 0;
301 ino_i->i_atime = 0;
302 ino_i->i_mtime = 0;
303 ino_i->i_ctime = 0;
304
305 memset(ino_i->i_dzone, 0, sizeof(uint32_t) * V2_NR_DIRECT_ZONES);
306 memset(ino_i->i_izone, 0, sizeof(uint32_t) * V2_NR_INDIRECT_ZONES);
307
308 mfsdebug("new node idx = %d\n", (int) inum);
309
310 ino_i->index = inum;
311 ino_i->dirty = true;
312 mnode->ino_i = ino_i;
313 mnode->instance = inst;
314
315 r = put_inode(mnode);
316 on_error(r, goto out_err_2);
317
318 fs_node_initialize(fsnode);
319 fsnode->data = mnode;
320 *rfn = fsnode;
321
322 return EOK;
323
324out_err_2:
325 free(mnode);
326out_err_1:
327 free(ino_i);
328out_err:
329 return r;
330}
331
332static int mfs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
333{
334 struct mfs_node *mnode = pfn->data;
335 struct mfs_ino_info *ino_i = mnode->ino_i;
336 struct mfs_dentry_info *d_info;
337 int r;
338
339 mfsdebug("mfs_match()\n");
340
341 if (!S_ISDIR(ino_i->i_mode))
342 return ENOTDIR;
343
344 struct mfs_sb_info *sbi = mnode->instance->sbi;
345 const size_t comp_size = str_size(component);
346
347 int i = 2;
348 while (1) {
349 r = read_directory_entry(mnode, &d_info, i++);
350 on_error(r, return r);
351
352 if (!d_info) {
353 /*Reached the end of the directory entry list*/
354 break;
355 }
356
357 if (!d_info->d_inum) {
358 /*This entry is not used*/
359 free(d_info);
360 continue;
361 }
362
363 if (!bcmp(component, d_info->d_name, min(sbi->max_name_len,
364 comp_size))) {
365 /*Hit!*/
366 mfs_node_core_get(rfn, mnode->instance,
367 d_info->d_inum);
368 free(d_info);
369 goto found;
370 }
371 free(d_info);
372 }
373 *rfn = NULL;
374found:
375 return EOK;
376}
377
378static aoff64_t mfs_size_get(fs_node_t *node)
379{
380 assert(node);
381
382 const struct mfs_node *mnode = node->data;
383 assert(mnode);
384 assert(mnode->ino_i);
385
386 return mnode->ino_i->i_size;
387}
388
389void mfs_stat(ipc_callid_t rid, ipc_call_t *request)
390{
391 libfs_stat(&mfs_libfs_ops, mfs_reg.fs_handle, rid, request);
392}
393
394static int mfs_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle,
395 fs_index_t index)
396{
397 int rc;
398 struct mfs_instance *instance;
399
400 rc = mfs_instance_get(devmap_handle, &instance);
401 on_error(rc, return rc);
402
403 return mfs_node_core_get(rfn, instance, index);
404}
405
406static int mfs_node_put(fs_node_t *fsnode)
407{
408 struct mfs_node *mnode = fsnode->data;
409
410 put_inode(mnode);
411 free(mnode->ino_i);
412 free(mnode);
413
414 return EOK;
415}
416
417static int mfs_node_open(fs_node_t *fsnode)
418{
419 /*
420 * Opening a file is stateless, nothing
421 * to be done here.
422 */
423 return EOK;
424}
425
426static fs_index_t mfs_index_get(fs_node_t *fsnode)
427{
428 struct mfs_node *mnode = fsnode->data;
429
430 assert(mnode->ino_i);
431 return mnode->ino_i->index;
432}
433
434static unsigned mfs_lnkcnt_get(fs_node_t *fsnode)
435{
436 unsigned rc;
437 struct mfs_node *mnode = fsnode->data;
438
439 assert(mnode);
440 assert(mnode->ino_i);
441
442 rc = mnode->ino_i->i_nlinks;
443 return rc;
444}
445
446static int mfs_node_core_get(fs_node_t **rfn, struct mfs_instance *inst,
447 fs_index_t index)
448{
449 fs_node_t *node = NULL;
450 struct mfs_node *mnode = NULL;
451 int rc;
452
453 node = malloc(sizeof(fs_node_t));
454 if (!node) {
455 rc = ENOMEM;
456 goto out_err;
457 }
458
459 fs_node_initialize(node);
460
461 mnode = malloc(sizeof(*mnode));
462 if (!mnode) {
463 rc = ENOMEM;
464 goto out_err;
465 }
466
467 struct mfs_ino_info *ino_i;
468
469 rc = get_inode(inst, &ino_i, index);
470 on_error(rc, goto out_err);
471
472 ino_i->index = index;
473 mnode->ino_i = ino_i;
474
475 mnode->instance = inst;
476 node->data = mnode;
477 *rfn = node;
478
479 return EOK;
480
481out_err:
482 if (node)
483 free(node);
484 if (mnode)
485 free(mnode);
486 return rc;
487}
488
489static bool mfs_is_directory(fs_node_t *fsnode)
490{
491 const struct mfs_node *node = fsnode->data;
492 return S_ISDIR(node->ino_i->i_mode);
493}
494
495static bool mfs_is_file(fs_node_t *fsnode)
496{
497 struct mfs_node *node = fsnode->data;
498 return S_ISREG(node->ino_i->i_mode);
499}
500
501static int mfs_root_get(fs_node_t **rfn, devmap_handle_t handle)
502{
503 int rc = mfs_node_get(rfn, handle, MFS_ROOT_INO);
504 return rc;
505}
506
507void mfs_lookup(ipc_callid_t rid, ipc_call_t *request)
508{
509 libfs_lookup(&mfs_libfs_ops, mfs_reg.fs_handle, rid, request);
510}
511
512static char mfs_plb_get_char(unsigned pos)
513{
514 return mfs_reg.plb_ro[pos % PLB_SIZE];
515}
516
517static int mfs_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
518{
519 struct mfs_node *parent = pfn->data;
520 struct mfs_node *child = cfn->data;
521 struct mfs_sb_info *sbi = parent->instance->sbi;
522
523 mfsdebug("mfs_link() %d\n", (int) child->ino_i->index);
524
525 if (str_size(name) > sbi->max_name_len)
526 return ENAMETOOLONG;
527
528 int r = insert_dentry(parent, name, child->ino_i->index);
529
530 return r;
531}
532
533static int mfs_has_children(bool *has_children, fs_node_t *fsnode)
534{
535 struct mfs_node *mnode = fsnode->data;
536 int r;
537
538 *has_children = false;
539
540 if (!S_ISDIR(mnode->ino_i->i_mode))
541 goto out;
542
543 struct mfs_dentry_info *d_info;
544
545 /* The first two dentries are always . and .. */
546 int i = 2;
547 while (1) {
548 r = read_directory_entry(mnode, &d_info, i++);
549 on_error(r, return r);
550
551 if (!d_info) {
552 /*Reached the end of the dentries list*/
553 break;
554 }
555
556 if (d_info->d_inum) {
557 /*A valid entry has been found*/
558 *has_children = true;
559 free(d_info);
560 break;
561 }
562
563 free(d_info);
564 }
565
566out:
567
568 return EOK;
569}
570
571void
572mfs_read(ipc_callid_t rid, ipc_call_t *request)
573{
574 int rc;
575 devmap_handle_t handle = (devmap_handle_t) IPC_GET_ARG1(*request);
576 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
577 aoff64_t pos = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request),
578 IPC_GET_ARG4(*request));
579 fs_node_t *fn;
580
581 mfsdebug("mfs_read()\n");
582
583 rc = mfs_node_get(&fn, handle, index);
584 if (rc != EOK) {
585 async_answer_0(rid, rc);
586 return;
587 }
588 if (!fn) {
589 async_answer_0(rid, ENOENT);
590 return;
591 }
592
593 struct mfs_node *mnode;
594 struct mfs_ino_info *ino_i;
595 size_t len, bytes = 0;
596 ipc_callid_t callid;
597
598 mnode = fn->data;
599 ino_i = mnode->ino_i;
600
601 if (!async_data_read_receive(&callid, &len)) {
602 rc = EINVAL;
603 goto out_error;
604 }
605
606 if (S_ISDIR(ino_i->i_mode)) {
607 aoff64_t spos = pos;
608 struct mfs_dentry_info *d_info;
609
610 while (1) {
611 rc = read_directory_entry(mnode, &d_info, pos);
612 on_error(rc, goto out_error);
613
614 if (!d_info) {
615 /*Reached the end of the dentries list*/
616 break;
617 }
618
619 if (d_info->d_inum) {
620 /*Dentry found!*/
621 mfsdebug("DENTRY FOUND %s!!\n", d_info->d_name);
622 goto found;
623 }
624
625 free(d_info);
626 pos++;
627 }
628
629 rc = mfs_node_put(fn);
630 async_answer_0(callid, rc != EOK ? rc : ENOENT);
631 async_answer_1(rid, rc != EOK ? rc : ENOENT, 0);
632 return;
633found:
634 async_data_read_finalize(callid, d_info->d_name,
635 str_size(d_info->d_name) + 1);
636 bytes = ((pos - spos) + 1);
637 } else {
638 struct mfs_sb_info *sbi = mnode->instance->sbi;
639
640 if (pos >= (size_t) ino_i->i_size) {
641 /*Trying to read beyond the end of file*/
642 bytes = 0;
643 (void) async_data_read_finalize(callid, NULL, 0);
644 goto out_success;
645 }
646
647 bytes = min(len, sbi->block_size - pos % sbi->block_size);
648 bytes = min(bytes, ino_i->i_size - pos);
649
650 uint32_t zone;
651 block_t *b;
652
653 rc = read_map(&zone, mnode, pos);
654 on_error(rc, goto out_error);
655
656 if (zone == 0) {
657 /*sparse file*/
658 uint8_t *buf = malloc(sbi->block_size);
659 if (!buf) {
660 rc = ENOMEM;
661 goto out_error;
662 }
663 memset(buf, 0, sizeof(sbi->block_size));
664 async_data_read_finalize(callid,
665 buf + pos % sbi->block_size, bytes);
666 free(buf);
667 goto out_success;
668 }
669
670 rc = block_get(&b, handle, zone, BLOCK_FLAGS_NONE);
671 on_error(rc, goto out_error);
672
673 async_data_read_finalize(callid, b->data +
674 pos % sbi->block_size, bytes);
675
676 rc = block_put(b);
677 if (rc != EOK) {
678 mfs_node_put(fn);
679 async_answer_0(rid, rc);
680 return;
681 }
682 }
683out_success:
684 rc = mfs_node_put(fn);
685 async_answer_1(rid, rc, (sysarg_t)bytes);
686 return;
687out_error:
688 ;
689 int tmp = mfs_node_put(fn);
690 async_answer_0(callid, tmp != EOK ? tmp : rc);
691 async_answer_0(rid, tmp != EOK ? tmp : rc);
692}
693
694void
695mfs_write(ipc_callid_t rid, ipc_call_t *request)
696{
697 mfsdebug("mfs_write()\n");
698
699 devmap_handle_t handle = (devmap_handle_t) IPC_GET_ARG1(*request);
700 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
701 aoff64_t pos = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request),
702 IPC_GET_ARG4(*request));
703
704 fs_node_t *fn;
705 int r;
706 int flags = BLOCK_FLAGS_NONE;
707
708 r = mfs_node_get(&fn, handle, index);
709 if (r != EOK) {
710 async_answer_0(rid, r);
711 return;
712 }
713
714 if (!fn) {
715 async_answer_0(rid, ENOENT);
716 return;
717 }
718
719 ipc_callid_t callid;
720 size_t len;
721
722 if (!async_data_write_receive(&callid, &len)) {
723 r = EINVAL;
724 goto out_err;
725 }
726
727 struct mfs_node *mnode = fn->data;
728 struct mfs_sb_info *sbi = mnode->instance->sbi;
729 struct mfs_ino_info *ino_i = mnode->ino_i;
730 const size_t bs = sbi->block_size;
731 size_t bytes = min(len, bs - pos % bs);
732 size_t boundary = ROUND_UP(ino_i->i_size, bs);
733 uint32_t block;
734
735 if (bytes == bs)
736 flags = BLOCK_FLAGS_NOREAD;
737
738 if (pos < boundary) {
739 r = read_map(&block, mnode, pos);
740 on_error(r, goto out_err);
741
742 if (block == 0) {
743 /*Writing in a sparse block*/
744 r = mfs_alloc_bit(mnode->instance, &block, BMAP_ZONE);
745 on_error(r, goto out_err);
746 flags = BLOCK_FLAGS_NOREAD;
747 }
748 } else {
749 uint32_t dummy;
750
751 r = mfs_alloc_bit(mnode->instance, &block, BMAP_ZONE);
752 on_error(r, goto out_err);
753
754 r = write_map(mnode, pos, block, &dummy);
755 on_error(r, goto out_err);
756 }
757
758 block_t *b;
759 r = block_get(&b, handle, block, flags);
760 on_error(r, goto out_err);
761
762 async_data_write_finalize(callid, b->data + pos % bs, bytes);
763 b->dirty = true;
764
765 r = block_put(b);
766 if (r != EOK) {
767 mfs_node_put(fn);
768 async_answer_0(rid, r);
769 return;
770 }
771
772 ino_i->i_size = pos + bytes;
773 ino_i->dirty = true;
774 r = mfs_node_put(fn);
775 async_answer_2(rid, r, bytes, pos + bytes);
776 return;
777
778out_err:
779 mfs_node_put(fn);
780 async_answer_0(callid, r);
781 async_answer_0(rid, r);
782}
783
784void
785mfs_truncate(ipc_callid_t rid, ipc_call_t *request)
786{
787 devmap_handle_t handle = (devmap_handle_t) IPC_GET_ARG1(*request);
788 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
789 aoff64_t size = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request),
790 IPC_GET_ARG4(*request));
791 fs_node_t *fn;
792 int r;
793
794 mfsdebug("mfs_truncate()\n");
795
796 r = mfs_node_get(&fn, handle, index);
797 if (r != EOK) {
798 async_answer_0(rid, r);
799 return;
800 }
801
802 if (!fn) {
803 async_answer_0(rid, r);
804 return;
805 }
806
807 struct mfs_node *mnode = fn->data;
808 struct mfs_ino_info *ino_i = mnode->ino_i;
809
810 if (ino_i->i_size == size)
811 r = EOK;
812 else
813 r = inode_shrink(mnode, ino_i->i_size - size);
814
815 async_answer_0(rid, r);
816 mfs_node_put(fn);
817}
818
819int mfs_instance_get(devmap_handle_t handle, struct mfs_instance **instance)
820{
821 link_t *link;
822 struct mfs_instance *instance_ptr;
823
824 fibril_mutex_lock(&inst_list_mutex);
825
826 for (link = inst_list.next; link != &inst_list; link = link->next) {
827 instance_ptr = list_get_instance(link, struct mfs_instance,
828 link);
829
830 if (instance_ptr->handle == handle) {
831 *instance = instance_ptr;
832 fibril_mutex_unlock(&inst_list_mutex);
833 return EOK;
834 }
835 }
836
837 mfsdebug("Instance not found\n");
838
839 fibril_mutex_unlock(&inst_list_mutex);
840 return EINVAL;
841}
842
843static bool check_magic_number(uint16_t magic, bool *native,
844 mfs_version_t *version, bool *longfilenames)
845{
846 bool rc = true;
847 *longfilenames = false;
848
849 if (magic == MFS_MAGIC_V1 || magic == MFS_MAGIC_V1R) {
850 *native = magic == MFS_MAGIC_V1;
851 *version = MFS_VERSION_V1;
852 } else if (magic == MFS_MAGIC_V1L || magic == MFS_MAGIC_V1LR) {
853 *native = magic == MFS_MAGIC_V1L;
854 *version = MFS_VERSION_V1;
855 *longfilenames = true;
856 } else if (magic == MFS_MAGIC_V2 || magic == MFS_MAGIC_V2R) {
857 *native = magic == MFS_MAGIC_V2;
858 *version = MFS_VERSION_V2;
859 } else if (magic == MFS_MAGIC_V2L || magic == MFS_MAGIC_V2LR) {
860 *native = magic == MFS_MAGIC_V2L;
861 *version = MFS_VERSION_V2;
862 *longfilenames = true;
863 } else if (magic == MFS_MAGIC_V3 || magic == MFS_MAGIC_V3R) {
864 *native = magic == MFS_MAGIC_V3;
865 *version = MFS_VERSION_V3;
866 } else
867 rc = false;
868
869 return rc;
870}
871
872void
873mfs_close(ipc_callid_t rid, ipc_call_t *request)
874{
875 async_answer_0(rid, EOK);
876}
877
878void
879mfs_open_node(ipc_callid_t rid, ipc_call_t *request)
880{
881 libfs_open_node(&mfs_libfs_ops, mfs_reg.fs_handle, rid, request);
882}
883
884/**
885 * @}
886 */
887
Note: See TracBrowser for help on using the repository browser.