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