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