77e6e5191b7bc7cd0a41f88274ec7f228eafa624
[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_blockdev.h"
39 #include "ext4_types.h"
40 #include "ext4_debug.h"
41 #include "ext4_errno.h"
42 #include "ext4_fs.h"
43 #include "ext4_dir.h"
44 #include "ext4_inode.h"
45 #include "ext4_super.h"
46 #include "ext4_dir_idx.h"
47 #include "ext4.h"
48
49 #include <stdlib.h>
50 #include <string.h>
51
52 /**@brief   Mount point OS dependent lock*/
53 #define EXT4_MP_LOCK(_m)                                                       \
54         do {                                                                   \
55                 if ((_m)->os_locks)                                            \
56                         (_m)->os_locks->lock();                                \
57         } while (0)
58
59 /**@brief   Mount point OS dependent unlock*/
60 #define EXT4_MP_UNLOCK(_m)                                                     \
61         do {                                                                   \
62                 if ((_m)->os_locks)                                            \
63                         (_m)->os_locks->unlock();                              \
64         } while (0)
65
66 /**@brief   Mount point descriptor.*/
67 struct ext4_mountpoint {
68
69         /**@brief   Mount done flag.*/
70         bool mounted;
71
72         /**@brief   Mount point name (@ref ext4_mount)*/
73         char name[32];
74
75         /**@brief   OS dependent lock/unlock functions.*/
76         const struct ext4_lock *os_locks;
77
78         /**@brief   Ext4 filesystem internals.*/
79         struct ext4_fs fs;
80
81         /**@brief   Dynamic allocation cache flag.*/
82         bool cache_dynamic;
83 };
84
85 /**@brief   Block devices descriptor.*/
86 struct _ext4_devices {
87
88         /**@brief   Block device name (@ref ext4_device_register)*/
89         char name[32];
90
91         /**@brief   Block device handle.*/
92         struct ext4_blockdev *bd;
93
94         /**@brief   Block cache handle.*/
95         struct ext4_bcache *bc;
96 };
97
98 /**@brief   Block devices.*/
99 struct _ext4_devices _bdevices[CONFIG_EXT4_BLOCKDEVS_COUNT];
100
101 /**@brief   Mountpoints.*/
102 struct ext4_mountpoint _mp[CONFIG_EXT4_MOUNTPOINTS_COUNT];
103
104 int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
105                          const char *dev_name)
106 {
107         uint32_t i;
108         ext4_assert(bd && dev_name);
109
110         for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
111                 if (!_bdevices[i].bd) {
112                         strcpy(_bdevices[i].name, dev_name);
113                         _bdevices[i].bd = bd;
114                         _bdevices[i].bc = bc;
115                         return EOK;
116                 }
117
118                 if (!strcmp(_bdevices[i].name, dev_name))
119                         return EOK;
120         }
121         return ENOSPC;
122 }
123
124 /****************************************************************************/
125
126 static bool ext4_is_dots(const uint8_t *name, size_t name_size)
127 {
128         if ((name_size == 1) && (name[0] == '.'))
129                 return true;
130
131         if ((name_size == 2) && (name[0] == '.') && (name[1] == '.'))
132                 return true;
133
134         return false;
135 }
136
137 static int ext4_has_children(bool *has_children, struct ext4_inode_ref *enode)
138 {
139         struct ext4_fs *fs = enode->fs;
140
141         /* Check if node is directory */
142         if (!ext4_inode_is_type(&fs->sb, enode->inode,
143                                 EXT4_INODE_MODE_DIRECTORY)) {
144                 *has_children = false;
145                 return EOK;
146         }
147
148         struct ext4_directory_iterator it;
149         int rc = ext4_dir_iterator_init(&it, enode, 0);
150         if (rc != EOK)
151                 return rc;
152
153         /* Find a non-empty directory entry */
154         bool found = false;
155         while (it.current != NULL) {
156                 if (ext4_dir_entry_ll_get_inode(it.current) != 0) {
157                         uint16_t name_size = ext4_dir_entry_ll_get_name_length(
158                             &fs->sb, it.current);
159                         if (!ext4_is_dots(it.current->name, name_size)) {
160                                 found = true;
161                                 break;
162                         }
163                 }
164
165                 rc = ext4_dir_iterator_next(&it);
166                 if (rc != EOK) {
167                         ext4_dir_iterator_fini(&it);
168                         return rc;
169                 }
170         }
171
172         rc = ext4_dir_iterator_fini(&it);
173         if (rc != EOK)
174                 return rc;
175
176         *has_children = found;
177
178         return EOK;
179 }
180
181 static int ext4_link(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent,
182                      struct ext4_inode_ref *child, const char *name,
183                      uint32_t name_len)
184 {
185         /* Check maximum name length */
186         if (name_len > EXT4_DIRECTORY_FILENAME_LEN)
187                 return EINVAL;
188
189         /* Add entry to parent directory */
190         int rc = ext4_dir_add_entry(parent, name, name_len, child);
191         if (rc != EOK)
192                 return rc;
193
194         /* Fill new dir -> add '.' and '..' entries.
195          * Also newly allocated inode should have 0 link count.
196          */
197         if (ext4_inode_is_type(&mp->fs.sb, child->inode,
198                                EXT4_INODE_MODE_DIRECTORY) &&
199             ext4_inode_get_links_count(child->inode) == 0) {
200                 rc = ext4_dir_add_entry(child, ".", strlen("."), child);
201                 if (rc != EOK) {
202                         ext4_dir_remove_entry(parent, name, strlen(name));
203                         return rc;
204                 }
205
206                 rc = ext4_dir_add_entry(child, "..", strlen(".."), parent);
207                 if (rc != EOK) {
208                         ext4_dir_remove_entry(parent, name, strlen(name));
209                         ext4_dir_remove_entry(child, ".", strlen("."));
210                         return rc;
211                 }
212
213                 /*New empty directory. Two links (. and ..) */
214                 ext4_inode_set_links_count(child->inode, 2);
215
216 #if CONFIG_DIR_INDEX_ENABLE
217                 /* Initialize directory index if supported */
218                 if (ext4_sb_has_feature_compatible(
219                         &mp->fs.sb, EXT4_FEATURE_COMPAT_DIR_INDEX)) {
220                         rc = ext4_dir_dx_init(child);
221                         if (rc != EOK)
222                                 return rc;
223
224                         ext4_inode_set_flag(child->inode,
225                                             EXT4_INODE_FLAG_INDEX);
226                         child->dirty = true;
227                 }
228 #endif
229
230                 ext4_fs_inode_links_count_inc(parent);
231                 child->dirty = true;
232                 parent->dirty = true;
233         } else {
234                 if (ext4_inode_is_type(&mp->fs.sb, child->inode,
235                                         EXT4_INODE_MODE_DIRECTORY)) {
236                         int has_flag_index =
237                                 ext4_inode_has_flag(child->inode,
238                                                     EXT4_INODE_FLAG_INDEX);
239                         struct ext4_directory_search_result result;
240                         if (!has_flag_index) {
241                                 rc = ext4_dir_find_entry(&result,
242                                                          child, "..",
243                                                          strlen(".."));
244                                 if (rc != EOK)
245                                         return EIO;
246
247                                 ext4_dir_entry_ll_set_inode(result.dentry,
248                                                             parent->index);
249                                 result.block.dirty = true;
250                                 rc = ext4_dir_destroy_result(child, &result);
251                                 if (rc != EOK)
252                                         return rc;
253
254                         } else {
255 #if CONFIG_DIR_INDEX_ENABLE
256                                 rc = ext4_dir_dx_reset_parent_inode(parent,
257                                                 parent->index);
258                                 if (rc != EOK)
259                                         return rc;
260
261 #endif
262                         }
263
264                         ext4_fs_inode_links_count_inc(parent);
265                         parent->dirty = true;
266                 } else {
267                         ext4_fs_inode_links_count_inc(child);
268                         child->dirty = true;
269                 }
270         }
271
272         return rc;
273 }
274
275 static int ext4_unlink(struct ext4_mountpoint *mp,
276                        struct ext4_inode_ref *parent,
277                        struct ext4_inode_ref *child_inode_ref, const char *name,
278                        uint32_t name_len)
279 {
280         bool has_children;
281         int rc = ext4_has_children(&has_children, child_inode_ref);
282         if (rc != EOK)
283                 return rc;
284
285         /* Cannot unlink non-empty node */
286         if (has_children)
287                 return ENOTSUP;
288
289         /* Remove entry from parent directory */
290         rc = ext4_dir_remove_entry(parent, name, name_len);
291         if (rc != EOK)
292                 return rc;
293
294         bool is_dir = ext4_inode_is_type(&mp->fs.sb, child_inode_ref->inode,
295                                          EXT4_INODE_MODE_DIRECTORY);
296
297         /* If directory - handle links from parent */
298         if (is_dir) {
299                 // ext4_assert(ext4_inode_get_links_count(child_inode_ref->inode)
300                 // == 1);
301                 ext4_fs_inode_links_count_dec(parent);
302                 parent->dirty = true;
303         }
304
305         /*
306          * TODO: Update timestamps of the parent
307          * (when we have wall-clock time).
308          *
309          * ext4_inode_set_change_inode_time(parent->inode, (uint32_t) now);
310          * ext4_inode_set_modification_time(parent->inode, (uint32_t) now);
311          * parent->dirty = true;
312          */
313
314         /*
315          * TODO: Update timestamp for inode.
316          *
317          * ext4_inode_set_change_inode_time(child_inode_ref->inode,
318          *     (uint32_t) now);
319          */
320         if (ext4_inode_get_links_count(child_inode_ref->inode)) {
321                 ext4_fs_inode_links_count_dec(child_inode_ref);
322                 child_inode_ref->dirty = true;
323         }
324
325         return EOK;
326 }
327
328 /****************************************************************************/
329
330 int ext4_mount(const char *dev_name, const char *mount_point)
331 {
332         ext4_assert(mount_point && dev_name);
333         int r;
334         int i;
335
336         uint32_t bsize;
337         struct ext4_blockdev *bd = 0;
338         struct ext4_bcache *bc = 0;
339         struct ext4_mountpoint *mp = 0;
340
341         if (mount_point[strlen(mount_point) - 1] != '/')
342                 return ENOTSUP;
343
344         for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
345                 if (_bdevices[i].name) {
346                         if (!strcmp(dev_name, _bdevices[i].name)) {
347                                 bd = _bdevices[i].bd;
348                                 bc = _bdevices[i].bc;
349                                 break;
350                         }
351                 }
352         }
353
354         if (!bd)
355                 return ENODEV;
356
357         for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
358                 if (!_mp[i].mounted) {
359                         strcpy(_mp[i].name, mount_point);
360                         _mp[i].mounted = 1;
361                         mp = &_mp[i];
362                         break;
363                 }
364
365                 if (!strcmp(_mp[i].name, mount_point))
366                         return EOK;
367         }
368
369         if (!mp)
370                 return ENOMEM;
371
372         r = ext4_block_init(bd);
373         if (r != EOK)
374                 return r;
375
376         r = ext4_fs_init(&mp->fs, bd);
377         if (r != EOK) {
378                 ext4_block_fini(bd);
379                 return r;
380         }
381
382         bsize = ext4_sb_get_block_size(&mp->fs.sb);
383         ext4_block_set_lb_size(bd, bsize);
384
385         mp->cache_dynamic = 0;
386
387         if (!bc) {
388                 /*Automatic block cache alloc.*/
389                 mp->cache_dynamic = 1;
390                 bc = malloc(sizeof(struct ext4_bcache));
391
392                 r = ext4_bcache_init_dynamic(bc, CONFIG_BLOCK_DEV_CACHE_SIZE,
393                                              bsize);
394                 if (r != EOK) {
395                         free(bc);
396                         ext4_block_fini(bd);
397                         return r;
398                 }
399         }
400
401         if (bsize != bc->itemsize)
402                 return ENOTSUP;
403
404         /*Bind block cache to block device*/
405         r = ext4_block_bind_bcache(bd, bc);
406         if (r != EOK) {
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 int ext4_umount(const char *mount_point)
419 {
420         int i;
421         int r;
422         struct ext4_mountpoint *mp = 0;
423
424         for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
425                 if (!strcmp(_mp[i].name, mount_point)) {
426                         mp = &_mp[i];
427                         break;
428                 }
429         }
430
431         if (!mp)
432                 return ENODEV;
433
434         r = ext4_fs_fini(&mp->fs);
435         if (r != EOK)
436                 return r;
437
438         mp->mounted = 0;
439
440         if (mp->cache_dynamic) {
441                 ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
442                 free(mp->fs.bdev->bc);
443         }
444
445         return ext4_block_fini(mp->fs.bdev);
446 }
447
448 int ext4_mount_point_stats(const char *mount_point,
449                            struct ext4_mount_stats *stats)
450 {
451         uint32_t i;
452         struct ext4_mountpoint *mp = 0;
453
454         for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
455                 if (!strcmp(_mp[i].name, mount_point)) {
456                         mp = &_mp[i];
457                         break;
458                 }
459         }
460         if (!mp)
461                 return ENOENT;
462
463         EXT4_MP_LOCK(mp);
464         stats->inodes_count = ext4_get32(&mp->fs.sb, inodes_count);
465         stats->free_inodes_count = ext4_get32(&mp->fs.sb, free_inodes_count);
466         stats->blocks_count = ext4_sb_get_blocks_cnt(&mp->fs.sb);
467         stats->free_blocks_count = ext4_sb_get_free_blocks_cnt(&mp->fs.sb);
468         stats->block_size = ext4_sb_get_block_size(&mp->fs.sb);
469
470         stats->block_group_count = ext4_block_group_cnt(&mp->fs.sb);
471         stats->blocks_per_group = ext4_get32(&mp->fs.sb, blocks_per_group);
472         stats->inodes_per_group = ext4_get32(&mp->fs.sb, inodes_per_group);
473
474         memcpy(stats->volume_name, mp->fs.sb.volume_name, 16);
475         EXT4_MP_UNLOCK(mp);
476
477         return EOK;
478 }
479
480 int ext4_mount_setup_locks(const char *mount_point,
481                            const struct ext4_lock *locks)
482 {
483         uint32_t i;
484         struct ext4_mountpoint *mp = 0;
485
486         for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
487                 if (!strcmp(_mp[i].name, mount_point)) {
488                         mp = &_mp[i];
489                         break;
490                 }
491         }
492         if (!mp)
493                 return ENOENT;
494
495         mp->os_locks = locks;
496         return EOK;
497 }
498
499 /********************************FILE OPERATIONS*****************************/
500
501 static struct ext4_mountpoint *ext4_get_mount(const char *path)
502 {
503         int i;
504         for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
505
506                 if (!_mp[i].mounted)
507                         continue;
508
509                 if (!strncmp(_mp[i].name, path, strlen(_mp[i].name)))
510                         return &_mp[i];
511         }
512         return 0;
513 }
514
515 static int ext4_path_check(const char *path, bool *is_goal)
516 {
517         int i;
518
519         for (i = 0; i < EXT4_DIRECTORY_FILENAME_LEN; ++i) {
520
521                 if (path[i] == '/') {
522                         *is_goal = false;
523                         return i;
524                 }
525
526                 if (path[i] == 0) {
527                         *is_goal = true;
528                         return i;
529                 }
530         }
531
532         return 0;
533 }
534
535 static bool ext4_parse_flags(const char *flags, uint32_t *file_flags)
536 {
537         if (!flags)
538                 return false;
539
540         if (!strcmp(flags, "r") || !strcmp(flags, "rb")) {
541                 *file_flags = O_RDONLY;
542                 return true;
543         }
544
545         if (!strcmp(flags, "w") || !strcmp(flags, "wb")) {
546                 *file_flags = O_WRONLY | O_CREAT | O_TRUNC;
547                 return true;
548         }
549
550         if (!strcmp(flags, "a") || !strcmp(flags, "ab")) {
551                 *file_flags = O_WRONLY | O_CREAT | O_APPEND;
552                 return true;
553         }
554
555         if (!strcmp(flags, "r+") || !strcmp(flags, "rb+") ||
556             !strcmp(flags, "r+b")) {
557                 *file_flags = O_RDWR;
558                 return true;
559         }
560
561         if (!strcmp(flags, "w+") || !strcmp(flags, "wb+") ||
562             !strcmp(flags, "w+b")) {
563                 *file_flags = O_RDWR | O_CREAT | O_TRUNC;
564                 return true;
565         }
566
567         if (!strcmp(flags, "a+") || !strcmp(flags, "ab+") ||
568             !strcmp(flags, "a+b")) {
569                 *file_flags = O_RDWR | O_CREAT | O_APPEND;
570                 return true;
571         }
572
573         return false;
574 }
575
576 /*
577  * NOTICE: if filetype is equal to EXT4_DIRECTORY_FILETYPE_UNKNOWN,
578  * any filetype of the target dir entry will be accepted.
579  */
580 static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
581                               int filetype, uint32_t *parent_inode,
582                               uint32_t *name_off)
583 {
584         bool is_goal = false;
585         uint8_t inode_type = EXT4_DIRECTORY_FILETYPE_DIR;
586         uint32_t next_inode;
587
588         int r;
589         struct ext4_mountpoint *mp = ext4_get_mount(path);
590         struct ext4_directory_search_result result;
591         struct ext4_inode_ref ref;
592
593         f->mp = 0;
594
595         if (!mp)
596                 return ENOENT;
597
598         f->flags = flags;
599
600         /*Skip mount point*/
601         path += strlen(mp->name);
602
603         if (name_off)
604                 *name_off = strlen(mp->name);
605
606         /*Load root*/
607         r = ext4_fs_get_inode_ref(&mp->fs, EXT4_INODE_ROOT_INDEX, &ref);
608
609         if (r != EOK)
610                 return r;
611
612         if (parent_inode)
613                 *parent_inode = ref.index;
614
615         int len = ext4_path_check(path, &is_goal);
616
617         while (1) {
618
619                 len = ext4_path_check(path, &is_goal);
620
621                 if (!len) {
622                         /*If root open was request.*/
623                         if (is_goal &&
624                             ((filetype == EXT4_DIRECTORY_FILETYPE_DIR) ||
625                              (filetype == EXT4_DIRECTORY_FILETYPE_UNKNOWN)))
626                                 break;
627
628                         r = ENOENT;
629                         break;
630                 }
631
632                 r = ext4_dir_find_entry(&result, &ref, path, len);
633                 if (r != EOK) {
634
635                         if (r != ENOENT)
636                                 break;
637
638                         if (!(f->flags & O_CREAT))
639                                 break;
640
641                         /*O_CREAT allows create new entry*/
642                         struct ext4_inode_ref child_ref;
643                         r = ext4_fs_alloc_inode(
644                             &mp->fs, &child_ref,
645                             is_goal ? (filetype == EXT4_DIRECTORY_FILETYPE_DIR)
646                                     : true);
647                         if (r != EOK)
648                                 break;
649
650                         /*Destroy last result*/
651                         ext4_dir_destroy_result(&ref, &result);
652
653                         /*Link with root dir.*/
654                         r = ext4_link(mp, &ref, &child_ref, path, len);
655                         if (r != EOK) {
656                                 /*Fail. Free new inode.*/
657                                 ext4_fs_free_inode(&child_ref);
658                                 /*We do not want to write new inode.
659                                   But block has to be released.*/
660                                 child_ref.dirty = false;
661                                 ext4_fs_put_inode_ref(&child_ref);
662                                 break;
663                         }
664
665                         ext4_fs_put_inode_ref(&child_ref);
666
667                         continue;
668                 }
669
670                 if (parent_inode)
671                         *parent_inode = ref.index;
672
673                 next_inode = ext4_dir_entry_ll_get_inode(result.dentry);
674                 inode_type =
675                     ext4_dir_entry_ll_get_inode_type(&mp->fs.sb, result.dentry);
676
677                 r = ext4_dir_destroy_result(&ref, &result);
678                 if (r != EOK)
679                         break;
680
681                 /*If expected file error*/
682                 if (inode_type != EXT4_DIRECTORY_FILETYPE_DIR && !is_goal) {
683                         r = ENOENT;
684                         break;
685                 }
686                 if (filetype != EXT4_DIRECTORY_FILETYPE_UNKNOWN) {
687                         if ((inode_type != filetype) && is_goal) {
688                                 r = ENOENT;
689                                 break;
690                         }
691                 }
692
693                 r = ext4_fs_put_inode_ref(&ref);
694                 if (r != EOK)
695                         break;
696
697                 r = ext4_fs_get_inode_ref(&mp->fs, next_inode, &ref);
698                 if (r != EOK)
699                         break;
700
701                 if (is_goal)
702                         break;
703
704                 path += len + 1;
705
706                 if (name_off)
707                         *name_off += len + 1;
708         };
709
710         if (r != EOK) {
711                 ext4_fs_put_inode_ref(&ref);
712                 return r;
713         }
714
715         if (is_goal) {
716
717                 if ((f->flags & O_TRUNC) &&
718                     (inode_type == EXT4_DIRECTORY_FILETYPE_REG_FILE)) {
719
720                         r = ext4_fs_truncate_inode(&ref, 0);
721                         if (r != EOK) {
722                                 ext4_fs_put_inode_ref(&ref);
723                                 return r;
724                         }
725                 }
726
727                 f->mp = mp;
728                 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
729                 f->inode = ref.index;
730                 f->fpos = 0;
731
732                 if (f->flags & O_APPEND)
733                         f->fpos = f->fsize;
734         }
735
736         r = ext4_fs_put_inode_ref(&ref);
737         return r;
738 }
739
740 /****************************************************************************/
741
742 static int ext4_generic_open(ext4_file *f, const char *path, const char *flags,
743                              bool file_expect, uint32_t *parent_inode,
744                              uint32_t *name_off)
745 {
746         uint32_t iflags;
747         int filetype;
748         if (ext4_parse_flags(flags, &iflags) == false)
749                 return EINVAL;
750
751         if (file_expect == true)
752                 filetype = EXT4_DIRECTORY_FILETYPE_REG_FILE;
753         else
754                 filetype = EXT4_DIRECTORY_FILETYPE_DIR;
755
756         return ext4_generic_open2(f, path, iflags, filetype, parent_inode,
757                                   name_off);
758 }
759
760 static int __ext4_create_hardlink(const char *path,
761                 struct ext4_inode_ref *child_ref)
762 {
763         bool is_goal = false;
764         uint8_t inode_type = EXT4_DIRECTORY_FILETYPE_DIR;
765         uint32_t next_inode;
766
767         int r;
768         struct ext4_mountpoint *mp = ext4_get_mount(path);
769         struct ext4_directory_search_result result;
770         struct ext4_inode_ref ref;
771
772         if (!mp)
773                 return ENOENT;
774
775         /*Skip mount point*/
776         path += strlen(mp->name);
777
778         /*Load root*/
779         r = ext4_fs_get_inode_ref(&mp->fs, EXT4_INODE_ROOT_INDEX, &ref);
780
781         if (r != EOK)
782                 return r;
783
784         int len = ext4_path_check(path, &is_goal);
785
786         while (1) {
787
788                 len = ext4_path_check(path, &is_goal);
789
790                 if (!len) {
791                         /*If root open was request.*/
792                         if (is_goal)
793                                 r = EINVAL;
794                         else
795                                 r = ENOENT;
796                         break;
797                 }
798
799                 r = ext4_dir_find_entry(&result, &ref, path, len);
800                 if (r != EOK) {
801
802                         if (r != ENOENT || !is_goal)
803                                 break;
804
805                         /*Destroy last result*/
806                         ext4_dir_destroy_result(&ref, &result);
807
808                         /*Link with root dir.*/
809                         r = ext4_link(mp, &ref, child_ref, path, len);
810                         break;
811                 }
812
813                 next_inode = result.dentry->inode;
814                 inode_type =
815                         ext4_dir_entry_ll_get_inode_type(&mp->fs.sb, result.dentry);
816
817                 r = ext4_dir_destroy_result(&ref, &result);
818                 if (r != EOK)
819                         break;
820
821                 if (inode_type == EXT4_DIRECTORY_FILETYPE_REG_FILE) {
822                         if (is_goal)
823                                 r = EEXIST;
824                         else
825                                 r = ENOENT;
826
827                         break;
828                 }
829
830                 r = ext4_fs_put_inode_ref(&ref);
831                 if (r != EOK)
832                         break;
833
834                 r = ext4_fs_get_inode_ref(&mp->fs, next_inode, &ref);
835                 if (r != EOK)
836                         break;
837
838                 if (is_goal)
839                         break;
840
841                 path += len + 1;
842         };
843
844         if (r != EOK) {
845                 ext4_fs_put_inode_ref(&ref);
846                 return r;
847         }
848
849         r = ext4_fs_put_inode_ref(&ref);
850         return r;
851 }
852
853 static int __ext4_remove_hardlink(const char *path,
854                                   uint32_t name_off,
855                                   struct ext4_inode_ref *parent_ref,
856                                   struct ext4_inode_ref *child_ref)
857 {
858         bool is_goal;
859         int r;
860         int len;
861         struct ext4_mountpoint *mp = ext4_get_mount(path);
862
863         if (!mp)
864                 return ENOENT;
865
866         /*Set path*/
867         path += name_off;
868
869         len = ext4_path_check(path, &is_goal);
870
871         /*Unlink from parent*/
872         r = ext4_unlink(mp, parent_ref, child_ref, path, len);
873         if (r != EOK)
874                 goto Finish;
875
876 Finish:
877         if (r != EOK)
878                 ext4_fs_put_inode_ref(child_ref);
879
880         ext4_fs_put_inode_ref(parent_ref);
881         return r;
882 }
883
884 int ext4_frename(const char *path, const char *new_path)
885 {
886         int r;
887         ext4_file f;
888         uint32_t name_off;
889         bool parent_loaded = false, child_loaded = false;
890         uint32_t parent_inode, child_inode;
891         struct ext4_mountpoint *mp = ext4_get_mount(path);
892         struct ext4_inode_ref child_ref, parent_ref;
893
894         if (!mp)
895                 return ENOENT;
896
897         EXT4_MP_LOCK(mp);
898
899         r = ext4_generic_open2(&f, path, O_RDONLY,
900                         EXT4_DIRECTORY_FILETYPE_UNKNOWN,
901                         &parent_inode, &name_off);
902         if (r != EOK)
903                 goto Finish;
904
905         child_inode = f.inode;
906         ext4_fclose(&f);
907
908         /*Load parent*/
909         r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent_ref);
910         if (r != EOK)
911                 goto Finish;
912
913         parent_loaded = true;
914
915         /*We have file to unlink. Load it.*/
916         r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
917         if (r != EOK)
918                 goto Finish;
919
920         child_loaded = true;
921
922         r = __ext4_create_hardlink(new_path, &child_ref);
923         if (r != EOK)
924                 goto Finish;
925
926         r = __ext4_remove_hardlink(path, name_off, &parent_ref, &child_ref);
927         if (r != EOK)
928                 goto Finish;
929
930 Finish:
931         if (parent_loaded)
932                 ext4_fs_put_inode_ref(&parent_ref);
933
934         if (child_loaded)
935                 ext4_fs_put_inode_ref(&child_ref);
936
937         EXT4_MP_UNLOCK(mp);
938         return r;
939
940 }
941
942 /****************************************************************************/
943
944 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb)
945 {
946         struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
947
948         if (!mp)
949                 return ENOENT;
950
951         *sb = &mp->fs.sb;
952         return EOK;
953 }
954
955 int ext4_cache_write_back(const char *path, bool on)
956 {
957         struct ext4_mountpoint *mp = ext4_get_mount(path);
958
959         if (!mp)
960                 return ENOENT;
961
962         EXT4_MP_LOCK(mp);
963         ext4_block_cache_write_back(mp->fs.bdev, on);
964         EXT4_MP_UNLOCK(mp);
965         return EOK;
966 }
967
968 int ext4_fremove(const char *path)
969 {
970         ext4_file f;
971         uint32_t parent_inode;
972         uint32_t name_off;
973         bool is_goal;
974         int r;
975         int len;
976         struct ext4_inode_ref child;
977         struct ext4_inode_ref parent;
978         struct ext4_mountpoint *mp = ext4_get_mount(path);
979
980         if (!mp)
981                 return ENOENT;
982
983         EXT4_MP_LOCK(mp);
984         r = ext4_generic_open(&f, path, "r", true, &parent_inode, &name_off);
985         if (r != EOK) {
986                 EXT4_MP_UNLOCK(mp);
987                 return r;
988         }
989
990         /*Load parent*/
991         r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
992         if (r != EOK) {
993                 EXT4_MP_UNLOCK(mp);
994                 return r;
995         }
996
997         /*We have file to delete. Load it.*/
998         r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
999         if (r != EOK) {
1000                 ext4_fs_put_inode_ref(&parent);
1001                 EXT4_MP_UNLOCK(mp);
1002                 return r;
1003         }
1004
1005         /*Set path*/
1006         path += name_off;
1007
1008         len = ext4_path_check(path, &is_goal);
1009
1010         /*Unlink from parent*/
1011         r = ext4_unlink(mp, &parent, &child, path, len);
1012         if (r != EOK)
1013                 goto Finish;
1014
1015         /*Link count is zero, the inode should be freed. */
1016         if (!ext4_inode_get_links_count(child.inode)) {
1017                 printf("ttttt\n");
1018                 ext4_inode_set_deletion_time(child.inode, 0xFFFFFFFF);
1019                 /*Turncate*/
1020                 ext4_block_cache_write_back(mp->fs.bdev, 1);
1021                 /*Truncate may be IO heavy. Do it writeback cache mode.*/
1022                 r = ext4_fs_truncate_inode(&child, 0);
1023                 ext4_block_cache_write_back(mp->fs.bdev, 0);
1024
1025                 if (r != EOK)
1026                         goto Finish;
1027
1028                 r = ext4_fs_free_inode(&child);
1029                 if (r != EOK)
1030                         goto Finish;
1031         }
1032
1033 Finish:
1034         ext4_fs_put_inode_ref(&child);
1035         ext4_fs_put_inode_ref(&parent);
1036         EXT4_MP_UNLOCK(mp);
1037         return r;
1038 }
1039
1040 int ext4_fill_raw_inode(const char *mount_point, uint32_t ino,
1041                         struct ext4_inode *inode)
1042 {
1043         int r;
1044         struct ext4_inode_ref inode_ref;
1045         struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
1046
1047         if (!mp)
1048                 return ENOENT;
1049
1050         EXT4_MP_LOCK(mp);
1051
1052         /*Load parent*/
1053         r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1054         if (r != EOK) {
1055                 EXT4_MP_UNLOCK(mp);
1056                 return r;
1057         }
1058
1059         memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
1060
1061         ext4_fs_put_inode_ref(&inode_ref);
1062         EXT4_MP_UNLOCK(mp);
1063         return r;
1064 }
1065
1066 int ext4_fopen(ext4_file *f, const char *path, const char *flags)
1067 {
1068         struct ext4_mountpoint *mp = ext4_get_mount(path);
1069         int r;
1070
1071         if (!mp)
1072                 return ENOENT;
1073
1074         EXT4_MP_LOCK(mp);
1075         ext4_block_cache_write_back(mp->fs.bdev, 1);
1076         r = ext4_generic_open(f, path, flags, true, 0, 0);
1077         ext4_block_cache_write_back(mp->fs.bdev, 0);
1078         EXT4_MP_UNLOCK(mp);
1079         return r;
1080 }
1081
1082 int ext4_fopen2(ext4_file *f, const char *path, int flags, bool file_expect)
1083 {
1084         struct ext4_mountpoint *mp = ext4_get_mount(path);
1085         int r;
1086         int filetype;
1087
1088         if (!mp)
1089                 return ENOENT;
1090
1091         if (file_expect == true)
1092                 filetype = EXT4_DIRECTORY_FILETYPE_REG_FILE;
1093         else
1094                 filetype = EXT4_DIRECTORY_FILETYPE_DIR;
1095
1096         EXT4_MP_LOCK(mp);
1097         ext4_block_cache_write_back(mp->fs.bdev, 1);
1098         r = ext4_generic_open2(f, path, flags, filetype, 0, 0);
1099         ext4_block_cache_write_back(mp->fs.bdev, 0);
1100         EXT4_MP_UNLOCK(mp);
1101         return r;
1102 }
1103
1104 int ext4_fclose(ext4_file *f)
1105 {
1106         ext4_assert(f && f->mp);
1107
1108         f->mp = 0;
1109         f->flags = 0;
1110         f->inode = 0;
1111         f->fpos = f->fsize = 0;
1112
1113         return EOK;
1114 }
1115
1116 int ext4_ftruncate(ext4_file *f, uint64_t size)
1117 {
1118         struct ext4_inode_ref ref;
1119         int r;
1120
1121         ext4_assert(f && f->mp);
1122
1123         if (f->flags & O_RDONLY)
1124                 return EPERM;
1125
1126         EXT4_MP_LOCK(f->mp);
1127
1128         r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1129         if (r != EOK) {
1130                 EXT4_MP_UNLOCK(f->mp);
1131                 return r;
1132         }
1133
1134         /*Sync file size*/
1135         f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1136         if (f->fsize <= size) {
1137                 r = EOK;
1138                 goto Finish;
1139         }
1140
1141         /*Start write back cache mode.*/
1142         r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1143         if (r != EOK)
1144                 goto Finish;
1145
1146         r = ext4_fs_truncate_inode(&ref, size);
1147         if (r != EOK)
1148                 goto Finish;
1149
1150         f->fsize = size;
1151         if (f->fpos > size)
1152                 f->fpos = size;
1153
1154         /*Stop write back cache mode*/
1155         ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1156
1157         if (r != EOK)
1158                 goto Finish;
1159
1160 Finish:
1161         ext4_fs_put_inode_ref(&ref);
1162         EXT4_MP_UNLOCK(f->mp);
1163         return r;
1164 }
1165
1166 int ext4_fread(ext4_file *f, void *buf, uint32_t size, uint32_t *rcnt)
1167 {
1168         uint32_t u;
1169         uint32_t fblock;
1170         uint32_t fblock_start;
1171         uint32_t fblock_cnt;
1172         uint32_t sblock;
1173         uint32_t sblock_end;
1174         uint32_t block_size;
1175         uint8_t *u8_buf = buf;
1176         int r;
1177         struct ext4_block b;
1178         struct ext4_inode_ref ref;
1179
1180         ext4_assert(f && f->mp);
1181
1182         if (f->flags & O_WRONLY)
1183                 return EPERM;
1184
1185         if (!size)
1186                 return EOK;
1187
1188         EXT4_MP_LOCK(f->mp);
1189
1190         if (rcnt)
1191                 *rcnt = 0;
1192
1193         r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1194         if (r != EOK) {
1195                 EXT4_MP_UNLOCK(f->mp);
1196                 return r;
1197         }
1198
1199         /*Sync file size*/
1200         f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1201
1202         block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1203         size = size > (f->fsize - f->fpos) ? (f->fsize - f->fpos) : size;
1204         sblock = (f->fpos) / block_size;
1205         sblock_end = (f->fpos + size) / block_size;
1206         u = (f->fpos) % block_size;
1207
1208         if (u) {
1209
1210                 uint32_t ll = size > (block_size - u) ? (block_size - u) : size;
1211
1212                 r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
1213                 if (r != EOK)
1214                         goto Finish;
1215
1216                 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1217                 if (r != EOK)
1218                         goto Finish;
1219
1220                 memcpy(u8_buf, b.data + u, ll);
1221
1222                 r = ext4_block_set(f->mp->fs.bdev, &b);
1223                 if (r != EOK)
1224                         goto Finish;
1225
1226                 u8_buf += ll;
1227                 size -= ll;
1228                 f->fpos += ll;
1229
1230                 if (rcnt)
1231                         *rcnt += ll;
1232
1233                 sblock++;
1234         }
1235
1236         fblock_start = 0;
1237         fblock_cnt = 0;
1238         while (size >= block_size) {
1239                 while (sblock < sblock_end) {
1240                         r = ext4_fs_get_inode_data_block_index(&ref, sblock,
1241                                                                &fblock);
1242                         if (r != EOK)
1243                                 goto Finish;
1244
1245                         sblock++;
1246
1247                         if (!fblock_start) {
1248                                 fblock_start = fblock;
1249                         }
1250
1251                         if ((fblock_start + fblock_cnt) != fblock)
1252                                 break;
1253
1254                         fblock_cnt++;
1255                 }
1256
1257                 r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1258                                            fblock_cnt);
1259                 if (r != EOK)
1260                         goto Finish;
1261
1262                 size -= block_size * fblock_cnt;
1263                 u8_buf += block_size * fblock_cnt;
1264                 f->fpos += block_size * fblock_cnt;
1265
1266                 if (rcnt)
1267                         *rcnt += block_size * fblock_cnt;
1268
1269                 fblock_start = fblock;
1270                 fblock_cnt = 1;
1271         }
1272
1273         if (size) {
1274                 r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
1275                 if (r != EOK)
1276                         goto Finish;
1277
1278                 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1279                 if (r != EOK)
1280                         goto Finish;
1281
1282                 memcpy(u8_buf, b.data, size);
1283
1284                 r = ext4_block_set(f->mp->fs.bdev, &b);
1285                 if (r != EOK)
1286                         goto Finish;
1287
1288                 f->fpos += size;
1289
1290                 if (rcnt)
1291                         *rcnt += size;
1292         }
1293
1294 Finish:
1295         ext4_fs_put_inode_ref(&ref);
1296         EXT4_MP_UNLOCK(f->mp);
1297         return r;
1298 }
1299
1300 int ext4_fwrite(ext4_file *f, const void *buf, uint32_t size, uint32_t *wcnt)
1301 {
1302         uint32_t u;
1303         uint32_t fblock;
1304
1305         uint32_t sblock;
1306         uint32_t sblock_end;
1307         uint32_t file_blocks;
1308         uint32_t block_size;
1309         uint32_t fblock_start;
1310         uint32_t fblock_cnt;
1311
1312         struct ext4_block b;
1313         struct ext4_inode_ref ref;
1314         const uint8_t *u8_buf = buf;
1315         int r;
1316
1317         ext4_assert(f && f->mp);
1318
1319         if (f->flags & O_RDONLY)
1320                 return EPERM;
1321
1322         if (!size)
1323                 return EOK;
1324
1325         EXT4_MP_LOCK(f->mp);
1326
1327         if (wcnt)
1328                 *wcnt = 0;
1329
1330         r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1331         if (r != EOK) {
1332                 EXT4_MP_UNLOCK(f->mp);
1333                 return r;
1334         }
1335
1336         /*Sync file size*/
1337         f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1338
1339         block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1340
1341         sblock_end = (f->fpos + size) > f->fsize ? (f->fpos + size) : f->fsize;
1342         sblock_end /= block_size;
1343         file_blocks = (f->fsize / block_size);
1344
1345         if (f->fsize % block_size)
1346                 file_blocks++;
1347
1348         sblock = (f->fpos) / block_size;
1349
1350         u = (f->fpos) % block_size;
1351
1352         if (u) {
1353                 uint32_t ll = size > (block_size - u) ? (block_size - u) : size;
1354
1355                 r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
1356                 if (r != EOK)
1357                         goto Finish;
1358
1359                 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1360                 if (r != EOK)
1361                         goto Finish;
1362
1363                 memcpy(b.data + u, u8_buf, ll);
1364                 b.dirty = true;
1365
1366                 r = ext4_block_set(f->mp->fs.bdev, &b);
1367                 if (r != EOK)
1368                         goto Finish;
1369
1370                 u8_buf += ll;
1371                 size -= ll;
1372                 f->fpos += ll;
1373
1374                 if (wcnt)
1375                         *wcnt += ll;
1376
1377                 sblock++;
1378         }
1379
1380         /*Start write back cache mode.*/
1381         r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1382         if (r != EOK)
1383                 goto Finish;
1384
1385         fblock_start = 0;
1386         fblock_cnt = 0;
1387         while (size >= block_size) {
1388
1389                 while (sblock < sblock_end) {
1390                         if (sblock < file_blocks) {
1391                                 r = ext4_fs_get_inode_data_block_index(
1392                                     &ref, sblock, &fblock);
1393                                 if (r != EOK)
1394                                         break;
1395                         } else {
1396                                 r = ext4_fs_append_inode_block(&ref, &fblock,
1397                                                                &sblock);
1398                                 if (r != EOK)
1399                                         break;
1400                         }
1401
1402                         sblock++;
1403
1404                         if (!fblock_start) {
1405                                 fblock_start = fblock;
1406                         }
1407
1408                         if ((fblock_start + fblock_cnt) != fblock)
1409                                 break;
1410
1411                         fblock_cnt++;
1412                 }
1413
1414                 r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1415                                            fblock_cnt);
1416                 if (r != EOK)
1417                         break;
1418
1419                 size -= block_size * fblock_cnt;
1420                 u8_buf += block_size * fblock_cnt;
1421                 f->fpos += block_size * fblock_cnt;
1422
1423                 if (wcnt)
1424                         *wcnt += block_size * fblock_cnt;
1425
1426                 fblock_start = fblock;
1427                 fblock_cnt = 1;
1428         }
1429
1430         /*Stop write back cache mode*/
1431         ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1432
1433         if (r != EOK)
1434                 goto Finish;
1435
1436         if (size) {
1437                 if (sblock < file_blocks) {
1438                         r = ext4_fs_get_inode_data_block_index(&ref, sblock,
1439                                                                &fblock);
1440                         if (r != EOK)
1441                                 goto Finish;
1442                 } else {
1443                         r = ext4_fs_append_inode_block(&ref, &fblock, &sblock);
1444                         if (r != EOK)
1445                                 goto Finish;
1446                 }
1447
1448                 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1449                 if (r != EOK)
1450                         goto Finish;
1451
1452                 memcpy(b.data, u8_buf, size);
1453                 b.dirty = true;
1454
1455                 r = ext4_block_set(f->mp->fs.bdev, &b);
1456                 if (r != EOK)
1457                         goto Finish;
1458
1459                 f->fpos += size;
1460
1461                 if (wcnt)
1462                         *wcnt += size;
1463         }
1464
1465         if (f->fpos > f->fsize) {
1466                 f->fsize = f->fpos;
1467                 ext4_inode_set_size(ref.inode, f->fsize);
1468                 ref.dirty = true;
1469         }
1470
1471 Finish:
1472         ext4_fs_put_inode_ref(&ref);
1473         EXT4_MP_UNLOCK(f->mp);
1474         return r;
1475 }
1476
1477 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin)
1478 {
1479         switch (origin) {
1480         case SEEK_SET:
1481                 if (offset > f->fsize)
1482                         return EINVAL;
1483
1484                 f->fpos = offset;
1485                 return EOK;
1486         case SEEK_CUR:
1487                 if ((offset + f->fpos) > f->fsize)
1488                         return EINVAL;
1489
1490                 f->fpos += offset;
1491                 return EOK;
1492         case SEEK_END:
1493                 if (offset > f->fsize)
1494                         return EINVAL;
1495
1496                 f->fpos = f->fsize - offset;
1497                 return EOK;
1498         }
1499         return EINVAL;
1500 }
1501
1502 uint64_t ext4_ftell(ext4_file *f) { return f->fpos; }
1503
1504 uint64_t ext4_fsize(ext4_file *f) { return f->fsize; }
1505
1506 int ext4_fchmod(ext4_file *f, uint32_t mode)
1507 {
1508         int r;
1509         uint32_t ino;
1510         struct ext4_sblock *sb;
1511         struct ext4_inode_ref inode_ref;
1512         struct ext4_mountpoint *mp = f->mp;
1513
1514         if (!mp)
1515                 return ENOENT;
1516
1517         EXT4_MP_LOCK(mp);
1518
1519         ino = f->inode;
1520         r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1521         if (r != EOK) {
1522                 EXT4_MP_UNLOCK(mp);
1523                 return r;
1524         }
1525
1526         sb = &f->mp->fs.sb;
1527         ext4_inode_set_mode(sb, inode_ref.inode, mode);
1528         inode_ref.dirty = true;
1529
1530         ext4_fs_put_inode_ref(&inode_ref);
1531         EXT4_MP_UNLOCK(mp);
1532         return r;
1533 }
1534
1535 int ext4_fchown(ext4_file *f, uint32_t uid, uint32_t gid)
1536 {
1537         int r;
1538         uint32_t ino;
1539         struct ext4_inode_ref inode_ref;
1540         struct ext4_mountpoint *mp = f->mp;
1541
1542         if (!mp)
1543                 return ENOENT;
1544
1545         EXT4_MP_LOCK(mp);
1546
1547         ino = f->inode;
1548         r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1549         if (r != EOK) {
1550                 EXT4_MP_UNLOCK(mp);
1551                 return r;
1552         }
1553
1554         ext4_inode_set_uid(inode_ref.inode, uid);
1555         ext4_inode_set_gid(inode_ref.inode, gid);
1556         inode_ref.dirty = true;
1557
1558         ext4_fs_put_inode_ref(&inode_ref);
1559         EXT4_MP_UNLOCK(mp);
1560         return r;
1561 }
1562
1563 int ext4_file_set_atime(ext4_file *f, uint32_t atime)
1564 {
1565         int r;
1566         uint32_t ino;
1567         struct ext4_inode_ref inode_ref;
1568         struct ext4_mountpoint *mp = f->mp;
1569
1570         if (!mp)
1571                 return ENOENT;
1572
1573         EXT4_MP_LOCK(mp);
1574
1575         ino = f->inode;
1576         r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1577         if (r != EOK) {
1578                 EXT4_MP_UNLOCK(mp);
1579                 return r;
1580         }
1581
1582         ext4_inode_set_access_time(inode_ref.inode, atime);
1583         inode_ref.dirty = true;
1584
1585         ext4_fs_put_inode_ref(&inode_ref);
1586         EXT4_MP_UNLOCK(mp);
1587         return r;
1588 }
1589
1590 int ext4_file_set_mtime(ext4_file *f, uint32_t mtime)
1591 {
1592         int r;
1593         uint32_t ino;
1594         struct ext4_inode_ref inode_ref;
1595         struct ext4_mountpoint *mp = f->mp;
1596
1597         if (!mp)
1598                 return ENOENT;
1599
1600         EXT4_MP_LOCK(mp);
1601
1602         ino = f->inode;
1603         r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1604         if (r != EOK) {
1605                 EXT4_MP_UNLOCK(mp);
1606                 return r;
1607         }
1608
1609         ext4_inode_set_modification_time(inode_ref.inode, mtime);
1610         inode_ref.dirty = true;
1611
1612         ext4_fs_put_inode_ref(&inode_ref);
1613         EXT4_MP_UNLOCK(mp);
1614         return r;
1615 }
1616
1617 int ext4_file_set_ctime(ext4_file *f, uint32_t ctime)
1618 {
1619         int r;
1620         uint32_t ino;
1621         struct ext4_inode_ref inode_ref;
1622         struct ext4_mountpoint *mp = f->mp;
1623
1624         if (!mp)
1625                 return ENOENT;
1626
1627         EXT4_MP_LOCK(mp);
1628
1629         ino = f->inode;
1630         r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1631         if (r != EOK) {
1632                 EXT4_MP_UNLOCK(mp);
1633                 return r;
1634         }
1635
1636         ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
1637         inode_ref.dirty = true;
1638
1639         ext4_fs_put_inode_ref(&inode_ref);
1640         EXT4_MP_UNLOCK(mp);
1641         return r;
1642 }
1643
1644 /*********************************DIRECTORY OPERATION************************/
1645
1646 int ext4_dir_rm(const char *path)
1647 {
1648         int r;
1649         int len;
1650         ext4_file f;
1651
1652         struct ext4_mountpoint *mp = ext4_get_mount(path);
1653         struct ext4_inode_ref current;
1654         struct ext4_inode_ref child;
1655         struct ext4_directory_iterator it;
1656
1657         uint32_t name_off;
1658         uint32_t inode_up;
1659         uint32_t inode_current;
1660         uint32_t depth = 1;
1661
1662         bool has_children;
1663         bool is_goal;
1664         bool dir_end;
1665
1666         if (!mp)
1667                 return ENOENT;
1668
1669         EXT4_MP_LOCK(mp);
1670
1671         /*Check if exist.*/
1672         r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
1673         if (r != EOK) {
1674                 EXT4_MP_UNLOCK(mp);
1675                 return r;
1676         }
1677
1678         path += name_off;
1679         len = ext4_path_check(path, &is_goal);
1680
1681         inode_current = f.inode;
1682         dir_end = false;
1683
1684         ext4_block_cache_write_back(mp->fs.bdev, 1);
1685
1686         do {
1687                 /*Load directory node.*/
1688                 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_current, &current);
1689                 if (r != EOK) {
1690                         break;
1691                 }
1692
1693                 /*Initialize iterator.*/
1694                 r = ext4_dir_iterator_init(&it, &current, 0);
1695                 if (r != EOK) {
1696                         ext4_fs_put_inode_ref(&current);
1697                         break;
1698                 }
1699
1700                 while (r == EOK) {
1701
1702                         if (!it.current) {
1703                                 dir_end = true;
1704                                 break;
1705                         }
1706
1707                         /*Get up directory inode when ".." entry*/
1708                         if ((it.current->name_length == 2) &&
1709                             ext4_is_dots(it.current->name,
1710                                          it.current->name_length)) {
1711                                 inode_up = ext4_dir_entry_ll_get_inode(it.current);
1712                         }
1713
1714                         /*If directory or file entry,  but not "." ".." entry*/
1715                         if (!ext4_is_dots(it.current->name,
1716                                           it.current->name_length)) {
1717
1718                                 /*Get child inode reference do unlink
1719                                  * directory/file.*/
1720                                 r = ext4_fs_get_inode_ref(&f.mp->fs,
1721                                         ext4_dir_entry_ll_get_inode(it.current),
1722                                         &child);
1723                                 if (r != EOK)
1724                                         break;
1725
1726                                 /*If directory with no leaf children*/
1727                                 r = ext4_has_children(&has_children, &child);
1728                                 if (r != EOK) {
1729                                         ext4_fs_put_inode_ref(&child);
1730                                         break;
1731                                 }
1732
1733                                 if (has_children) {
1734                                         /*Has directory children. Go into this
1735                                          * directory.*/
1736                                         inode_up = inode_current;
1737                                         inode_current = ext4_dir_entry_ll_get_inode(it.current);
1738                                         depth++;
1739                                         ext4_fs_put_inode_ref(&child);
1740                                         break;
1741                                 }
1742
1743                                 /*No children in child directory or file. Just
1744                                  * unlink.*/
1745                                 r = ext4_unlink(f.mp, &current, &child,
1746                                                 (char *)it.current->name,
1747                                                 it.current->name_length);
1748                                 if (r != EOK) {
1749                                         ext4_fs_put_inode_ref(&child);
1750                                         break;
1751                                 }
1752
1753                                 ext4_inode_set_deletion_time(child.inode,
1754                                                              0xFFFFFFFF);
1755                                 ext4_inode_set_links_count(child.inode, 0);
1756                                 child.dirty = true;
1757                                 /*Turncate*/
1758                                 r = ext4_fs_truncate_inode(&child, 0);
1759                                 if (r != EOK) {
1760                                         ext4_fs_put_inode_ref(&child);
1761                                         break;
1762                                 }
1763
1764                                 r = ext4_fs_free_inode(&child);
1765                                 if (r != EOK) {
1766                                         ext4_fs_put_inode_ref(&child);
1767                                         break;
1768                                 }
1769
1770                                 r = ext4_fs_put_inode_ref(&child);
1771                                 if (r != EOK)
1772                                         break;
1773                         }
1774
1775                         r = ext4_dir_iterator_next(&it);
1776                 }
1777
1778                 if (dir_end) {
1779                         /*Directory iterator reached last entry*/
1780                         ext4_has_children(&has_children, &current);
1781                         if (!has_children) {
1782                                 inode_current = inode_up;
1783                                 if (depth)
1784                                         depth--;
1785                         }
1786                         /*Last unlink*/
1787                         if (!depth) {
1788                                 /*Load parent.*/
1789                                 struct ext4_inode_ref parent;
1790                                 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
1791                                                           &parent);
1792                                 if (r != EOK)
1793                                         goto End;
1794
1795                                 /* In this place all directories should be
1796                                  * unlinked.
1797                                  * Last unlink from root of current directory*/
1798                                 r = ext4_unlink(f.mp, &parent, &current,
1799                                                 (char *)path, len);
1800                                 if (r != EOK) {
1801                                         ext4_fs_put_inode_ref(&parent);
1802                                         goto End;
1803                                 }
1804
1805                                 if (ext4_inode_get_links_count(current.inode) ==
1806                                     2) {
1807                                         ext4_inode_set_deletion_time(
1808                                             current.inode, 0xFFFFFFFF);
1809                                         ext4_inode_set_links_count(
1810                                             current.inode, 0);
1811                                         current.dirty = true;
1812                                         /*Turncate*/
1813                                         r = ext4_fs_truncate_inode(&current, 0);
1814                                         if (r != EOK) {
1815                                                 ext4_fs_put_inode_ref(&parent);
1816                                                 goto End;
1817                                         }
1818
1819                                         r = ext4_fs_free_inode(&current);
1820                                         if (r != EOK) {
1821                                                 ext4_fs_put_inode_ref(&parent);
1822                                                 goto End;
1823                                         }
1824                                 }
1825
1826                                 r = ext4_fs_put_inode_ref(&parent);
1827                                 if (r != EOK)
1828                                         goto End;
1829                         }
1830                 }
1831
1832         End:
1833                 ext4_dir_iterator_fini(&it);
1834                 ext4_fs_put_inode_ref(&current);
1835                 dir_end = false;
1836
1837                 /*When something goes wrong. End loop.*/
1838                 if (r != EOK)
1839                         break;
1840
1841         } while (depth);
1842
1843         ext4_block_cache_write_back(mp->fs.bdev, 0);
1844         EXT4_MP_UNLOCK(mp);
1845         return r;
1846 }
1847
1848 int ext4_dir_mk(const char *path)
1849 {
1850         int r;
1851         ext4_file f;
1852
1853         struct ext4_mountpoint *mp = ext4_get_mount(path);
1854
1855         if (!mp)
1856                 return ENOENT;
1857
1858         EXT4_MP_LOCK(mp);
1859
1860         /*Check if exist.*/
1861         r = ext4_generic_open(&f, path, "r", false, 0, 0);
1862         if (r == EOK) {
1863                 /*Directory already created*/
1864                 EXT4_MP_UNLOCK(mp);
1865                 return r;
1866         }
1867
1868         /*Create new dir*/
1869         r = ext4_generic_open(&f, path, "w", false, 0, 0);
1870         if (r != EOK) {
1871                 EXT4_MP_UNLOCK(mp);
1872                 return r;
1873         }
1874
1875         EXT4_MP_UNLOCK(mp);
1876         return r;
1877 }
1878
1879 int ext4_dir_open(ext4_dir *d, const char *path)
1880 {
1881         struct ext4_mountpoint *mp = ext4_get_mount(path);
1882         int r;
1883
1884         if (!mp)
1885                 return ENOENT;
1886
1887         EXT4_MP_LOCK(mp);
1888         r = ext4_generic_open(&d->f, path, "r", false, 0, 0);
1889         d->next_off = 0;
1890         EXT4_MP_UNLOCK(mp);
1891         return r;
1892 }
1893
1894 int ext4_dir_close(ext4_dir *d) { return ext4_fclose(&d->f); }
1895
1896 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d)
1897 {
1898 #define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
1899
1900         int r;
1901         ext4_direntry *de = 0;
1902         struct ext4_inode_ref dir;
1903         struct ext4_directory_iterator it;
1904
1905         EXT4_MP_LOCK(d->f.mp);
1906
1907         if (d->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) {
1908                 EXT4_MP_UNLOCK(d->f.mp);
1909                 return 0;
1910         }
1911
1912         r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
1913         if (r != EOK) {
1914                 goto Finish;
1915         }
1916
1917         r = ext4_dir_iterator_init(&it, &dir, d->next_off);
1918         if (r != EOK) {
1919                 ext4_fs_put_inode_ref(&dir);
1920                 goto Finish;
1921         }
1922
1923         memcpy(&d->de, it.current, sizeof(ext4_direntry));
1924         de = &d->de;
1925
1926         ext4_dir_iterator_next(&it);
1927
1928         d->next_off =
1929             it.current ? it.current_offset : EXT4_DIR_ENTRY_OFFSET_TERM;
1930
1931         ext4_dir_iterator_fini(&it);
1932         ext4_fs_put_inode_ref(&dir);
1933
1934 Finish:
1935         EXT4_MP_UNLOCK(d->f.mp);
1936         return de;
1937 }
1938
1939 /**
1940  * @}
1941  */