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