forked from gkostka/lwext4
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathext4.c
3247 lines (2567 loc) · 65.3 KB
/
ext4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @addtogroup lwext4
* @{
*/
/**
* @file ext4.h
* @brief Ext4 high level operations (file, directory, mountpoints...)
*/
#include <ext4_config.h>
#include <ext4_types.h>
#include <ext4_misc.h>
#include <ext4_errno.h>
#include <ext4_oflags.h>
#include <ext4_debug.h>
#include <ext4.h>
#include <ext4_trans.h>
#include <ext4_blockdev.h>
#include <ext4_fs.h>
#include <ext4_dir.h>
#include <ext4_inode.h>
#include <ext4_super.h>
#include <ext4_block_group.h>
#include <ext4_dir_idx.h>
#include <ext4_xattr.h>
#include <ext4_journal.h>
#include <stdlib.h>
#include <string.h>
#include <droidboot_error.h>
#include <droidboot_logging.h>
/**@brief Mount point OS dependent lock*/
#define EXT4_MP_LOCK(_m) \
do { \
if ((_m)->os_locks) \
(_m)->os_locks->lock(); \
} while (0)
/**@brief Mount point OS dependent unlock*/
#define EXT4_MP_UNLOCK(_m) \
do { \
if ((_m)->os_locks) \
(_m)->os_locks->unlock(); \
} while (0)
/**@brief Mount point descriptor.*/
struct ext4_mountpoint {
/**@brief Mount done flag.*/
bool mounted;
/**@brief Mount point name (@ref ext4_mount)*/
char name[CONFIG_EXT4_MAX_MP_NAME + 1];
/**@brief OS dependent lock/unlock functions.*/
const struct ext4_lock *os_locks;
/**@brief Ext4 filesystem internals.*/
struct ext4_fs fs;
/**@brief JBD fs.*/
struct jbd_fs jbd_fs;
/**@brief Journal.*/
struct jbd_journal jbd_journal;
/**@brief Block cache.*/
struct ext4_bcache bc;
};
/**@brief Block devices descriptor.*/
struct ext4_block_devices {
/**@brief Block device name.*/
char name[CONFIG_EXT4_MAX_BLOCKDEV_NAME + 1];
/**@brief Block device handle.*/
struct ext4_blockdev *bd;
};
/**@brief Block devices.*/
static struct ext4_block_devices s_bdevices[CONFIG_EXT4_BLOCKDEVS_COUNT];
/**@brief Mountpoints.*/
static struct ext4_mountpoint s_mp[CONFIG_EXT4_MOUNTPOINTS_COUNT];
int ext4_device_register(struct ext4_blockdev *bd,
const char *dev_name)
{
ext4_assert(bd && dev_name);
if (strlen(dev_name) > CONFIG_EXT4_MAX_BLOCKDEV_NAME)
return EINVAL;
for (size_t i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
if (!strcmp(s_bdevices[i].name, dev_name))
return EEXIST;
}
for (size_t i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
if (!s_bdevices[i].bd) {
strcpy(s_bdevices[i].name, dev_name);
s_bdevices[i].bd = bd;
return EOK;
}
}
return ENOSPC;
}
int ext4_device_unregister(const char *dev_name)
{
ext4_assert(dev_name);
for (size_t i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
if (strcmp(s_bdevices[i].name, dev_name))
continue;
memset(&s_bdevices[i], 0, sizeof(s_bdevices[i]));
}
return ENOENT;
}
int ext4_device_unregister_all(void)
{
memset(s_bdevices, 0, sizeof(s_bdevices));
return EOK;
}
/****************************************************************************/
static bool ext4_is_dots(const uint8_t *name, size_t name_size)
{
if ((name_size == 1) && (name[0] == '.'))
return true;
if ((name_size == 2) && (name[0] == '.') && (name[1] == '.'))
return true;
return false;
}
static int ext4_has_children(bool *has_children, struct ext4_inode_ref *enode)
{
struct ext4_sblock *sb = &enode->fs->sb;
/* Check if node is directory */
if (!ext4_inode_is_type(sb, enode->inode, EXT4_INODE_MODE_DIRECTORY)) {
*has_children = false;
return EOK;
}
struct ext4_dir_iter it;
int rc = ext4_dir_iterator_init(&it, enode, 0);
if (rc != EOK)
return rc;
/* Find a non-empty directory entry */
bool found = false;
while (it.curr != NULL) {
if (ext4_dir_en_get_inode(it.curr) != 0) {
uint16_t nsize;
nsize = ext4_dir_en_get_name_len(sb, it.curr);
if (!ext4_is_dots(it.curr->name, nsize)) {
found = true;
break;
}
}
rc = ext4_dir_iterator_next(&it);
if (rc != EOK) {
ext4_dir_iterator_fini(&it);
return rc;
}
}
rc = ext4_dir_iterator_fini(&it);
if (rc != EOK)
return rc;
*has_children = found;
return EOK;
}
static int ext4_link(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent,
struct ext4_inode_ref *ch, const char *n,
uint32_t len, bool rename)
{
/* Check maximum name length */
if (len > EXT4_DIRECTORY_FILENAME_LEN)
return EINVAL;
/* Add entry to parent directory */
int r = ext4_dir_add_entry(parent, n, len, ch);
if (r != EOK)
return r;
/* Fill new dir -> add '.' and '..' entries.
* Also newly allocated inode should have 0 link count.
*/
bool is_dir = ext4_inode_is_type(&mp->fs.sb, ch->inode,
EXT4_INODE_MODE_DIRECTORY);
if (is_dir && !rename) {
#if CONFIG_DIR_INDEX_ENABLE
/* Initialize directory index if supported */
if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
r = ext4_dir_dx_init(ch, parent);
if (r != EOK)
return r;
ext4_inode_set_flag(ch->inode, EXT4_INODE_FLAG_INDEX);
ch->dirty = true;
} else
#endif
{
r = ext4_dir_add_entry(ch, ".", strlen("."), ch);
if (r != EOK) {
ext4_dir_remove_entry(parent, n, strlen(n));
return r;
}
r = ext4_dir_add_entry(ch, "..", strlen(".."), parent);
if (r != EOK) {
ext4_dir_remove_entry(parent, n, strlen(n));
ext4_dir_remove_entry(ch, ".", strlen("."));
return r;
}
}
/*New empty directory. Two links (. and ..) */
ext4_inode_set_links_cnt(ch->inode, 2);
ext4_fs_inode_links_count_inc(parent);
ch->dirty = true;
parent->dirty = true;
return r;
}
/*
* In case we want to rename a directory,
* we reset the original '..' pointer.
*/
if (is_dir) {
bool idx;
idx = ext4_inode_has_flag(ch->inode, EXT4_INODE_FLAG_INDEX);
struct ext4_dir_search_result res;
if (!idx) {
r = ext4_dir_find_entry(&res, ch, "..", strlen(".."));
if (r != EOK)
return EIO;
ext4_dir_en_set_inode(res.dentry, parent->index);
ext4_trans_set_block_dirty(res.block.buf);
r = ext4_dir_destroy_result(ch, &res);
if (r != EOK)
return r;
} else {
#if CONFIG_DIR_INDEX_ENABLE
r = ext4_dir_dx_reset_parent_inode(ch, parent->index);
if (r != EOK)
return r;
#endif
}
ext4_fs_inode_links_count_inc(parent);
parent->dirty = true;
}
if (!rename) {
ext4_fs_inode_links_count_inc(ch);
ch->dirty = true;
}
return r;
}
static int ext4_unlink(struct ext4_mountpoint *mp,
struct ext4_inode_ref *parent,
struct ext4_inode_ref *child, const char *name,
uint32_t name_len)
{
bool has_children;
int rc = ext4_has_children(&has_children, child);
if (rc != EOK)
return rc;
/* Cannot unlink non-empty node */
if (has_children)
return ENOTEMPTY;
/* Remove entry from parent directory */
rc = ext4_dir_remove_entry(parent, name, name_len);
if (rc != EOK)
return rc;
bool is_dir = ext4_inode_is_type(&mp->fs.sb, child->inode,
EXT4_INODE_MODE_DIRECTORY);
/* If directory - handle links from parent */
if (is_dir) {
ext4_fs_inode_links_count_dec(parent);
parent->dirty = true;
}
/*
* TODO: Update timestamps of the parent
* (when we have wall-clock time).
*
* ext4_inode_set_change_inode_time(parent->inode, (uint32_t) now);
* ext4_inode_set_modification_time(parent->inode, (uint32_t) now);
* parent->dirty = true;
*/
/*
* TODO: Update timestamp for inode.
*
* ext4_inode_set_change_inode_time(child->inode,
* (uint32_t) now);
*/
if (ext4_inode_get_links_cnt(child->inode)) {
ext4_fs_inode_links_count_dec(child);
child->dirty = true;
}
return EOK;
}
/****************************************************************************/
int ext4_mount(const char *dev_name, const char *mount_point,
bool read_only)
{
int r;
uint32_t bsize;
struct ext4_bcache *bc;
struct ext4_blockdev *bd = 0;
struct ext4_mountpoint *mp = 0;
ext4_assert(mount_point && dev_name);
size_t mp_len = strlen(mount_point);
if (mp_len > CONFIG_EXT4_MAX_MP_NAME)
return EINVAL;
if (mount_point[mp_len - 1] != '/'){
droidboot_log(DROIDBOOT_LOG_ERROR, "mount_point should end with /\n");
return ENOTSUP;
}
for (size_t i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
if (!strcmp(dev_name, s_bdevices[i].name)) {
bd = s_bdevices[i].bd;
break;
}
}
if (!bd)
return ENODEV;
for (size_t i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
if (!s_mp[i].mounted) {
strcpy(s_mp[i].name, mount_point);
s_mp[i].mounted = 1;
mp = &s_mp[i];
break;
}
if (!strcmp(s_mp[i].name, mount_point))
return EOK;
}
if (!mp)
return ENOMEM;
r = ext4_block_init(bd);
if (r != EOK)
return r;
r = ext4_fs_init(&mp->fs, bd, read_only);
if (r != EOK) {
ext4_block_fini(bd);
return r;
}
bsize = ext4_sb_get_block_size(&mp->fs.sb);
ext4_block_set_lb_size(bd, bsize);
bc = &mp->bc;
r = ext4_bcache_init_dynamic(bc, CONFIG_BLOCK_DEV_CACHE_SIZE, bsize);
if (r != EOK) {
ext4_block_fini(bd);
return r;
}
if (bsize != bc->itemsize){
droidboot_log(DROIDBOOT_LOG_ERROR, "Wrong blocksize\n");
return ENOTSUP;
}
/*Bind block cache to block device*/
r = ext4_block_bind_bcache(bd, bc);
if (r != EOK) {
ext4_bcache_cleanup(bc);
ext4_block_fini(bd);
ext4_bcache_fini_dynamic(bc);
return r;
}
bd->fs = &mp->fs;
return r;
}
int ext4_umount(const char *mount_point)
{
int i;
int r;
struct ext4_mountpoint *mp = 0;
for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
if (!strcmp(s_mp[i].name, mount_point)) {
mp = &s_mp[i];
break;
}
}
if (!mp)
return ENODEV;
r = ext4_fs_fini(&mp->fs);
if (r != EOK)
goto Finish;
mp->mounted = 0;
ext4_bcache_cleanup(mp->fs.bdev->bc);
ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
r = ext4_block_fini(mp->fs.bdev);
Finish:
mp->fs.bdev->fs = NULL;
return r;
}
static struct ext4_mountpoint *ext4_get_mount(const char *path)
{
for (size_t i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
if (!s_mp[i].mounted)
continue;
if (!strncmp(s_mp[i].name, path, strlen(s_mp[i].name)))
return &s_mp[i];
}
return NULL;
}
__unused
static int __ext4_journal_start(const char *mount_point)
{
int r = EOK;
struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
if (!mp)
return ENOENT;
if (mp->fs.read_only)
return EOK;
if (ext4_sb_feature_com(&mp->fs.sb,
EXT4_FCOM_HAS_JOURNAL)) {
r = jbd_get_fs(&mp->fs, &mp->jbd_fs);
if (r != EOK)
goto Finish;
r = jbd_journal_start(&mp->jbd_fs, &mp->jbd_journal);
if (r != EOK) {
mp->jbd_fs.dirty = false;
jbd_put_fs(&mp->jbd_fs);
goto Finish;
}
mp->fs.jbd_fs = &mp->jbd_fs;
mp->fs.jbd_journal = &mp->jbd_journal;
}
Finish:
return r;
}
__unused
static int __ext4_journal_stop(const char *mount_point)
{
int r = EOK;
struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
if (!mp)
return ENOENT;
if (mp->fs.read_only)
return EOK;
if (ext4_sb_feature_com(&mp->fs.sb,
EXT4_FCOM_HAS_JOURNAL)) {
r = jbd_journal_stop(&mp->jbd_journal);
if (r != EOK) {
mp->jbd_fs.dirty = false;
jbd_put_fs(&mp->jbd_fs);
mp->fs.jbd_journal = NULL;
mp->fs.jbd_fs = NULL;
goto Finish;
}
r = jbd_put_fs(&mp->jbd_fs);
if (r != EOK) {
mp->fs.jbd_journal = NULL;
mp->fs.jbd_fs = NULL;
goto Finish;
}
mp->fs.jbd_journal = NULL;
mp->fs.jbd_fs = NULL;
}
Finish:
return r;
}
__unused
static int __ext4_recover(const char *mount_point)
{
struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
int r = ENOTSUP;
if (!mp)
return ENOENT;
EXT4_MP_LOCK(mp);
if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_HAS_JOURNAL)) {
struct jbd_fs *jbd_fs = ext4_calloc(1, sizeof(struct jbd_fs));
if (!jbd_fs) {
r = ENOMEM;
goto Finish;
}
r = jbd_get_fs(&mp->fs, jbd_fs);
if (r != EOK) {
ext4_free(jbd_fs);
goto Finish;
}
r = jbd_recover(jbd_fs);
jbd_put_fs(jbd_fs);
ext4_free(jbd_fs);
}
if (r == EOK && !mp->fs.read_only) {
uint32_t bgid;
uint64_t free_blocks_count = 0;
uint32_t free_inodes_count = 0;
struct ext4_block_group_ref bg_ref;
/* Update superblock's stats */
for (bgid = 0;bgid < ext4_block_group_cnt(&mp->fs.sb);bgid++) {
r = ext4_fs_get_block_group_ref(&mp->fs, bgid, &bg_ref);
if (r != EOK)
goto Finish;
free_blocks_count +=
ext4_bg_get_free_blocks_count(bg_ref.block_group,
&mp->fs.sb);
free_inodes_count +=
ext4_bg_get_free_inodes_count(bg_ref.block_group,
&mp->fs.sb);
ext4_fs_put_block_group_ref(&bg_ref);
}
ext4_sb_set_free_blocks_cnt(&mp->fs.sb, free_blocks_count);
ext4_set32(&mp->fs.sb, free_inodes_count, free_inodes_count);
/* We don't need to save the superblock stats immediately. */
}
Finish:
EXT4_MP_UNLOCK(mp);
return r;
}
__unused
static int __ext4_trans_start(struct ext4_mountpoint *mp)
{
int r = EOK;
if (mp->fs.jbd_journal && !mp->fs.curr_trans) {
struct jbd_journal *journal = mp->fs.jbd_journal;
struct jbd_trans *trans;
trans = jbd_journal_new_trans(journal);
if (!trans) {
r = ENOMEM;
goto Finish;
}
mp->fs.curr_trans = trans;
}
Finish:
return r;
}
__unused
static int __ext4_trans_stop(struct ext4_mountpoint *mp)
{
int r = EOK;
if (mp->fs.jbd_journal && mp->fs.curr_trans) {
struct jbd_journal *journal = mp->fs.jbd_journal;
struct jbd_trans *trans = mp->fs.curr_trans;
r = jbd_journal_commit_trans(journal, trans);
mp->fs.curr_trans = NULL;
}
return r;
}
__unused
static void __ext4_trans_abort(struct ext4_mountpoint *mp)
{
if (mp->fs.jbd_journal && mp->fs.curr_trans) {
struct jbd_journal *journal = mp->fs.jbd_journal;
struct jbd_trans *trans = mp->fs.curr_trans;
jbd_journal_free_trans(journal, trans, true);
mp->fs.curr_trans = NULL;
}
}
int ext4_journal_start(const char *mount_point __unused)
{
int r = EOK;
#if CONFIG_JOURNALING_ENABLE
r = __ext4_journal_start(mount_point);
#endif
return r;
}
int ext4_journal_stop(const char *mount_point __unused)
{
int r = EOK;
#if CONFIG_JOURNALING_ENABLE
r = __ext4_journal_stop(mount_point);
#endif
return r;
}
int ext4_recover(const char *mount_point __unused)
{
int r = EOK;
#if CONFIG_JOURNALING_ENABLE
r = __ext4_recover(mount_point);
#endif
return r;
}
static int ext4_trans_start(struct ext4_mountpoint *mp __unused)
{
int r = EOK;
#if CONFIG_JOURNALING_ENABLE
r = __ext4_trans_start(mp);
#endif
return r;
}
static int ext4_trans_stop(struct ext4_mountpoint *mp __unused)
{
int r = EOK;
#if CONFIG_JOURNALING_ENABLE
r = __ext4_trans_stop(mp);
#endif
return r;
}
static void ext4_trans_abort(struct ext4_mountpoint *mp __unused)
{
#if CONFIG_JOURNALING_ENABLE
__ext4_trans_abort(mp);
#endif
}
int ext4_mount_point_stats(const char *mount_point,
struct ext4_mount_stats *stats)
{
struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
if (!mp)
return ENOENT;
EXT4_MP_LOCK(mp);
stats->inodes_count = ext4_get32(&mp->fs.sb, inodes_count);
stats->free_inodes_count = ext4_get32(&mp->fs.sb, free_inodes_count);
stats->blocks_count = ext4_sb_get_blocks_cnt(&mp->fs.sb);
stats->free_blocks_count = ext4_sb_get_free_blocks_cnt(&mp->fs.sb);
stats->block_size = ext4_sb_get_block_size(&mp->fs.sb);
stats->block_group_count = ext4_block_group_cnt(&mp->fs.sb);
stats->blocks_per_group = ext4_get32(&mp->fs.sb, blocks_per_group);
stats->inodes_per_group = ext4_get32(&mp->fs.sb, inodes_per_group);
memcpy(stats->volume_name, mp->fs.sb.volume_name, 16);
EXT4_MP_UNLOCK(mp);
return EOK;
}
int ext4_mount_setup_locks(const char *mount_point,
const struct ext4_lock *locks)
{
uint32_t i;
struct ext4_mountpoint *mp = 0;
for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
if (!strcmp(s_mp[i].name, mount_point)) {
mp = &s_mp[i];
break;
}
}
if (!mp)
return ENOENT;
mp->os_locks = locks;
return EOK;
}
/********************************FILE OPERATIONS*****************************/
static int ext4_path_check(const char *path, bool *is_goal)
{
int i;
for (i = 0; i < EXT4_DIRECTORY_FILENAME_LEN; ++i) {
if (path[i] == '/') {
*is_goal = false;
return i;
}
if (path[i] == 0) {
*is_goal = true;
return i;
}
}
return 0;
}
static bool ext4_parse_flags(const char *flags, uint32_t *file_flags)
{
if (!flags)
return false;
if (!strcmp(flags, "r") || !strcmp(flags, "rb")) {
*file_flags = O_RDONLY;
return true;
}
if (!strcmp(flags, "w") || !strcmp(flags, "wb")) {
*file_flags = O_WRONLY | O_CREAT | O_TRUNC;
return true;
}
if (!strcmp(flags, "a") || !strcmp(flags, "ab")) {
*file_flags = O_WRONLY | O_CREAT | O_APPEND;
return true;
}
if (!strcmp(flags, "r+") || !strcmp(flags, "rb+") ||
!strcmp(flags, "r+b")) {
*file_flags = O_RDWR;
return true;
}
if (!strcmp(flags, "w+") || !strcmp(flags, "wb+") ||
!strcmp(flags, "w+b")) {
*file_flags = O_RDWR | O_CREAT | O_TRUNC;
return true;
}
if (!strcmp(flags, "a+") || !strcmp(flags, "ab+") ||
!strcmp(flags, "a+b")) {
*file_flags = O_RDWR | O_CREAT | O_APPEND;
return true;
}
return false;
}
static int ext4_trunc_inode(struct ext4_mountpoint *mp,
uint32_t index, uint64_t new_size)
{
int r = EOK;
struct ext4_fs *const fs = &mp->fs;
struct ext4_inode_ref inode_ref;
uint64_t inode_size;
bool has_trans = mp->fs.jbd_journal && mp->fs.curr_trans;
r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
if (r != EOK)
return r;
inode_size = ext4_inode_get_size(&fs->sb, inode_ref.inode);
ext4_fs_put_inode_ref(&inode_ref);
if (has_trans)
ext4_trans_stop(mp);
while (inode_size > new_size + CONFIG_MAX_TRUNCATE_SIZE) {
inode_size -= CONFIG_MAX_TRUNCATE_SIZE;
ext4_trans_start(mp);
r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
if (r != EOK) {
ext4_trans_abort(mp);
break;
}
r = ext4_fs_truncate_inode(&inode_ref, inode_size);
if (r != EOK)
ext4_fs_put_inode_ref(&inode_ref);
else
r = ext4_fs_put_inode_ref(&inode_ref);
if (r != EOK) {
ext4_trans_abort(mp);
goto Finish;
} else
ext4_trans_stop(mp);
}
if (inode_size > new_size) {
inode_size = new_size;
ext4_trans_start(mp);
r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
if (r != EOK) {
ext4_trans_abort(mp);
goto Finish;
}
r = ext4_fs_truncate_inode(&inode_ref, inode_size);
if (r != EOK)
ext4_fs_put_inode_ref(&inode_ref);
else
r = ext4_fs_put_inode_ref(&inode_ref);
if (r != EOK)
ext4_trans_abort(mp);
else
ext4_trans_stop(mp);
}
Finish:
if (has_trans)
ext4_trans_start(mp);
return r;
}
static int ext4_trunc_dir(struct ext4_mountpoint *mp,
struct ext4_inode_ref *parent,
struct ext4_inode_ref *dir)
{
int r = EOK;
bool is_dir = ext4_inode_is_type(&mp->fs.sb, dir->inode,
EXT4_INODE_MODE_DIRECTORY);
uint32_t block_size = ext4_sb_get_block_size(&mp->fs.sb);
if (!is_dir)
return EINVAL;
#if CONFIG_DIR_INDEX_ENABLE
/* Initialize directory index if supported */
if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
r = ext4_dir_dx_init(dir, parent);
if (r != EOK)
return r;
r = ext4_trunc_inode(mp, dir->index,
EXT4_DIR_DX_INIT_BCNT * block_size);
if (r != EOK)
return r;
} else
#endif
{
r = ext4_trunc_inode(mp, dir->index, block_size);
if (r != EOK)
return r;
}
return ext4_fs_truncate_inode(dir, 0);
}
/*
* NOTICE: if filetype is equal to EXT4_DIRENTRY_UNKNOWN,
* any filetype of the target dir entry will be accepted.
*/
static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
int ftype, uint32_t *parent_inode,
uint32_t *name_off)
{
bool is_goal = false;
uint32_t imode = EXT4_INODE_MODE_DIRECTORY;
uint32_t next_inode;
int r;
int len;
struct ext4_mountpoint *mp = ext4_get_mount(path);
struct ext4_dir_search_result result;
struct ext4_inode_ref ref;
f->mp = 0;
if (!mp)
return ENOENT;
struct ext4_fs *const fs = &mp->fs;
struct ext4_sblock *const sb = &mp->fs.sb;
if (fs->read_only && flags & O_CREAT)
return EROFS;
f->flags = flags;
/*Skip mount point*/
path += strlen(mp->name);
if (name_off)
*name_off = strlen(mp->name);
/*Load root*/
r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
if (r != EOK)
return r;
if (parent_inode)
*parent_inode = ref.index;
len = ext4_path_check(path, &is_goal);
while (1) {
len = ext4_path_check(path, &is_goal);
if (!len) {
/*If root open was request.*/
if (ftype == EXT4_DE_DIR || ftype == EXT4_DE_UNKNOWN)
if (is_goal)
break;
r = ENOENT;
break;
}
r = ext4_dir_find_entry(&result, &ref, path, len);
if (r != EOK) {
/*Destroy last result*/
ext4_dir_destroy_result(&ref, &result);
if (r != ENOENT)
break;
if (!(f->flags & O_CREAT))
break;
/*O_CREAT allows create new entry*/
struct ext4_inode_ref child_ref;