c51dd8cf660fa42d2e4b0617b5cfe9ac69d9dd72
[lwext4.git] / src / ext4.c
1 /*
2  * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
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 lwext4
30  * @{
31  */
32 /**
33  * @file  ext4.h
34  * @brief Ext4 high level operations (file, directory, mountpoints...)
35  */
36
37 #include "ext4_config.h"
38 #include "ext4_types.h"
39 #include "ext4_misc.h"
40 #include "ext4_errno.h"
41 #include "ext4_oflags.h"
42 #include "ext4_debug.h"
43
44 #include "ext4.h"
45 #include "ext4_trans.h"
46 #include "ext4_blockdev.h"
47 #include "ext4_fs.h"
48 #include "ext4_dir.h"
49 #include "ext4_inode.h"
50 #include "ext4_super.h"
51 #include "ext4_block_group.h"
52 #include "ext4_dir_idx.h"
53 #include "ext4_xattr.h"
54 #include "ext4_journal.h"
55
56
57 #include <stdlib.h>
58 #include <string.h>
59
60 /**@brief   Mount point OS dependent lock*/
61 #define EXT4_MP_LOCK(_m)                                                       \
62         do {                                                                   \
63                 if ((_m)->os_locks)                                            \
64                         (_m)->os_locks->lock();                                \
65         } while (0)
66
67 /**@brief   Mount point OS dependent unlock*/
68 #define EXT4_MP_UNLOCK(_m)                                                     \
69         do {                                                                   \
70                 if ((_m)->os_locks)                                            \
71                         (_m)->os_locks->unlock();                              \
72         } while (0)
73
74 /**@brief   Mount point descriptor.*/
75 struct ext4_mountpoint {
76
77         /**@brief   Mount done flag.*/
78         bool mounted;
79
80         /**@brief   Mount point name (@ref ext4_mount)*/
81         char name[32];
82
83         /**@brief   OS dependent lock/unlock functions.*/
84         const struct ext4_lock *os_locks;
85
86         /**@brief   Ext4 filesystem internals.*/
87         struct ext4_fs fs;
88
89         /**@brief   Dynamic allocation cache flag.*/
90         bool cache_dynamic;
91
92         struct jbd_fs jbd_fs;
93         struct jbd_journal jbd_journal;
94 };
95
96 /**@brief   Block devices descriptor.*/
97 struct _ext4_devices {
98
99         /**@brief   Block device name (@ref ext4_device_register)*/
100         char name[32];
101
102         /**@brief   Block device handle.*/
103         struct ext4_blockdev *bd;
104
105         /**@brief   Block cache handle.*/
106         struct ext4_bcache *bc;
107 };
108
109 /**@brief   Block devices.*/
110 struct _ext4_devices _bdevices[CONFIG_EXT4_BLOCKDEVS_COUNT];
111
112 /**@brief   Mountpoints.*/
113 struct ext4_mountpoint _mp[CONFIG_EXT4_MOUNTPOINTS_COUNT];
114
115 int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
116                          const char *dev_name)
117 {
118         uint32_t i;
119         ext4_assert(bd && dev_name);
120
121         for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
122                 if (!_bdevices[i].bd) {
123                         strcpy(_bdevices[i].name, dev_name);
124                         _bdevices[i].bd = bd;
125                         _bdevices[i].bc = bc;
126                         return EOK;
127                 }
128
129                 if (!strcmp(_bdevices[i].name, dev_name))
130                         return EOK;
131         }
132         return ENOSPC;
133 }
134
135 /****************************************************************************/
136
137 static bool ext4_is_dots(const uint8_t *name, size_t name_size)
138 {
139         if ((name_size == 1) && (name[0] == '.'))
140                 return true;
141
142         if ((name_size == 2) && (name[0] == '.') && (name[1] == '.'))
143                 return true;
144
145         return false;
146 }
147
148 static int ext4_has_children(bool *has_children, struct ext4_inode_ref *enode)
149 {
150         struct ext4_sblock *sb = &enode->fs->sb;
151
152         /* Check if node is directory */
153         if (!ext4_inode_is_type(sb, enode->inode, EXT4_INODE_MODE_DIRECTORY)) {
154                 *has_children = false;
155                 return EOK;
156         }
157
158         struct ext4_dir_iter it;
159         int rc = ext4_dir_iterator_init(&it, enode, 0);
160         if (rc != EOK)
161                 return rc;
162
163         /* Find a non-empty directory entry */
164         bool found = false;
165         while (it.curr != NULL) {
166                 if (ext4_dir_en_get_inode(it.curr) != 0) {
167                         uint16_t nsize;
168                         nsize = ext4_dir_en_get_name_len(sb, it.curr);
169                         if (!ext4_is_dots(it.curr->name, nsize)) {
170                                 found = true;
171                                 break;
172                         }
173                 }
174
175                 rc = ext4_dir_iterator_next(&it);
176                 if (rc != EOK) {
177                         ext4_dir_iterator_fini(&it);
178                         return rc;
179                 }
180         }
181
182         rc = ext4_dir_iterator_fini(&it);
183         if (rc != EOK)
184                 return rc;
185
186         *has_children = found;
187
188         return EOK;
189 }
190
191 static int ext4_link(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent,
192                      struct ext4_inode_ref *ch, const char *n,
193                      uint32_t len, bool rename)
194 {
195         /* Check maximum name length */
196         if (len > EXT4_DIRECTORY_FILENAME_LEN)
197                 return EINVAL;
198
199         /* Add entry to parent directory */
200         int r = ext4_dir_add_entry(parent, n, len, ch);
201         if (r != EOK)
202                 return r;
203
204         /* Fill new dir -> add '.' and '..' entries.
205          * Also newly allocated inode should have 0 link count.
206          */
207
208         bool is_dir = ext4_inode_is_type(&mp->fs.sb, ch->inode,
209                                EXT4_INODE_MODE_DIRECTORY);
210         if (is_dir && !rename) {
211
212 #if CONFIG_DIR_INDEX_ENABLE
213                 /* Initialize directory index if supported */
214                 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
215                         r = ext4_dir_dx_init(ch, parent);
216                         if (r != EOK)
217                                 return r;
218
219                         ext4_inode_set_flag(ch->inode, EXT4_INODE_FLAG_INDEX);
220                         ch->dirty = true;
221                 } else
222 #endif
223                 {
224                         r = ext4_dir_add_entry(ch, ".", strlen("."), ch);
225                         if (r != EOK) {
226                                 ext4_dir_remove_entry(parent, n, strlen(n));
227                                 return r;
228                         }
229
230                         r = ext4_dir_add_entry(ch, "..", strlen(".."), parent);
231                         if (r != EOK) {
232                                 ext4_dir_remove_entry(parent, n, strlen(n));
233                                 ext4_dir_remove_entry(ch, ".", strlen("."));
234                                 return r;
235                         }
236                 }
237
238                 /*New empty directory. Two links (. and ..) */
239                 ext4_inode_set_links_cnt(ch->inode, 2);
240                 ext4_fs_inode_links_count_inc(parent);
241                 ch->dirty = true;
242                 parent->dirty = true;
243                 return r;
244         }
245         /*
246          * In case we want to rename a directory,
247          * we reset the original '..' pointer.
248          */
249         if (is_dir) {
250                 bool idx;
251                 idx = ext4_inode_has_flag(ch->inode, EXT4_INODE_FLAG_INDEX);
252                 struct ext4_dir_search_result res;
253                 if (!idx) {
254                         r = ext4_dir_find_entry(&res, ch, "..", strlen(".."));
255                         if (r != EOK)
256                                 return EIO;
257
258                         ext4_dir_en_set_inode(res.dentry, parent->index);
259                         ext4_trans_set_block_dirty(res.block.buf);
260                         r = ext4_dir_destroy_result(ch, &res);
261                         if (r != EOK)
262                                 return r;
263
264                 } else {
265 #if CONFIG_DIR_INDEX_ENABLE
266                         r = ext4_dir_dx_reset_parent_inode(ch, parent->index);
267                         if (r != EOK)
268                                 return r;
269
270 #endif
271                 }
272
273                 ext4_fs_inode_links_count_inc(parent);
274                 parent->dirty = true;
275         }
276         if (!rename) {
277                 ext4_fs_inode_links_count_inc(ch);
278                 ch->dirty = true;
279         }
280
281         return r;
282 }
283
284 static int ext4_unlink(struct ext4_mountpoint *mp,
285                        struct ext4_inode_ref *parent,
286                        struct ext4_inode_ref *child, const char *name,
287                        uint32_t name_len)
288 {
289         bool has_children;
290         int rc = ext4_has_children(&has_children, child);
291         if (rc != EOK)
292                 return rc;
293
294         /* Cannot unlink non-empty node */
295         if (has_children)
296                 return ENOTEMPTY;
297
298         /* Remove entry from parent directory */
299         rc = ext4_dir_remove_entry(parent, name, name_len);
300         if (rc != EOK)
301                 return rc;
302
303         bool is_dir = ext4_inode_is_type(&mp->fs.sb, child->inode,
304                                          EXT4_INODE_MODE_DIRECTORY);
305
306         /* If directory - handle links from parent */
307         if (is_dir) {
308                 ext4_fs_inode_links_count_dec(parent);
309                 parent->dirty = true;
310         }
311
312         /*
313          * TODO: Update timestamps of the parent
314          * (when we have wall-clock time).
315          *
316          * ext4_inode_set_change_inode_time(parent->inode, (uint32_t) now);
317          * ext4_inode_set_modification_time(parent->inode, (uint32_t) now);
318          * parent->dirty = true;
319          */
320
321         /*
322          * TODO: Update timestamp for inode.
323          *
324          * ext4_inode_set_change_inode_time(child->inode,
325          *     (uint32_t) now);
326          */
327         if (ext4_inode_get_links_cnt(child->inode)) {
328                 ext4_fs_inode_links_count_dec(child);
329                 child->dirty = true;
330         }
331
332         return EOK;
333 }
334
335 /****************************************************************************/
336
337 int ext4_mount(const char *dev_name, const char *mount_point,
338                bool read_only)
339 {
340         ext4_assert(mount_point && dev_name);
341         int r;
342         int i;
343
344         uint32_t bsize;
345         struct ext4_blockdev *bd = 0;
346         struct ext4_bcache *bc = 0;
347         struct ext4_mountpoint *mp = 0;
348
349         if (mount_point[strlen(mount_point) - 1] != '/')
350                 return ENOTSUP;
351
352         for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
353                 if (_bdevices[i].name) {
354                         if (!strcmp(dev_name, _bdevices[i].name)) {
355                                 bd = _bdevices[i].bd;
356                                 bc = _bdevices[i].bc;
357                                 break;
358                         }
359                 }
360         }
361
362         if (!bd)
363                 return ENODEV;
364
365         for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
366                 if (!_mp[i].mounted) {
367                         strcpy(_mp[i].name, mount_point);
368                         _mp[i].mounted = 1;
369                         mp = &_mp[i];
370                         break;
371                 }
372
373                 if (!strcmp(_mp[i].name, mount_point))
374                         return EOK;
375         }
376
377         if (!mp)
378                 return ENOMEM;
379
380         r = ext4_block_init(bd);
381         if (r != EOK)
382                 return r;
383
384         r = ext4_fs_init(&mp->fs, bd, read_only);
385         if (r != EOK) {
386                 ext4_block_fini(bd);
387                 return r;
388         }
389
390         bsize = ext4_sb_get_block_size(&mp->fs.sb);
391         ext4_block_set_lb_size(bd, bsize);
392
393         mp->cache_dynamic = 0;
394
395         if (!bc) {
396                 /*Automatic block cache alloc.*/
397                 mp->cache_dynamic = 1;
398                 bc = malloc(sizeof(struct ext4_bcache));
399
400                 r = ext4_bcache_init_dynamic(bc, CONFIG_BLOCK_DEV_CACHE_SIZE,
401                                              bsize);
402                 if (r != EOK) {
403                         free(bc);
404                         ext4_block_fini(bd);
405                         return r;
406                 }
407         }
408
409         if (bsize != bc->itemsize)
410                 return ENOTSUP;
411
412         /*Bind block cache to block device*/
413         r = ext4_block_bind_bcache(bd, bc);
414         if (r != EOK) {
415                 ext4_bcache_cleanup(bc);
416                 ext4_block_fini(bd);
417                 if (mp->cache_dynamic) {
418                         ext4_bcache_fini_dynamic(bc);
419                         free(bc);
420                 }
421                 return r;
422         }
423         bd->fs = &mp->fs;
424
425         return r;
426 }
427
428
429 int ext4_umount(const char *mount_point)
430 {
431         int i;
432         int r;
433         struct ext4_mountpoint *mp = 0;
434
435         for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
436                 if (!strcmp(_mp[i].name, mount_point)) {
437                         mp = &_mp[i];
438                         break;
439                 }
440         }
441
442         if (!mp)
443                 return ENODEV;
444
445         r = ext4_fs_fini(&mp->fs);
446         if (r != EOK)
447                 goto Finish;
448
449         mp->mounted = 0;
450
451         ext4_bcache_cleanup(mp->fs.bdev->bc);
452         if (mp->cache_dynamic) {
453                 ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
454                 free(mp->fs.bdev->bc);
455         }
456         r = ext4_block_fini(mp->fs.bdev);
457 Finish:
458         mp->fs.bdev->fs = NULL;
459         return r;
460 }
461
462 static struct ext4_mountpoint *ext4_get_mount(const char *path)
463 {
464         int i;
465         for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
466
467                 if (!_mp[i].mounted)
468                         continue;
469
470                 if (!strncmp(_mp[i].name, path, strlen(_mp[i].name)))
471                         return &_mp[i];
472         }
473         return NULL;
474 }
475
476 __unused
477 static int __ext4_journal_start(const char *mount_point)
478 {
479         int r = EOK;
480         struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
481         if (!mp)
482                 return ENOENT;
483
484         if (mp->fs.read_only)
485                 return EOK;
486
487         if (ext4_sb_feature_com(&mp->fs.sb,
488                                 EXT4_FCOM_HAS_JOURNAL)) {
489                 r = jbd_get_fs(&mp->fs, &mp->jbd_fs);
490                 if (r != EOK)
491                         goto Finish;
492
493                 r = jbd_journal_start(&mp->jbd_fs, &mp->jbd_journal);
494                 if (r != EOK) {
495                         mp->jbd_fs.dirty = false;
496                         jbd_put_fs(&mp->jbd_fs);
497                         goto Finish;
498                 }
499                 mp->fs.jbd_fs = &mp->jbd_fs;
500                 mp->fs.jbd_journal = &mp->jbd_journal;
501         }
502 Finish:
503         return r;
504 }
505
506 __unused
507 static int __ext4_journal_stop(const char *mount_point)
508 {
509         int r = EOK;
510         struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
511         if (!mp)
512                 return ENOENT;
513
514         if (mp->fs.read_only)
515                 return EOK;
516
517         if (ext4_sb_feature_com(&mp->fs.sb,
518                                 EXT4_FCOM_HAS_JOURNAL)) {
519                 r = jbd_journal_stop(&mp->jbd_journal);
520                 if (r != EOK) {
521                         mp->jbd_fs.dirty = false;
522                         jbd_put_fs(&mp->jbd_fs);
523                         mp->fs.jbd_journal = NULL;
524                         mp->fs.jbd_fs = NULL;
525                         goto Finish;
526                 }
527
528                 r = jbd_put_fs(&mp->jbd_fs);
529                 if (r != EOK) {
530                         mp->fs.jbd_journal = NULL;
531                         mp->fs.jbd_fs = NULL;
532                         goto Finish;
533                 }
534
535                 mp->fs.jbd_journal = NULL;
536                 mp->fs.jbd_fs = NULL;
537         }
538 Finish:
539         return r;
540 }
541
542 __unused
543 static int __ext4_recover(const char *mount_point)
544 {
545         struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
546         if (!mp)
547                 return ENOENT;
548
549         int r = ENOTSUP;
550         EXT4_MP_LOCK(mp);
551         if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_HAS_JOURNAL)) {
552                 struct jbd_fs *jbd_fs = calloc(1, sizeof(struct jbd_fs));
553                 if (!jbd_fs) {
554                          r = ENOMEM;
555                          goto Finish;
556                 }
557
558
559                 r = jbd_get_fs(&mp->fs, jbd_fs);
560                 if (r != EOK) {
561                         free(jbd_fs);
562                         goto Finish;
563                 }
564
565                 r = jbd_recover(jbd_fs);
566                 jbd_put_fs(jbd_fs);
567                 free(jbd_fs);
568         }
569         if (r == EOK && !mp->fs.read_only) {
570                 uint32_t bgid;
571                 uint64_t free_blocks_count = 0;
572                 uint32_t free_inodes_count = 0;
573                 struct ext4_block_group_ref bg_ref;
574
575                 /* Update superblock's stats */
576                 for (bgid = 0;bgid < ext4_block_group_cnt(&mp->fs.sb);bgid++) {
577                         r = ext4_fs_get_block_group_ref(&mp->fs, bgid, &bg_ref);
578                         if (r != EOK)
579                                 goto Finish;
580
581                         free_blocks_count +=
582                                 ext4_bg_get_free_blocks_count(bg_ref.block_group,
583                                                 &mp->fs.sb);
584                         free_inodes_count +=
585                                 ext4_bg_get_free_inodes_count(bg_ref.block_group,
586                                                 &mp->fs.sb);
587
588                         ext4_fs_put_block_group_ref(&bg_ref);
589                 }
590                 ext4_sb_set_free_blocks_cnt(&mp->fs.sb, free_blocks_count);
591                 ext4_set32(&mp->fs.sb, free_inodes_count, free_inodes_count);
592                 /* We don't need to save the superblock stats immediately. */
593         }
594
595 Finish:
596         EXT4_MP_UNLOCK(mp);
597         return r;
598 }
599
600 __unused
601 static int __ext4_trans_start(struct ext4_mountpoint *mp)
602 {
603         int r = EOK;
604         if (mp->fs.jbd_journal && !mp->fs.curr_trans) {
605                 struct jbd_journal *journal = mp->fs.jbd_journal;
606                 struct jbd_trans *trans;
607                 trans = jbd_journal_new_trans(journal);
608                 if (!trans) {
609                         r = ENOMEM;
610                         goto Finish;
611                 }
612                 mp->fs.curr_trans = trans;
613         }
614 Finish:
615         return r;
616 }
617
618 __unused
619 static int __ext4_trans_stop(struct ext4_mountpoint *mp)
620 {
621         int r = EOK;
622         if (mp->fs.jbd_journal && mp->fs.curr_trans) {
623                 struct jbd_journal *journal = mp->fs.jbd_journal;
624                 struct jbd_trans *trans = mp->fs.curr_trans;
625                 r = jbd_journal_commit_trans(journal, trans);
626                 mp->fs.curr_trans = NULL;
627         }
628         return r;
629 }
630
631 __unused
632 static void __ext4_trans_abort(struct ext4_mountpoint *mp)
633 {
634         if (mp->fs.jbd_journal && mp->fs.curr_trans) {
635                 struct jbd_journal *journal = mp->fs.jbd_journal;
636                 struct jbd_trans *trans = mp->fs.curr_trans;
637                 jbd_journal_free_trans(journal, trans, true);
638                 mp->fs.curr_trans = NULL;
639         }
640 }
641
642 int ext4_journal_start(const char *mount_point __unused)
643 {
644         int r = EOK;
645 #if CONFIG_JOURNALING_ENABLE
646         r = __ext4_journal_start(mount_point);
647 #endif
648         return r;
649 }
650
651 int ext4_journal_stop(const char *mount_point __unused)
652 {
653         int r = EOK;
654 #if CONFIG_JOURNALING_ENABLE
655         r = __ext4_journal_stop(mount_point);
656 #endif
657         return r;
658 }
659
660 int ext4_recover(const char *mount_point __unused)
661 {
662         int r = EOK;
663 #if CONFIG_JOURNALING_ENABLE
664         r = __ext4_recover(mount_point);
665 #endif
666         return r;
667 }
668
669 static int ext4_trans_start(struct ext4_mountpoint *mp __unused)
670 {
671         int r = EOK;
672 #if CONFIG_JOURNALING_ENABLE
673         r = __ext4_trans_start(mp);
674 #endif
675         return r;
676 }
677
678 static int ext4_trans_stop(struct ext4_mountpoint *mp __unused)
679 {
680         int r = EOK;
681 #if CONFIG_JOURNALING_ENABLE
682         r = __ext4_trans_stop(mp);
683 #endif
684         return r;
685 }
686
687 static void ext4_trans_abort(struct ext4_mountpoint *mp __unused)
688 {
689 #if CONFIG_JOURNALING_ENABLE
690         __ext4_trans_abort(mp);
691 #endif
692 }
693
694
695 int ext4_mount_point_stats(const char *mount_point,
696                            struct ext4_mount_stats *stats)
697 {
698         struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
699
700         if (!mp)
701                 return ENOENT;
702
703         EXT4_MP_LOCK(mp);
704         stats->inodes_count = ext4_get32(&mp->fs.sb, inodes_count);
705         stats->free_inodes_count = ext4_get32(&mp->fs.sb, free_inodes_count);
706         stats->blocks_count = ext4_sb_get_blocks_cnt(&mp->fs.sb);
707         stats->free_blocks_count = ext4_sb_get_free_blocks_cnt(&mp->fs.sb);
708         stats->block_size = ext4_sb_get_block_size(&mp->fs.sb);
709
710         stats->block_group_count = ext4_block_group_cnt(&mp->fs.sb);
711         stats->blocks_per_group = ext4_get32(&mp->fs.sb, blocks_per_group);
712         stats->inodes_per_group = ext4_get32(&mp->fs.sb, inodes_per_group);
713
714         memcpy(stats->volume_name, mp->fs.sb.volume_name, 16);
715         EXT4_MP_UNLOCK(mp);
716
717         return EOK;
718 }
719
720 int ext4_mount_setup_locks(const char *mount_point,
721                            const struct ext4_lock *locks)
722 {
723         uint32_t i;
724         struct ext4_mountpoint *mp = 0;
725
726         for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
727                 if (!strcmp(_mp[i].name, mount_point)) {
728                         mp = &_mp[i];
729                         break;
730                 }
731         }
732         if (!mp)
733                 return ENOENT;
734
735         mp->os_locks = locks;
736         return EOK;
737 }
738
739 /********************************FILE OPERATIONS*****************************/
740
741 static int ext4_path_check(const char *path, bool *is_goal)
742 {
743         int i;
744
745         for (i = 0; i < EXT4_DIRECTORY_FILENAME_LEN; ++i) {
746
747                 if (path[i] == '/') {
748                         *is_goal = false;
749                         return i;
750                 }
751
752                 if (path[i] == 0) {
753                         *is_goal = true;
754                         return i;
755                 }
756         }
757
758         return 0;
759 }
760
761 static bool ext4_parse_flags(const char *flags, uint32_t *file_flags)
762 {
763         if (!flags)
764                 return false;
765
766         if (!strcmp(flags, "r") || !strcmp(flags, "rb")) {
767                 *file_flags = O_RDONLY;
768                 return true;
769         }
770
771         if (!strcmp(flags, "w") || !strcmp(flags, "wb")) {
772                 *file_flags = O_WRONLY | O_CREAT | O_TRUNC;
773                 return true;
774         }
775
776         if (!strcmp(flags, "a") || !strcmp(flags, "ab")) {
777                 *file_flags = O_WRONLY | O_CREAT | O_APPEND;
778                 return true;
779         }
780
781         if (!strcmp(flags, "r+") || !strcmp(flags, "rb+") ||
782             !strcmp(flags, "r+b")) {
783                 *file_flags = O_RDWR;
784                 return true;
785         }
786
787         if (!strcmp(flags, "w+") || !strcmp(flags, "wb+") ||
788             !strcmp(flags, "w+b")) {
789                 *file_flags = O_RDWR | O_CREAT | O_TRUNC;
790                 return true;
791         }
792
793         if (!strcmp(flags, "a+") || !strcmp(flags, "ab+") ||
794             !strcmp(flags, "a+b")) {
795                 *file_flags = O_RDWR | O_CREAT | O_APPEND;
796                 return true;
797         }
798
799         return false;
800 }
801
802 static int ext4_trunc_inode(struct ext4_mountpoint *mp,
803                             uint32_t index, uint64_t new_size)
804 {
805         int r = EOK;
806         struct ext4_fs *const fs = &mp->fs;
807         struct ext4_inode_ref inode_ref;
808         uint64_t inode_size;
809         bool has_trans = mp->fs.jbd_journal && mp->fs.curr_trans;
810         r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
811         if (r != EOK)
812                 return r;
813
814         inode_size = ext4_inode_get_size(&fs->sb, inode_ref.inode);
815         ext4_fs_put_inode_ref(&inode_ref);
816         if (has_trans)
817                 ext4_trans_stop(mp);
818
819         while (inode_size > new_size + CONFIG_MAX_TRUNCATE_SIZE) {
820
821                 inode_size -= CONFIG_MAX_TRUNCATE_SIZE;
822
823                 ext4_trans_start(mp);
824                 r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
825                 if (r != EOK) {
826                         ext4_trans_abort(mp);
827                         break;
828                 }
829                 r = ext4_fs_truncate_inode(&inode_ref, inode_size);
830                 if (r != EOK)
831                         ext4_fs_put_inode_ref(&inode_ref);
832                 else
833                         r = ext4_fs_put_inode_ref(&inode_ref);
834
835                 if (r != EOK) {
836                         ext4_trans_abort(mp);
837                         goto Finish;
838                 } else
839                         ext4_trans_stop(mp);
840         }
841
842         if (inode_size > new_size) {
843
844                 inode_size = new_size;
845
846                 ext4_trans_start(mp);
847                 r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
848                 if (r != EOK) {
849                         ext4_trans_abort(mp);
850                         goto Finish;
851                 }
852                 r = ext4_fs_truncate_inode(&inode_ref, inode_size);
853                 if (r != EOK)
854                         ext4_fs_put_inode_ref(&inode_ref);
855                 else
856                         r = ext4_fs_put_inode_ref(&inode_ref);
857
858                 if (r != EOK)
859                         ext4_trans_abort(mp);
860                 else
861                         ext4_trans_stop(mp);
862
863         }
864
865 Finish:
866
867         if (has_trans)
868                 ext4_trans_start(mp);
869
870         return r;
871 }
872
873 static int ext4_trunc_dir(struct ext4_mountpoint *mp,
874                           struct ext4_inode_ref *parent,
875                           struct ext4_inode_ref *dir)
876 {
877         int r = EOK;
878         bool is_dir = ext4_inode_is_type(&mp->fs.sb, dir->inode,
879                         EXT4_INODE_MODE_DIRECTORY);
880         uint32_t block_size = ext4_sb_get_block_size(&mp->fs.sb);
881         if (!is_dir)
882                 return EINVAL;
883
884 #if CONFIG_DIR_INDEX_ENABLE
885         /* Initialize directory index if supported */
886         if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
887                 r = ext4_dir_dx_init(dir, parent);
888                 if (r != EOK)
889                         return r;
890
891                 r = ext4_trunc_inode(mp, dir->index,
892                                      EXT4_DIR_DX_INIT_BCNT * block_size);
893                 if (r != EOK)
894                         return r;
895         } else
896 #endif
897         {
898                 r = ext4_trunc_inode(mp, dir->index, block_size);
899                 if (r != EOK)
900                         return r;
901         }
902
903         return ext4_fs_truncate_inode(dir, 0);
904 }
905
906 /*
907  * NOTICE: if filetype is equal to EXT4_DIRENTRY_UNKNOWN,
908  * any filetype of the target dir entry will be accepted.
909  */
910 static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
911                               int ftype, uint32_t *parent_inode,
912                               uint32_t *name_off)
913 {
914         bool is_goal = false;
915         uint32_t imode = EXT4_INODE_MODE_DIRECTORY;
916         uint32_t next_inode;
917
918         int r;
919         int len;
920         struct ext4_mountpoint *mp = ext4_get_mount(path);
921         struct ext4_dir_search_result result;
922         struct ext4_inode_ref ref;
923
924         f->mp = 0;
925
926         if (!mp)
927                 return ENOENT;
928
929         struct ext4_fs *const fs = &mp->fs;
930         struct ext4_sblock *const sb = &mp->fs.sb;
931
932         if (fs->read_only && flags & O_CREAT)
933                 return EROFS;
934
935         f->flags = flags;
936
937         /*Skip mount point*/
938         path += strlen(mp->name);
939
940         if (name_off)
941                 *name_off = strlen(mp->name);
942
943         /*Load root*/
944         r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
945         if (r != EOK)
946                 return r;
947
948         if (parent_inode)
949                 *parent_inode = ref.index;
950
951         if (flags & O_CREAT)
952                 ext4_trans_start(mp);
953
954         len = ext4_path_check(path, &is_goal);
955         while (1) {
956
957                 len = ext4_path_check(path, &is_goal);
958                 if (!len) {
959                         /*If root open was request.*/
960                         if (ftype == EXT4_DE_DIR || ftype == EXT4_DE_UNKNOWN)
961                                 if (is_goal)
962                                         break;
963
964                         r = ENOENT;
965                         break;
966                 }
967
968                 r = ext4_dir_find_entry(&result, &ref, path, len);
969                 if (r != EOK) {
970
971                         /*Destroy last result*/
972                         ext4_dir_destroy_result(&ref, &result);
973                         if (r != ENOENT)
974                                 break;
975
976                         if (!(f->flags & O_CREAT))
977                                 break;
978
979                         /*O_CREAT allows create new entry*/
980                         struct ext4_inode_ref child_ref;
981                         r = ext4_fs_alloc_inode(fs, &child_ref,
982                                         is_goal ? ftype : EXT4_DE_DIR);
983                         if (r != EOK)
984                                 break;
985
986
987                         /*Link with root dir.*/
988                         r = ext4_link(mp, &ref, &child_ref, path, len, false);
989                         if (r != EOK) {
990                                 /*Fail. Free new inode.*/
991                                 ext4_fs_free_inode(&child_ref);
992                                 /*We do not want to write new inode.
993                                   But block has to be released.*/
994                                 child_ref.dirty = false;
995                                 ext4_fs_put_inode_ref(&child_ref);
996                                 break;
997                         }
998
999                         ext4_fs_put_inode_ref(&child_ref);
1000                         continue;
1001                 }
1002
1003                 if (parent_inode)
1004                         *parent_inode = ref.index;
1005
1006                 next_inode = ext4_dir_en_get_inode(result.dentry);
1007                 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
1008                         uint8_t t;
1009                         t = ext4_dir_en_get_inode_type(sb, result.dentry);
1010                         imode = ext4_fs_correspond_inode_mode(t);
1011                 } else {
1012                         struct ext4_inode_ref child_ref;
1013                         r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
1014                         if (r != EOK)
1015                                 break;
1016
1017                         imode = ext4_inode_type(sb, child_ref.inode);
1018                         ext4_fs_put_inode_ref(&child_ref);
1019                 }
1020
1021                 r = ext4_dir_destroy_result(&ref, &result);
1022                 if (r != EOK)
1023                         break;
1024
1025                 /*If expected file error*/
1026                 if (imode != EXT4_INODE_MODE_DIRECTORY && !is_goal) {
1027                         r = ENOENT;
1028                         break;
1029                 }
1030                 if (ftype != EXT4_DE_UNKNOWN) {
1031                         bool df = imode != ext4_fs_correspond_inode_mode(ftype);
1032                         if (df && is_goal) {
1033                                 r = ENOENT;
1034                                 break;
1035                         }
1036                 }
1037
1038                 r = ext4_fs_put_inode_ref(&ref);
1039                 if (r != EOK)
1040                         break;
1041
1042                 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
1043                 if (r != EOK)
1044                         break;
1045
1046                 if (is_goal)
1047                         break;
1048
1049                 path += len + 1;
1050
1051                 if (name_off)
1052                         *name_off += len + 1;
1053         };
1054
1055         if (r != EOK) {
1056                 ext4_fs_put_inode_ref(&ref);
1057                 return r;
1058         }
1059
1060         if (is_goal) {
1061
1062                 if ((f->flags & O_TRUNC) && (imode == EXT4_INODE_MODE_FILE)) {
1063                         r = ext4_trunc_inode(mp, ref.index, 0);
1064                         if (r != EOK) {
1065                                 ext4_fs_put_inode_ref(&ref);
1066                                 return r;
1067                         }
1068                 }
1069
1070                 f->mp = mp;
1071                 f->fsize = ext4_inode_get_size(sb, ref.inode);
1072                 f->inode = ref.index;
1073                 f->fpos = 0;
1074
1075                 if (f->flags & O_APPEND)
1076                         f->fpos = f->fsize;
1077
1078         }
1079
1080         r = ext4_fs_put_inode_ref(&ref);
1081         if (flags & O_CREAT) {
1082                 if (r == EOK)
1083                         ext4_trans_stop(mp);
1084                 else
1085                         ext4_trans_abort(mp);
1086
1087         }
1088
1089         return r;
1090 }
1091
1092 /****************************************************************************/
1093
1094 static int ext4_generic_open(ext4_file *f, const char *path, const char *flags,
1095                              bool file_expect, uint32_t *parent_inode,
1096                              uint32_t *name_off)
1097 {
1098         uint32_t iflags;
1099         int filetype;
1100         if (ext4_parse_flags(flags, &iflags) == false)
1101                 return EINVAL;
1102
1103         if (file_expect == true)
1104                 filetype = EXT4_DE_REG_FILE;
1105         else
1106                 filetype = EXT4_DE_DIR;
1107
1108         return ext4_generic_open2(f, path, iflags, filetype, parent_inode,
1109                                   name_off);
1110 }
1111
1112 static int ext4_create_hardlink(const char *path,
1113                 struct ext4_inode_ref *child_ref, bool rename)
1114 {
1115         bool is_goal = false;
1116         uint32_t inode_mode = EXT4_INODE_MODE_DIRECTORY;
1117         uint32_t next_inode;
1118
1119         int r;
1120         int len;
1121         struct ext4_mountpoint *mp = ext4_get_mount(path);
1122         struct ext4_dir_search_result result;
1123         struct ext4_inode_ref ref;
1124
1125         if (!mp)
1126                 return ENOENT;
1127
1128         struct ext4_fs *const fs = &mp->fs;
1129         struct ext4_sblock *const sb = &mp->fs.sb;
1130
1131         /*Skip mount point*/
1132         path += strlen(mp->name);
1133
1134         /*Load root*/
1135         r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
1136         if (r != EOK)
1137                 return r;
1138
1139         len = ext4_path_check(path, &is_goal);
1140         while (1) {
1141
1142                 len = ext4_path_check(path, &is_goal);
1143                 if (!len) {
1144                         /*If root open was request.*/
1145                         r = is_goal ? EINVAL : ENOENT;
1146                         break;
1147                 }
1148
1149                 r = ext4_dir_find_entry(&result, &ref, path, len);
1150                 if (r != EOK) {
1151
1152                         /*Destroy last result*/
1153                         ext4_dir_destroy_result(&ref, &result);
1154
1155                         if (r != ENOENT || !is_goal)
1156                                 break;
1157
1158                         /*Link with root dir.*/
1159                         r = ext4_link(mp, &ref, child_ref, path, len, rename);
1160                         break;
1161                 } else if (r == EOK && is_goal) {
1162                         /*Destroy last result*/
1163                         ext4_dir_destroy_result(&ref, &result);
1164                         r = EEXIST;
1165                         break;
1166                 }
1167
1168                 next_inode = result.dentry->inode;
1169                 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
1170                         uint8_t t;
1171                         t = ext4_dir_en_get_inode_type(sb, result.dentry);
1172                         inode_mode = ext4_fs_correspond_inode_mode(t);
1173                 } else {
1174                         struct ext4_inode_ref child_ref;
1175                         r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
1176                         if (r != EOK)
1177                                 break;
1178
1179                         inode_mode = ext4_inode_type(sb, child_ref.inode);
1180                         ext4_fs_put_inode_ref(&child_ref);
1181                 }
1182
1183                 r = ext4_dir_destroy_result(&ref, &result);
1184                 if (r != EOK)
1185                         break;
1186
1187                 if (inode_mode != EXT4_INODE_MODE_DIRECTORY) {
1188                         r = is_goal ? EEXIST : ENOENT;
1189                         break;
1190                 }
1191
1192                 r = ext4_fs_put_inode_ref(&ref);
1193                 if (r != EOK)
1194                         break;
1195
1196                 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
1197                 if (r != EOK)
1198                         break;
1199
1200                 if (is_goal)
1201                         break;
1202
1203                 path += len + 1;
1204         };
1205
1206         if (r != EOK) {
1207                 ext4_fs_put_inode_ref(&ref);
1208                 return r;
1209         }
1210
1211         r = ext4_fs_put_inode_ref(&ref);
1212         return r;
1213 }
1214
1215 static int ext4_remove_orig_reference(const char *path, uint32_t name_off,
1216                                       struct ext4_inode_ref *parent_ref,
1217                                       struct ext4_inode_ref *child_ref)
1218 {
1219         bool is_goal;
1220         int r;
1221         int len;
1222         struct ext4_mountpoint *mp = ext4_get_mount(path);
1223
1224         if (!mp)
1225                 return ENOENT;
1226
1227         /*Set path*/
1228         path += name_off;
1229
1230         len = ext4_path_check(path, &is_goal);
1231
1232         /* Remove entry from parent directory */
1233         r = ext4_dir_remove_entry(parent_ref, path, len);
1234         if (r != EOK)
1235                 goto Finish;
1236
1237         if (ext4_inode_is_type(&mp->fs.sb, child_ref->inode,
1238                                EXT4_INODE_MODE_DIRECTORY)) {
1239                 ext4_fs_inode_links_count_dec(parent_ref);
1240                 parent_ref->dirty = true;
1241         }
1242 Finish:
1243         return r;
1244 }
1245
1246 int ext4_flink(const char *path, const char *hardlink_path)
1247 {
1248         int r;
1249         ext4_file f;
1250         uint32_t name_off;
1251         bool child_loaded = false;
1252         uint32_t parent_inode, child_inode;
1253         struct ext4_mountpoint *mp = ext4_get_mount(path);
1254         struct ext4_mountpoint *target_mp = ext4_get_mount(hardlink_path);
1255         struct ext4_inode_ref child_ref;
1256
1257         if (!mp)
1258                 return ENOENT;
1259
1260         if (mp->fs.read_only)
1261                 return EROFS;
1262
1263         /* Will that happen? Anyway return EINVAL for such case. */
1264         if (mp != target_mp)
1265                 return EINVAL;
1266
1267         EXT4_MP_LOCK(mp);
1268         ext4_trans_start(mp);
1269
1270         r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1271                                &parent_inode, &name_off);
1272         if (r != EOK)
1273                 goto Finish;
1274
1275         child_inode = f.inode;
1276         ext4_fclose(&f);
1277
1278         /*We have file to unlink. Load it.*/
1279         r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1280         if (r != EOK)
1281                 goto Finish;
1282
1283         child_loaded = true;
1284
1285         /* Creating hardlink for directory is not allowed. */
1286         if (ext4_inode_is_type(&mp->fs.sb, child_ref.inode,
1287                                EXT4_INODE_MODE_DIRECTORY)) {
1288                 r = EINVAL;
1289                 goto Finish;
1290         }
1291
1292         r = ext4_create_hardlink(hardlink_path, &child_ref, false);
1293
1294 Finish:
1295         if (child_loaded)
1296                 ext4_fs_put_inode_ref(&child_ref);
1297
1298         if (r != EOK)
1299                 ext4_trans_abort(mp);
1300         else
1301                 ext4_trans_stop(mp);
1302
1303         EXT4_MP_UNLOCK(mp);
1304         return r;
1305
1306 }
1307
1308 int ext4_frename(const char *path, const char *new_path)
1309 {
1310         int r;
1311         ext4_file f;
1312         uint32_t name_off;
1313         bool parent_loaded = false, child_loaded = false;
1314         uint32_t parent_inode, child_inode;
1315         struct ext4_mountpoint *mp = ext4_get_mount(path);
1316         struct ext4_inode_ref child_ref, parent_ref;
1317
1318         if (!mp)
1319                 return ENOENT;
1320
1321         if (mp->fs.read_only)
1322                 return EROFS;
1323
1324         EXT4_MP_LOCK(mp);
1325         ext4_trans_start(mp);
1326
1327         r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1328                                 &parent_inode, &name_off);
1329         if (r != EOK)
1330                 goto Finish;
1331
1332         child_inode = f.inode;
1333         ext4_fclose(&f);
1334
1335         /*Load parent*/
1336         r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent_ref);
1337         if (r != EOK)
1338                 goto Finish;
1339
1340         parent_loaded = true;
1341
1342         /*We have file to unlink. Load it.*/
1343         r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1344         if (r != EOK)
1345                 goto Finish;
1346
1347         child_loaded = true;
1348
1349         r = ext4_create_hardlink(new_path, &child_ref, true);
1350         if (r != EOK)
1351                 goto Finish;
1352
1353         r = ext4_remove_orig_reference(path, name_off, &parent_ref, &child_ref);
1354         if (r != EOK)
1355                 goto Finish;
1356
1357 Finish:
1358         if (parent_loaded)
1359                 ext4_fs_put_inode_ref(&parent_ref);
1360
1361         if (child_loaded)
1362                 ext4_fs_put_inode_ref(&child_ref);
1363
1364         if (r != EOK)
1365                 ext4_trans_abort(mp);
1366         else
1367                 ext4_trans_stop(mp);
1368
1369         EXT4_MP_UNLOCK(mp);
1370         return r;
1371
1372 }
1373
1374 /****************************************************************************/
1375
1376 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb)
1377 {
1378         struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
1379
1380         if (!mp)
1381                 return ENOENT;
1382
1383         *sb = &mp->fs.sb;
1384         return EOK;
1385 }
1386
1387 int ext4_cache_write_back(const char *path, bool on)
1388 {
1389         struct ext4_mountpoint *mp = ext4_get_mount(path);
1390
1391         if (!mp)
1392                 return ENOENT;
1393
1394         EXT4_MP_LOCK(mp);
1395         ext4_block_cache_write_back(mp->fs.bdev, on);
1396         EXT4_MP_UNLOCK(mp);
1397         return EOK;
1398 }
1399
1400 int ext4_fremove(const char *path)
1401 {
1402         ext4_file f;
1403         uint32_t parent_inode;
1404         uint32_t name_off;
1405         bool is_goal;
1406         int r;
1407         int len;
1408         struct ext4_inode_ref child;
1409         struct ext4_inode_ref parent;
1410         struct ext4_mountpoint *mp = ext4_get_mount(path);
1411
1412         if (!mp)
1413                 return ENOENT;
1414
1415         if (mp->fs.read_only)
1416                 return EROFS;
1417
1418         EXT4_MP_LOCK(mp);
1419         ext4_trans_start(mp);
1420
1421         r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN,
1422                                &parent_inode, &name_off);
1423         if (r != EOK) {
1424                 ext4_trans_abort(mp);
1425                 EXT4_MP_UNLOCK(mp);
1426                 return r;
1427         }
1428
1429         /*Load parent*/
1430         r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
1431         if (r != EOK) {
1432                 ext4_trans_abort(mp);
1433                 EXT4_MP_UNLOCK(mp);
1434                 return r;
1435         }
1436
1437         /*We have file to delete. Load it.*/
1438         r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
1439         if (r != EOK) {
1440                 ext4_fs_put_inode_ref(&parent);
1441                 ext4_trans_abort(mp);
1442                 EXT4_MP_UNLOCK(mp);
1443                 return r;
1444         }
1445         /* We do not allow opening files here. */
1446         if (ext4_inode_type(&mp->fs.sb, child.inode) ==
1447             EXT4_INODE_MODE_DIRECTORY) {
1448                 ext4_fs_put_inode_ref(&parent);
1449                 ext4_fs_put_inode_ref(&child);
1450                 ext4_trans_abort(mp);
1451                 EXT4_MP_UNLOCK(mp);
1452                 return r;
1453         }
1454
1455         /*Link count will be zero, the inode should be freed. */
1456         if (ext4_inode_get_links_cnt(child.inode) == 1) {
1457                 ext4_block_cache_write_back(mp->fs.bdev, 1);
1458                 r = ext4_trunc_inode(mp, child.index, 0);
1459                 if (r != EOK) {
1460                         ext4_fs_put_inode_ref(&parent);
1461                         ext4_fs_put_inode_ref(&child);
1462                         ext4_trans_abort(mp);
1463                         EXT4_MP_UNLOCK(mp);
1464                         return r;
1465                 }
1466                 ext4_block_cache_write_back(mp->fs.bdev, 0);
1467         }
1468
1469         /*Set path*/
1470         path += name_off;
1471
1472         len = ext4_path_check(path, &is_goal);
1473
1474         /*Unlink from parent*/
1475         r = ext4_unlink(mp, &parent, &child, path, len);
1476         if (r != EOK)
1477                 goto Finish;
1478
1479         /*Link count is zero, the inode should be freed. */
1480         if (!ext4_inode_get_links_cnt(child.inode)) {
1481                 ext4_inode_set_del_time(child.inode, -1L);
1482
1483                 r = ext4_fs_free_inode(&child);
1484                 if (r != EOK)
1485                         goto Finish;
1486         }
1487
1488 Finish:
1489         ext4_fs_put_inode_ref(&child);
1490         ext4_fs_put_inode_ref(&parent);
1491
1492         if (r != EOK)
1493                 ext4_trans_abort(mp);
1494         else
1495                 ext4_trans_stop(mp);
1496
1497         EXT4_MP_UNLOCK(mp);
1498         return r;
1499 }
1500
1501 int ext4_fill_raw_inode(const char *path, uint32_t *ret_ino,
1502                         struct ext4_inode *inode)
1503 {
1504         int r;
1505         ext4_file f;
1506         struct ext4_inode_ref inode_ref;
1507         struct ext4_mountpoint *mp = ext4_get_mount(path);
1508         uint32_t ino;
1509
1510         if (!mp)
1511                 return ENOENT;
1512
1513         EXT4_MP_LOCK(mp);
1514
1515         r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
1516         if (r != EOK) {
1517                 EXT4_MP_UNLOCK(mp);
1518                 return r;
1519         }
1520
1521         ino = f.inode;
1522         ext4_fclose(&f);
1523
1524         /*Load parent*/
1525         r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1526         if (r != EOK) {
1527                 EXT4_MP_UNLOCK(mp);
1528                 return r;
1529         }
1530
1531         memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
1532         ext4_fs_put_inode_ref(&inode_ref);
1533         EXT4_MP_UNLOCK(mp);
1534
1535         if (ret_ino)
1536                 *ret_ino = ino;
1537
1538         return r;
1539 }
1540
1541 int ext4_fopen(ext4_file *f, const char *path, const char *flags)
1542 {
1543         struct ext4_mountpoint *mp = ext4_get_mount(path);
1544         int r;
1545
1546         if (!mp)
1547                 return ENOENT;
1548
1549         EXT4_MP_LOCK(mp);
1550
1551         ext4_block_cache_write_back(mp->fs.bdev, 1);
1552         r = ext4_generic_open(f, path, flags, true, 0, 0);
1553         ext4_block_cache_write_back(mp->fs.bdev, 0);
1554
1555         EXT4_MP_UNLOCK(mp);
1556         return r;
1557 }
1558
1559 int ext4_fopen2(ext4_file *f, const char *path, int flags)
1560 {
1561         struct ext4_mountpoint *mp = ext4_get_mount(path);
1562         int r;
1563         int filetype;
1564
1565         if (!mp)
1566                 return ENOENT;
1567
1568         filetype = EXT4_DE_REG_FILE;
1569
1570         EXT4_MP_LOCK(mp);
1571
1572         ext4_block_cache_write_back(mp->fs.bdev, 1);
1573         r = ext4_generic_open2(f, path, flags, filetype, NULL, NULL);
1574         ext4_block_cache_write_back(mp->fs.bdev, 0);
1575
1576         EXT4_MP_UNLOCK(mp);
1577         return r;
1578 }
1579
1580 int ext4_fclose(ext4_file *f)
1581 {
1582         ext4_assert(f && f->mp);
1583
1584         f->mp = 0;
1585         f->flags = 0;
1586         f->inode = 0;
1587         f->fpos = f->fsize = 0;
1588
1589         return EOK;
1590 }
1591
1592 static int ext4_ftruncate_no_lock(ext4_file *f, uint64_t size)
1593 {
1594         struct ext4_inode_ref ref;
1595         int r;
1596
1597
1598         r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1599         if (r != EOK) {
1600                 EXT4_MP_UNLOCK(f->mp);
1601                 return r;
1602         }
1603
1604         /*Sync file size*/
1605         f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1606         if (f->fsize <= size) {
1607                 r = EOK;
1608                 goto Finish;
1609         }
1610
1611         /*Start write back cache mode.*/
1612         r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1613         if (r != EOK)
1614                 goto Finish;
1615
1616         r = ext4_trunc_inode(f->mp, ref.index, size);
1617         if (r != EOK)
1618                 goto Finish;
1619
1620         f->fsize = size;
1621         if (f->fpos > size)
1622                 f->fpos = size;
1623
1624         /*Stop write back cache mode*/
1625         ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1626
1627         if (r != EOK)
1628                 goto Finish;
1629
1630 Finish:
1631         ext4_fs_put_inode_ref(&ref);
1632         return r;
1633
1634 }
1635
1636 int ext4_ftruncate(ext4_file *f, uint64_t size)
1637 {
1638         int r;
1639         ext4_assert(f && f->mp);
1640
1641         if (f->mp->fs.read_only)
1642                 return EROFS;
1643
1644         if (f->flags & O_RDONLY)
1645                 return EPERM;
1646
1647         EXT4_MP_LOCK(f->mp);
1648
1649         ext4_trans_start(f->mp);
1650         r = ext4_ftruncate_no_lock(f, size);
1651         if (r != EOK)
1652                 ext4_trans_abort(f->mp);
1653         else
1654                 ext4_trans_stop(f->mp);
1655
1656         EXT4_MP_UNLOCK(f->mp);
1657         return r;
1658 }
1659
1660 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
1661 {
1662         uint32_t unalg;
1663         uint32_t iblock_idx;
1664         uint32_t iblock_last;
1665         uint32_t block_size;
1666
1667         ext4_fsblk_t fblock;
1668         ext4_fsblk_t fblock_start;
1669         uint32_t fblock_count;
1670
1671         uint8_t *u8_buf = buf;
1672         int r;
1673         struct ext4_inode_ref ref;
1674
1675         ext4_assert(f && f->mp);
1676
1677         if (f->flags & O_WRONLY)
1678                 return EPERM;
1679
1680         if (!size)
1681                 return EOK;
1682
1683         EXT4_MP_LOCK(f->mp);
1684
1685         struct ext4_fs *const fs = &f->mp->fs;
1686         struct ext4_sblock *const sb = &f->mp->fs.sb;
1687
1688         if (rcnt)
1689                 *rcnt = 0;
1690
1691         r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1692         if (r != EOK) {
1693                 EXT4_MP_UNLOCK(f->mp);
1694                 return r;
1695         }
1696
1697         /*Sync file size*/
1698         f->fsize = ext4_inode_get_size(sb, ref.inode);
1699
1700         block_size = ext4_sb_get_block_size(sb);
1701         size = ((uint64_t)size > (f->fsize - f->fpos))
1702                 ? ((size_t)(f->fsize - f->fpos)) : size;
1703
1704         iblock_idx = (uint32_t)((f->fpos) / block_size);
1705         iblock_last = (uint32_t)((f->fpos + size) / block_size);
1706         unalg = (f->fpos) % block_size;
1707
1708         /*If the size of symlink is smaller than 60 bytes*/
1709         bool softlink;
1710         softlink = ext4_inode_is_type(sb, ref.inode, EXT4_INODE_MODE_SOFTLINK);
1711         if (softlink && f->fsize < sizeof(ref.inode->blocks)
1712                      && !ext4_inode_get_blocks_count(sb, ref.inode)) {
1713
1714                 char *content = (char *)ref.inode->blocks;
1715                 if (f->fpos < f->fsize) {
1716                         size_t len = size;
1717                         if (unalg + size > (uint32_t)f->fsize)
1718                                 len = (uint32_t)f->fsize - unalg;
1719                         memcpy(buf, content + unalg, len);
1720                         if (rcnt)
1721                                 *rcnt = len;
1722
1723                 }
1724
1725                 r = EOK;
1726                 goto Finish;
1727         }
1728
1729         if (unalg) {
1730                 size_t len =  size;
1731                 if (size > (block_size - unalg))
1732                         len = block_size - unalg;
1733
1734                 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1735                 if (r != EOK)
1736                         goto Finish;
1737
1738                 /* Do we get an unwritten range? */
1739                 if (fblock != 0) {
1740                         uint64_t off = fblock * block_size + unalg;
1741                         r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, len);
1742                         if (r != EOK)
1743                                 goto Finish;
1744
1745                 } else {
1746                         /* Yes, we do. */
1747                         memset(u8_buf, 0, len);
1748                 }
1749
1750                 u8_buf += len;
1751                 size -= len;
1752                 f->fpos += len;
1753
1754                 if (rcnt)
1755                         *rcnt += len;
1756
1757                 iblock_idx++;
1758         }
1759
1760         fblock_start = 0;
1761         fblock_count = 0;
1762         while (size >= block_size) {
1763                 while (iblock_idx < iblock_last) {
1764                         r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx,
1765                                                        &fblock, true);
1766                         if (r != EOK)
1767                                 goto Finish;
1768
1769                         iblock_idx++;
1770
1771                         if (!fblock_start)
1772                                 fblock_start = fblock;
1773
1774                         if ((fblock_start + fblock_count) != fblock)
1775                                 break;
1776
1777                         fblock_count++;
1778                 }
1779
1780                 r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1781                                            fblock_count);
1782                 if (r != EOK)
1783                         goto Finish;
1784
1785                 size -= block_size * fblock_count;
1786                 u8_buf += block_size * fblock_count;
1787                 f->fpos += block_size * fblock_count;
1788
1789                 if (rcnt)
1790                         *rcnt += block_size * fblock_count;
1791
1792                 fblock_start = fblock;
1793                 fblock_count = 1;
1794         }
1795
1796         if (size) {
1797                 uint64_t off;
1798                 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1799                 if (r != EOK)
1800                         goto Finish;
1801
1802                 off = fblock * block_size;
1803                 r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, size);
1804                 if (r != EOK)
1805                         goto Finish;
1806
1807                 f->fpos += size;
1808
1809                 if (rcnt)
1810                         *rcnt += size;
1811         }
1812
1813 Finish:
1814         ext4_fs_put_inode_ref(&ref);
1815         EXT4_MP_UNLOCK(f->mp);
1816         return r;
1817 }
1818
1819 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
1820 {
1821         uint32_t unalg;
1822         uint32_t iblk_idx;
1823         uint32_t iblock_last;
1824         uint32_t ifile_blocks;
1825         uint32_t block_size;
1826
1827         uint32_t fblock_count;
1828         ext4_fsblk_t fblk;
1829         ext4_fsblk_t fblock_start;
1830
1831         struct ext4_inode_ref ref;
1832         const uint8_t *u8_buf = buf;
1833         int r, rr = EOK;
1834
1835         ext4_assert(f && f->mp);
1836
1837         if (f->mp->fs.read_only)
1838                 return EROFS;
1839
1840         if (f->flags & O_RDONLY)
1841                 return EPERM;
1842
1843         if (!size)
1844                 return EOK;
1845
1846         EXT4_MP_LOCK(f->mp);
1847         ext4_trans_start(f->mp);
1848
1849         struct ext4_fs *const fs = &f->mp->fs;
1850         struct ext4_sblock *const sb = &f->mp->fs.sb;
1851
1852         if (wcnt)
1853                 *wcnt = 0;
1854
1855         r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1856         if (r != EOK) {
1857                 ext4_trans_abort(f->mp);
1858                 EXT4_MP_UNLOCK(f->mp);
1859                 return r;
1860         }
1861
1862         /*Sync file size*/
1863         f->fsize = ext4_inode_get_size(sb, ref.inode);
1864         block_size = ext4_sb_get_block_size(sb);
1865
1866         iblock_last = (uint32_t)((f->fpos + size) / block_size);
1867         iblk_idx = (uint32_t)(f->fpos / block_size);
1868         ifile_blocks = (uint32_t)((f->fsize + block_size - 1) / block_size);
1869
1870         unalg = (f->fpos) % block_size;
1871
1872         if (unalg) {
1873                 size_t len =  size;
1874                 uint64_t off;
1875                 if (size > (block_size - unalg))
1876                         len = block_size - unalg;
1877
1878                 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1879                 if (r != EOK)
1880                         goto Finish;
1881
1882                 off = fblk * block_size + unalg;
1883                 r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, len);
1884                 if (r != EOK)
1885                         goto Finish;
1886
1887                 u8_buf += len;
1888                 size -= len;
1889                 f->fpos += len;
1890
1891                 if (wcnt)
1892                         *wcnt += len;
1893
1894                 iblk_idx++;
1895         }
1896
1897         /*Start write back cache mode.*/
1898         r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1899         if (r != EOK)
1900                 goto Finish;
1901
1902         fblock_start = 0;
1903         fblock_count = 0;
1904         while (size >= block_size) {
1905
1906                 while (iblk_idx < iblock_last) {
1907                         if (iblk_idx < ifile_blocks) {
1908                                 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx,
1909                                                                 &fblk);
1910                                 if (r != EOK)
1911                                         goto Finish;
1912                         } else {
1913                                 rr = ext4_fs_append_inode_dblk(&ref, &fblk,
1914                                                                &iblk_idx);
1915                                 if (rr != EOK) {
1916                                         /* Unable to append more blocks. But
1917                                          * some block might be allocated already
1918                                          * */
1919                                         break;
1920                                 }
1921                         }
1922
1923                         iblk_idx++;
1924
1925                         if (!fblock_start) {
1926                                 fblock_start = fblk;
1927                         }
1928
1929                         if ((fblock_start + fblock_count) != fblk)
1930                                 break;
1931
1932                         fblock_count++;
1933                 }
1934
1935                 r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1936                                            fblock_count);
1937                 if (r != EOK)
1938                         break;
1939
1940                 size -= block_size * fblock_count;
1941                 u8_buf += block_size * fblock_count;
1942                 f->fpos += block_size * fblock_count;
1943
1944                 if (wcnt)
1945                         *wcnt += block_size * fblock_count;
1946
1947                 fblock_start = fblk;
1948                 fblock_count = 1;
1949
1950                 if (rr != EOK) {
1951                         /*ext4_fs_append_inode_block has failed and no
1952                          * more blocks might be written. But node size
1953                          * should be updated.*/
1954                         r = rr;
1955                         goto out_fsize;
1956                 }
1957         }
1958
1959         /*Stop write back cache mode*/
1960         ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1961
1962         if (r != EOK)
1963                 goto Finish;
1964
1965         if (size) {
1966                 uint64_t off;
1967                 if (iblk_idx < ifile_blocks) {
1968                         r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1969                         if (r != EOK)
1970                                 goto Finish;
1971                 } else {
1972                         r = ext4_fs_append_inode_dblk(&ref, &fblk, &iblk_idx);
1973                         if (r != EOK)
1974                                 /*Node size sholud be updated.*/
1975                                 goto out_fsize;
1976                 }
1977
1978                 off = fblk * block_size;
1979                 r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, size);
1980                 if (r != EOK)
1981                         goto Finish;
1982
1983                 f->fpos += size;
1984
1985                 if (wcnt)
1986                         *wcnt += size;
1987         }
1988
1989 out_fsize:
1990         if (f->fpos > f->fsize) {
1991                 f->fsize = f->fpos;
1992                 ext4_inode_set_size(ref.inode, f->fsize);
1993                 ref.dirty = true;
1994         }
1995
1996 Finish:
1997         r = ext4_fs_put_inode_ref(&ref);
1998
1999         if (r != EOK)
2000                 ext4_trans_abort(f->mp);
2001         else
2002                 ext4_trans_stop(f->mp);
2003
2004         EXT4_MP_UNLOCK(f->mp);
2005         return r;
2006 }
2007
2008 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin)
2009 {
2010         switch (origin) {
2011         case SEEK_SET:
2012                 if (offset > f->fsize)
2013                         return EINVAL;
2014
2015                 f->fpos = offset;
2016                 return EOK;
2017         case SEEK_CUR:
2018                 if ((offset + f->fpos) > f->fsize)
2019                         return EINVAL;
2020
2021                 f->fpos += offset;
2022                 return EOK;
2023         case SEEK_END:
2024                 if (offset > f->fsize)
2025                         return EINVAL;
2026
2027                 f->fpos = f->fsize - offset;
2028                 return EOK;
2029         }
2030         return EINVAL;
2031 }
2032
2033 uint64_t ext4_ftell(ext4_file *f)
2034 {
2035         return f->fpos;
2036 }
2037
2038 uint64_t ext4_fsize(ext4_file *f)
2039 {
2040         return f->fsize;
2041 }
2042
2043 int ext4_chmod(const char *path, uint32_t mode)
2044 {
2045         int r;
2046         uint32_t ino, orig_mode;
2047         ext4_file f;
2048         struct ext4_sblock *sb;
2049         struct ext4_inode_ref inode_ref;
2050         struct ext4_mountpoint *mp = ext4_get_mount(path);
2051
2052         if (!mp)
2053                 return ENOENT;
2054
2055         if (mp->fs.read_only)
2056                 return EROFS;
2057
2058         EXT4_MP_LOCK(mp);
2059         ext4_trans_start(mp);
2060
2061         r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2062         if (r != EOK) {
2063                 ext4_trans_abort(mp);
2064                 EXT4_MP_UNLOCK(mp);
2065                 return r;
2066         }
2067         ino = f.inode;
2068         sb = &mp->fs.sb;
2069         ext4_fclose(&f);
2070         r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2071         if (r != EOK) {
2072                 ext4_trans_abort(mp);
2073                 EXT4_MP_UNLOCK(mp);
2074                 return r;
2075         }
2076
2077         orig_mode = ext4_inode_get_mode(sb, inode_ref.inode);
2078         orig_mode &= ~0xFFF;
2079         orig_mode |= mode & 0xFFF;
2080         ext4_inode_set_mode(sb, inode_ref.inode, orig_mode);
2081         inode_ref.dirty = true;
2082
2083         r = ext4_fs_put_inode_ref(&inode_ref);
2084         if (r != EOK)
2085                 ext4_trans_abort(mp);
2086         else
2087                 ext4_trans_stop(mp);
2088
2089         EXT4_MP_UNLOCK(mp);
2090         return r;
2091 }
2092
2093 int ext4_chown(const char *path, uint32_t uid, uint32_t gid)
2094 {
2095         int r;
2096         ext4_file f;
2097         uint32_t ino;
2098         struct ext4_inode_ref inode_ref;
2099         struct ext4_mountpoint *mp = ext4_get_mount(path);
2100
2101         if (!mp)
2102                 return ENOENT;
2103
2104         if (mp->fs.read_only)
2105                 return EROFS;
2106
2107         EXT4_MP_LOCK(mp);
2108         ext4_trans_start(mp);
2109
2110         r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2111         if (r != EOK) {
2112                 ext4_trans_abort(mp);
2113                 EXT4_MP_UNLOCK(mp);
2114                 return r;
2115         }
2116         ino = f.inode;
2117         ext4_fclose(&f);
2118         r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2119         if (r != EOK) {
2120                 ext4_trans_abort(mp);
2121                 EXT4_MP_UNLOCK(mp);
2122                 return r;
2123         }
2124
2125         ext4_inode_set_uid(inode_ref.inode, uid);
2126         ext4_inode_set_gid(inode_ref.inode, gid);
2127         inode_ref.dirty = true;
2128
2129         r = ext4_fs_put_inode_ref(&inode_ref);
2130         if (r != EOK)
2131                 ext4_trans_abort(mp);
2132         else
2133                 ext4_trans_stop(mp);
2134
2135         EXT4_MP_UNLOCK(mp);
2136         return r;
2137 }
2138
2139 int ext4_file_set_atime(const char *path, uint32_t atime)
2140 {
2141         int r;
2142         ext4_file f;
2143         uint32_t ino;
2144         struct ext4_inode_ref inode_ref;
2145         struct ext4_mountpoint *mp = ext4_get_mount(path);
2146
2147         if (!mp)
2148                 return ENOENT;
2149
2150         if (mp->fs.read_only)
2151                 return EROFS;
2152
2153         EXT4_MP_LOCK(mp);
2154         ext4_trans_start(mp);
2155
2156         r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2157         if (r != EOK) {
2158                 ext4_trans_abort(mp);
2159                 EXT4_MP_UNLOCK(mp);
2160                 return r;
2161         }
2162         ino = f.inode;
2163         ext4_fclose(&f);
2164         r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2165         if (r != EOK) {
2166                 ext4_trans_abort(mp);
2167                 EXT4_MP_UNLOCK(mp);
2168                 return r;
2169         }
2170
2171         ext4_inode_set_access_time(inode_ref.inode, atime);
2172         inode_ref.dirty = true;
2173
2174         r = ext4_fs_put_inode_ref(&inode_ref);
2175         if (r != EOK)
2176                 ext4_trans_abort(mp);
2177         else
2178                 ext4_trans_stop(mp);
2179
2180         EXT4_MP_UNLOCK(mp);
2181         return r;
2182 }
2183
2184 int ext4_file_set_mtime(const char *path, uint32_t mtime)
2185 {
2186         int r;
2187         ext4_file f;
2188         uint32_t ino;
2189         struct ext4_inode_ref inode_ref;
2190         struct ext4_mountpoint *mp = ext4_get_mount(path);
2191
2192         if (!mp)
2193                 return ENOENT;
2194
2195         if (mp->fs.read_only)
2196                 return EROFS;
2197
2198         EXT4_MP_LOCK(mp);
2199         ext4_trans_start(mp);
2200
2201         r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2202         if (r != EOK) {
2203                 ext4_trans_abort(mp);
2204                 EXT4_MP_UNLOCK(mp);
2205                 return r;
2206         }
2207         ino = f.inode;
2208         ext4_fclose(&f);
2209         r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2210         if (r != EOK) {
2211                 ext4_trans_abort(mp);
2212                 EXT4_MP_UNLOCK(mp);
2213                 return r;
2214         }
2215
2216         ext4_inode_set_modif_time(inode_ref.inode, mtime);
2217         inode_ref.dirty = true;
2218
2219         r = ext4_fs_put_inode_ref(&inode_ref);
2220         if (r != EOK)
2221                 ext4_trans_abort(mp);
2222         else
2223                 ext4_trans_stop(mp);
2224
2225         EXT4_MP_UNLOCK(mp);
2226         return r;
2227 }
2228
2229 int ext4_file_set_ctime(const char *path, uint32_t ctime)
2230 {
2231         int r;
2232         ext4_file f;
2233         uint32_t ino;
2234         struct ext4_inode_ref inode_ref;
2235         struct ext4_mountpoint *mp = ext4_get_mount(path);
2236
2237         if (!mp)
2238                 return ENOENT;
2239
2240         if (mp->fs.read_only)
2241                 return EROFS;
2242
2243         EXT4_MP_LOCK(mp);
2244         ext4_trans_start(mp);
2245
2246         r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2247         if (r != EOK) {
2248                 ext4_trans_abort(mp);
2249                 EXT4_MP_UNLOCK(mp);
2250                 return r;
2251         }
2252         ino = f.inode;
2253         ext4_fclose(&f);
2254         r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2255         if (r != EOK) {
2256                 ext4_trans_abort(mp);
2257                 EXT4_MP_UNLOCK(mp);
2258                 return r;
2259         }
2260
2261         ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
2262         inode_ref.dirty = true;
2263
2264         r = ext4_fs_put_inode_ref(&inode_ref);
2265         if (r != EOK)
2266                 ext4_trans_abort(mp);
2267         else
2268                 ext4_trans_stop(mp);
2269
2270         EXT4_MP_UNLOCK(mp);
2271         return r;
2272 }
2273
2274 static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
2275 {
2276         struct ext4_inode_ref ref;
2277         uint32_t sblock;
2278         ext4_fsblk_t fblock;
2279         uint32_t block_size;
2280         int r;
2281
2282         ext4_assert(f && f->mp);
2283
2284         if (!size)
2285                 return EOK;
2286
2287         r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
2288         if (r != EOK)
2289                 return r;
2290
2291         /*Sync file size*/
2292         block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
2293         if (size > block_size) {
2294                 r = EINVAL;
2295                 goto Finish;
2296         }
2297         r = ext4_ftruncate_no_lock(f, 0);
2298         if (r != EOK)
2299                 goto Finish;
2300
2301         /*Start write back cache mode.*/
2302         r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
2303         if (r != EOK)
2304                 goto Finish;
2305
2306         /*If the size of symlink is smaller than 60 bytes*/
2307         if (size < sizeof(ref.inode->blocks)) {
2308                 memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
2309                 memcpy(ref.inode->blocks, buf, size);
2310                 ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
2311         } else {
2312                 ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
2313                 r = ext4_fs_append_inode_dblk(&ref, &fblock, &sblock);
2314                 if (r != EOK)
2315                         goto Finish;
2316
2317                 r = ext4_block_writebytes(f->mp->fs.bdev, 0, buf, size);
2318                 if (r != EOK)
2319                         goto Finish;
2320
2321         }
2322
2323         /*Stop write back cache mode*/
2324         ext4_block_cache_write_back(f->mp->fs.bdev, 0);
2325
2326         if (r != EOK)
2327                 goto Finish;
2328
2329         ext4_inode_set_size(ref.inode, size);
2330         ref.dirty = true;
2331
2332         f->fsize = size;
2333         if (f->fpos > size)
2334                 f->fpos = size;
2335
2336 Finish:
2337         ext4_fs_put_inode_ref(&ref);
2338         return r;
2339 }
2340
2341 int ext4_fsymlink(const char *target, const char *path)
2342 {
2343         struct ext4_mountpoint *mp = ext4_get_mount(path);
2344         int r;
2345         ext4_file f;
2346         int filetype;
2347
2348         if (!mp)
2349                 return ENOENT;
2350
2351         if (mp->fs.read_only)
2352                 return EROFS;
2353
2354         filetype = EXT4_DE_SYMLINK;
2355
2356         EXT4_MP_LOCK(mp);
2357         ext4_trans_start(mp);
2358
2359         ext4_block_cache_write_back(mp->fs.bdev, 1);
2360         r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, NULL, NULL);
2361         if (r == EOK)
2362                 r = ext4_fsymlink_set(&f, target, strlen(target));
2363         else
2364                 goto Finish;
2365
2366         ext4_fclose(&f);
2367
2368 Finish:
2369         ext4_block_cache_write_back(mp->fs.bdev, 0);
2370
2371         if (r != EOK)
2372                 ext4_trans_abort(mp);
2373         else
2374                 ext4_trans_stop(mp);
2375
2376         EXT4_MP_UNLOCK(mp);
2377         return r;
2378 }
2379
2380 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
2381 {
2382         struct ext4_mountpoint *mp = ext4_get_mount(path);
2383         int r;
2384         ext4_file f;
2385         int filetype;
2386
2387         if (!mp)
2388                 return ENOENT;
2389
2390         if (!buf)
2391                 return EINVAL;
2392
2393         filetype = EXT4_DE_SYMLINK;
2394
2395         EXT4_MP_LOCK(mp);
2396         ext4_block_cache_write_back(mp->fs.bdev, 1);
2397         r = ext4_generic_open2(&f, path, O_RDONLY, filetype, NULL, NULL);
2398         if (r == EOK)
2399                 r = ext4_fread(&f, buf, bufsize, rcnt);
2400         else
2401                 goto Finish;
2402
2403         ext4_fclose(&f);
2404
2405 Finish:
2406         ext4_block_cache_write_back(mp->fs.bdev, 0);
2407         if (r != EOK)
2408                 ext4_trans_abort(mp);
2409         else
2410                 ext4_trans_stop(mp);
2411
2412         EXT4_MP_UNLOCK(mp);
2413         return r;
2414 }
2415
2416 int ext4_setxattr(const char *path, const char *name, size_t name_len,
2417                   const void *data, size_t data_size, bool replace)
2418 {
2419         bool found;
2420         int r = EOK;
2421         ext4_file f;
2422         uint32_t inode;
2423         uint8_t name_index;
2424         const char *dissected_name = NULL;
2425         size_t dissected_len = 0;
2426         struct ext4_xattr_ref xattr_ref;
2427         struct ext4_inode_ref inode_ref;
2428         struct ext4_mountpoint *mp = ext4_get_mount(path);
2429         if (!mp)
2430                 return ENOENT;
2431
2432         if (mp->fs.read_only)
2433                 return EROFS;
2434
2435         dissected_name = ext4_extract_xattr_name(name, name_len,
2436                                 &name_index, &dissected_len,
2437                                 &found);
2438         if (!found)
2439                 return EINVAL;
2440
2441         EXT4_MP_LOCK(mp);
2442         ext4_trans_start(mp);
2443
2444         r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2445         if (r != EOK)
2446                 goto Finish;
2447         inode = f.inode;
2448         ext4_fclose(&f);
2449
2450         r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2451         if (r != EOK)
2452                 goto Finish;
2453
2454         r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2455         if (r != EOK) {
2456                 ext4_fs_put_inode_ref(&inode_ref);
2457                 goto Finish;
2458         }
2459
2460         r = ext4_fs_set_xattr(&xattr_ref, name_index, dissected_name,
2461                         dissected_len, data, data_size, replace);
2462
2463         ext4_fs_put_xattr_ref(&xattr_ref);
2464         ext4_fs_put_inode_ref(&inode_ref);
2465 Finish:
2466         if (r != EOK)
2467                 ext4_trans_abort(mp);
2468         else
2469                 ext4_trans_stop(mp);
2470
2471         EXT4_MP_UNLOCK(mp);
2472         return r;
2473 }
2474
2475 int ext4_getxattr(const char *path, const char *name, size_t name_len,
2476                   void *buf, size_t buf_size, size_t *data_size)
2477 {
2478         bool found;
2479         int r = EOK;
2480         ext4_file f;
2481         uint32_t inode;
2482         uint8_t name_index;
2483         const char *dissected_name = NULL;
2484         size_t dissected_len = 0;
2485         struct ext4_xattr_ref xattr_ref;
2486         struct ext4_inode_ref inode_ref;
2487         struct ext4_mountpoint *mp = ext4_get_mount(path);
2488         if (!mp)
2489                 return ENOENT;
2490
2491         dissected_name = ext4_extract_xattr_name(name, name_len,
2492                                 &name_index, &dissected_len,
2493                                 &found);
2494         if (!found)
2495                 return EINVAL;
2496
2497         EXT4_MP_LOCK(mp);
2498         r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2499         if (r != EOK)
2500                 goto Finish;
2501         inode = f.inode;
2502         ext4_fclose(&f);
2503
2504         r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2505         if (r != EOK)
2506                 goto Finish;
2507
2508         r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2509         if (r != EOK) {
2510                 ext4_fs_put_inode_ref(&inode_ref);
2511                 goto Finish;
2512         }
2513
2514         r = ext4_fs_get_xattr(&xattr_ref, name_index, dissected_name,
2515                                 dissected_len, buf, buf_size, data_size);
2516
2517         ext4_fs_put_xattr_ref(&xattr_ref);
2518         ext4_fs_put_inode_ref(&inode_ref);
2519 Finish:
2520         EXT4_MP_UNLOCK(mp);
2521         return r;
2522 }
2523
2524 struct ext4_listxattr_iterator {
2525         char *list;
2526         char *list_ptr;
2527         size_t size;
2528         size_t ret_size;
2529         bool list_too_small;
2530         bool get_required_size;
2531 };
2532
2533 static int ext4_iterate_ea_list(struct ext4_xattr_ref *ref,
2534                                 struct ext4_xattr_item *item)
2535 {
2536         struct ext4_listxattr_iterator *lxi;
2537         lxi = ref->iter_arg;
2538         if (!lxi->get_required_size) {
2539                 size_t plen;
2540                 const char *prefix;
2541                 prefix = ext4_get_xattr_name_prefix(item->name_index, &plen);
2542                 if (lxi->ret_size + plen + item->name_len + 1 > lxi->size) {
2543                         lxi->list_too_small = 1;
2544                         return EXT4_XATTR_ITERATE_STOP;
2545                 }
2546                 if (prefix) {
2547                         memcpy(lxi->list_ptr, prefix, plen);
2548                         lxi->list_ptr += plen;
2549                         lxi->ret_size += plen;
2550                 }
2551                 memcpy(lxi->list_ptr, item->name, item->name_len);
2552                 lxi->list_ptr[item->name_len] = 0;
2553                 lxi->list_ptr += item->name_len + 1;
2554         }
2555         lxi->ret_size += item->name_len + 1;
2556         return EXT4_XATTR_ITERATE_CONT;
2557 }
2558
2559 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
2560 {
2561         int r = EOK;
2562         ext4_file f;
2563         uint32_t inode;
2564         struct ext4_xattr_ref xattr_ref;
2565         struct ext4_inode_ref inode_ref;
2566         struct ext4_listxattr_iterator lxi;
2567         struct ext4_mountpoint *mp = ext4_get_mount(path);
2568         if (!mp)
2569                 return ENOENT;
2570
2571         lxi.list = list;
2572         lxi.list_ptr = list;
2573         lxi.size = size;
2574         lxi.ret_size = 0;
2575         lxi.list_too_small = false;
2576         lxi.get_required_size = (!size) ? true : false;
2577
2578         EXT4_MP_LOCK(mp);
2579         r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2580         if (r != EOK)
2581                 goto Finish;
2582         inode = f.inode;
2583         ext4_fclose(&f);
2584
2585         r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2586         if (r != EOK)
2587                 goto Finish;
2588
2589         r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2590         if (r != EOK) {
2591                 ext4_fs_put_inode_ref(&inode_ref);
2592                 goto Finish;
2593         }
2594
2595         xattr_ref.iter_arg = &lxi;
2596         ext4_fs_xattr_iterate(&xattr_ref, ext4_iterate_ea_list);
2597         if (lxi.list_too_small)
2598                 r = ERANGE;
2599
2600         if (r == EOK) {
2601                 if (ret_size)
2602                         *ret_size = lxi.ret_size;
2603
2604         }
2605         ext4_fs_put_xattr_ref(&xattr_ref);
2606         ext4_fs_put_inode_ref(&inode_ref);
2607 Finish:
2608         EXT4_MP_UNLOCK(mp);
2609         return r;
2610
2611 }
2612
2613 int ext4_removexattr(const char *path, const char *name, size_t name_len)
2614 {
2615         bool found;
2616         int r = EOK;
2617         ext4_file f;
2618         uint32_t inode;
2619         uint8_t name_index;
2620         const char *dissected_name = NULL;
2621         size_t dissected_len = 0;
2622         struct ext4_xattr_ref xattr_ref;
2623         struct ext4_inode_ref inode_ref;
2624         struct ext4_mountpoint *mp = ext4_get_mount(path);
2625         if (!mp)
2626                 return ENOENT;
2627
2628         if (mp->fs.read_only)
2629                 return EROFS;
2630
2631         dissected_name = ext4_extract_xattr_name(name, name_len,
2632                                                 &name_index, &dissected_len,
2633                                                 &found);
2634         if (!found)
2635                 return EINVAL;
2636
2637         EXT4_MP_LOCK(mp);
2638         ext4_trans_start(mp);
2639
2640         r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2641         if (r != EOK)
2642                 goto Finish;
2643         inode = f.inode;
2644         ext4_fclose(&f);
2645
2646         r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2647         if (r != EOK)
2648                 goto Finish;
2649
2650         r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2651         if (r != EOK) {
2652                 ext4_fs_put_inode_ref(&inode_ref);
2653                 goto Finish;
2654         }
2655
2656         r = ext4_fs_remove_xattr(&xattr_ref, name_index, dissected_name,
2657                                 dissected_len);
2658
2659         ext4_fs_put_xattr_ref(&xattr_ref);
2660         ext4_fs_put_inode_ref(&inode_ref);
2661 Finish:
2662         if (r != EOK)
2663                 ext4_trans_abort(mp);
2664         else
2665                 ext4_trans_stop(mp);
2666
2667         EXT4_MP_UNLOCK(mp);
2668         return r;
2669
2670 }
2671
2672 /*********************************DIRECTORY OPERATION************************/
2673
2674 int ext4_dir_rm(const char *path)
2675 {
2676         int r;
2677         int len;
2678         ext4_file f;
2679
2680         struct ext4_mountpoint *mp = ext4_get_mount(path);
2681         struct ext4_inode_ref act;
2682         struct ext4_inode_ref child;
2683         struct ext4_dir_iter it;
2684
2685         uint32_t name_off;
2686         uint32_t inode_up;
2687         uint32_t inode_current;
2688         uint32_t depth = 1;
2689
2690         bool has_children;
2691         bool is_goal;
2692         bool dir_end;
2693
2694         if (!mp)
2695                 return ENOENT;
2696
2697         if (mp->fs.read_only)
2698                 return EROFS;
2699
2700         EXT4_MP_LOCK(mp);
2701
2702         struct ext4_fs *const fs = &mp->fs;
2703
2704         /*Check if exist.*/
2705         r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
2706         if (r != EOK) {
2707                 ext4_trans_abort(mp);
2708                 EXT4_MP_UNLOCK(mp);
2709                 return r;
2710         }
2711
2712         path += name_off;
2713         len = ext4_path_check(path, &is_goal);
2714
2715         inode_current = f.inode;
2716
2717         ext4_block_cache_write_back(mp->fs.bdev, 1);
2718
2719         do {
2720
2721                 uint64_t act_curr_pos = 0;
2722                 has_children = false;
2723                 dir_end = false;
2724
2725                 while (r == EOK && !has_children && !dir_end) {
2726
2727                         /*Load directory node.*/
2728                         r = ext4_fs_get_inode_ref(fs, inode_current, &act);
2729                         if (r != EOK) {
2730                                 break;
2731                         }
2732
2733                         /*Initialize iterator.*/
2734                         r = ext4_dir_iterator_init(&it, &act, act_curr_pos);
2735                         if (r != EOK) {
2736                                 ext4_fs_put_inode_ref(&act);
2737                                 break;
2738                         }
2739
2740                         if (!it.curr) {
2741                                 dir_end = true;
2742                                 goto End;
2743                         }
2744
2745                         ext4_trans_start(mp);
2746
2747                         /*Get up directory inode when ".." entry*/
2748                         if ((it.curr->name_len == 2) &&
2749                             ext4_is_dots(it.curr->name, it.curr->name_len)) {
2750                                 inode_up = ext4_dir_en_get_inode(it.curr);
2751                         }
2752
2753                         /*If directory or file entry,  but not "." ".." entry*/
2754                         if (!ext4_is_dots(it.curr->name, it.curr->name_len)) {
2755
2756                                 /*Get child inode reference do unlink
2757                                  * directory/file.*/
2758                                 uint32_t cinode;
2759                                 uint32_t inode_type;
2760                                 cinode = ext4_dir_en_get_inode(it.curr);
2761                                 r = ext4_fs_get_inode_ref(fs, cinode, &child);
2762                                 if (r != EOK)
2763                                         goto End;
2764
2765                                 /*If directory with no leaf children*/
2766                                 r = ext4_has_children(&has_children, &child);
2767                                 if (r != EOK) {
2768                                         ext4_fs_put_inode_ref(&child);
2769                                         goto End;
2770                                 }
2771
2772                                 if (has_children) {
2773                                         /*Has directory children. Go into this
2774                                          * directory.*/
2775                                         inode_up = inode_current;
2776                                         inode_current = cinode;
2777                                         depth++;
2778                                         ext4_fs_put_inode_ref(&child);
2779                                         goto End;
2780                                 }
2781                                 inode_type = ext4_inode_type(&mp->fs.sb,
2782                                                 child.inode);
2783
2784                                 /* Truncate */
2785                                 if (inode_type != EXT4_INODE_MODE_DIRECTORY)
2786                                         r = ext4_trunc_inode(mp, child.index, 0);
2787                                 else
2788                                         r = ext4_trunc_dir(mp, &act, &child);
2789
2790                                 if (r != EOK) {
2791                                         ext4_fs_put_inode_ref(&child);
2792                                         goto End;
2793                                 }
2794
2795                                 /*No children in child directory or file. Just
2796                                  * unlink.*/
2797                                 r = ext4_unlink(f.mp, &act, &child,
2798                                                 (char *)it.curr->name,
2799                                                 it.curr->name_len);
2800                                 if (r != EOK) {
2801                                         ext4_fs_put_inode_ref(&child);
2802                                         goto End;
2803                                 }
2804
2805                                 ext4_inode_set_del_time(child.inode, -1L);
2806                                 ext4_inode_set_links_cnt(child.inode, 0);
2807                                 child.dirty = true;
2808
2809                                 r = ext4_fs_free_inode(&child);
2810                                 if (r != EOK) {
2811                                         ext4_fs_put_inode_ref(&child);
2812                                         goto End;
2813                                 }
2814
2815                                 r = ext4_fs_put_inode_ref(&child);
2816                                 if (r != EOK)
2817                                         goto End;
2818
2819                         }
2820
2821                         r = ext4_dir_iterator_next(&it);
2822                         if (r != EOK)
2823                                 goto End;
2824
2825                         act_curr_pos = it.curr_off;
2826 End:
2827                         ext4_dir_iterator_fini(&it);
2828                         if (r == EOK)
2829                                 r = ext4_fs_put_inode_ref(&act);
2830                         else
2831                                 ext4_fs_put_inode_ref(&act);
2832
2833                         if (r != EOK)
2834                                 ext4_trans_abort(mp);
2835                         else
2836                                 ext4_trans_stop(mp);
2837                 }
2838
2839                 if (dir_end) {
2840                         /*Directory iterator reached last entry*/
2841                         depth--;
2842                         if (depth)
2843                                 inode_current = inode_up;
2844
2845                 }
2846
2847                 if (r != EOK)
2848                         break;
2849
2850         } while (depth);
2851
2852         /*Last unlink*/
2853         if (r == EOK && !depth) {
2854                 /*Load parent.*/
2855                 struct ext4_inode_ref parent;
2856                 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
2857                                 &parent);
2858                 if (r != EOK)
2859                         goto Finish;
2860                 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_current,
2861                                 &act);
2862                 if (r != EOK) {
2863                         ext4_fs_put_inode_ref(&act);
2864                         goto Finish;
2865                 }
2866
2867                 ext4_trans_start(mp);
2868
2869                 /* In this place all directories should be
2870                  * unlinked.
2871                  * Last unlink from root of current directory*/
2872                 r = ext4_unlink(f.mp, &parent, &act,
2873                                 (char *)path, len);
2874                 if (r != EOK) {
2875                         ext4_fs_put_inode_ref(&parent);
2876                         ext4_fs_put_inode_ref(&act);
2877                         goto Finish;
2878                 }
2879
2880                 if (ext4_inode_get_links_cnt(act.inode) == 2) {
2881                         ext4_inode_set_del_time(act.inode, -1L);
2882                         ext4_inode_set_links_cnt(act.inode, 0);
2883                         act.dirty = true;
2884                         /*Truncate*/
2885                         r = ext4_trunc_dir(mp, &parent, &act);
2886                         if (r != EOK) {
2887                                 ext4_fs_put_inode_ref(&parent);
2888                                 ext4_fs_put_inode_ref(&act);
2889                                 goto Finish;
2890                         }
2891
2892                         r = ext4_fs_free_inode(&act);
2893                         if (r != EOK) {
2894                                 ext4_fs_put_inode_ref(&parent);
2895                                 ext4_fs_put_inode_ref(&act);
2896                                 goto Finish;
2897                         }
2898                 }
2899
2900                 r = ext4_fs_put_inode_ref(&parent);
2901                 if (r != EOK)
2902                         goto Finish;
2903
2904                 r = ext4_fs_put_inode_ref(&act);
2905         Finish:
2906                 if (r != EOK)
2907                         ext4_trans_abort(mp);
2908                 else
2909                         ext4_trans_stop(mp);
2910         }
2911
2912         ext4_block_cache_write_back(mp->fs.bdev, 0);
2913         EXT4_MP_UNLOCK(mp);
2914
2915         return r;
2916 }
2917
2918 int ext4_dir_mv(const char *path, const char *new_path)
2919 {
2920         return ext4_frename(path, new_path);
2921 }
2922
2923 int ext4_dir_mk(const char *path)
2924 {
2925         int r;
2926         ext4_file f;
2927
2928         struct ext4_mountpoint *mp = ext4_get_mount(path);
2929
2930         if (!mp)
2931                 return ENOENT;
2932
2933         if (mp->fs.read_only)
2934                 return EROFS;
2935
2936         EXT4_MP_LOCK(mp);
2937         ext4_trans_start(mp);
2938
2939         /*Check if exist.*/
2940         r = ext4_generic_open(&f, path, "r", false, 0, 0);
2941         if (r == EOK) {
2942                 /*Directory already created*/
2943                 ext4_trans_stop(mp);
2944                 EXT4_MP_UNLOCK(mp);
2945                 return r;
2946         }
2947
2948         /*Create new dir*/
2949         r = ext4_generic_open(&f, path, "w", false, 0, 0);
2950         if (r != EOK) {
2951                 ext4_trans_abort(mp);
2952                 EXT4_MP_UNLOCK(mp);
2953                 return r;
2954         }
2955
2956         ext4_trans_stop(mp);
2957         EXT4_MP_UNLOCK(mp);
2958         return r;
2959 }
2960
2961 int ext4_dir_open(ext4_dir *d, const char *path)
2962 {
2963         struct ext4_mountpoint *mp = ext4_get_mount(path);
2964         int r;
2965
2966         if (!mp)
2967                 return ENOENT;
2968
2969         EXT4_MP_LOCK(mp);
2970         r = ext4_generic_open(&d->f, path, "r", false, 0, 0);
2971         d->next_off = 0;
2972         EXT4_MP_UNLOCK(mp);
2973         return r;
2974 }
2975
2976 int ext4_dir_close(ext4_dir *d)
2977 {
2978     return ext4_fclose(&d->f);
2979 }
2980
2981 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d)
2982 {
2983 #define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
2984
2985         int r;
2986         ext4_direntry *de = 0;
2987         struct ext4_inode_ref dir;
2988         struct ext4_dir_iter it;
2989
2990         EXT4_MP_LOCK(d->f.mp);
2991
2992         if (d->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) {
2993                 EXT4_MP_UNLOCK(d->f.mp);
2994                 return 0;
2995         }
2996
2997         r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
2998         if (r != EOK) {
2999                 goto Finish;
3000         }
3001
3002         r = ext4_dir_iterator_init(&it, &dir, d->next_off);
3003         if (r != EOK) {
3004                 ext4_fs_put_inode_ref(&dir);
3005                 goto Finish;
3006         }
3007
3008         memcpy(&d->de, it.curr, sizeof(ext4_direntry));
3009         de = &d->de;
3010
3011         ext4_dir_iterator_next(&it);
3012
3013         d->next_off = it.curr ? it.curr_off : EXT4_DIR_ENTRY_OFFSET_TERM;
3014
3015         ext4_dir_iterator_fini(&it);
3016         ext4_fs_put_inode_ref(&dir);
3017
3018 Finish:
3019         EXT4_MP_UNLOCK(d->f.mp);
3020         return de;
3021 }
3022
3023 void ext4_dir_entry_rewind(ext4_dir *d)
3024 {
3025         d->next_off = 0;
3026 }
3027
3028 /**
3029  * @}
3030  */