ext4: during journal test, use jbd_trans_try_revoke_block instead.
[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 *ch, const char *n,
185                      uint32_t len, bool rename)
186 {
187         /* Check maximum name length */
188         if (len > EXT4_DIRECTORY_FILENAME_LEN)
189                 return EINVAL;
190
191         /* Add entry to parent directory */
192         int r = ext4_dir_add_entry(parent, n, len, ch);
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, ch->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(ch, parent);
208                         if (r != EOK)
209                                 return r;
210
211                         ext4_inode_set_flag(ch->inode, EXT4_INODE_FLAG_INDEX);
212                         ch->dirty = true;
213                 } else
214 #endif
215                 {
216                         r = ext4_dir_add_entry(ch, ".", strlen("."), ch);
217                         if (r != EOK) {
218                                 ext4_dir_remove_entry(parent, n, strlen(n));
219                                 return r;
220                         }
221
222                         r = ext4_dir_add_entry(ch, "..", strlen(".."), parent);
223                         if (r != EOK) {
224                                 ext4_dir_remove_entry(parent, n, strlen(n));
225                                 ext4_dir_remove_entry(ch, ".", strlen("."));
226                                 return r;
227                         }
228                 }
229
230                 /*New empty directory. Two links (. and ..) */
231                 ext4_inode_set_links_cnt(ch->inode, 2);
232                 ext4_fs_inode_links_count_inc(parent);
233                 ch->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(ch->inode, EXT4_INODE_FLAG_INDEX);
244                 struct ext4_dir_search_result res;
245                 if (!idx) {
246                         r = ext4_dir_find_entry(&res, ch, "..", strlen(".."));
247                         if (r != EOK)
248                                 return EIO;
249
250                         ext4_dir_en_set_inode(res.dentry, parent->index);
251                         ext4_bcache_set_dirty(res.block.buf);
252                         r = ext4_dir_destroy_result(ch, &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(ch, 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(ch);
270                 ch->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, const char *name,
279                        uint32_t name_len)
280 {
281         bool has_children;
282         int rc = ext4_has_children(&has_children, child);
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,
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,
317          *     (uint32_t) now);
318          */
319         if (ext4_inode_get_links_cnt(child->inode)) {
320                 ext4_fs_inode_links_count_dec(child);
321                 child->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_bcache_cleanup(bc);
407                 ext4_block_fini(bd);
408                 if (mp->cache_dynamic) {
409                         ext4_bcache_fini_dynamic(bc);
410                         free(bc);
411                 }
412                 return r;
413         }
414
415         return r;
416 }
417
418
419 int ext4_umount(const char *mount_point)
420 {
421         int i;
422         int r;
423         struct ext4_mountpoint *mp = 0;
424
425         for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
426                 if (!strcmp(_mp[i].name, mount_point)) {
427                         mp = &_mp[i];
428                         break;
429                 }
430         }
431
432         if (!mp)
433                 return ENODEV;
434
435         r = ext4_fs_fini(&mp->fs);
436         if (r != EOK)
437                 return r;
438
439         mp->mounted = 0;
440
441         ext4_bcache_cleanup(mp->fs.bdev->bc);
442         if (mp->cache_dynamic) {
443                 ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
444                 free(mp->fs.bdev->bc);
445         }
446
447         return ext4_block_fini(mp->fs.bdev);
448 }
449
450 static struct ext4_mountpoint *ext4_get_mount(const char *path)
451 {
452         int i;
453         for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
454
455                 if (!_mp[i].mounted)
456                         continue;
457
458                 if (!strncmp(_mp[i].name, path, strlen(_mp[i].name)))
459                         return &_mp[i];
460         }
461         return 0;
462 }
463
464 int ext4_recover(const char *mount_point)
465 {
466         struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
467         if (!mp)
468                 return ENOENT;
469
470         int r = ENOTSUP;
471         EXT4_MP_LOCK(mp);
472         if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_HAS_JOURNAL)) {
473                 struct jbd_fs *jbd_fs = calloc(1, sizeof(struct jbd_fs));
474                 if (!jbd_fs) {
475                          r = ENOMEM;
476                          goto Finish;
477                 }
478
479
480                 r = jbd_get_fs(&mp->fs, jbd_fs);
481                 if (r != EOK) {
482                         free(jbd_fs);
483                         goto Finish;
484                 }
485
486                 r = jbd_recover(jbd_fs);
487                 jbd_put_fs(jbd_fs);
488                 free(jbd_fs);
489         }
490
491
492 Finish:
493         EXT4_MP_UNLOCK(mp);
494         return r;
495 }
496
497
498 int ext4_mount_point_stats(const char *mount_point,
499                            struct ext4_mount_stats *stats)
500 {
501         struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
502
503         if (!mp)
504                 return ENOENT;
505
506         EXT4_MP_LOCK(mp);
507         stats->inodes_count = ext4_get32(&mp->fs.sb, inodes_count);
508         stats->free_inodes_count = ext4_get32(&mp->fs.sb, free_inodes_count);
509         stats->blocks_count = ext4_sb_get_blocks_cnt(&mp->fs.sb);
510         stats->free_blocks_count = ext4_sb_get_free_blocks_cnt(&mp->fs.sb);
511         stats->block_size = ext4_sb_get_block_size(&mp->fs.sb);
512
513         stats->block_group_count = ext4_block_group_cnt(&mp->fs.sb);
514         stats->blocks_per_group = ext4_get32(&mp->fs.sb, blocks_per_group);
515         stats->inodes_per_group = ext4_get32(&mp->fs.sb, inodes_per_group);
516
517         memcpy(stats->volume_name, mp->fs.sb.volume_name, 16);
518         EXT4_MP_UNLOCK(mp);
519
520         return EOK;
521 }
522
523 int ext4_mount_setup_locks(const char *mount_point,
524                            const struct ext4_lock *locks)
525 {
526         uint32_t i;
527         struct ext4_mountpoint *mp = 0;
528
529         for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
530                 if (!strcmp(_mp[i].name, mount_point)) {
531                         mp = &_mp[i];
532                         break;
533                 }
534         }
535         if (!mp)
536                 return ENOENT;
537
538         mp->os_locks = locks;
539         return EOK;
540 }
541
542 /********************************FILE OPERATIONS*****************************/
543
544 static int ext4_path_check(const char *path, bool *is_goal)
545 {
546         int i;
547
548         for (i = 0; i < EXT4_DIRECTORY_FILENAME_LEN; ++i) {
549
550                 if (path[i] == '/') {
551                         *is_goal = false;
552                         return i;
553                 }
554
555                 if (path[i] == 0) {
556                         *is_goal = true;
557                         return i;
558                 }
559         }
560
561         return 0;
562 }
563
564 static bool ext4_parse_flags(const char *flags, uint32_t *file_flags)
565 {
566         if (!flags)
567                 return false;
568
569         if (!strcmp(flags, "r") || !strcmp(flags, "rb")) {
570                 *file_flags = O_RDONLY;
571                 return true;
572         }
573
574         if (!strcmp(flags, "w") || !strcmp(flags, "wb")) {
575                 *file_flags = O_WRONLY | O_CREAT | O_TRUNC;
576                 return true;
577         }
578
579         if (!strcmp(flags, "a") || !strcmp(flags, "ab")) {
580                 *file_flags = O_WRONLY | O_CREAT | O_APPEND;
581                 return true;
582         }
583
584         if (!strcmp(flags, "r+") || !strcmp(flags, "rb+") ||
585             !strcmp(flags, "r+b")) {
586                 *file_flags = O_RDWR;
587                 return true;
588         }
589
590         if (!strcmp(flags, "w+") || !strcmp(flags, "wb+") ||
591             !strcmp(flags, "w+b")) {
592                 *file_flags = O_RDWR | O_CREAT | O_TRUNC;
593                 return true;
594         }
595
596         if (!strcmp(flags, "a+") || !strcmp(flags, "ab+") ||
597             !strcmp(flags, "a+b")) {
598                 *file_flags = O_RDWR | O_CREAT | O_APPEND;
599                 return true;
600         }
601
602         return false;
603 }
604
605 /*
606  * NOTICE: if filetype is equal to EXT4_DIRENTRY_UNKNOWN,
607  * any filetype of the target dir entry will be accepted.
608  */
609 static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
610                               int ftype, uint32_t *parent_inode,
611                               uint32_t *name_off)
612 {
613         bool is_goal = false;
614         uint32_t imode = EXT4_INODE_MODE_DIRECTORY;
615         uint32_t next_inode;
616
617         int r;
618         int len;
619         struct ext4_mountpoint *mp = ext4_get_mount(path);
620         struct ext4_dir_search_result result;
621         struct ext4_inode_ref ref;
622
623         f->mp = 0;
624
625         if (!mp)
626                 return ENOENT;
627
628         struct ext4_fs *const fs = &mp->fs;
629         struct ext4_sblock *const sb = &mp->fs.sb;
630
631         f->flags = flags;
632
633         /*Skip mount point*/
634         path += strlen(mp->name);
635
636         if (name_off)
637                 *name_off = strlen(mp->name);
638
639         /*Load root*/
640         r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
641         if (r != EOK)
642                 return r;
643
644         if (parent_inode)
645                 *parent_inode = ref.index;
646
647         len = ext4_path_check(path, &is_goal);
648         while (1) {
649
650                 len = ext4_path_check(path, &is_goal);
651                 if (!len) {
652                         /*If root open was request.*/
653                         if (ftype == EXT4_DE_DIR || ftype == EXT4_DE_UNKNOWN)
654                                 if (is_goal)
655                                         break;
656
657                         r = ENOENT;
658                         break;
659                 }
660
661                 r = ext4_dir_find_entry(&result, &ref, path, len);
662                 if (r != EOK) {
663
664                         /*Destroy last result*/
665                         ext4_dir_destroy_result(&ref, &result);
666                         if (r != ENOENT)
667                                 break;
668
669                         if (!(f->flags & O_CREAT))
670                                 break;
671
672                         /*O_CREAT allows create new entry*/
673                         struct ext4_inode_ref child_ref;
674                         r = ext4_fs_alloc_inode(fs, &child_ref,
675                                         is_goal ? ftype : EXT4_DE_DIR);
676                         if (r != EOK)
677                                 break;
678
679
680                         /*Link with root dir.*/
681                         r = ext4_link(mp, &ref, &child_ref, path, len, false);
682                         if (r != EOK) {
683                                 /*Fail. Free new inode.*/
684                                 ext4_fs_free_inode(&child_ref);
685                                 /*We do not want to write new inode.
686                                   But block has to be released.*/
687                                 child_ref.dirty = false;
688                                 ext4_fs_put_inode_ref(&child_ref);
689                                 break;
690                         }
691
692                         ext4_fs_put_inode_ref(&child_ref);
693                         continue;
694                 }
695
696                 if (parent_inode)
697                         *parent_inode = ref.index;
698
699                 next_inode = ext4_dir_en_get_inode(result.dentry);
700                 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
701                         uint8_t t;
702                         t = ext4_dir_en_get_inode_type(sb, result.dentry);
703                         imode = ext4_fs_correspond_inode_mode(t);
704                 } else {
705                         struct ext4_inode_ref child_ref;
706                         r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
707                         if (r != EOK)
708                                 break;
709
710                         imode = ext4_inode_type(sb, child_ref.inode);
711                         ext4_fs_put_inode_ref(&child_ref);
712                 }
713
714                 r = ext4_dir_destroy_result(&ref, &result);
715                 if (r != EOK)
716                         break;
717
718                 /*If expected file error*/
719                 if (imode != EXT4_INODE_MODE_DIRECTORY && !is_goal) {
720                         r = ENOENT;
721                         break;
722                 }
723                 if (ftype != EXT4_DE_UNKNOWN) {
724                         bool df = imode != ext4_fs_correspond_inode_mode(ftype);
725                         if (df && is_goal) {
726                                 r = ENOENT;
727                                 break;
728                         }
729                 }
730
731                 r = ext4_fs_put_inode_ref(&ref);
732                 if (r != EOK)
733                         break;
734
735                 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
736                 if (r != EOK)
737                         break;
738
739                 if (is_goal)
740                         break;
741
742                 path += len + 1;
743
744                 if (name_off)
745                         *name_off += len + 1;
746         };
747
748         if (r != EOK) {
749                 ext4_fs_put_inode_ref(&ref);
750                 return r;
751         }
752
753         if (is_goal) {
754
755                 if ((f->flags & O_TRUNC) && (imode == EXT4_INODE_MODE_FILE)) {
756                         r = ext4_fs_truncate_inode(&ref, 0);
757                         if (r != EOK) {
758                                 ext4_fs_put_inode_ref(&ref);
759                                 return r;
760                         }
761                 }
762
763                 f->mp = mp;
764                 f->fsize = ext4_inode_get_size(sb, ref.inode);
765                 f->inode = ref.index;
766                 f->fpos = 0;
767
768                 if (f->flags & O_APPEND)
769                         f->fpos = f->fsize;
770
771         }
772
773         r = ext4_fs_put_inode_ref(&ref);
774         return r;
775 }
776
777 /****************************************************************************/
778
779 static int ext4_generic_open(ext4_file *f, const char *path, const char *flags,
780                              bool file_expect, uint32_t *parent_inode,
781                              uint32_t *name_off)
782 {
783         uint32_t iflags;
784         int filetype;
785         if (ext4_parse_flags(flags, &iflags) == false)
786                 return EINVAL;
787
788         if (file_expect == true)
789                 filetype = EXT4_DE_REG_FILE;
790         else
791                 filetype = EXT4_DE_DIR;
792
793         return ext4_generic_open2(f, path, iflags, filetype, parent_inode,
794                                   name_off);
795 }
796
797 static int ext4_create_hardlink(const char *path,
798                 struct ext4_inode_ref *child_ref, bool rename)
799 {
800         bool is_goal = false;
801         uint32_t inode_mode = EXT4_INODE_MODE_DIRECTORY;
802         uint32_t next_inode;
803
804         int r;
805         int len;
806         struct ext4_mountpoint *mp = ext4_get_mount(path);
807         struct ext4_dir_search_result result;
808         struct ext4_inode_ref ref;
809
810         if (!mp)
811                 return ENOENT;
812
813         struct ext4_fs *const fs = &mp->fs;
814         struct ext4_sblock *const sb = &mp->fs.sb;
815
816         /*Skip mount point*/
817         path += strlen(mp->name);
818
819         /*Load root*/
820         r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
821         if (r != EOK)
822                 return r;
823
824         len = ext4_path_check(path, &is_goal);
825         while (1) {
826
827                 len = ext4_path_check(path, &is_goal);
828                 if (!len) {
829                         /*If root open was request.*/
830                         r = is_goal ? EINVAL : ENOENT;
831                         break;
832                 }
833
834                 r = ext4_dir_find_entry(&result, &ref, path, len);
835                 if (r != EOK) {
836
837                         /*Destroy last result*/
838                         ext4_dir_destroy_result(&ref, &result);
839
840                         if (r != ENOENT || !is_goal)
841                                 break;
842
843                         /*Link with root dir.*/
844                         r = ext4_link(mp, &ref, child_ref, path, len, rename);
845                         break;
846                 } else if (r == EOK && is_goal) {
847                         /*Destroy last result*/
848                         ext4_dir_destroy_result(&ref, &result);
849                         r = EEXIST;
850                         break;
851                 }
852
853                 next_inode = result.dentry->inode;
854                 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
855                         uint8_t t;
856                         t = ext4_dir_en_get_inode_type(sb, result.dentry);
857                         inode_mode = ext4_fs_correspond_inode_mode(t);
858                 } else {
859                         struct ext4_inode_ref child_ref;
860                         r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
861                         if (r != EOK)
862                                 break;
863
864                         inode_mode = ext4_inode_type(sb, child_ref.inode);
865                         ext4_fs_put_inode_ref(&child_ref);
866                 }
867
868                 r = ext4_dir_destroy_result(&ref, &result);
869                 if (r != EOK)
870                         break;
871
872                 if (inode_mode != EXT4_INODE_MODE_DIRECTORY) {
873                         r = is_goal ? EEXIST : ENOENT;
874                         break;
875                 }
876
877                 r = ext4_fs_put_inode_ref(&ref);
878                 if (r != EOK)
879                         break;
880
881                 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
882                 if (r != EOK)
883                         break;
884
885                 if (is_goal)
886                         break;
887
888                 path += len + 1;
889         };
890
891         if (r != EOK) {
892                 ext4_fs_put_inode_ref(&ref);
893                 return r;
894         }
895
896         r = ext4_fs_put_inode_ref(&ref);
897         return r;
898 }
899
900 static int ext4_remove_orig_reference(const char *path, uint32_t name_off,
901                                       struct ext4_inode_ref *parent_ref,
902                                       struct ext4_inode_ref *child_ref)
903 {
904         bool is_goal;
905         int r;
906         int len;
907         struct ext4_mountpoint *mp = ext4_get_mount(path);
908
909         if (!mp)
910                 return ENOENT;
911
912         /*Set path*/
913         path += name_off;
914
915         len = ext4_path_check(path, &is_goal);
916
917         /* Remove entry from parent directory */
918         r = ext4_dir_remove_entry(parent_ref, path, len);
919         if (r != EOK)
920                 goto Finish;
921
922         if (ext4_inode_is_type(&mp->fs.sb, child_ref->inode,
923                                EXT4_INODE_MODE_DIRECTORY)) {
924                 ext4_fs_inode_links_count_dec(parent_ref);
925                 parent_ref->dirty = true;
926         }
927 Finish:
928         return r;
929 }
930
931 int ext4_flink(const char *path, const char *hardlink_path)
932 {
933         int r;
934         ext4_file f;
935         uint32_t name_off;
936         bool child_loaded = false;
937         uint32_t parent_inode, child_inode;
938         struct ext4_mountpoint *mp = ext4_get_mount(path);
939         struct ext4_mountpoint *target_mp = ext4_get_mount(hardlink_path);
940         struct ext4_inode_ref child_ref;
941
942         if (!mp)
943                 return ENOENT;
944
945         /* Will that happen? Anyway return EINVAL for such case. */
946         if (mp != target_mp)
947                 return EINVAL;
948
949         EXT4_MP_LOCK(mp);
950
951         r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
952                                &parent_inode, &name_off);
953         if (r != EOK)
954                 goto Finish;
955
956         child_inode = f.inode;
957         ext4_fclose(&f);
958
959         /*We have file to unlink. Load it.*/
960         r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
961         if (r != EOK)
962                 goto Finish;
963
964         child_loaded = true;
965
966         /* Creating hardlink for directory is not allowed. */
967         if (ext4_inode_is_type(&mp->fs.sb, child_ref.inode,
968                                EXT4_INODE_MODE_DIRECTORY)) {
969                 r = EINVAL;
970                 goto Finish;
971         }
972
973         r = ext4_create_hardlink(hardlink_path, &child_ref, false);
974
975 Finish:
976         if (child_loaded)
977                 ext4_fs_put_inode_ref(&child_ref);
978
979         EXT4_MP_UNLOCK(mp);
980         return r;
981
982 }
983
984 int ext4_frename(const char *path, const char *new_path)
985 {
986         int r;
987         ext4_file f;
988         uint32_t name_off;
989         bool parent_loaded = false, child_loaded = false;
990         uint32_t parent_inode, child_inode;
991         struct ext4_mountpoint *mp = ext4_get_mount(path);
992         struct ext4_inode_ref child_ref, parent_ref;
993
994         if (!mp)
995                 return ENOENT;
996
997         EXT4_MP_LOCK(mp);
998
999         r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1000                                 &parent_inode, &name_off);
1001         if (r != EOK)
1002                 goto Finish;
1003
1004         child_inode = f.inode;
1005         ext4_fclose(&f);
1006
1007         /*Load parent*/
1008         r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent_ref);
1009         if (r != EOK)
1010                 goto Finish;
1011
1012         parent_loaded = true;
1013
1014         /*We have file to unlink. Load it.*/
1015         r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1016         if (r != EOK)
1017                 goto Finish;
1018
1019         child_loaded = true;
1020
1021         r = ext4_create_hardlink(new_path, &child_ref, true);
1022         if (r != EOK)
1023                 goto Finish;
1024
1025         r = ext4_remove_orig_reference(path, name_off, &parent_ref, &child_ref);
1026         if (r != EOK)
1027                 goto Finish;
1028
1029 Finish:
1030         if (parent_loaded)
1031                 ext4_fs_put_inode_ref(&parent_ref);
1032
1033         if (child_loaded)
1034                 ext4_fs_put_inode_ref(&child_ref);
1035
1036         EXT4_MP_UNLOCK(mp);
1037         return r;
1038
1039 }
1040
1041 /****************************************************************************/
1042
1043 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb)
1044 {
1045         struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
1046
1047         if (!mp)
1048                 return ENOENT;
1049
1050         *sb = &mp->fs.sb;
1051         return EOK;
1052 }
1053
1054 int ext4_cache_write_back(const char *path, bool on)
1055 {
1056         struct ext4_mountpoint *mp = ext4_get_mount(path);
1057
1058         if (!mp)
1059                 return ENOENT;
1060
1061         EXT4_MP_LOCK(mp);
1062         ext4_block_cache_write_back(mp->fs.bdev, on);
1063         EXT4_MP_UNLOCK(mp);
1064         return EOK;
1065 }
1066
1067 int ext4_fremove(const char *path)
1068 {
1069         ext4_file f;
1070         uint32_t parent_inode;
1071         uint32_t name_off;
1072         bool is_goal;
1073         int r;
1074         int len;
1075         struct ext4_inode_ref child;
1076         struct ext4_inode_ref parent;
1077         struct ext4_mountpoint *mp = ext4_get_mount(path);
1078
1079         if (!mp)
1080                 return ENOENT;
1081
1082         EXT4_MP_LOCK(mp);
1083         r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN,
1084                                &parent_inode, &name_off);
1085         if (r != EOK) {
1086                 EXT4_MP_UNLOCK(mp);
1087                 return r;
1088         }
1089
1090         /*Load parent*/
1091         r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
1092         if (r != EOK) {
1093                 EXT4_MP_UNLOCK(mp);
1094                 return r;
1095         }
1096
1097         /*We have file to delete. Load it.*/
1098         r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
1099         if (r != EOK) {
1100                 ext4_fs_put_inode_ref(&parent);
1101                 EXT4_MP_UNLOCK(mp);
1102                 return r;
1103         }
1104
1105         /*Set path*/
1106         path += name_off;
1107
1108         len = ext4_path_check(path, &is_goal);
1109
1110         /*Unlink from parent*/
1111         r = ext4_unlink(mp, &parent, &child, path, len);
1112         if (r != EOK)
1113                 goto Finish;
1114
1115         /*Link count is zero, the inode should be freed. */
1116         if (!ext4_inode_get_links_cnt(child.inode)) {
1117                 ext4_inode_set_del_time(child.inode, -1L);
1118                 /*Turncate*/
1119                 ext4_block_cache_write_back(mp->fs.bdev, 1);
1120                 /*Truncate may be IO heavy. Do it writeback cache mode.*/
1121                 r = ext4_fs_truncate_inode(&child, 0);
1122                 ext4_block_cache_write_back(mp->fs.bdev, 0);
1123
1124                 if (r != EOK)
1125                         goto Finish;
1126
1127                 r = ext4_fs_free_inode(&child);
1128                 if (r != EOK)
1129                         goto Finish;
1130         }
1131
1132 Finish:
1133         ext4_fs_put_inode_ref(&child);
1134         ext4_fs_put_inode_ref(&parent);
1135         EXT4_MP_UNLOCK(mp);
1136         return r;
1137 }
1138
1139 int ext4_fill_raw_inode(const char *path, uint32_t *ret_ino,
1140                         struct ext4_inode *inode)
1141 {
1142         int r;
1143         ext4_file f;
1144         struct ext4_inode_ref inode_ref;
1145         struct ext4_mountpoint *mp = ext4_get_mount(path);
1146         uint32_t ino;
1147
1148         if (!mp)
1149                 return ENOENT;
1150
1151         EXT4_MP_LOCK(mp);
1152
1153         r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
1154         if (r != EOK) {
1155                 EXT4_MP_UNLOCK(mp);
1156                 return r;
1157         }
1158
1159         ino = f.inode;
1160         ext4_fclose(&f);
1161
1162         /*Load parent*/
1163         r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1164         if (r != EOK) {
1165                 EXT4_MP_UNLOCK(mp);
1166                 return r;
1167         }
1168
1169         memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
1170         ext4_fs_put_inode_ref(&inode_ref);
1171         EXT4_MP_UNLOCK(mp);
1172
1173         if (ret_ino)
1174                 *ret_ino = ino;
1175
1176         return r;
1177 }
1178
1179 int ext4_fopen(ext4_file *f, const char *path, const char *flags)
1180 {
1181         struct ext4_mountpoint *mp = ext4_get_mount(path);
1182         int r;
1183
1184         if (!mp)
1185                 return ENOENT;
1186
1187         EXT4_MP_LOCK(mp);
1188         ext4_block_cache_write_back(mp->fs.bdev, 1);
1189         r = ext4_generic_open(f, path, flags, true, 0, 0);
1190         ext4_block_cache_write_back(mp->fs.bdev, 0);
1191         EXT4_MP_UNLOCK(mp);
1192         return r;
1193 }
1194
1195 int ext4_fopen2(ext4_file *f, const char *path, int flags)
1196 {
1197         struct ext4_mountpoint *mp = ext4_get_mount(path);
1198         int r;
1199         int filetype;
1200
1201         if (!mp)
1202                 return ENOENT;
1203
1204         filetype = EXT4_DE_REG_FILE;
1205
1206         EXT4_MP_LOCK(mp);
1207         ext4_block_cache_write_back(mp->fs.bdev, 1);
1208         r = ext4_generic_open2(f, path, flags, filetype, NULL, NULL);
1209         ext4_block_cache_write_back(mp->fs.bdev, 0);
1210         EXT4_MP_UNLOCK(mp);
1211         return r;
1212 }
1213
1214 int ext4_fclose(ext4_file *f)
1215 {
1216         ext4_assert(f && f->mp);
1217
1218         f->mp = 0;
1219         f->flags = 0;
1220         f->inode = 0;
1221         f->fpos = f->fsize = 0;
1222
1223         return EOK;
1224 }
1225
1226 static int ext4_ftruncate_no_lock(ext4_file *f, uint64_t size)
1227 {
1228         struct ext4_inode_ref ref;
1229         int r;
1230
1231
1232         r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1233         if (r != EOK) {
1234                 EXT4_MP_UNLOCK(f->mp);
1235                 return r;
1236         }
1237
1238         /*Sync file size*/
1239         f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1240         if (f->fsize <= size) {
1241                 r = EOK;
1242                 goto Finish;
1243         }
1244
1245         /*Start write back cache mode.*/
1246         r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1247         if (r != EOK)
1248                 goto Finish;
1249
1250         r = ext4_fs_truncate_inode(&ref, size);
1251         if (r != EOK)
1252                 goto Finish;
1253
1254         f->fsize = size;
1255         if (f->fpos > size)
1256                 f->fpos = size;
1257
1258         /*Stop write back cache mode*/
1259         ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1260
1261         if (r != EOK)
1262                 goto Finish;
1263
1264 Finish:
1265         ext4_fs_put_inode_ref(&ref);
1266         return r;
1267
1268 }
1269
1270 int ext4_ftruncate(ext4_file *f, uint64_t size)
1271 {
1272         int r;
1273         ext4_assert(f && f->mp);
1274
1275         if (f->flags & O_RDONLY)
1276                 return EPERM;
1277
1278         EXT4_MP_LOCK(f->mp);
1279         r = ext4_ftruncate_no_lock(f, size);
1280         EXT4_MP_UNLOCK(f->mp);
1281         return r;
1282 }
1283
1284 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
1285 {
1286         uint32_t unalg;
1287         uint32_t iblock_idx;
1288         uint32_t iblock_last;
1289         uint32_t block_size;
1290
1291         ext4_fsblk_t fblock;
1292         ext4_fsblk_t fblock_start;
1293         uint32_t fblock_count;
1294
1295         uint8_t *u8_buf = buf;
1296         int r;
1297         struct ext4_inode_ref ref;
1298
1299         ext4_assert(f && f->mp);
1300
1301         if (f->flags & O_WRONLY)
1302                 return EPERM;
1303
1304         if (!size)
1305                 return EOK;
1306
1307         EXT4_MP_LOCK(f->mp);
1308
1309         struct ext4_fs *const fs = &f->mp->fs;
1310         struct ext4_sblock *const sb = &f->mp->fs.sb;
1311
1312         if (rcnt)
1313                 *rcnt = 0;
1314
1315         r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1316         if (r != EOK) {
1317                 EXT4_MP_UNLOCK(f->mp);
1318                 return r;
1319         }
1320
1321         /*Sync file size*/
1322         f->fsize = ext4_inode_get_size(sb, ref.inode);
1323
1324         block_size = ext4_sb_get_block_size(sb);
1325         size = size > (f->fsize - f->fpos) ? (f->fsize - f->fpos) : size;
1326
1327         iblock_idx = (f->fpos) / block_size;
1328         iblock_last = (f->fpos + size) / block_size;
1329         unalg = (f->fpos) % block_size;
1330
1331         /*If the size of symlink is smaller than 60 bytes*/
1332         bool softlink;
1333         softlink = ext4_inode_is_type(sb, ref.inode, EXT4_INODE_MODE_SOFTLINK);
1334         if (softlink && f->fsize < sizeof(ref.inode->blocks)
1335                      && !ext4_inode_get_blocks_count(sb, ref.inode)) {
1336
1337                 char *content = (char *)ref.inode->blocks;
1338                 if (f->fpos < f->fsize) {
1339                         size_t len = size;
1340                         if (unalg + size > f->fsize)
1341                                 len = f->fsize - unalg;
1342                         memcpy(buf, content + unalg, len);
1343                         if (rcnt)
1344                                 *rcnt = len;
1345
1346                 }
1347
1348                 r = EOK;
1349                 goto Finish;
1350         }
1351
1352         if (unalg) {
1353                 size_t len =  size;
1354                 if (size > (block_size - unalg))
1355                         len = block_size - unalg;
1356
1357                 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1358                 if (r != EOK)
1359                         goto Finish;
1360
1361                 /* Do we get an unwritten range? */
1362                 if (fblock != 0) {
1363                         uint64_t off = fblock * block_size + unalg;
1364                         r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, len);
1365                         if (r != EOK)
1366                                 goto Finish;
1367
1368                 } else {
1369                         /* Yes, we do. */
1370                         memset(u8_buf, 0, len);
1371                 }
1372
1373                 u8_buf += len;
1374                 size -= len;
1375                 f->fpos += len;
1376
1377                 if (rcnt)
1378                         *rcnt += len;
1379
1380                 iblock_idx++;
1381         }
1382
1383         fblock_start = 0;
1384         fblock_count = 0;
1385         while (size >= block_size) {
1386                 while (iblock_idx < iblock_last) {
1387                         r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx,
1388                                                        &fblock, true);
1389                         if (r != EOK)
1390                                 goto Finish;
1391
1392                         iblock_idx++;
1393
1394                         if (!fblock_start)
1395                                 fblock_start = fblock;
1396
1397                         if ((fblock_start + fblock_count) != fblock)
1398                                 break;
1399
1400                         fblock_count++;
1401                 }
1402
1403                 r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1404                                            fblock_count);
1405                 if (r != EOK)
1406                         goto Finish;
1407
1408                 size -= block_size * fblock_count;
1409                 u8_buf += block_size * fblock_count;
1410                 f->fpos += block_size * fblock_count;
1411
1412                 if (rcnt)
1413                         *rcnt += block_size * fblock_count;
1414
1415                 fblock_start = fblock;
1416                 fblock_count = 1;
1417         }
1418
1419         if (size) {
1420                 uint64_t off;
1421                 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1422                 if (r != EOK)
1423                         goto Finish;
1424
1425                 off = fblock * block_size;
1426                 r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, size);
1427                 if (r != EOK)
1428                         goto Finish;
1429
1430                 f->fpos += size;
1431
1432                 if (rcnt)
1433                         *rcnt += size;
1434         }
1435
1436 Finish:
1437         ext4_fs_put_inode_ref(&ref);
1438         EXT4_MP_UNLOCK(f->mp);
1439         return r;
1440 }
1441
1442 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
1443 {
1444         uint32_t unalg;
1445         uint32_t iblk_idx;
1446         uint32_t iblock_last;
1447         uint32_t ifile_blocks;
1448         uint32_t block_size;
1449
1450         uint32_t fblock_count;
1451         ext4_fsblk_t fblk;
1452         ext4_fsblk_t fblock_start;
1453
1454         struct ext4_inode_ref ref;
1455         const uint8_t *u8_buf = buf;
1456         int r, rr = EOK;
1457
1458         ext4_assert(f && f->mp);
1459
1460         if (f->flags & O_RDONLY)
1461                 return EPERM;
1462
1463         if (!size)
1464                 return EOK;
1465
1466         EXT4_MP_LOCK(f->mp);
1467
1468         struct ext4_fs *const fs = &f->mp->fs;
1469         struct ext4_sblock *const sb = &f->mp->fs.sb;
1470
1471         if (wcnt)
1472                 *wcnt = 0;
1473
1474         r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1475         if (r != EOK) {
1476                 EXT4_MP_UNLOCK(f->mp);
1477                 return r;
1478         }
1479
1480         /*Sync file size*/
1481         f->fsize = ext4_inode_get_size(sb, ref.inode);
1482         block_size = ext4_sb_get_block_size(sb);
1483
1484         iblock_last = (f->fpos + size) / block_size;
1485         iblk_idx = (f->fpos) / block_size;
1486         ifile_blocks = (f->fsize + block_size - 1) / block_size;
1487
1488         unalg = (f->fpos) % block_size;
1489
1490         if (unalg) {
1491                 size_t len =  size;
1492                 uint64_t off;
1493                 if (size > (block_size - unalg))
1494                         len = block_size - unalg;
1495
1496                 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1497                 if (r != EOK)
1498                         goto Finish;
1499
1500                 off = fblk * block_size + unalg;
1501                 r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, len);
1502                 if (r != EOK)
1503                         goto Finish;
1504
1505                 u8_buf += len;
1506                 size -= len;
1507                 f->fpos += len;
1508
1509                 if (wcnt)
1510                         *wcnt += len;
1511
1512                 iblk_idx++;
1513         }
1514
1515         /*Start write back cache mode.*/
1516         r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1517         if (r != EOK)
1518                 goto Finish;
1519
1520         fblock_start = 0;
1521         fblock_count = 0;
1522         while (size >= block_size) {
1523
1524                 while (iblk_idx < iblock_last) {
1525                         if (iblk_idx < ifile_blocks) {
1526                                 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx,
1527                                                                 &fblk);
1528                                 if (r != EOK)
1529                                         goto Finish;
1530                         } else {
1531                                 rr = ext4_fs_append_inode_dblk(&ref, &fblk,
1532                                                                &iblk_idx);
1533                                 if (rr != EOK) {
1534                                         /* Unable to append more blocks. But
1535                                          * some block might be allocated already
1536                                          * */
1537                                         break;
1538                                 }
1539                         }
1540
1541                         iblk_idx++;
1542
1543                         if (!fblock_start) {
1544                                 fblock_start = fblk;
1545                         }
1546
1547                         if ((fblock_start + fblock_count) != fblk)
1548                                 break;
1549
1550                         fblock_count++;
1551                 }
1552
1553                 r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1554                                            fblock_count);
1555                 if (r != EOK)
1556                         break;
1557
1558                 size -= block_size * fblock_count;
1559                 u8_buf += block_size * fblock_count;
1560                 f->fpos += block_size * fblock_count;
1561
1562                 if (wcnt)
1563                         *wcnt += block_size * fblock_count;
1564
1565                 fblock_start = fblk;
1566                 fblock_count = 1;
1567
1568                 if (rr != EOK) {
1569                         /*ext4_fs_append_inode_block has failed and no
1570                          * more blocks might be written. But node size
1571                          * should be updated.*/
1572                         r = rr;
1573                         goto out_fsize;
1574                 }
1575         }
1576
1577         /*Stop write back cache mode*/
1578         ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1579
1580         if (r != EOK)
1581                 goto Finish;
1582
1583         if (size) {
1584                 uint64_t off;
1585                 if (iblk_idx < ifile_blocks) {
1586                         r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1587                         if (r != EOK)
1588                                 goto Finish;
1589                 } else {
1590                         r = ext4_fs_append_inode_dblk(&ref, &fblk, &iblk_idx);
1591                         if (r != EOK)
1592                                 /*Node size sholud be updated.*/
1593                                 goto out_fsize;
1594                 }
1595
1596                 off = fblk * block_size;
1597                 r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, size);
1598                 if (r != EOK)
1599                         goto Finish;
1600
1601                 f->fpos += size;
1602
1603                 if (wcnt)
1604                         *wcnt += size;
1605         }
1606
1607 out_fsize:
1608         if (f->fpos > f->fsize) {
1609                 f->fsize = f->fpos;
1610                 ext4_inode_set_size(ref.inode, f->fsize);
1611                 ref.dirty = true;
1612         }
1613
1614 Finish:
1615         ext4_fs_put_inode_ref(&ref);
1616         EXT4_MP_UNLOCK(f->mp);
1617         return r;
1618 }
1619
1620 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin)
1621 {
1622         switch (origin) {
1623         case SEEK_SET:
1624                 if (offset > f->fsize)
1625                         return EINVAL;
1626
1627                 f->fpos = offset;
1628                 return EOK;
1629         case SEEK_CUR:
1630                 if ((offset + f->fpos) > f->fsize)
1631                         return EINVAL;
1632
1633                 f->fpos += offset;
1634                 return EOK;
1635         case SEEK_END:
1636                 if (offset > f->fsize)
1637                         return EINVAL;
1638
1639                 f->fpos = f->fsize - offset;
1640                 return EOK;
1641         }
1642         return EINVAL;
1643 }
1644
1645 uint64_t ext4_ftell(ext4_file *f)
1646 {
1647         return f->fpos;
1648 }
1649
1650 uint64_t ext4_fsize(ext4_file *f)
1651 {
1652         return f->fsize;
1653 }
1654
1655 int ext4_chmod(const char *path, uint32_t mode)
1656 {
1657         int r;
1658         uint32_t ino;
1659         ext4_file f;
1660         struct ext4_sblock *sb;
1661         struct ext4_inode_ref inode_ref;
1662         struct ext4_mountpoint *mp = ext4_get_mount(path);
1663
1664         if (!mp)
1665                 return ENOENT;
1666
1667         EXT4_MP_LOCK(mp);
1668
1669         r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1670         if (r != EOK) {
1671                 EXT4_MP_UNLOCK(mp);
1672                 return r;
1673         }
1674         ino = f.inode;
1675         sb = &mp->fs.sb;
1676         ext4_fclose(&f);
1677         r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1678         if (r != EOK) {
1679                 EXT4_MP_UNLOCK(mp);
1680                 return r;
1681         }
1682
1683         ext4_inode_set_mode(sb, inode_ref.inode, mode);
1684         inode_ref.dirty = true;
1685
1686         ext4_fs_put_inode_ref(&inode_ref);
1687         EXT4_MP_UNLOCK(mp);
1688         return r;
1689 }
1690
1691 int ext4_chown(const char *path, uint32_t uid, uint32_t gid)
1692 {
1693         int r;
1694         ext4_file f;
1695         uint32_t ino;
1696         struct ext4_inode_ref inode_ref;
1697         struct ext4_mountpoint *mp = ext4_get_mount(path);
1698
1699         if (!mp)
1700                 return ENOENT;
1701
1702         EXT4_MP_LOCK(mp);
1703
1704         r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1705         if (r != EOK) {
1706                 EXT4_MP_UNLOCK(mp);
1707                 return r;
1708         }
1709         ino = f.inode;
1710         ext4_fclose(&f);
1711         r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1712         if (r != EOK) {
1713                 EXT4_MP_UNLOCK(mp);
1714                 return r;
1715         }
1716
1717         ext4_inode_set_uid(inode_ref.inode, uid);
1718         ext4_inode_set_gid(inode_ref.inode, gid);
1719         inode_ref.dirty = true;
1720
1721         ext4_fs_put_inode_ref(&inode_ref);
1722         EXT4_MP_UNLOCK(mp);
1723         return r;
1724 }
1725
1726 int ext4_file_set_atime(const char *path, uint32_t atime)
1727 {
1728         int r;
1729         ext4_file f;
1730         uint32_t ino;
1731         struct ext4_inode_ref inode_ref;
1732         struct ext4_mountpoint *mp = ext4_get_mount(path);
1733
1734         if (!mp)
1735                 return ENOENT;
1736
1737         EXT4_MP_LOCK(mp);
1738
1739         r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1740         if (r != EOK) {
1741                 EXT4_MP_UNLOCK(mp);
1742                 return r;
1743         }
1744         ino = f.inode;
1745         ext4_fclose(&f);
1746         r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1747         if (r != EOK) {
1748                 EXT4_MP_UNLOCK(mp);
1749                 return r;
1750         }
1751
1752         ext4_inode_set_access_time(inode_ref.inode, atime);
1753         inode_ref.dirty = true;
1754
1755         ext4_fs_put_inode_ref(&inode_ref);
1756         EXT4_MP_UNLOCK(mp);
1757         return r;
1758 }
1759
1760 int ext4_file_set_mtime(const char *path, uint32_t mtime)
1761 {
1762         int r;
1763         ext4_file f;
1764         uint32_t ino;
1765         struct ext4_inode_ref inode_ref;
1766         struct ext4_mountpoint *mp = ext4_get_mount(path);
1767
1768         if (!mp)
1769                 return ENOENT;
1770
1771         EXT4_MP_LOCK(mp);
1772
1773         r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1774         if (r != EOK) {
1775                 EXT4_MP_UNLOCK(mp);
1776                 return r;
1777         }
1778         ino = f.inode;
1779         ext4_fclose(&f);
1780         r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1781         if (r != EOK) {
1782                 EXT4_MP_UNLOCK(mp);
1783                 return r;
1784         }
1785
1786         ext4_inode_set_modif_time(inode_ref.inode, mtime);
1787         inode_ref.dirty = true;
1788
1789         ext4_fs_put_inode_ref(&inode_ref);
1790         EXT4_MP_UNLOCK(mp);
1791         return r;
1792 }
1793
1794 int ext4_file_set_ctime(const char *path, uint32_t ctime)
1795 {
1796         int r;
1797         ext4_file f;
1798         uint32_t ino;
1799         struct ext4_inode_ref inode_ref;
1800         struct ext4_mountpoint *mp = ext4_get_mount(path);
1801
1802         if (!mp)
1803                 return ENOENT;
1804
1805         EXT4_MP_LOCK(mp);
1806
1807         r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1808         if (r != EOK) {
1809                 EXT4_MP_UNLOCK(mp);
1810                 return r;
1811         }
1812         ino = f.inode;
1813         ext4_fclose(&f);
1814         r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1815         if (r != EOK) {
1816                 EXT4_MP_UNLOCK(mp);
1817                 return r;
1818         }
1819
1820         ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
1821         inode_ref.dirty = true;
1822
1823         ext4_fs_put_inode_ref(&inode_ref);
1824         EXT4_MP_UNLOCK(mp);
1825         return r;
1826 }
1827
1828 static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
1829 {
1830         struct ext4_inode_ref ref;
1831         uint32_t sblock;
1832         ext4_fsblk_t fblock;
1833         uint32_t block_size;
1834         int r;
1835
1836         ext4_assert(f && f->mp);
1837
1838         if (!size)
1839                 return EOK;
1840
1841         r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1842         if (r != EOK)
1843                 return r;
1844
1845         /*Sync file size*/
1846         block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1847         if (size > block_size) {
1848                 r = EINVAL;
1849                 goto Finish;
1850         }
1851         r = ext4_ftruncate_no_lock(f, 0);
1852         if (r != EOK)
1853                 goto Finish;
1854
1855         /*Start write back cache mode.*/
1856         r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1857         if (r != EOK)
1858                 goto Finish;
1859
1860         /*If the size of symlink is smaller than 60 bytes*/
1861         if (size < sizeof(ref.inode->blocks)) {
1862                 memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
1863                 memcpy(ref.inode->blocks, buf, size);
1864                 ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
1865         } else {
1866                 ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
1867                 r = ext4_fs_append_inode_dblk(&ref, &fblock, &sblock);
1868                 if (r != EOK)
1869                         goto Finish;
1870
1871                 r = ext4_block_writebytes(f->mp->fs.bdev, 0, buf, size);
1872                 if (r != EOK)
1873                         goto Finish;
1874
1875         }
1876
1877         /*Stop write back cache mode*/
1878         ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1879
1880         if (r != EOK)
1881                 goto Finish;
1882
1883         ext4_inode_set_size(ref.inode, size);
1884         ref.dirty = true;
1885
1886         f->fsize = size;
1887         if (f->fpos > size)
1888                 f->fpos = size;
1889
1890 Finish:
1891         ext4_fs_put_inode_ref(&ref);
1892         return r;
1893 }
1894
1895 int ext4_fsymlink(const char *target, const char *path)
1896 {
1897         struct ext4_mountpoint *mp = ext4_get_mount(path);
1898         int r;
1899         ext4_file f;
1900         int filetype;
1901
1902         if (!mp)
1903                 return ENOENT;
1904
1905         filetype = EXT4_DE_SYMLINK;
1906
1907         EXT4_MP_LOCK(mp);
1908         ext4_block_cache_write_back(mp->fs.bdev, 1);
1909         r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, NULL, NULL);
1910         if (r == EOK)
1911                 r = ext4_fsymlink_set(&f, target, strlen(target));
1912         else
1913                 goto Finish;
1914
1915         ext4_fclose(&f);
1916
1917 Finish:
1918         ext4_block_cache_write_back(mp->fs.bdev, 0);
1919         EXT4_MP_UNLOCK(mp);
1920         return r;
1921 }
1922
1923 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
1924 {
1925         struct ext4_mountpoint *mp = ext4_get_mount(path);
1926         int r;
1927         ext4_file f;
1928         int filetype;
1929
1930         if (!mp)
1931                 return ENOENT;
1932
1933         if (!buf)
1934                 return EINVAL;
1935
1936         filetype = EXT4_DE_SYMLINK;
1937
1938         EXT4_MP_LOCK(mp);
1939         ext4_block_cache_write_back(mp->fs.bdev, 1);
1940         r = ext4_generic_open2(&f, path, O_RDONLY, filetype, NULL, NULL);
1941         if (r == EOK)
1942                 r = ext4_fread(&f, buf, bufsize, rcnt);
1943         else
1944                 goto Finish;
1945
1946         ext4_fclose(&f);
1947
1948 Finish:
1949         ext4_block_cache_write_back(mp->fs.bdev, 0);
1950         EXT4_MP_UNLOCK(mp);
1951         return r;
1952 }
1953
1954 int ext4_setxattr(const char *path, const char *name, size_t name_len,
1955                   const void *data, size_t data_size, bool replace)
1956 {
1957         int r = EOK;
1958         ext4_file f;
1959         uint32_t inode;
1960         uint8_t name_index;
1961         const char *dissected_name = NULL;
1962         size_t dissected_len = 0;
1963         struct ext4_xattr_ref xattr_ref;
1964         struct ext4_inode_ref inode_ref;
1965         struct ext4_mountpoint *mp = ext4_get_mount(path);
1966         if (!mp)
1967                 return ENOENT;
1968
1969         dissected_name = ext4_extract_xattr_name(name, name_len,
1970                                 &name_index, &dissected_len);
1971         if (!dissected_len)
1972                 return EINVAL;
1973
1974         EXT4_MP_LOCK(mp);
1975         r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1976         if (r != EOK)
1977                 goto Finish;
1978         inode = f.inode;
1979         ext4_fclose(&f);
1980
1981         r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
1982         if (r != EOK)
1983                 goto Finish;
1984
1985         r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
1986         if (r != EOK) {
1987                 ext4_fs_put_inode_ref(&inode_ref);
1988                 goto Finish;
1989         }
1990
1991         r = ext4_fs_set_xattr(&xattr_ref, name_index, dissected_name,
1992                         dissected_len, data, data_size, replace);
1993
1994         ext4_fs_put_xattr_ref(&xattr_ref);
1995         ext4_fs_put_inode_ref(&inode_ref);
1996 Finish:
1997         EXT4_MP_UNLOCK(mp);
1998         return r;
1999 }
2000
2001 int ext4_getxattr(const char *path, const char *name, size_t name_len,
2002                   void *buf, size_t buf_size, size_t *data_size)
2003 {
2004         int r = EOK;
2005         ext4_file f;
2006         uint32_t inode;
2007         uint8_t name_index;
2008         const char *dissected_name = NULL;
2009         size_t dissected_len = 0;
2010         struct ext4_xattr_ref xattr_ref;
2011         struct ext4_inode_ref inode_ref;
2012         struct ext4_mountpoint *mp = ext4_get_mount(path);
2013         if (!mp)
2014                 return ENOENT;
2015
2016         dissected_name = ext4_extract_xattr_name(name, name_len,
2017                                 &name_index, &dissected_len);
2018         if (!dissected_len)
2019                 return EINVAL;
2020
2021         EXT4_MP_LOCK(mp);
2022         r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2023         if (r != EOK)
2024                 goto Finish;
2025         inode = f.inode;
2026         ext4_fclose(&f);
2027
2028         r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2029         if (r != EOK)
2030                 goto Finish;
2031
2032         r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2033         if (r != EOK) {
2034                 ext4_fs_put_inode_ref(&inode_ref);
2035                 goto Finish;
2036         }
2037
2038         r = ext4_fs_get_xattr(&xattr_ref, name_index, dissected_name,
2039                                 dissected_len, buf, buf_size, data_size);
2040
2041         ext4_fs_put_xattr_ref(&xattr_ref);
2042         ext4_fs_put_inode_ref(&inode_ref);
2043 Finish:
2044         EXT4_MP_UNLOCK(mp);
2045         return r;
2046 }
2047
2048 struct ext4_listxattr_iterator {
2049         char *list;
2050         char *list_ptr;
2051         size_t size;
2052         size_t ret_size;
2053         bool list_too_small;
2054         bool get_required_size;
2055 };
2056
2057 static int ext4_iterate_ea_list(struct ext4_xattr_ref *ref,
2058                                 struct ext4_xattr_item *item)
2059 {
2060         struct ext4_listxattr_iterator *lxi;
2061         lxi = ref->iter_arg;
2062         if (!lxi->get_required_size) {
2063                 size_t plen;
2064                 const char *prefix;
2065                 prefix = ext4_get_xattr_name_prefix(item->name_index, &plen);
2066                 if (lxi->ret_size + plen + item->name_len + 1 > lxi->size) {
2067                         lxi->list_too_small = 1;
2068                         return EXT4_XATTR_ITERATE_STOP;
2069                 }
2070                 if (prefix) {
2071                         memcpy(lxi->list_ptr, prefix, plen);
2072                         lxi->list_ptr += plen;
2073                         lxi->ret_size += plen;
2074                 }
2075                 memcpy(lxi->list_ptr, item->name, item->name_len);
2076                 lxi->list_ptr[item->name_len] = 0;
2077                 lxi->list_ptr += item->name_len + 1;
2078         }
2079         lxi->ret_size += item->name_len + 1;
2080         return EXT4_XATTR_ITERATE_CONT;
2081 }
2082
2083 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
2084 {
2085         int r = EOK;
2086         ext4_file f;
2087         uint32_t inode;
2088         struct ext4_xattr_ref xattr_ref;
2089         struct ext4_inode_ref inode_ref;
2090         struct ext4_listxattr_iterator lxi;
2091         struct ext4_mountpoint *mp = ext4_get_mount(path);
2092         if (!mp)
2093                 return ENOENT;
2094
2095         lxi.list = list;
2096         lxi.list_ptr = list;
2097         lxi.size = size;
2098         lxi.ret_size = 0;
2099         lxi.list_too_small = false;
2100         lxi.get_required_size = (!size) ? true : false;
2101
2102         EXT4_MP_LOCK(mp);
2103         r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2104         if (r != EOK)
2105                 goto Finish;
2106         inode = f.inode;
2107         ext4_fclose(&f);
2108
2109         r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2110         if (r != EOK)
2111                 goto Finish;
2112
2113         r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2114         if (r != EOK) {
2115                 ext4_fs_put_inode_ref(&inode_ref);
2116                 goto Finish;
2117         }
2118
2119         xattr_ref.iter_arg = &lxi;
2120         ext4_fs_xattr_iterate(&xattr_ref, ext4_iterate_ea_list);
2121         if (lxi.list_too_small)
2122                 r = ERANGE;
2123
2124         if (r == EOK) {
2125                 if (ret_size)
2126                         *ret_size = lxi.ret_size;
2127
2128         }
2129         ext4_fs_put_xattr_ref(&xattr_ref);
2130         ext4_fs_put_inode_ref(&inode_ref);
2131 Finish:
2132         EXT4_MP_UNLOCK(mp);
2133         return r;
2134
2135 }
2136
2137 int ext4_removexattr(const char *path, const char *name, size_t name_len)
2138 {
2139         int r = EOK;
2140         ext4_file f;
2141         uint32_t inode;
2142         uint8_t name_index;
2143         const char *dissected_name = NULL;
2144         size_t dissected_len = 0;
2145         struct ext4_xattr_ref xattr_ref;
2146         struct ext4_inode_ref inode_ref;
2147         struct ext4_mountpoint *mp = ext4_get_mount(path);
2148         if (!mp)
2149                 return ENOENT;
2150
2151         dissected_name = ext4_extract_xattr_name(name, name_len,
2152                                                 &name_index, &dissected_len);
2153         if (!dissected_len)
2154                 return EINVAL;
2155
2156         EXT4_MP_LOCK(mp);
2157         r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2158         if (r != EOK)
2159                 goto Finish;
2160         inode = f.inode;
2161         ext4_fclose(&f);
2162
2163         r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2164         if (r != EOK)
2165                 goto Finish;
2166
2167         r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2168         if (r != EOK) {
2169                 ext4_fs_put_inode_ref(&inode_ref);
2170                 goto Finish;
2171         }
2172
2173         r = ext4_fs_remove_xattr(&xattr_ref, name_index, dissected_name,
2174                                 dissected_len);
2175
2176         ext4_fs_put_xattr_ref(&xattr_ref);
2177         ext4_fs_put_inode_ref(&inode_ref);
2178 Finish:
2179         EXT4_MP_UNLOCK(mp);
2180         return r;
2181
2182 }
2183
2184 /*********************************DIRECTORY OPERATION************************/
2185
2186 int ext4_dir_rm(const char *path)
2187 {
2188         int r;
2189         int len;
2190         ext4_file f;
2191
2192         struct ext4_mountpoint *mp = ext4_get_mount(path);
2193         struct ext4_inode_ref act;
2194         struct ext4_inode_ref child;
2195         struct ext4_dir_iter it;
2196
2197         uint32_t name_off;
2198         uint32_t inode_up;
2199         uint32_t inode_current;
2200         uint32_t depth = 1;
2201
2202         bool has_children;
2203         bool is_goal;
2204         bool dir_end;
2205
2206         if (!mp)
2207                 return ENOENT;
2208
2209         EXT4_MP_LOCK(mp);
2210
2211         struct ext4_fs *const fs = &mp->fs;
2212
2213         /*Check if exist.*/
2214         r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
2215         if (r != EOK) {
2216                 EXT4_MP_UNLOCK(mp);
2217                 return r;
2218         }
2219
2220         path += name_off;
2221         len = ext4_path_check(path, &is_goal);
2222
2223         inode_current = f.inode;
2224         dir_end = false;
2225
2226         ext4_block_cache_write_back(mp->fs.bdev, 1);
2227
2228         do {
2229                 /*Load directory node.*/
2230                 r = ext4_fs_get_inode_ref(fs, inode_current, &act);
2231                 if (r != EOK) {
2232                         break;
2233                 }
2234
2235                 /*Initialize iterator.*/
2236                 r = ext4_dir_iterator_init(&it, &act, 0);
2237                 if (r != EOK) {
2238                         ext4_fs_put_inode_ref(&act);
2239                         break;
2240                 }
2241
2242                 while (r == EOK) {
2243
2244                         if (!it.curr) {
2245                                 dir_end = true;
2246                                 break;
2247                         }
2248
2249                         /*Get up directory inode when ".." entry*/
2250                         if ((it.curr->name_len == 2) &&
2251                             ext4_is_dots(it.curr->name, it.curr->name_len)) {
2252                                 inode_up = ext4_dir_en_get_inode(it.curr);
2253                         }
2254
2255                         /*If directory or file entry,  but not "." ".." entry*/
2256                         if (!ext4_is_dots(it.curr->name, it.curr->name_len)) {
2257
2258                                 /*Get child inode reference do unlink
2259                                  * directory/file.*/
2260                                 uint32_t cinode;
2261                                 cinode = ext4_dir_en_get_inode(it.curr);
2262                                 r = ext4_fs_get_inode_ref(fs, cinode, &child);
2263                                 if (r != EOK)
2264                                         break;
2265
2266                                 /*If directory with no leaf children*/
2267                                 r = ext4_has_children(&has_children, &child);
2268                                 if (r != EOK) {
2269                                         ext4_fs_put_inode_ref(&child);
2270                                         break;
2271                                 }
2272
2273                                 if (has_children) {
2274                                         /*Has directory children. Go into this
2275                                          * directory.*/
2276                                         inode_up = inode_current;
2277                                         inode_current = cinode;
2278                                         depth++;
2279                                         ext4_fs_put_inode_ref(&child);
2280                                         break;
2281                                 }
2282
2283                                 /*No children in child directory or file. Just
2284                                  * unlink.*/
2285                                 r = ext4_unlink(f.mp, &act, &child,
2286                                                 (char *)it.curr->name,
2287                                                 it.curr->name_len);
2288                                 if (r != EOK) {
2289                                         ext4_fs_put_inode_ref(&child);
2290                                         break;
2291                                 }
2292
2293                                 ext4_inode_set_del_time(child.inode, -1L);
2294                                 ext4_inode_set_links_cnt(child.inode, 0);
2295                                 child.dirty = true;
2296                                 /*Turncate*/
2297                                 r = ext4_fs_truncate_inode(&child, 0);
2298                                 if (r != EOK) {
2299                                         ext4_fs_put_inode_ref(&child);
2300                                         break;
2301                                 }
2302
2303                                 r = ext4_fs_free_inode(&child);
2304                                 if (r != EOK) {
2305                                         ext4_fs_put_inode_ref(&child);
2306                                         break;
2307                                 }
2308
2309                                 r = ext4_fs_put_inode_ref(&child);
2310                                 if (r != EOK)
2311                                         break;
2312                         }
2313
2314                         r = ext4_dir_iterator_next(&it);
2315                 }
2316
2317                 if (dir_end) {
2318                         /*Directory iterator reached last entry*/
2319                         ext4_has_children(&has_children, &act);
2320                         if (!has_children) {
2321                                 inode_current = inode_up;
2322                                 if (depth)
2323                                         depth--;
2324                         }
2325                         /*Last unlink*/
2326                         if (!depth) {
2327                                 /*Load parent.*/
2328                                 struct ext4_inode_ref parent;
2329                                 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
2330                                                           &parent);
2331                                 if (r != EOK)
2332                                         goto End;
2333
2334                                 /* In this place all directories should be
2335                                  * unlinked.
2336                                  * Last unlink from root of current directory*/
2337                                 r = ext4_unlink(f.mp, &parent, &act,
2338                                                 (char *)path, len);
2339                                 if (r != EOK) {
2340                                         ext4_fs_put_inode_ref(&parent);
2341                                         goto End;
2342                                 }
2343
2344                                 if (ext4_inode_get_links_cnt(act.inode) == 2) {
2345                                         ext4_inode_set_del_time(act.inode, -1L);
2346                                         ext4_inode_set_links_cnt(act.inode, 0);
2347                                         act.dirty = true;
2348                                         /*Turncate*/
2349                                         r = ext4_fs_truncate_inode(&act, 0);
2350                                         if (r != EOK) {
2351                                                 ext4_fs_put_inode_ref(&parent);
2352                                                 goto End;
2353                                         }
2354
2355                                         r = ext4_fs_free_inode(&act);
2356                                         if (r != EOK) {
2357                                                 ext4_fs_put_inode_ref(&parent);
2358                                                 goto End;
2359                                         }
2360                                 }
2361
2362                                 r = ext4_fs_put_inode_ref(&parent);
2363                                 if (r != EOK)
2364                                         goto End;
2365                         }
2366                 }
2367
2368         End:
2369                 ext4_dir_iterator_fini(&it);
2370                 ext4_fs_put_inode_ref(&act);
2371                 dir_end = false;
2372
2373                 /*When something goes wrong. End loop.*/
2374                 if (r != EOK)
2375                         break;
2376
2377         } while (depth);
2378
2379         ext4_block_cache_write_back(mp->fs.bdev, 0);
2380         EXT4_MP_UNLOCK(mp);
2381         return r;
2382 }
2383
2384 int ext4_dir_mk(const char *path)
2385 {
2386         int r;
2387         ext4_file f;
2388
2389         struct ext4_mountpoint *mp = ext4_get_mount(path);
2390
2391         if (!mp)
2392                 return ENOENT;
2393
2394         EXT4_MP_LOCK(mp);
2395
2396         /*Check if exist.*/
2397         r = ext4_generic_open(&f, path, "r", false, 0, 0);
2398         if (r == EOK) {
2399                 /*Directory already created*/
2400                 EXT4_MP_UNLOCK(mp);
2401                 return r;
2402         }
2403
2404         /*Create new dir*/
2405         r = ext4_generic_open(&f, path, "w", false, 0, 0);
2406         if (r != EOK) {
2407                 EXT4_MP_UNLOCK(mp);
2408                 return r;
2409         }
2410
2411         EXT4_MP_UNLOCK(mp);
2412         return r;
2413 }
2414
2415 int ext4_dir_open(ext4_dir *d, const char *path)
2416 {
2417         struct ext4_mountpoint *mp = ext4_get_mount(path);
2418         int r;
2419
2420         if (!mp)
2421                 return ENOENT;
2422
2423         EXT4_MP_LOCK(mp);
2424         r = ext4_generic_open(&d->f, path, "r", false, 0, 0);
2425         d->next_off = 0;
2426         EXT4_MP_UNLOCK(mp);
2427         return r;
2428 }
2429
2430 int ext4_dir_close(ext4_dir *d)
2431 {
2432     return ext4_fclose(&d->f);
2433 }
2434
2435 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d)
2436 {
2437 #define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
2438
2439         int r;
2440         ext4_direntry *de = 0;
2441         struct ext4_inode_ref dir;
2442         struct ext4_dir_iter it;
2443
2444         EXT4_MP_LOCK(d->f.mp);
2445
2446         if (d->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) {
2447                 EXT4_MP_UNLOCK(d->f.mp);
2448                 return 0;
2449         }
2450
2451         r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
2452         if (r != EOK) {
2453                 goto Finish;
2454         }
2455
2456         r = ext4_dir_iterator_init(&it, &dir, d->next_off);
2457         if (r != EOK) {
2458                 ext4_fs_put_inode_ref(&dir);
2459                 goto Finish;
2460         }
2461
2462         memcpy(&d->de, it.curr, sizeof(ext4_direntry));
2463         de = &d->de;
2464
2465         ext4_dir_iterator_next(&it);
2466
2467         d->next_off = it.curr ? it.curr_off : EXT4_DIR_ENTRY_OFFSET_TERM;
2468
2469         ext4_dir_iterator_fini(&it);
2470         ext4_fs_put_inode_ref(&dir);
2471
2472 Finish:
2473         EXT4_MP_UNLOCK(d->f.mp);
2474         return de;
2475 }
2476
2477 void ext4_dir_entry_rewind(ext4_dir *d)
2478 {
2479         d->next_off = 0;
2480 }
2481
2482 int ext4_test_journal(const char *mount_point)
2483 {
2484         struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
2485         if (!mp)
2486                 return ENOENT;
2487
2488         int r = ENOTSUP;
2489         EXT4_MP_LOCK(mp);
2490         ext4_block_cache_write_back(mp->fs.bdev, 1);
2491         if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_HAS_JOURNAL)) {
2492                 struct jbd_fs *jbd_fs = calloc(1, sizeof(struct jbd_fs));
2493                 struct jbd_journal *journal;
2494                 if (!jbd_fs) {
2495                          r = ENOMEM;
2496                          goto Finish;
2497                 }
2498                 journal = calloc(1, sizeof(struct jbd_journal));
2499                 if (!journal) {
2500                         free(jbd_fs);
2501                         r = ENOMEM;
2502                         goto Finish;
2503                 }
2504
2505                 r = jbd_get_fs(&mp->fs, jbd_fs);
2506                 if (r != EOK) {
2507                         free(jbd_fs);
2508                         goto Finish;
2509                 }
2510                 r = jbd_journal_start(jbd_fs, journal);
2511                 if (r != EOK) {
2512                         jbd_put_fs(jbd_fs);
2513                         free(journal);
2514                         free(jbd_fs);
2515                         goto Finish;
2516                 }
2517
2518                 int i;
2519                 for (i = 0;i < 50;i++) {
2520                         ext4_fsblk_t rand_block = rand() % 4096;
2521                         if (!rand_block)
2522                                 rand_block = 1;
2523                         struct ext4_block block;
2524                         r = ext4_block_get(mp->fs.bdev, &block, rand_block);
2525                         if (r != EOK)
2526                                 goto out;
2527
2528                         struct jbd_trans *t = jbd_journal_new_trans(journal);
2529                         if (!t) {
2530                                 ext4_block_set(mp->fs.bdev, &block);
2531                                 r = ENOMEM;
2532                                 goto out;
2533                         }
2534
2535                         switch (rand() % 2) {
2536                         case 0:
2537                                 r = jbd_trans_get_access(journal, t, &block);
2538                                 if (r != EOK) {
2539                                         jbd_journal_free_trans(journal, t,
2540                                                                true);
2541                                         ext4_block_set(mp->fs.bdev, &block);
2542                                         r = ENOMEM;
2543                                         goto out;
2544                                 }
2545                                 r = jbd_trans_set_block_dirty(t, &block);
2546                                 if (r != EOK) {
2547                                         jbd_journal_free_trans(journal, t,
2548                                                                true);
2549                                         ext4_block_set(mp->fs.bdev, &block);
2550                                         r = ENOMEM;
2551                                         goto out;
2552                                 }
2553                                 break;
2554                         case 1:
2555                                 r = jbd_trans_try_revoke_block(t, rand_block);
2556                                 if (r != EOK) {
2557                                         jbd_journal_free_trans(journal, t,
2558                                                                true);
2559                                         ext4_block_set(mp->fs.bdev, &block);
2560                                         r = ENOMEM;
2561                                         goto out;
2562                                 }
2563                         }
2564                         ext4_block_set(mp->fs.bdev, &block);
2565                         jbd_journal_submit_trans(journal, t);
2566                         jbd_journal_commit_one(journal);
2567                 }
2568 out:
2569                 jbd_journal_stop(journal);
2570                 jbd_put_fs(jbd_fs);
2571                 free(journal);
2572                 free(jbd_fs);
2573         }
2574
2575 Finish:
2576         ext4_block_cache_write_back(mp->fs.bdev, 0);
2577         EXT4_MP_UNLOCK(mp);
2578         return r;
2579 }
2580
2581 /**
2582  * @}
2583  */