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

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

The read_directory_entry() function has been rewritten to avoid dynamic memory allocation
of the d_info structure.

  • Property mode set to 100644
File size: 21.3 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 if (has_children)
542 return ENOTEMPTY;
543
544 r = remove_dentry(parent, name);
545 on_error(r, return r);
546
547 struct mfs_ino_info *chino = child->ino_i;
548
549 assert(chino->i_nlinks >= 1);
550 --chino->i_nlinks;
551
552 chino->dirty = true;
553
554 return EOK;
555}
556
557static int mfs_has_children(bool *has_children, fs_node_t *fsnode)
558{
559 struct mfs_node *mnode = fsnode->data;
560 struct mfs_sb_info *sbi = mnode->instance->sbi;
561 int r;
562
563 *has_children = false;
564
565 if (!S_ISDIR(mnode->ino_i->i_mode))
566 goto out;
567
568 struct mfs_dentry_info d_info;
569
570 /* The first two dentries are always . and .. */
571 unsigned i;
572 for (i = 0; i < mnode->ino_i->i_size / sbi->dirsize; ++i) {
573 r = read_directory_entry(mnode, &d_info, i);
574 on_error(r, return r);
575
576 if (d_info.d_inum) {
577 /*A valid entry has been found*/
578 *has_children = true;
579 break;
580 }
581 }
582out:
583
584 return EOK;
585}
586
587void
588mfs_read(ipc_callid_t rid, ipc_call_t *request)
589{
590 int rc;
591 devmap_handle_t handle = (devmap_handle_t) IPC_GET_ARG1(*request);
592 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
593 aoff64_t pos = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request),
594 IPC_GET_ARG4(*request));
595 fs_node_t *fn;
596
597 rc = mfs_node_get(&fn, handle, index);
598 if (rc != EOK) {
599 async_answer_0(rid, rc);
600 return;
601 }
602 if (!fn) {
603 async_answer_0(rid, ENOENT);
604 return;
605 }
606
607 struct mfs_node *mnode;
608 struct mfs_ino_info *ino_i;
609 size_t len, bytes = 0;
610 ipc_callid_t callid;
611
612 mnode = fn->data;
613 ino_i = mnode->ino_i;
614
615 if (!async_data_read_receive(&callid, &len)) {
616 rc = EINVAL;
617 goto out_error;
618 }
619
620 if (S_ISDIR(ino_i->i_mode)) {
621 aoff64_t spos = pos;
622 struct mfs_dentry_info d_info;
623 struct mfs_sb_info *sbi = mnode->instance->sbi;
624
625 unsigned i;
626 for (i = pos; i < mnode->ino_i->i_size / sbi->dirsize; ++i) {
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 pos++;
635 }
636
637 rc = mfs_node_put(fn);
638 async_answer_0(callid, rc != EOK ? rc : ENOENT);
639 async_answer_1(rid, rc != EOK ? rc : ENOENT, 0);
640 return;
641found:
642 async_data_read_finalize(callid, d_info.d_name,
643 str_size(d_info.d_name) + 1);
644 bytes = ((pos - spos) + 1);
645 } else {
646 struct mfs_sb_info *sbi = mnode->instance->sbi;
647
648 if (pos >= (size_t) ino_i->i_size) {
649 /*Trying to read beyond the end of file*/
650 bytes = 0;
651 (void) async_data_read_finalize(callid, NULL, 0);
652 goto out_success;
653 }
654
655 bytes = min(len, sbi->block_size - pos % sbi->block_size);
656 bytes = min(bytes, ino_i->i_size - pos);
657
658 uint32_t zone;
659 block_t *b;
660
661 rc = read_map(&zone, mnode, pos);
662 on_error(rc, goto out_error);
663
664 if (zone == 0) {
665 /*sparse file*/
666 uint8_t *buf = malloc(sbi->block_size);
667 if (!buf) {
668 rc = ENOMEM;
669 goto out_error;
670 }
671 memset(buf, 0, sizeof(sbi->block_size));
672 async_data_read_finalize(callid,
673 buf + pos % sbi->block_size, bytes);
674 free(buf);
675 goto out_success;
676 }
677
678 rc = block_get(&b, handle, zone, BLOCK_FLAGS_NONE);
679 on_error(rc, goto out_error);
680
681 async_data_read_finalize(callid, b->data +
682 pos % sbi->block_size, bytes);
683
684 rc = block_put(b);
685 if (rc != EOK) {
686 mfs_node_put(fn);
687 async_answer_0(rid, rc);
688 return;
689 }
690 }
691out_success:
692 rc = mfs_node_put(fn);
693 async_answer_1(rid, rc, (sysarg_t)bytes);
694 return;
695out_error:
696 ;
697 int tmp = mfs_node_put(fn);
698 async_answer_0(callid, tmp != EOK ? tmp : rc);
699 async_answer_0(rid, tmp != EOK ? tmp : rc);
700}
701
702void
703mfs_write(ipc_callid_t rid, ipc_call_t *request)
704{
705 devmap_handle_t handle = (devmap_handle_t) IPC_GET_ARG1(*request);
706 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
707 aoff64_t pos = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request),
708 IPC_GET_ARG4(*request));
709
710 fs_node_t *fn;
711 int r;
712 int flags = BLOCK_FLAGS_NONE;
713
714 r = mfs_node_get(&fn, handle, index);
715 if (r != EOK) {
716 async_answer_0(rid, r);
717 return;
718 }
719
720 if (!fn) {
721 async_answer_0(rid, ENOENT);
722 return;
723 }
724
725 ipc_callid_t callid;
726 size_t len;
727
728 if (!async_data_write_receive(&callid, &len)) {
729 r = EINVAL;
730 goto out_err;
731 }
732
733 struct mfs_node *mnode = fn->data;
734 struct mfs_sb_info *sbi = mnode->instance->sbi;
735 struct mfs_ino_info *ino_i = mnode->ino_i;
736 const size_t bs = sbi->block_size;
737 size_t bytes = min(len, bs - pos % bs);
738 size_t boundary = ROUND_UP(ino_i->i_size, bs);
739 uint32_t block;
740
741 if (bytes == bs)
742 flags = BLOCK_FLAGS_NOREAD;
743
744 if (pos < boundary) {
745 r = read_map(&block, mnode, pos);
746 on_error(r, goto out_err);
747
748 if (block == 0) {
749 /*Writing in a sparse block*/
750 r = mfs_alloc_zone(mnode->instance, &block);
751 on_error(r, goto out_err);
752 flags = BLOCK_FLAGS_NOREAD;
753 }
754 } else {
755 uint32_t dummy;
756
757 r = mfs_alloc_zone(mnode->instance, &block);
758 on_error(r, goto out_err);
759
760 r = write_map(mnode, pos, block, &dummy);
761 on_error(r, goto out_err);
762 }
763
764 block_t *b;
765 r = block_get(&b, handle, block, flags);
766 on_error(r, goto out_err);
767
768 async_data_write_finalize(callid, b->data + pos % bs, bytes);
769 b->dirty = true;
770
771 r = block_put(b);
772 if (r != EOK) {
773 mfs_node_put(fn);
774 async_answer_0(rid, r);
775 return;
776 }
777
778 ino_i->i_size = pos + bytes;
779 ino_i->dirty = true;
780 r = mfs_node_put(fn);
781 async_answer_2(rid, r, bytes, pos + bytes);
782 return;
783
784out_err:
785 mfs_node_put(fn);
786 async_answer_0(callid, r);
787 async_answer_0(rid, r);
788}
789
790void
791mfs_destroy(ipc_callid_t rid, ipc_call_t *request)
792{
793 devmap_handle_t handle = (devmap_handle_t)IPC_GET_ARG1(*request);
794 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
795 fs_node_t *fn;
796 int r;
797
798 r = mfs_node_get(&fn, handle, index);
799 if (r != EOK) {
800 async_answer_0(rid, r);
801 return;
802 }
803 if (!fn) {
804 async_answer_0(rid, ENOENT);
805 return;
806 }
807
808 /*Destroy the inode*/
809 r = mfs_destroy_node(fn);
810 async_answer_0(rid, r);
811}
812
813static int
814mfs_destroy_node(fs_node_t *fn)
815{
816 struct mfs_node *mnode = fn->data;
817 bool has_children;
818 int r;
819
820 mfsdebug("mfs_destroy_node %d\n", mnode->ino_i->index);
821
822 r = mfs_has_children(&has_children, fn);
823 on_error(r, return r);
824
825 assert(!has_children);
826
827 /*Free the entire inode content*/
828 r = inode_shrink(mnode, mnode->ino_i->i_size);
829 on_error(r, return r);
830 r = mfs_free_inode(mnode->instance, mnode->ino_i->index);
831 on_error(r, return r);
832
833 free(mnode->ino_i);
834 free(mnode);
835 return r;
836}
837
838void
839mfs_truncate(ipc_callid_t rid, ipc_call_t *request)
840{
841 devmap_handle_t handle = (devmap_handle_t) IPC_GET_ARG1(*request);
842 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
843 aoff64_t size = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request),
844 IPC_GET_ARG4(*request));
845 fs_node_t *fn;
846 int r;
847
848 r = mfs_node_get(&fn, handle, index);
849 if (r != EOK) {
850 async_answer_0(rid, r);
851 return;
852 }
853
854 if (!fn) {
855 async_answer_0(rid, r);
856 return;
857 }
858
859 struct mfs_node *mnode = fn->data;
860 struct mfs_ino_info *ino_i = mnode->ino_i;
861
862 if (ino_i->i_size == size)
863 r = EOK;
864 else
865 r = inode_shrink(mnode, ino_i->i_size - size);
866
867 async_answer_0(rid, r);
868 mfs_node_put(fn);
869}
870
871int mfs_instance_get(devmap_handle_t handle, struct mfs_instance **instance)
872{
873 link_t *link;
874 struct mfs_instance *instance_ptr;
875
876 fibril_mutex_lock(&inst_list_mutex);
877
878 for (link = inst_list.next; link != &inst_list; link = link->next) {
879 instance_ptr = list_get_instance(link, struct mfs_instance,
880 link);
881
882 if (instance_ptr->handle == handle) {
883 *instance = instance_ptr;
884 fibril_mutex_unlock(&inst_list_mutex);
885 return EOK;
886 }
887 }
888
889 mfsdebug("Instance not found\n");
890
891 fibril_mutex_unlock(&inst_list_mutex);
892 return EINVAL;
893}
894
895static bool check_magic_number(uint16_t magic, bool *native,
896 mfs_version_t *version, bool *longfilenames)
897{
898 bool rc = true;
899 *longfilenames = false;
900
901 if (magic == MFS_MAGIC_V1 || magic == MFS_MAGIC_V1R) {
902 *native = magic == MFS_MAGIC_V1;
903 *version = MFS_VERSION_V1;
904 } else if (magic == MFS_MAGIC_V1L || magic == MFS_MAGIC_V1LR) {
905 *native = magic == MFS_MAGIC_V1L;
906 *version = MFS_VERSION_V1;
907 *longfilenames = true;
908 } else if (magic == MFS_MAGIC_V2 || magic == MFS_MAGIC_V2R) {
909 *native = magic == MFS_MAGIC_V2;
910 *version = MFS_VERSION_V2;
911 } else if (magic == MFS_MAGIC_V2L || magic == MFS_MAGIC_V2LR) {
912 *native = magic == MFS_MAGIC_V2L;
913 *version = MFS_VERSION_V2;
914 *longfilenames = true;
915 } else if (magic == MFS_MAGIC_V3 || magic == MFS_MAGIC_V3R) {
916 *native = magic == MFS_MAGIC_V3;
917 *version = MFS_VERSION_V3;
918 } else
919 rc = false;
920
921 return rc;
922}
923
924void
925mfs_close(ipc_callid_t rid, ipc_call_t *request)
926{
927 async_answer_0(rid, EOK);
928}
929
930void
931mfs_open_node(ipc_callid_t rid, ipc_call_t *request)
932{
933 libfs_open_node(&mfs_libfs_ops, mfs_reg.fs_handle, rid, request);
934}
935
936/**
937 * @}
938 */
939
Note: See TracBrowser for help on using the repository browser.