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