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