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

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

remove old debug messages

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