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

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

The first two dentries (dot and dotdot) must be ignored from the mfs_has_children() function

  • Property mode set to 100644
File size: 21.2 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_inode(inst, &inum);
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 unsigned i;
347 for (i = 0; i < mnode->ino_i->i_size / sbi->dirsize; ++i) {
348 r = read_directory_entry(mnode, &d_info, i);
349 on_error(r, return r);
350
351 if (!d_info.d_inum) {
352 /*This entry is not used*/
353 continue;
354 }
355
356 if (!bcmp(component, d_info.d_name, min(sbi->max_name_len,
357 comp_size))) {
358 /*Hit!*/
359 mfs_node_core_get(rfn, mnode->instance,
360 d_info.d_inum);
361 goto found;
362 }
363 }
364 *rfn = NULL;
365found:
366 return EOK;
367}
368
369static aoff64_t mfs_size_get(fs_node_t *node)
370{
371 assert(node);
372
373 const struct mfs_node *mnode = node->data;
374 assert(mnode);
375 assert(mnode->ino_i);
376
377 return mnode->ino_i->i_size;
378}
379
380void mfs_stat(ipc_callid_t rid, ipc_call_t *request)
381{
382 libfs_stat(&mfs_libfs_ops, mfs_reg.fs_handle, rid, request);
383}
384
385static int mfs_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle,
386 fs_index_t index)
387{
388 int rc;
389 struct mfs_instance *instance;
390
391 rc = mfs_instance_get(devmap_handle, &instance);
392 on_error(rc, return rc);
393
394 return mfs_node_core_get(rfn, instance, index);
395}
396
397static int mfs_node_put(fs_node_t *fsnode)
398{
399 struct mfs_node *mnode = fsnode->data;
400
401 put_inode(mnode);
402 free(mnode->ino_i);
403 free(mnode);
404
405 return EOK;
406}
407
408static int mfs_node_open(fs_node_t *fsnode)
409{
410 /*
411 * Opening a file is stateless, nothing
412 * to be done here.
413 */
414 return EOK;
415}
416
417static fs_index_t mfs_index_get(fs_node_t *fsnode)
418{
419 struct mfs_node *mnode = fsnode->data;
420
421 assert(mnode->ino_i);
422 return mnode->ino_i->index;
423}
424
425static unsigned mfs_lnkcnt_get(fs_node_t *fsnode)
426{
427 struct mfs_node *mnode = fsnode->data;
428
429 assert(mnode);
430 assert(mnode->ino_i);
431
432 return mnode->ino_i->i_nlinks;;
433}
434
435static int mfs_node_core_get(fs_node_t **rfn, struct mfs_instance *inst,
436 fs_index_t index)
437{
438 fs_node_t *node = NULL;
439 struct mfs_node *mnode = NULL;
440 int rc;
441
442 node = malloc(sizeof(fs_node_t));
443 if (!node) {
444 rc = ENOMEM;
445 goto out_err;
446 }
447
448 fs_node_initialize(node);
449
450 mnode = malloc(sizeof(*mnode));
451 if (!mnode) {
452 rc = ENOMEM;
453 goto out_err;
454 }
455
456 struct mfs_ino_info *ino_i;
457
458 rc = get_inode(inst, &ino_i, index);
459 on_error(rc, goto out_err);
460
461 ino_i->index = index;
462 mnode->ino_i = ino_i;
463
464 mnode->instance = inst;
465 node->data = mnode;
466 *rfn = node;
467
468 return EOK;
469
470out_err:
471 if (node)
472 free(node);
473 if (mnode)
474 free(mnode);
475 return rc;
476}
477
478static bool mfs_is_directory(fs_node_t *fsnode)
479{
480 const struct mfs_node *node = fsnode->data;
481 return S_ISDIR(node->ino_i->i_mode);
482}
483
484static bool mfs_is_file(fs_node_t *fsnode)
485{
486 struct mfs_node *node = fsnode->data;
487 return S_ISREG(node->ino_i->i_mode);
488}
489
490static int mfs_root_get(fs_node_t **rfn, devmap_handle_t handle)
491{
492 int rc = mfs_node_get(rfn, handle, MFS_ROOT_INO);
493 return rc;
494}
495
496void mfs_lookup(ipc_callid_t rid, ipc_call_t *request)
497{
498 libfs_lookup(&mfs_libfs_ops, mfs_reg.fs_handle, rid, request);
499}
500
501static char mfs_plb_get_char(unsigned pos)
502{
503 return mfs_reg.plb_ro[pos % PLB_SIZE];
504}
505
506static int mfs_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
507{
508 struct mfs_node *parent = pfn->data;
509 struct mfs_node *child = cfn->data;
510 struct mfs_sb_info *sbi = parent->instance->sbi;
511
512 if (str_size(name) > sbi->max_name_len)
513 return ENAMETOOLONG;
514
515 int r = insert_dentry(parent, name, child->ino_i->index);
516 on_error(r, goto exit_error);
517
518 if (S_ISDIR(child->ino_i->i_mode)) {
519 r = insert_dentry(child, ".", child->ino_i->index);
520 on_error(r, goto exit_error);
521 r = insert_dentry(child, "..", parent->ino_i->index);
522 }
523
524exit_error:
525 return r;
526}
527
528static int
529mfs_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *name)
530{
531 struct mfs_node *parent = pfn->data;
532 struct mfs_node *child = cfn->data;
533 bool has_children;
534 int r;
535
536 if (!parent)
537 return EBUSY;
538
539 r = mfs_has_children(&has_children, cfn);
540 on_error(r, return r);
541
542 if (has_children)
543 return ENOTEMPTY;
544
545 r = remove_dentry(parent, name);
546 on_error(r, return r);
547
548 struct mfs_ino_info *chino = child->ino_i;
549
550 assert(chino->i_nlinks >= 1);
551 --chino->i_nlinks;
552
553 chino->dirty = true;
554
555 return EOK;
556}
557
558static int mfs_has_children(bool *has_children, fs_node_t *fsnode)
559{
560 struct mfs_node *mnode = fsnode->data;
561 struct mfs_sb_info *sbi = mnode->instance->sbi;
562 int r;
563
564 *has_children = false;
565
566 if (!S_ISDIR(mnode->ino_i->i_mode))
567 goto out;
568
569 struct mfs_dentry_info d_info;
570
571 /* The first two dentries are always . and .. */
572 unsigned i;
573 for (i = 2; i < mnode->ino_i->i_size / sbi->dirsize; ++i) {
574 r = read_directory_entry(mnode, &d_info, i);
575 on_error(r, return r);
576
577 if (d_info.d_inum) {
578 /*A valid entry has been found*/
579 *has_children = true;
580 break;
581 }
582 }
583out:
584
585 return EOK;
586}
587
588void
589mfs_read(ipc_callid_t rid, ipc_call_t *request)
590{
591 int rc;
592 devmap_handle_t handle = (devmap_handle_t) IPC_GET_ARG1(*request);
593 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
594 aoff64_t pos = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request),
595 IPC_GET_ARG4(*request));
596 fs_node_t *fn;
597
598 rc = mfs_node_get(&fn, handle, index);
599 if (rc != EOK) {
600 async_answer_0(rid, rc);
601 return;
602 }
603 if (!fn) {
604 async_answer_0(rid, ENOENT);
605 return;
606 }
607
608 struct mfs_node *mnode;
609 struct mfs_ino_info *ino_i;
610 size_t len, bytes = 0;
611 ipc_callid_t callid;
612
613 mnode = fn->data;
614 ino_i = mnode->ino_i;
615
616 if (!async_data_read_receive(&callid, &len)) {
617 rc = EINVAL;
618 goto out_error;
619 }
620
621 if (S_ISDIR(ino_i->i_mode)) {
622 aoff64_t spos = pos;
623 struct mfs_dentry_info d_info;
624 struct mfs_sb_info *sbi = mnode->instance->sbi;
625
626 for (; pos < mnode->ino_i->i_size / sbi->dirsize; ++pos) {
627 rc = read_directory_entry(mnode, &d_info, pos);
628 on_error(rc, goto out_error);
629
630 if (d_info.d_inum) {
631 /*Dentry found!*/
632 goto found;
633 }
634 }
635
636 rc = mfs_node_put(fn);
637 async_answer_0(callid, rc != EOK ? rc : ENOENT);
638 async_answer_1(rid, rc != EOK ? rc : ENOENT, 0);
639 return;
640found:
641 async_data_read_finalize(callid, d_info.d_name,
642 str_size(d_info.d_name) + 1);
643 bytes = ((pos - spos) + 1);
644 } else {
645 struct mfs_sb_info *sbi = mnode->instance->sbi;
646
647 if (pos >= (size_t) ino_i->i_size) {
648 /*Trying to read beyond the end of file*/
649 bytes = 0;
650 (void) async_data_read_finalize(callid, NULL, 0);
651 goto out_success;
652 }
653
654 bytes = min(len, sbi->block_size - pos % sbi->block_size);
655 bytes = min(bytes, ino_i->i_size - pos);
656
657 uint32_t zone;
658 block_t *b;
659
660 rc = read_map(&zone, mnode, pos);
661 on_error(rc, goto out_error);
662
663 if (zone == 0) {
664 /*sparse file*/
665 uint8_t *buf = malloc(sbi->block_size);
666 if (!buf) {
667 rc = ENOMEM;
668 goto out_error;
669 }
670 memset(buf, 0, sizeof(sbi->block_size));
671 async_data_read_finalize(callid,
672 buf + pos % sbi->block_size, bytes);
673 free(buf);
674 goto out_success;
675 }
676
677 rc = block_get(&b, handle, zone, BLOCK_FLAGS_NONE);
678 on_error(rc, goto out_error);
679
680 async_data_read_finalize(callid, b->data +
681 pos % sbi->block_size, bytes);
682
683 rc = block_put(b);
684 if (rc != EOK) {
685 mfs_node_put(fn);
686 async_answer_0(rid, rc);
687 return;
688 }
689 }
690out_success:
691 rc = mfs_node_put(fn);
692 async_answer_1(rid, rc, (sysarg_t)bytes);
693 return;
694out_error:
695 ;
696 int tmp = mfs_node_put(fn);
697 async_answer_0(callid, tmp != EOK ? tmp : rc);
698 async_answer_0(rid, tmp != EOK ? tmp : rc);
699}
700
701void
702mfs_write(ipc_callid_t rid, ipc_call_t *request)
703{
704 devmap_handle_t handle = (devmap_handle_t) IPC_GET_ARG1(*request);
705 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
706 aoff64_t pos = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request),
707 IPC_GET_ARG4(*request));
708
709 fs_node_t *fn;
710 int r;
711 int flags = BLOCK_FLAGS_NONE;
712
713 r = mfs_node_get(&fn, handle, index);
714 if (r != EOK) {
715 async_answer_0(rid, r);
716 return;
717 }
718
719 if (!fn) {
720 async_answer_0(rid, ENOENT);
721 return;
722 }
723
724 ipc_callid_t callid;
725 size_t len;
726
727 if (!async_data_write_receive(&callid, &len)) {
728 r = EINVAL;
729 goto out_err;
730 }
731
732 struct mfs_node *mnode = fn->data;
733 struct mfs_sb_info *sbi = mnode->instance->sbi;
734 struct mfs_ino_info *ino_i = mnode->ino_i;
735 const size_t bs = sbi->block_size;
736 size_t bytes = min(len, bs - pos % bs);
737 size_t boundary = ROUND_UP(ino_i->i_size, bs);
738 uint32_t block;
739
740 if (bytes == bs)
741 flags = BLOCK_FLAGS_NOREAD;
742
743 if (pos < boundary) {
744 r = read_map(&block, mnode, pos);
745 on_error(r, goto out_err);
746
747 if (block == 0) {
748 /*Writing in a sparse block*/
749 r = mfs_alloc_zone(mnode->instance, &block);
750 on_error(r, goto out_err);
751 flags = BLOCK_FLAGS_NOREAD;
752 }
753 } else {
754 uint32_t dummy;
755
756 r = mfs_alloc_zone(mnode->instance, &block);
757 on_error(r, goto out_err);
758
759 r = write_map(mnode, pos, block, &dummy);
760 on_error(r, goto out_err);
761 }
762
763 block_t *b;
764 r = block_get(&b, handle, block, flags);
765 on_error(r, goto out_err);
766
767 async_data_write_finalize(callid, b->data + pos % bs, bytes);
768 b->dirty = true;
769
770 r = block_put(b);
771 if (r != EOK) {
772 mfs_node_put(fn);
773 async_answer_0(rid, r);
774 return;
775 }
776
777 ino_i->i_size = pos + bytes;
778 ino_i->dirty = true;
779 r = mfs_node_put(fn);
780 async_answer_2(rid, r, bytes, pos + bytes);
781 return;
782
783out_err:
784 mfs_node_put(fn);
785 async_answer_0(callid, r);
786 async_answer_0(rid, r);
787}
788
789void
790mfs_destroy(ipc_callid_t rid, ipc_call_t *request)
791{
792 devmap_handle_t handle = (devmap_handle_t)IPC_GET_ARG1(*request);
793 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
794 fs_node_t *fn;
795 int r;
796
797 r = mfs_node_get(&fn, handle, index);
798 if (r != EOK) {
799 async_answer_0(rid, r);
800 return;
801 }
802 if (!fn) {
803 async_answer_0(rid, ENOENT);
804 return;
805 }
806
807 /*Destroy the inode*/
808 r = mfs_destroy_node(fn);
809 async_answer_0(rid, r);
810}
811
812static int
813mfs_destroy_node(fs_node_t *fn)
814{
815 struct mfs_node *mnode = fn->data;
816 bool has_children;
817 int r;
818
819 mfsdebug("mfs_destroy_node %d\n", mnode->ino_i->index);
820
821 r = mfs_has_children(&has_children, fn);
822 on_error(r, return r);
823
824 assert(!has_children);
825
826 /*Free the entire inode content*/
827 r = inode_shrink(mnode, mnode->ino_i->i_size);
828 on_error(r, return r);
829 r = mfs_free_inode(mnode->instance, mnode->ino_i->index);
830 on_error(r, return r);
831
832 free(mnode->ino_i);
833 free(mnode);
834 return r;
835}
836
837void
838mfs_truncate(ipc_callid_t rid, ipc_call_t *request)
839{
840 devmap_handle_t handle = (devmap_handle_t) IPC_GET_ARG1(*request);
841 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
842 aoff64_t size = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request),
843 IPC_GET_ARG4(*request));
844 fs_node_t *fn;
845 int r;
846
847 r = mfs_node_get(&fn, handle, index);
848 if (r != EOK) {
849 async_answer_0(rid, r);
850 return;
851 }
852
853 if (!fn) {
854 async_answer_0(rid, r);
855 return;
856 }
857
858 struct mfs_node *mnode = fn->data;
859 struct mfs_ino_info *ino_i = mnode->ino_i;
860
861 if (ino_i->i_size == size)
862 r = EOK;
863 else
864 r = inode_shrink(mnode, ino_i->i_size - size);
865
866 async_answer_0(rid, r);
867 mfs_node_put(fn);
868}
869
870int mfs_instance_get(devmap_handle_t handle, struct mfs_instance **instance)
871{
872 link_t *link;
873 struct mfs_instance *instance_ptr;
874
875 fibril_mutex_lock(&inst_list_mutex);
876
877 for (link = inst_list.next; link != &inst_list; link = link->next) {
878 instance_ptr = list_get_instance(link, struct mfs_instance,
879 link);
880
881 if (instance_ptr->handle == handle) {
882 *instance = instance_ptr;
883 fibril_mutex_unlock(&inst_list_mutex);
884 return EOK;
885 }
886 }
887
888 mfsdebug("Instance not found\n");
889
890 fibril_mutex_unlock(&inst_list_mutex);
891 return EINVAL;
892}
893
894static bool check_magic_number(uint16_t magic, bool *native,
895 mfs_version_t *version, bool *longfilenames)
896{
897 bool rc = true;
898 *longfilenames = false;
899
900 if (magic == MFS_MAGIC_V1 || magic == MFS_MAGIC_V1R) {
901 *native = magic == MFS_MAGIC_V1;
902 *version = MFS_VERSION_V1;
903 } else if (magic == MFS_MAGIC_V1L || magic == MFS_MAGIC_V1LR) {
904 *native = magic == MFS_MAGIC_V1L;
905 *version = MFS_VERSION_V1;
906 *longfilenames = true;
907 } else if (magic == MFS_MAGIC_V2 || magic == MFS_MAGIC_V2R) {
908 *native = magic == MFS_MAGIC_V2;
909 *version = MFS_VERSION_V2;
910 } else if (magic == MFS_MAGIC_V2L || magic == MFS_MAGIC_V2LR) {
911 *native = magic == MFS_MAGIC_V2L;
912 *version = MFS_VERSION_V2;
913 *longfilenames = true;
914 } else if (magic == MFS_MAGIC_V3 || magic == MFS_MAGIC_V3R) {
915 *native = magic == MFS_MAGIC_V3;
916 *version = MFS_VERSION_V3;
917 } else
918 rc = false;
919
920 return rc;
921}
922
923void
924mfs_close(ipc_callid_t rid, ipc_call_t *request)
925{
926 async_answer_0(rid, EOK);
927}
928
929void
930mfs_open_node(ipc_callid_t rid, ipc_call_t *request)
931{
932 libfs_open_node(&mfs_libfs_ops, mfs_reg.fs_handle, rid, request);
933}
934
935/**
936 * @}
937 */
938
Note: See TracBrowser for help on using the repository browser.