1 | /*
|
---|
2 | * Copyright (c) 2011 Frantisek Princ
|
---|
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 | /**
|
---|
34 | * @file ext4fs_ops.c
|
---|
35 | * @brief VFS operations for EXT4 filesystem.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <errno.h>
|
---|
39 | #include <fibril_synch.h>
|
---|
40 | #include <libext4.h>
|
---|
41 | #include <libfs.h>
|
---|
42 | #include <macros.h>
|
---|
43 | #include <malloc.h>
|
---|
44 | #include <string.h>
|
---|
45 | #include <adt/hash_table.h>
|
---|
46 | #include <ipc/loc.h>
|
---|
47 | #include "ext4fs.h"
|
---|
48 | #include "../../vfs/vfs.h"
|
---|
49 |
|
---|
50 | #define EXT4FS_NODE(node) ((node) ? (ext4fs_node_t *) (node)->data : NULL)
|
---|
51 |
|
---|
52 | #define OPEN_NODES_KEYS 2
|
---|
53 | #define OPEN_NODES_DEV_HANDLE_KEY 0
|
---|
54 | #define OPEN_NODES_INODE_KEY 1
|
---|
55 | #define OPEN_NODES_BUCKETS 256
|
---|
56 |
|
---|
57 | typedef struct ext4fs_instance {
|
---|
58 | link_t link;
|
---|
59 | service_id_t service_id;
|
---|
60 | ext4_filesystem_t *filesystem;
|
---|
61 | unsigned int open_nodes_count;
|
---|
62 | } ext4fs_instance_t;
|
---|
63 |
|
---|
64 | typedef struct ext4fs_node {
|
---|
65 | ext4fs_instance_t *instance;
|
---|
66 | ext4_inode_ref_t *inode_ref;
|
---|
67 | fs_node_t *fs_node;
|
---|
68 | link_t link;
|
---|
69 | unsigned int references;
|
---|
70 | } ext4fs_node_t;
|
---|
71 |
|
---|
72 | /*
|
---|
73 | * Forward declarations of auxiliary functions
|
---|
74 | */
|
---|
75 |
|
---|
76 | static int ext4fs_read_directory(ipc_callid_t, aoff64_t, size_t,
|
---|
77 | ext4fs_instance_t *, ext4_inode_ref_t *, size_t *);
|
---|
78 | static int ext4fs_read_file(ipc_callid_t, aoff64_t, size_t, ext4fs_instance_t *,
|
---|
79 | ext4_inode_ref_t *, size_t *);
|
---|
80 | static bool ext4fs_is_dots(const uint8_t *, size_t);
|
---|
81 | static int ext4fs_instance_get(service_id_t, ext4fs_instance_t **);
|
---|
82 | static int ext4fs_node_get_core(fs_node_t **, ext4fs_instance_t *, fs_index_t);
|
---|
83 | static int ext4fs_node_put_core(ext4fs_node_t *);
|
---|
84 |
|
---|
85 | /*
|
---|
86 | * Forward declarations of EXT4 libfs operations.
|
---|
87 | */
|
---|
88 | static int ext4fs_root_get(fs_node_t **, service_id_t);
|
---|
89 | static int ext4fs_match(fs_node_t **, fs_node_t *, const char *);
|
---|
90 | static int ext4fs_node_get(fs_node_t **, service_id_t, fs_index_t);
|
---|
91 | static int ext4fs_node_open(fs_node_t *);
|
---|
92 | static int ext4fs_node_put(fs_node_t *);
|
---|
93 | static int ext4fs_create_node(fs_node_t **, service_id_t, int);
|
---|
94 | static int ext4fs_destroy_node(fs_node_t *);
|
---|
95 | static int ext4fs_link(fs_node_t *, fs_node_t *, const char *);
|
---|
96 | static int ext4fs_unlink(fs_node_t *, fs_node_t *, const char *);
|
---|
97 | static int ext4fs_has_children(bool *, fs_node_t *);
|
---|
98 | static fs_index_t ext4fs_index_get(fs_node_t *);
|
---|
99 | static aoff64_t ext4fs_size_get(fs_node_t *);
|
---|
100 | static unsigned ext4fs_lnkcnt_get(fs_node_t *);
|
---|
101 | static bool ext4fs_is_directory(fs_node_t *);
|
---|
102 | static bool ext4fs_is_file(fs_node_t *node);
|
---|
103 | static service_id_t ext4fs_service_get(fs_node_t *node);
|
---|
104 |
|
---|
105 | /*
|
---|
106 | * Static variables
|
---|
107 | */
|
---|
108 | static LIST_INITIALIZE(instance_list);
|
---|
109 | static FIBRIL_MUTEX_INITIALIZE(instance_list_mutex);
|
---|
110 | static hash_table_t open_nodes;
|
---|
111 | static FIBRIL_MUTEX_INITIALIZE(open_nodes_lock);
|
---|
112 |
|
---|
113 | /* Hash table interface for open nodes hash table */
|
---|
114 | static hash_index_t open_nodes_hash(unsigned long key[])
|
---|
115 | {
|
---|
116 | /* TODO: This is very simple and probably can be improved */
|
---|
117 | return key[OPEN_NODES_INODE_KEY] % OPEN_NODES_BUCKETS;
|
---|
118 | }
|
---|
119 |
|
---|
120 | static int open_nodes_compare(unsigned long key[], hash_count_t keys,
|
---|
121 | link_t *item)
|
---|
122 | {
|
---|
123 | ext4fs_node_t *enode = hash_table_get_instance(item, ext4fs_node_t, link);
|
---|
124 | assert(keys > 0);
|
---|
125 | if (enode->instance->service_id !=
|
---|
126 | ((service_id_t) key[OPEN_NODES_DEV_HANDLE_KEY])) {
|
---|
127 | return false;
|
---|
128 | }
|
---|
129 | if (keys == 1) {
|
---|
130 | return true;
|
---|
131 | }
|
---|
132 | assert(keys == 2);
|
---|
133 | return (enode->inode_ref->index == key[OPEN_NODES_INODE_KEY]);
|
---|
134 | }
|
---|
135 |
|
---|
136 | static void open_nodes_remove_cb(link_t *link)
|
---|
137 | {
|
---|
138 | /* We don't use remove callback for this hash table */
|
---|
139 | }
|
---|
140 |
|
---|
141 | static hash_table_operations_t open_nodes_ops = {
|
---|
142 | .hash = open_nodes_hash,
|
---|
143 | .compare = open_nodes_compare,
|
---|
144 | .remove_callback = open_nodes_remove_cb,
|
---|
145 | };
|
---|
146 |
|
---|
147 |
|
---|
148 | int ext4fs_global_init(void)
|
---|
149 | {
|
---|
150 | if (!hash_table_create(&open_nodes, OPEN_NODES_BUCKETS,
|
---|
151 | OPEN_NODES_KEYS, &open_nodes_ops)) {
|
---|
152 | return ENOMEM;
|
---|
153 | }
|
---|
154 | return EOK;
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | int ext4fs_global_fini(void)
|
---|
159 | {
|
---|
160 | hash_table_destroy(&open_nodes);
|
---|
161 | return EOK;
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | /*
|
---|
166 | * EXT4 libfs operations.
|
---|
167 | */
|
---|
168 |
|
---|
169 | int ext4fs_instance_get(service_id_t service_id, ext4fs_instance_t **inst)
|
---|
170 | {
|
---|
171 | ext4fs_instance_t *tmp;
|
---|
172 |
|
---|
173 | fibril_mutex_lock(&instance_list_mutex);
|
---|
174 |
|
---|
175 | if (list_empty(&instance_list)) {
|
---|
176 | fibril_mutex_unlock(&instance_list_mutex);
|
---|
177 | return EINVAL;
|
---|
178 | }
|
---|
179 |
|
---|
180 | list_foreach(instance_list, link) {
|
---|
181 | tmp = list_get_instance(link, ext4fs_instance_t, link);
|
---|
182 |
|
---|
183 | if (tmp->service_id == service_id) {
|
---|
184 | *inst = tmp;
|
---|
185 | fibril_mutex_unlock(&instance_list_mutex);
|
---|
186 | return EOK;
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | fibril_mutex_unlock(&instance_list_mutex);
|
---|
191 | return EINVAL;
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | int ext4fs_root_get(fs_node_t **rfn, service_id_t service_id)
|
---|
196 | {
|
---|
197 | return ext4fs_node_get(rfn, service_id, EXT4_INODE_ROOT_INDEX);
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 | int ext4fs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
|
---|
202 | {
|
---|
203 | ext4fs_node_t *eparent = EXT4FS_NODE(pfn);
|
---|
204 | ext4_filesystem_t *fs;
|
---|
205 | ext4_directory_iterator_t it;
|
---|
206 | int rc;
|
---|
207 |
|
---|
208 | fs = eparent->instance->filesystem;
|
---|
209 |
|
---|
210 | if (!ext4_inode_is_type(fs->superblock, eparent->inode_ref->inode,
|
---|
211 | EXT4_INODE_MODE_DIRECTORY)) {
|
---|
212 | return ENOTDIR;
|
---|
213 | }
|
---|
214 |
|
---|
215 | rc = ext4_directory_iterator_init(&it, fs, eparent->inode_ref, 0);
|
---|
216 | if (rc != EOK) {
|
---|
217 | return rc;
|
---|
218 | }
|
---|
219 |
|
---|
220 | rc = ext4_directory_find_entry(&it, eparent->inode_ref, component);
|
---|
221 | if (rc != EOK) {
|
---|
222 | ext4_directory_iterator_fini(&it);
|
---|
223 | return rc;
|
---|
224 | }
|
---|
225 |
|
---|
226 | uint32_t inode = ext4_directory_entry_ll_get_inode(it.current);
|
---|
227 |
|
---|
228 | rc = ext4fs_node_get_core(rfn, eparent->instance, inode);
|
---|
229 | if (rc != EOK) {
|
---|
230 | ext4_directory_iterator_fini(&it);
|
---|
231 | return rc;
|
---|
232 | }
|
---|
233 |
|
---|
234 | ext4_directory_iterator_fini(&it);
|
---|
235 | return EOK;
|
---|
236 | }
|
---|
237 |
|
---|
238 |
|
---|
239 | int ext4fs_node_get(fs_node_t **rfn, service_id_t service_id, fs_index_t index)
|
---|
240 | {
|
---|
241 | ext4fs_instance_t *inst = NULL;
|
---|
242 | int rc;
|
---|
243 |
|
---|
244 | rc = ext4fs_instance_get(service_id, &inst);
|
---|
245 | if (rc != EOK) {
|
---|
246 | return rc;
|
---|
247 | }
|
---|
248 |
|
---|
249 | return ext4fs_node_get_core(rfn, inst, index);
|
---|
250 | }
|
---|
251 |
|
---|
252 |
|
---|
253 | int ext4fs_node_get_core(fs_node_t **rfn, ext4fs_instance_t *inst,
|
---|
254 | fs_index_t index)
|
---|
255 | {
|
---|
256 | int rc;
|
---|
257 | fs_node_t *node = NULL;
|
---|
258 | ext4fs_node_t *enode = NULL;
|
---|
259 |
|
---|
260 | ext4_inode_ref_t *inode_ref = NULL;
|
---|
261 |
|
---|
262 | fibril_mutex_lock(&open_nodes_lock);
|
---|
263 |
|
---|
264 | /* Check if the node is not already open */
|
---|
265 | unsigned long key[] = {
|
---|
266 | [OPEN_NODES_DEV_HANDLE_KEY] = inst->service_id,
|
---|
267 | [OPEN_NODES_INODE_KEY] = index,
|
---|
268 | };
|
---|
269 | link_t *already_open = hash_table_find(&open_nodes, key);
|
---|
270 |
|
---|
271 | if (already_open) {
|
---|
272 | enode = hash_table_get_instance(already_open, ext4fs_node_t, link);
|
---|
273 | *rfn = enode->fs_node;
|
---|
274 | enode->references++;
|
---|
275 |
|
---|
276 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
277 | return EOK;
|
---|
278 | }
|
---|
279 |
|
---|
280 | enode = malloc(sizeof(ext4fs_node_t));
|
---|
281 | if (enode == NULL) {
|
---|
282 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
283 | return ENOMEM;
|
---|
284 | }
|
---|
285 |
|
---|
286 | node = malloc(sizeof(fs_node_t));
|
---|
287 | if (node == NULL) {
|
---|
288 | free(enode);
|
---|
289 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
290 | return ENOMEM;
|
---|
291 | }
|
---|
292 | fs_node_initialize(node);
|
---|
293 |
|
---|
294 | rc = ext4_filesystem_get_inode_ref(inst->filesystem, index, &inode_ref);
|
---|
295 | if (rc != EOK) {
|
---|
296 | free(enode);
|
---|
297 | free(node);
|
---|
298 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
299 | return rc;
|
---|
300 | }
|
---|
301 |
|
---|
302 | enode->inode_ref = inode_ref;
|
---|
303 | enode->instance = inst;
|
---|
304 | enode->references = 1;
|
---|
305 | enode->fs_node = node;
|
---|
306 | link_initialize(&enode->link);
|
---|
307 |
|
---|
308 | node->data = enode;
|
---|
309 | *rfn = node;
|
---|
310 |
|
---|
311 | hash_table_insert(&open_nodes, key, &enode->link);
|
---|
312 | inst->open_nodes_count++;
|
---|
313 |
|
---|
314 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
315 |
|
---|
316 | return EOK;
|
---|
317 | }
|
---|
318 |
|
---|
319 |
|
---|
320 | int ext4fs_node_put_core(ext4fs_node_t *enode)
|
---|
321 | {
|
---|
322 | int rc;
|
---|
323 | unsigned long key[] = {
|
---|
324 | [OPEN_NODES_DEV_HANDLE_KEY] = enode->instance->service_id,
|
---|
325 | [OPEN_NODES_INODE_KEY] = enode->inode_ref->index,
|
---|
326 | };
|
---|
327 |
|
---|
328 | hash_table_remove(&open_nodes, key, OPEN_NODES_KEYS);
|
---|
329 | assert(enode->instance->open_nodes_count > 0);
|
---|
330 | enode->instance->open_nodes_count--;
|
---|
331 |
|
---|
332 | rc = ext4_filesystem_put_inode_ref(enode->inode_ref);
|
---|
333 | if (rc != EOK) {
|
---|
334 | return rc;
|
---|
335 | }
|
---|
336 |
|
---|
337 | free(enode->fs_node);
|
---|
338 | free(enode);
|
---|
339 |
|
---|
340 | return EOK;
|
---|
341 | }
|
---|
342 |
|
---|
343 |
|
---|
344 | int ext4fs_node_open(fs_node_t *fn)
|
---|
345 | {
|
---|
346 | // Stateless operation
|
---|
347 | return EOK;
|
---|
348 | }
|
---|
349 |
|
---|
350 | int ext4fs_node_put(fs_node_t *fn)
|
---|
351 | {
|
---|
352 | int rc;
|
---|
353 | ext4fs_node_t *enode = EXT4FS_NODE(fn);
|
---|
354 |
|
---|
355 | fibril_mutex_lock(&open_nodes_lock);
|
---|
356 |
|
---|
357 | assert(enode->references > 0);
|
---|
358 | enode->references--;
|
---|
359 | if (enode->references == 0) {
|
---|
360 | rc = ext4fs_node_put_core(enode);
|
---|
361 | if (rc != EOK) {
|
---|
362 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
363 | return rc;
|
---|
364 | }
|
---|
365 | }
|
---|
366 |
|
---|
367 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
368 |
|
---|
369 | return EOK;
|
---|
370 | }
|
---|
371 |
|
---|
372 |
|
---|
373 | int ext4fs_create_node(fs_node_t **rfn, service_id_t service_id, int flags)
|
---|
374 | {
|
---|
375 | EXT4FS_DBG("not supported");
|
---|
376 |
|
---|
377 | // TODO
|
---|
378 | return ENOTSUP;
|
---|
379 | }
|
---|
380 |
|
---|
381 |
|
---|
382 | int ext4fs_destroy_node(fs_node_t *fn)
|
---|
383 | {
|
---|
384 | EXT4FS_DBG("");
|
---|
385 |
|
---|
386 | int rc;
|
---|
387 |
|
---|
388 | bool has_children;
|
---|
389 | rc = ext4fs_has_children(&has_children, fn);
|
---|
390 | if (rc != EOK) {
|
---|
391 | ext4fs_node_put(fn);
|
---|
392 | return rc;
|
---|
393 | }
|
---|
394 |
|
---|
395 | if (has_children) {
|
---|
396 | EXT4FS_DBG("destroying non-empty node");
|
---|
397 | ext4fs_node_put(fn);
|
---|
398 | return EINVAL;
|
---|
399 | }
|
---|
400 |
|
---|
401 | ext4fs_node_t *enode = EXT4FS_NODE(fn);
|
---|
402 | ext4_filesystem_t *fs = enode->instance->filesystem;
|
---|
403 | ext4_inode_ref_t *inode_ref = enode->inode_ref;
|
---|
404 |
|
---|
405 | EXT4FS_DBG("destroying \%u", inode_ref->index);
|
---|
406 |
|
---|
407 | rc = ext4_filesystem_truncate_inode(fs, inode_ref, 0);
|
---|
408 | if (rc != EOK) {
|
---|
409 | ext4fs_node_put(fn);
|
---|
410 | return rc;
|
---|
411 | }
|
---|
412 |
|
---|
413 | rc = ext4_filesystem_free_inode(fs, inode_ref);
|
---|
414 | if (rc != EOK) {
|
---|
415 | ext4fs_node_put(fn);
|
---|
416 | return rc;
|
---|
417 | }
|
---|
418 |
|
---|
419 | ext4fs_node_put(fn);
|
---|
420 | return EOK;
|
---|
421 | }
|
---|
422 |
|
---|
423 |
|
---|
424 | int ext4fs_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
|
---|
425 | {
|
---|
426 | EXT4FS_DBG("not supported");
|
---|
427 |
|
---|
428 | // TODO
|
---|
429 | return ENOTSUP;
|
---|
430 | }
|
---|
431 |
|
---|
432 |
|
---|
433 | int ext4fs_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *name)
|
---|
434 | {
|
---|
435 | EXT4FS_DBG("unlinking \%s", name);
|
---|
436 |
|
---|
437 | int rc;
|
---|
438 |
|
---|
439 | bool has_children;
|
---|
440 | rc = ext4fs_has_children(&has_children, cfn);
|
---|
441 | if (rc != EOK) {
|
---|
442 | EXT4FS_DBG("\%s error: \%u", name, rc);
|
---|
443 | return rc;
|
---|
444 | }
|
---|
445 |
|
---|
446 | // Cannot unlink non-empty node
|
---|
447 | if (has_children) {
|
---|
448 | EXT4FS_DBG("\%s is not empty", name);
|
---|
449 | return ENOTEMPTY;
|
---|
450 | }
|
---|
451 |
|
---|
452 | // Remove entry from parent directory
|
---|
453 | ext4_inode_ref_t *parent = EXT4FS_NODE(pfn)->inode_ref;
|
---|
454 | ext4_filesystem_t *fs = EXT4FS_NODE(pfn)->instance->filesystem;
|
---|
455 | rc = ext4_directory_remove_entry(fs, parent, name);
|
---|
456 | if (rc != EOK) {
|
---|
457 | EXT4FS_DBG("\%s removing entry failed: \%u", name, rc);
|
---|
458 | return rc;
|
---|
459 | }
|
---|
460 |
|
---|
461 | // Decrement links count
|
---|
462 | ext4_inode_ref_t * child_inode_ref = EXT4FS_NODE(cfn)->inode_ref;
|
---|
463 |
|
---|
464 | uint32_t lnk_count = ext4_inode_get_links_count(child_inode_ref->inode);
|
---|
465 | EXT4FS_DBG("link count before == \%u", lnk_count);
|
---|
466 | lnk_count--;
|
---|
467 |
|
---|
468 |
|
---|
469 | // If directory - handle links from parent
|
---|
470 | if (lnk_count <= 1 && ext4fs_is_directory(cfn)) {
|
---|
471 | ext4_inode_ref_t *parent_inode_ref = EXT4FS_NODE(pfn)->inode_ref;
|
---|
472 |
|
---|
473 | EXT4FS_DBG("parent index = \%u", parent_inode_ref->index);
|
---|
474 |
|
---|
475 | uint32_t parent_lnk_count = ext4_inode_get_links_count(
|
---|
476 | parent_inode_ref->inode);
|
---|
477 |
|
---|
478 | EXT4FS_DBG("directory will be removed, parent link count = \%u", parent_lnk_count);
|
---|
479 |
|
---|
480 | parent_lnk_count--;
|
---|
481 | ext4_inode_set_links_count(parent_inode_ref->inode, parent_lnk_count);
|
---|
482 |
|
---|
483 | parent_inode_ref->dirty = true;
|
---|
484 | }
|
---|
485 |
|
---|
486 | ext4_inode_set_links_count(child_inode_ref->inode, lnk_count);
|
---|
487 | child_inode_ref->dirty = true;
|
---|
488 |
|
---|
489 | EXT4FS_DBG("link count after == \%u", lnk_count);
|
---|
490 |
|
---|
491 | return EOK;
|
---|
492 | }
|
---|
493 |
|
---|
494 |
|
---|
495 | int ext4fs_has_children(bool *has_children, fs_node_t *fn)
|
---|
496 | {
|
---|
497 | ext4fs_node_t *enode = EXT4FS_NODE(fn);
|
---|
498 | ext4_directory_iterator_t it;
|
---|
499 | ext4_filesystem_t *fs;
|
---|
500 | int rc;
|
---|
501 | bool found = false;
|
---|
502 | size_t name_size;
|
---|
503 |
|
---|
504 | fs = enode->instance->filesystem;
|
---|
505 |
|
---|
506 | if (!ext4_inode_is_type(fs->superblock, enode->inode_ref->inode,
|
---|
507 | EXT4_INODE_MODE_DIRECTORY)) {
|
---|
508 | *has_children = false;
|
---|
509 | return EOK;
|
---|
510 | }
|
---|
511 |
|
---|
512 | rc = ext4_directory_iterator_init(&it, fs, enode->inode_ref, 0);
|
---|
513 | if (rc != EOK) {
|
---|
514 | return rc;
|
---|
515 | }
|
---|
516 |
|
---|
517 | /* Find a non-empty directory entry */
|
---|
518 | while (it.current != NULL) {
|
---|
519 | if (it.current->inode != 0) {
|
---|
520 | name_size = ext4_directory_entry_ll_get_name_length(fs->superblock,
|
---|
521 | it.current);
|
---|
522 | if (!ext4fs_is_dots(it.current->name, name_size)) {
|
---|
523 | found = true;
|
---|
524 | break;
|
---|
525 | }
|
---|
526 | }
|
---|
527 |
|
---|
528 | rc = ext4_directory_iterator_next(&it);
|
---|
529 | if (rc != EOK) {
|
---|
530 | ext4_directory_iterator_fini(&it);
|
---|
531 | return rc;
|
---|
532 | }
|
---|
533 | }
|
---|
534 |
|
---|
535 | rc = ext4_directory_iterator_fini(&it);
|
---|
536 | if (rc != EOK) {
|
---|
537 | return rc;
|
---|
538 | }
|
---|
539 |
|
---|
540 | *has_children = found;
|
---|
541 |
|
---|
542 | return EOK;
|
---|
543 | }
|
---|
544 |
|
---|
545 |
|
---|
546 | fs_index_t ext4fs_index_get(fs_node_t *fn)
|
---|
547 | {
|
---|
548 | ext4fs_node_t *enode = EXT4FS_NODE(fn);
|
---|
549 | return enode->inode_ref->index;
|
---|
550 | }
|
---|
551 |
|
---|
552 |
|
---|
553 | aoff64_t ext4fs_size_get(fs_node_t *fn)
|
---|
554 | {
|
---|
555 | ext4fs_node_t *enode = EXT4FS_NODE(fn);
|
---|
556 | aoff64_t size = ext4_inode_get_size(
|
---|
557 | enode->instance->filesystem->superblock, enode->inode_ref->inode);
|
---|
558 | return size;
|
---|
559 | }
|
---|
560 |
|
---|
561 |
|
---|
562 | unsigned ext4fs_lnkcnt_get(fs_node_t *fn)
|
---|
563 | {
|
---|
564 | if (ext4fs_is_directory(fn)) {
|
---|
565 | if (lnkcnt > 1) {
|
---|
566 | EXT4FS_DBG("dir: returning \%u", 1);
|
---|
567 | return 1;
|
---|
568 | } else {
|
---|
569 | EXT4FS_DBG("dir: returning \%u", 0);
|
---|
570 | return 0;
|
---|
571 | }
|
---|
572 | }
|
---|
573 |
|
---|
574 | // For regular files return real links count
|
---|
575 | ext4fs_node_t *enode = EXT4FS_NODE(fn);
|
---|
576 | return ext4_inode_get_links_count(enode->inode_ref->inode);
|
---|
577 | }
|
---|
578 |
|
---|
579 |
|
---|
580 | bool ext4fs_is_directory(fs_node_t *fn)
|
---|
581 | {
|
---|
582 | ext4fs_node_t *enode = EXT4FS_NODE(fn);
|
---|
583 | bool is_dir = ext4_inode_is_type(enode->instance->filesystem->superblock,
|
---|
584 | enode->inode_ref->inode, EXT4_INODE_MODE_DIRECTORY);
|
---|
585 | return is_dir;
|
---|
586 | }
|
---|
587 |
|
---|
588 |
|
---|
589 | bool ext4fs_is_file(fs_node_t *fn)
|
---|
590 | {
|
---|
591 | ext4fs_node_t *enode = EXT4FS_NODE(fn);
|
---|
592 | bool is_file = ext4_inode_is_type(enode->instance->filesystem->superblock,
|
---|
593 | enode->inode_ref->inode, EXT4_INODE_MODE_FILE);
|
---|
594 | return is_file;
|
---|
595 | }
|
---|
596 |
|
---|
597 |
|
---|
598 | service_id_t ext4fs_service_get(fs_node_t *fn)
|
---|
599 | {
|
---|
600 | ext4fs_node_t *enode = EXT4FS_NODE(fn);
|
---|
601 | return enode->instance->service_id;
|
---|
602 | }
|
---|
603 |
|
---|
604 | /*
|
---|
605 | * libfs operations.
|
---|
606 | */
|
---|
607 | libfs_ops_t ext4fs_libfs_ops = {
|
---|
608 | .root_get = ext4fs_root_get,
|
---|
609 | .match = ext4fs_match,
|
---|
610 | .node_get = ext4fs_node_get,
|
---|
611 | .node_open = ext4fs_node_open,
|
---|
612 | .node_put = ext4fs_node_put,
|
---|
613 | .create = ext4fs_create_node,
|
---|
614 | .destroy = ext4fs_destroy_node,
|
---|
615 | .link = ext4fs_link,
|
---|
616 | .unlink = ext4fs_unlink,
|
---|
617 | .has_children = ext4fs_has_children,
|
---|
618 | .index_get = ext4fs_index_get,
|
---|
619 | .size_get = ext4fs_size_get,
|
---|
620 | .lnkcnt_get = ext4fs_lnkcnt_get,
|
---|
621 | .is_directory = ext4fs_is_directory,
|
---|
622 | .is_file = ext4fs_is_file,
|
---|
623 | .service_get = ext4fs_service_get
|
---|
624 | };
|
---|
625 |
|
---|
626 |
|
---|
627 | /*
|
---|
628 | * VFS operations.
|
---|
629 | */
|
---|
630 |
|
---|
631 | static int ext4fs_mounted(service_id_t service_id, const char *opts,
|
---|
632 | fs_index_t *index, aoff64_t *size, unsigned *lnkcnt)
|
---|
633 | {
|
---|
634 | int rc;
|
---|
635 | ext4_filesystem_t *fs;
|
---|
636 | ext4fs_instance_t *inst;
|
---|
637 | bool read_only;
|
---|
638 |
|
---|
639 | /* Allocate libext4 filesystem structure */
|
---|
640 | fs = (ext4_filesystem_t *) malloc(sizeof(ext4_filesystem_t));
|
---|
641 | if (fs == NULL) {
|
---|
642 | return ENOMEM;
|
---|
643 | }
|
---|
644 |
|
---|
645 | /* Allocate instance structure */
|
---|
646 | inst = (ext4fs_instance_t *) malloc(sizeof(ext4fs_instance_t));
|
---|
647 | if (inst == NULL) {
|
---|
648 | free(fs);
|
---|
649 | return ENOMEM;
|
---|
650 | }
|
---|
651 |
|
---|
652 | /* Initialize the filesystem */
|
---|
653 | rc = ext4_filesystem_init(fs, service_id);
|
---|
654 | if (rc != EOK) {
|
---|
655 | free(fs);
|
---|
656 | free(inst);
|
---|
657 | return rc;
|
---|
658 | }
|
---|
659 |
|
---|
660 | /* Do some sanity checking */
|
---|
661 | rc = ext4_filesystem_check_sanity(fs);
|
---|
662 | if (rc != EOK) {
|
---|
663 | ext4_filesystem_fini(fs, false);
|
---|
664 | free(fs);
|
---|
665 | free(inst);
|
---|
666 | return rc;
|
---|
667 | }
|
---|
668 |
|
---|
669 | /* Check flags */
|
---|
670 | rc = ext4_filesystem_check_features(fs, &read_only);
|
---|
671 | if (rc != EOK) {
|
---|
672 | ext4_filesystem_fini(fs, false);
|
---|
673 | free(fs);
|
---|
674 | free(inst);
|
---|
675 | return rc;
|
---|
676 | }
|
---|
677 |
|
---|
678 | /* Initialize instance */
|
---|
679 | link_initialize(&inst->link);
|
---|
680 | inst->service_id = service_id;
|
---|
681 | inst->filesystem = fs;
|
---|
682 | inst->open_nodes_count = 0;
|
---|
683 |
|
---|
684 | /* Read root node */
|
---|
685 | fs_node_t *root_node;
|
---|
686 | rc = ext4fs_node_get_core(&root_node, inst, EXT4_INODE_ROOT_INDEX);
|
---|
687 | if (rc != EOK) {
|
---|
688 | ext4_filesystem_fini(fs, false);
|
---|
689 | free(fs);
|
---|
690 | free(inst);
|
---|
691 | return rc;
|
---|
692 | }
|
---|
693 |
|
---|
694 | /* Add instance to the list */
|
---|
695 | fibril_mutex_lock(&instance_list_mutex);
|
---|
696 | list_append(&inst->link, &instance_list);
|
---|
697 | fibril_mutex_unlock(&instance_list_mutex);
|
---|
698 |
|
---|
699 | *index = EXT4_INODE_ROOT_INDEX;
|
---|
700 | *size = 0;
|
---|
701 | *lnkcnt = 1;
|
---|
702 |
|
---|
703 | ext4fs_node_put(root_node);
|
---|
704 |
|
---|
705 | return EOK;
|
---|
706 | }
|
---|
707 |
|
---|
708 |
|
---|
709 | static int ext4fs_unmounted(service_id_t service_id)
|
---|
710 | {
|
---|
711 | int rc;
|
---|
712 | ext4fs_instance_t *inst;
|
---|
713 |
|
---|
714 | rc = ext4fs_instance_get(service_id, &inst);
|
---|
715 |
|
---|
716 | if (rc != EOK) {
|
---|
717 | return rc;
|
---|
718 | }
|
---|
719 |
|
---|
720 | fibril_mutex_lock(&open_nodes_lock);
|
---|
721 |
|
---|
722 | if (inst->open_nodes_count != 0) {
|
---|
723 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
724 | return EBUSY;
|
---|
725 | }
|
---|
726 |
|
---|
727 | /* Remove the instance from the list */
|
---|
728 | fibril_mutex_lock(&instance_list_mutex);
|
---|
729 | list_remove(&inst->link);
|
---|
730 | fibril_mutex_unlock(&instance_list_mutex);
|
---|
731 |
|
---|
732 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
733 |
|
---|
734 | return ext4_filesystem_fini(inst->filesystem, true);
|
---|
735 | }
|
---|
736 |
|
---|
737 |
|
---|
738 | static int ext4fs_read(service_id_t service_id, fs_index_t index,
|
---|
739 | aoff64_t pos, size_t *rbytes)
|
---|
740 | {
|
---|
741 | ext4fs_instance_t *inst;
|
---|
742 | ext4_inode_ref_t *inode_ref;
|
---|
743 | int rc;
|
---|
744 |
|
---|
745 | /*
|
---|
746 | * Receive the read request.
|
---|
747 | */
|
---|
748 | ipc_callid_t callid;
|
---|
749 | size_t size;
|
---|
750 | if (!async_data_read_receive(&callid, &size)) {
|
---|
751 | async_answer_0(callid, EINVAL);
|
---|
752 | return EINVAL;
|
---|
753 | }
|
---|
754 |
|
---|
755 | rc = ext4fs_instance_get(service_id, &inst);
|
---|
756 | if (rc != EOK) {
|
---|
757 | async_answer_0(callid, rc);
|
---|
758 | return rc;
|
---|
759 | }
|
---|
760 |
|
---|
761 | rc = ext4_filesystem_get_inode_ref(inst->filesystem, index, &inode_ref);
|
---|
762 | if (rc != EOK) {
|
---|
763 | async_answer_0(callid, rc);
|
---|
764 | return rc;
|
---|
765 | }
|
---|
766 |
|
---|
767 | if (ext4_inode_is_type(inst->filesystem->superblock, inode_ref->inode,
|
---|
768 | EXT4_INODE_MODE_FILE)) {
|
---|
769 | rc = ext4fs_read_file(callid, pos, size, inst, inode_ref,
|
---|
770 | rbytes);
|
---|
771 | } else if (ext4_inode_is_type(inst->filesystem->superblock,
|
---|
772 | inode_ref->inode, EXT4_INODE_MODE_DIRECTORY)) {
|
---|
773 | rc = ext4fs_read_directory(callid, pos, size, inst, inode_ref,
|
---|
774 | rbytes);
|
---|
775 | } else {
|
---|
776 | /* Other inode types not supported */
|
---|
777 | async_answer_0(callid, ENOTSUP);
|
---|
778 | rc = ENOTSUP;
|
---|
779 | }
|
---|
780 |
|
---|
781 | ext4_filesystem_put_inode_ref(inode_ref);
|
---|
782 |
|
---|
783 | return rc;
|
---|
784 | }
|
---|
785 |
|
---|
786 | bool ext4fs_is_dots(const uint8_t *name, size_t name_size)
|
---|
787 | {
|
---|
788 | if (name_size == 1 && name[0] == '.') {
|
---|
789 | return true;
|
---|
790 | }
|
---|
791 |
|
---|
792 | if (name_size == 2 && name[0] == '.' && name[1] == '.') {
|
---|
793 | return true;
|
---|
794 | }
|
---|
795 |
|
---|
796 | return false;
|
---|
797 | }
|
---|
798 |
|
---|
799 | int ext4fs_read_directory(ipc_callid_t callid, aoff64_t pos, size_t size,
|
---|
800 | ext4fs_instance_t *inst, ext4_inode_ref_t *inode_ref, size_t *rbytes)
|
---|
801 | {
|
---|
802 | ext4_directory_iterator_t it;
|
---|
803 | aoff64_t next;
|
---|
804 | uint8_t *buf;
|
---|
805 | size_t name_size;
|
---|
806 | int rc;
|
---|
807 | bool found = false;
|
---|
808 |
|
---|
809 | rc = ext4_directory_iterator_init(&it, inst->filesystem, inode_ref, pos);
|
---|
810 | if (rc != EOK) {
|
---|
811 | async_answer_0(callid, rc);
|
---|
812 | return rc;
|
---|
813 | }
|
---|
814 |
|
---|
815 | /* Find next interesting directory entry.
|
---|
816 | * We want to skip . and .. entries
|
---|
817 | * as these are not used in HelenOS
|
---|
818 | */
|
---|
819 | while (it.current != NULL) {
|
---|
820 |
|
---|
821 | if (it.current->inode == 0) {
|
---|
822 | goto skip;
|
---|
823 | }
|
---|
824 |
|
---|
825 | name_size = ext4_directory_entry_ll_get_name_length(
|
---|
826 | inst->filesystem->superblock, it.current);
|
---|
827 |
|
---|
828 | /* skip . and .. */
|
---|
829 | if (ext4fs_is_dots(it.current->name, name_size)) {
|
---|
830 | goto skip;
|
---|
831 | }
|
---|
832 |
|
---|
833 | /* The on-disk entry does not contain \0 at the end
|
---|
834 | * end of entry name, so we copy it to new buffer
|
---|
835 | * and add the \0 at the end
|
---|
836 | */
|
---|
837 | buf = malloc(name_size+1);
|
---|
838 | if (buf == NULL) {
|
---|
839 | ext4_directory_iterator_fini(&it);
|
---|
840 | async_answer_0(callid, ENOMEM);
|
---|
841 | return ENOMEM;
|
---|
842 | }
|
---|
843 | memcpy(buf, &it.current->name, name_size);
|
---|
844 | *(buf + name_size) = 0;
|
---|
845 | found = true;
|
---|
846 | (void) async_data_read_finalize(callid, buf, name_size + 1);
|
---|
847 | free(buf);
|
---|
848 | break;
|
---|
849 |
|
---|
850 | skip:
|
---|
851 | rc = ext4_directory_iterator_next(&it);
|
---|
852 | if (rc != EOK) {
|
---|
853 | ext4_directory_iterator_fini(&it);
|
---|
854 | async_answer_0(callid, rc);
|
---|
855 | return rc;
|
---|
856 | }
|
---|
857 | }
|
---|
858 |
|
---|
859 | if (found) {
|
---|
860 | rc = ext4_directory_iterator_next(&it);
|
---|
861 | if (rc != EOK) {
|
---|
862 | return rc;
|
---|
863 | }
|
---|
864 | next = it.current_offset;
|
---|
865 | }
|
---|
866 |
|
---|
867 | rc = ext4_directory_iterator_fini(&it);
|
---|
868 | if (rc != EOK) {
|
---|
869 | return rc;
|
---|
870 | }
|
---|
871 |
|
---|
872 | if (found) {
|
---|
873 | *rbytes = next - pos;
|
---|
874 | return EOK;
|
---|
875 | } else {
|
---|
876 | async_answer_0(callid, ENOENT);
|
---|
877 | return ENOENT;
|
---|
878 | }
|
---|
879 | }
|
---|
880 |
|
---|
881 | int ext4fs_read_file(ipc_callid_t callid, aoff64_t pos, size_t size,
|
---|
882 | ext4fs_instance_t *inst, ext4_inode_ref_t *inode_ref, size_t *rbytes)
|
---|
883 | {
|
---|
884 | int rc;
|
---|
885 | uint32_t block_size;
|
---|
886 | aoff64_t file_block;
|
---|
887 | uint64_t file_size;
|
---|
888 | uint32_t fs_block;
|
---|
889 | size_t offset_in_block;
|
---|
890 | size_t bytes;
|
---|
891 | block_t *block;
|
---|
892 | uint8_t *buffer;
|
---|
893 |
|
---|
894 | file_size = ext4_inode_get_size(inst->filesystem->superblock,
|
---|
895 | inode_ref->inode);
|
---|
896 |
|
---|
897 | if (pos >= file_size) {
|
---|
898 | /* Read 0 bytes successfully */
|
---|
899 | async_data_read_finalize(callid, NULL, 0);
|
---|
900 | *rbytes = 0;
|
---|
901 | return EOK;
|
---|
902 | }
|
---|
903 |
|
---|
904 | /* For now, we only read data from one block at a time */
|
---|
905 | block_size = ext4_superblock_get_block_size(inst->filesystem->superblock);
|
---|
906 | file_block = pos / block_size;
|
---|
907 | offset_in_block = pos % block_size;
|
---|
908 | bytes = min(block_size - offset_in_block, size);
|
---|
909 |
|
---|
910 | /* Handle end of file */
|
---|
911 | if (pos + bytes > file_size) {
|
---|
912 | bytes = file_size - pos;
|
---|
913 | }
|
---|
914 |
|
---|
915 | /* Get the real block number */
|
---|
916 | rc = ext4_filesystem_get_inode_data_block_index(inst->filesystem,
|
---|
917 | inode_ref->inode, file_block, &fs_block);
|
---|
918 | if (rc != EOK) {
|
---|
919 | async_answer_0(callid, rc);
|
---|
920 | return rc;
|
---|
921 | }
|
---|
922 |
|
---|
923 | /* Check for sparse file
|
---|
924 | * If ext4_filesystem_get_inode_data_block_index returned
|
---|
925 | * fs_block == 0, it means that the given block is not allocated for the
|
---|
926 | * file and we need to return a buffer of zeros
|
---|
927 | */
|
---|
928 | if (fs_block == 0) {
|
---|
929 | buffer = malloc(bytes);
|
---|
930 | if (buffer == NULL) {
|
---|
931 | async_answer_0(callid, ENOMEM);
|
---|
932 | return ENOMEM;
|
---|
933 | }
|
---|
934 |
|
---|
935 | memset(buffer, 0, bytes);
|
---|
936 |
|
---|
937 | async_data_read_finalize(callid, buffer, bytes);
|
---|
938 | *rbytes = bytes;
|
---|
939 |
|
---|
940 | free(buffer);
|
---|
941 |
|
---|
942 | return EOK;
|
---|
943 | }
|
---|
944 |
|
---|
945 | /* Usual case - we need to read a block from device */
|
---|
946 | rc = block_get(&block, inst->service_id, fs_block, BLOCK_FLAGS_NONE);
|
---|
947 | if (rc != EOK) {
|
---|
948 | async_answer_0(callid, rc);
|
---|
949 | return rc;
|
---|
950 | }
|
---|
951 |
|
---|
952 | assert(offset_in_block + bytes <= block_size);
|
---|
953 | async_data_read_finalize(callid, block->data + offset_in_block, bytes);
|
---|
954 |
|
---|
955 | rc = block_put(block);
|
---|
956 | if (rc != EOK) {
|
---|
957 | return rc;
|
---|
958 | }
|
---|
959 |
|
---|
960 | *rbytes = bytes;
|
---|
961 | return EOK;
|
---|
962 | }
|
---|
963 |
|
---|
964 | static int ext4fs_write(service_id_t service_id, fs_index_t index,
|
---|
965 | aoff64_t pos, size_t *wbytes, aoff64_t *nsize)
|
---|
966 | {
|
---|
967 | int rc;
|
---|
968 | int flags = BLOCK_FLAGS_NONE;
|
---|
969 | fs_node_t *fn;
|
---|
970 | ext4fs_node_t *enode;
|
---|
971 | ext4_filesystem_t *fs;
|
---|
972 | ext4_inode_ref_t *inode_ref;
|
---|
973 | ipc_callid_t callid;
|
---|
974 | size_t len, bytes, block_size;
|
---|
975 | block_t *write_block;
|
---|
976 | uint32_t fblock, iblock;
|
---|
977 | uint32_t old_inode_size;
|
---|
978 |
|
---|
979 | rc = ext4fs_node_get(&fn, service_id, index);
|
---|
980 | if (rc != EOK) {
|
---|
981 | EXT4FS_DBG("node get error");
|
---|
982 | return rc;
|
---|
983 | }
|
---|
984 |
|
---|
985 | if (!async_data_write_receive(&callid, &len)) {
|
---|
986 | rc = EINVAL;
|
---|
987 | ext4fs_node_put(fn);
|
---|
988 | async_answer_0(callid, rc);
|
---|
989 | EXT4FS_DBG("data write recv");
|
---|
990 | return rc;
|
---|
991 | }
|
---|
992 |
|
---|
993 |
|
---|
994 | enode = EXT4FS_NODE(fn);
|
---|
995 | inode_ref = enode->inode_ref;
|
---|
996 | fs = enode->instance->filesystem;
|
---|
997 |
|
---|
998 | block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
999 |
|
---|
1000 | // Prevent writing to more than one block
|
---|
1001 | bytes = min(len, block_size - (pos % block_size));
|
---|
1002 |
|
---|
1003 | if (bytes == block_size) {
|
---|
1004 | flags = BLOCK_FLAGS_NOREAD;
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | iblock = pos / block_size;
|
---|
1008 |
|
---|
1009 | rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref->inode, iblock, &fblock);
|
---|
1010 | if (rc != EOK) {
|
---|
1011 | // TODO error
|
---|
1012 | ext4fs_node_put(fn);
|
---|
1013 | EXT4FS_DBG("error loading block addr");
|
---|
1014 | return rc;
|
---|
1015 | }
|
---|
1016 |
|
---|
1017 | if (fblock == 0) {
|
---|
1018 | rc = ext4_balloc_alloc_block(fs, inode_ref, &fblock);
|
---|
1019 | if (rc != EOK) {
|
---|
1020 | ext4fs_node_put(fn);
|
---|
1021 | async_answer_0(callid, rc);
|
---|
1022 | return rc;
|
---|
1023 | }
|
---|
1024 |
|
---|
1025 | rc = ext4_filesystem_set_inode_data_block_index(fs, inode_ref, iblock, fblock);
|
---|
1026 | if (rc != EOK) {
|
---|
1027 | EXT4FS_DBG("ERROR: setting index failed");
|
---|
1028 | }
|
---|
1029 | inode_ref->dirty = true;
|
---|
1030 |
|
---|
1031 | flags = BLOCK_FLAGS_NOREAD;
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | rc = block_get(&write_block, service_id, fblock, flags);
|
---|
1035 | if (rc != EOK) {
|
---|
1036 | EXT4FS_DBG("error in loading block \%d", rc);
|
---|
1037 | ext4fs_node_put(fn);
|
---|
1038 | async_answer_0(callid, rc);
|
---|
1039 | return rc;
|
---|
1040 | }
|
---|
1041 |
|
---|
1042 | if (flags == BLOCK_FLAGS_NOREAD) {
|
---|
1043 | memset(write_block->data, 0, block_size);
|
---|
1044 | }
|
---|
1045 |
|
---|
1046 | rc = async_data_write_finalize(callid, write_block->data + (pos % block_size), bytes);
|
---|
1047 | if (rc != EOK) {
|
---|
1048 | // TODO error
|
---|
1049 | EXT4FS_DBG("error in write finalize \%d", rc);
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | write_block->dirty = true;
|
---|
1053 |
|
---|
1054 | rc = block_put(write_block);
|
---|
1055 | if (rc != EOK) {
|
---|
1056 | EXT4FS_DBG("error in writing block \%d", rc);
|
---|
1057 | ext4fs_node_put(fn);
|
---|
1058 | return rc;
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | old_inode_size = ext4_inode_get_size(fs->superblock, inode_ref->inode);
|
---|
1062 | if (pos + bytes > old_inode_size) {
|
---|
1063 | ext4_inode_set_size(inode_ref->inode, pos + bytes);
|
---|
1064 | inode_ref->dirty = true;
|
---|
1065 | }
|
---|
1066 |
|
---|
1067 | *nsize = ext4_inode_get_size(fs->superblock, inode_ref->inode);
|
---|
1068 | *wbytes = bytes;
|
---|
1069 |
|
---|
1070 | return ext4fs_node_put(fn);
|
---|
1071 | }
|
---|
1072 |
|
---|
1073 |
|
---|
1074 | static int ext4fs_truncate(service_id_t service_id, fs_index_t index,
|
---|
1075 | aoff64_t new_size)
|
---|
1076 | {
|
---|
1077 | int rc;
|
---|
1078 | fs_node_t *fn;
|
---|
1079 |
|
---|
1080 | rc = ext4fs_node_get(&fn, service_id, index);
|
---|
1081 | if (rc != EOK) {
|
---|
1082 | return rc;
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 | ext4fs_node_t *enode = EXT4FS_NODE(fn);
|
---|
1086 | ext4_inode_ref_t *inode_ref = enode->inode_ref;
|
---|
1087 | ext4_filesystem_t *fs = enode->instance->filesystem;
|
---|
1088 |
|
---|
1089 | rc = ext4_filesystem_truncate_inode(fs, inode_ref, new_size);
|
---|
1090 | ext4fs_node_put(fn);
|
---|
1091 |
|
---|
1092 | return rc;
|
---|
1093 | }
|
---|
1094 |
|
---|
1095 |
|
---|
1096 | static int ext4fs_close(service_id_t service_id, fs_index_t index)
|
---|
1097 | {
|
---|
1098 | return EOK;
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 |
|
---|
1102 | static int ext4fs_destroy(service_id_t service_id, fs_index_t index)
|
---|
1103 | {
|
---|
1104 | int rc;
|
---|
1105 | fs_node_t *fn;
|
---|
1106 |
|
---|
1107 | rc = ext4fs_node_get(&fn, service_id, index);
|
---|
1108 | if (rc != EOK) {
|
---|
1109 | return rc;
|
---|
1110 | }
|
---|
1111 |
|
---|
1112 | /* Destroy the inode */
|
---|
1113 | return ext4fs_destroy_node(fn);
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 |
|
---|
1117 | static int ext4fs_sync(service_id_t service_id, fs_index_t index)
|
---|
1118 | {
|
---|
1119 | int rc;
|
---|
1120 | fs_node_t *fn;
|
---|
1121 | ext4fs_node_t *enode;
|
---|
1122 |
|
---|
1123 | rc = ext4fs_node_get(&fn, service_id, index);
|
---|
1124 | if (rc != EOK) {
|
---|
1125 | return rc;
|
---|
1126 | }
|
---|
1127 |
|
---|
1128 | enode = EXT4FS_NODE(fn);
|
---|
1129 | enode->inode_ref->dirty = true;
|
---|
1130 |
|
---|
1131 | return ext4fs_node_put(fn);
|
---|
1132 | }
|
---|
1133 |
|
---|
1134 | vfs_out_ops_t ext4fs_ops = {
|
---|
1135 | .mounted = ext4fs_mounted,
|
---|
1136 | .unmounted = ext4fs_unmounted,
|
---|
1137 | .read = ext4fs_read,
|
---|
1138 | .write = ext4fs_write,
|
---|
1139 | .truncate = ext4fs_truncate,
|
---|
1140 | .close = ext4fs_close,
|
---|
1141 | .destroy = ext4fs_destroy,
|
---|
1142 | .sync = ext4fs_sync,
|
---|
1143 | };
|
---|
1144 |
|
---|
1145 | /**
|
---|
1146 | * @}
|
---|
1147 | */
|
---|