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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
|
/*
* Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
*
*
* HelenOS:
* Copyright (c) 2012 Martin Sucha
* Copyright (c) 2012 Frantisek Princ
* 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_fs.c
* @brief More complex filesystem functions.
*/
#include <ext4_config.h>
#include <ext4_types.h>
#include <ext4_misc.h>
#include <ext4_errno.h>
#include <ext4_debug.h>
#include <ext4_trans.h>
#include <ext4_fs.h>
#include <ext4_blockdev.h>
#include <ext4_super.h>
#include <ext4_crc32.h>
#include <ext4_block_group.h>
#include <ext4_balloc.h>
#include <ext4_bitmap.h>
#include <ext4_inode.h>
#include <ext4_ialloc.h>
#include <ext4_extent.h>
#include <string.h>
int ext4_fs_init(struct ext4_fs *fs, struct ext4_blockdev *bdev,
bool read_only)
{
int r, i;
uint16_t tmp;
uint32_t bsize;
ext4_assert(fs && bdev);
fs->bdev = bdev;
fs->read_only = read_only;
r = ext4_sb_read(fs->bdev, &fs->sb);
if (r != EOK)
return r;
if (!ext4_sb_check(&fs->sb))
return ENOTSUP;
bsize = ext4_sb_get_block_size(&fs->sb);
if (bsize > EXT4_MAX_BLOCK_SIZE)
return ENXIO;
r = ext4_fs_check_features(fs, &read_only);
if (r != EOK)
return r;
if (read_only)
fs->read_only = read_only;
/* Compute limits for indirect block levels */
uint32_t blocks_id = bsize / sizeof(uint32_t);
fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
fs->inode_blocks_per_level[0] = 1;
for (i = 1; i < 4; i++) {
fs->inode_blocks_per_level[i] =
fs->inode_blocks_per_level[i - 1] * blocks_id;
fs->inode_block_limits[i] = fs->inode_block_limits[i - 1] +
fs->inode_blocks_per_level[i];
}
/*Validate FS*/
tmp = ext4_get16(&fs->sb, state);
if (tmp & EXT4_SUPERBLOCK_STATE_ERROR_FS)
ext4_dbg(DEBUG_FS, DBG_WARN
"last umount error: superblock fs_error flag\n");
if (!fs->read_only) {
/* Mark system as mounted */
ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_ERROR_FS);
r = ext4_sb_write(fs->bdev, &fs->sb);
if (r != EOK)
return r;
/*Update mount count*/
ext4_set16(&fs->sb, mount_count, ext4_get16(&fs->sb, mount_count) + 1);
}
return r;
}
int ext4_fs_fini(struct ext4_fs *fs)
{
ext4_assert(fs);
/*Set superblock state*/
ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_VALID_FS);
if (!fs->read_only)
return ext4_sb_write(fs->bdev, &fs->sb);
return EOK;
}
static void ext4_fs_debug_features_inc(uint32_t features_incompatible)
{
if (features_incompatible & EXT4_FINCOM_COMPRESSION)
ext4_dbg(DEBUG_FS, DBG_NONE "compression\n");
if (features_incompatible & EXT4_FINCOM_FILETYPE)
ext4_dbg(DEBUG_FS, DBG_NONE "filetype\n");
if (features_incompatible & EXT4_FINCOM_RECOVER)
ext4_dbg(DEBUG_FS, DBG_NONE "recover\n");
if (features_incompatible & EXT4_FINCOM_JOURNAL_DEV)
ext4_dbg(DEBUG_FS, DBG_NONE "journal_dev\n");
if (features_incompatible & EXT4_FINCOM_META_BG)
ext4_dbg(DEBUG_FS, DBG_NONE "meta_bg\n");
if (features_incompatible & EXT4_FINCOM_EXTENTS)
ext4_dbg(DEBUG_FS, DBG_NONE "extents\n");
if (features_incompatible & EXT4_FINCOM_64BIT)
ext4_dbg(DEBUG_FS, DBG_NONE "64bit\n");
if (features_incompatible & EXT4_FINCOM_MMP)
ext4_dbg(DEBUG_FS, DBG_NONE "mnp\n");
if (features_incompatible & EXT4_FINCOM_FLEX_BG)
ext4_dbg(DEBUG_FS, DBG_NONE "flex_bg\n");
if (features_incompatible & EXT4_FINCOM_EA_INODE)
ext4_dbg(DEBUG_FS, DBG_NONE "ea_inode\n");
if (features_incompatible & EXT4_FINCOM_DIRDATA)
ext4_dbg(DEBUG_FS, DBG_NONE "dirdata\n");
if (features_incompatible & EXT4_FINCOM_BG_USE_META_CSUM)
ext4_dbg(DEBUG_FS, DBG_NONE "meta_csum\n");
if (features_incompatible & EXT4_FINCOM_LARGEDIR)
ext4_dbg(DEBUG_FS, DBG_NONE "largedir\n");
if (features_incompatible & EXT4_FINCOM_INLINE_DATA)
ext4_dbg(DEBUG_FS, DBG_NONE "inline_data\n");
}
static void ext4_fs_debug_features_comp(uint32_t features_compatible)
{
if (features_compatible & EXT4_FCOM_DIR_PREALLOC)
ext4_dbg(DEBUG_FS, DBG_NONE "dir_prealloc\n");
if (features_compatible & EXT4_FCOM_IMAGIC_INODES)
ext4_dbg(DEBUG_FS, DBG_NONE "imagic_inodes\n");
if (features_compatible & EXT4_FCOM_HAS_JOURNAL)
ext4_dbg(DEBUG_FS, DBG_NONE "has_journal\n");
if (features_compatible & EXT4_FCOM_EXT_ATTR)
ext4_dbg(DEBUG_FS, DBG_NONE "ext_attr\n");
if (features_compatible & EXT4_FCOM_RESIZE_INODE)
ext4_dbg(DEBUG_FS, DBG_NONE "resize_inode\n");
if (features_compatible & EXT4_FCOM_DIR_INDEX)
ext4_dbg(DEBUG_FS, DBG_NONE "dir_index\n");
}
static void ext4_fs_debug_features_ro(uint32_t features_ro)
{
if (features_ro & EXT4_FRO_COM_SPARSE_SUPER)
ext4_dbg(DEBUG_FS, DBG_NONE "sparse_super\n");
if (features_ro & EXT4_FRO_COM_LARGE_FILE)
ext4_dbg(DEBUG_FS, DBG_NONE "large_file\n");
if (features_ro & EXT4_FRO_COM_BTREE_DIR)
ext4_dbg(DEBUG_FS, DBG_NONE "btree_dir\n");
if (features_ro & EXT4_FRO_COM_HUGE_FILE)
ext4_dbg(DEBUG_FS, DBG_NONE "huge_file\n");
if (features_ro & EXT4_FRO_COM_GDT_CSUM)
ext4_dbg(DEBUG_FS, DBG_NONE "gtd_csum\n");
if (features_ro & EXT4_FRO_COM_DIR_NLINK)
ext4_dbg(DEBUG_FS, DBG_NONE "dir_nlink\n");
if (features_ro & EXT4_FRO_COM_EXTRA_ISIZE)
ext4_dbg(DEBUG_FS, DBG_NONE "extra_isize\n");
if (features_ro & EXT4_FRO_COM_QUOTA)
ext4_dbg(DEBUG_FS, DBG_NONE "quota\n");
if (features_ro & EXT4_FRO_COM_BIGALLOC)
ext4_dbg(DEBUG_FS, DBG_NONE "bigalloc\n");
if (features_ro & EXT4_FRO_COM_METADATA_CSUM)
ext4_dbg(DEBUG_FS, DBG_NONE "metadata_csum\n");
}
int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only)
{
ext4_assert(fs && read_only);
uint32_t v;
if (ext4_get32(&fs->sb, rev_level) == 0) {
*read_only = false;
return EOK;
}
ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_incompatible:\n");
ext4_fs_debug_features_inc(ext4_get32(&fs->sb, features_incompatible));
ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_compatible:\n");
ext4_fs_debug_features_comp(ext4_get32(&fs->sb, features_compatible));
ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_read_only:\n");
ext4_fs_debug_features_ro(ext4_get32(&fs->sb, features_read_only));
/*Check features_incompatible*/
v = (ext4_get32(&fs->sb, features_incompatible) &
(~CONFIG_SUPPORTED_FINCOM));
if (v) {
ext4_dbg(DEBUG_FS, DBG_ERROR
"sblock has unsupported features incompatible:\n");
ext4_fs_debug_features_inc(v);
return ENOTSUP;
}
/*Check features_read_only*/
v = ext4_get32(&fs->sb, features_read_only);
v &= ~CONFIG_SUPPORTED_FRO_COM;
if (v) {
ext4_dbg(DEBUG_FS, DBG_WARN
"sblock has unsupported features read only:\n");
ext4_fs_debug_features_ro(v);
*read_only = true;
return EOK;
}
*read_only = false;
return EOK;
}
/**@brief Determine whether the block is inside the group.
* @param baddr block address
* @param bgid block group id
* @return Error code
*/
static bool ext4_block_in_group(struct ext4_sblock *s, ext4_fsblk_t baddr,
uint32_t bgid)
{
uint32_t actual_bgid;
actual_bgid = ext4_balloc_get_bgid_of_block(s, baddr);
if (actual_bgid == bgid)
return true;
return false;
}
/**@brief To avoid calling the atomic setbit hundreds or thousands of times, we only
* need to use it within a single byte (to ensure we get endianness right).
* We can use memset for the rest of the bitmap as there are no other users.
*/
static void ext4_fs_mark_bitmap_end(int start_bit, int end_bit, void *bitmap)
{
int i;
if (start_bit >= end_bit)
return;
for (i = start_bit; (unsigned)i < ((start_bit + 7) & ~7UL); i++)
ext4_bmap_bit_set(bitmap, i);
if (i < end_bit)
memset((char *)bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
}
/**@brief Initialize block bitmap in block group.
* @param bg_ref Reference to block group
* @return Error code
*/
static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref)
{
struct ext4_sblock *sb = &bg_ref->fs->sb;
struct ext4_bgroup *bg = bg_ref->block_group;
int rc;
uint32_t bit, bit_max;
uint32_t group_blocks;
uint16_t inode_size = ext4_get16(sb, inode_size);
uint32_t block_size = ext4_sb_get_block_size(sb);
uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
ext4_fsblk_t i;
ext4_fsblk_t bmp_blk = ext4_bg_get_block_bitmap(bg, sb);
ext4_fsblk_t bmp_inode = ext4_bg_get_inode_bitmap(bg, sb);
ext4_fsblk_t inode_table = ext4_bg_get_inode_table_first_block(bg, sb);
ext4_fsblk_t first_bg = ext4_balloc_get_block_of_bgid(sb, bg_ref->index);
uint32_t dsc_per_block = block_size / ext4_sb_get_desc_size(sb);
bool flex_bg = ext4_sb_feature_incom(sb, EXT4_FINCOM_FLEX_BG);
bool meta_bg = ext4_sb_feature_incom(sb, EXT4_FINCOM_META_BG);
uint32_t inode_table_bcnt = inodes_per_group * inode_size / block_size;
struct ext4_block block_bitmap;
rc = ext4_trans_block_get_noread(bg_ref->fs->bdev, &block_bitmap, bmp_blk);
if (rc != EOK)
return rc;
memset(block_bitmap.data, 0, block_size);
bit_max = ext4_sb_is_super_in_bg(sb, bg_ref->index);
uint32_t count = ext4_sb_first_meta_bg(sb) * dsc_per_block;
if (!meta_bg || bg_ref->index < count) {
if (bit_max) {
bit_max += ext4_bg_num_gdb(sb, bg_ref->index);
bit_max += ext4_get16(sb, s_reserved_gdt_blocks);
}
} else { /* For META_BG_BLOCK_GROUPS */
bit_max += ext4_bg_num_gdb(sb, bg_ref->index);
}
for (bit = 0; bit < bit_max; bit++)
ext4_bmap_bit_set(block_bitmap.data, bit);
if (bg_ref->index == ext4_block_group_cnt(sb) - 1) {
/*
* Even though mke2fs always initialize first and last group
* if some other tool enabled the EXT4_BG_BLOCK_UNINIT we need
* to make sure we calculate the right free blocks
*/
group_blocks = (uint32_t)(ext4_sb_get_blocks_cnt(sb) -
ext4_get32(sb, first_data_block) -
ext4_get32(sb, blocks_per_group) *
(ext4_block_group_cnt(sb) - 1));
} else {
group_blocks = ext4_get32(sb, blocks_per_group);
}
bool in_bg;
in_bg = ext4_block_in_group(sb, bmp_blk, bg_ref->index);
if (!flex_bg || in_bg)
ext4_bmap_bit_set(block_bitmap.data,
(uint32_t)(bmp_blk - first_bg));
in_bg = ext4_block_in_group(sb, bmp_inode, bg_ref->index);
if (!flex_bg || in_bg)
ext4_bmap_bit_set(block_bitmap.data,
(uint32_t)(bmp_inode - first_bg));
for (i = inode_table; i < inode_table + inode_table_bcnt; i++) {
in_bg = ext4_block_in_group(sb, i, bg_ref->index);
if (!flex_bg || in_bg)
ext4_bmap_bit_set(block_bitmap.data,
(uint32_t)(i - first_bg));
}
/*
* Also if the number of blocks within the group is
* less than the blocksize * 8 ( which is the size
* of bitmap ), set rest of the block bitmap to 1
*/
ext4_fs_mark_bitmap_end(group_blocks, block_size * 8, block_bitmap.data);
ext4_trans_set_block_dirty(block_bitmap.buf);
ext4_balloc_set_bitmap_csum(sb, bg_ref->block_group, block_bitmap.data);
bg_ref->dirty = true;
/* Save bitmap */
return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
}
/**@brief Initialize i-node bitmap in block group.
* @param bg_ref Reference to block group
* @return Error code
*/
static int ext4_fs_init_inode_bitmap(struct ext4_block_group_ref *bg_ref)
{
int rc;
struct ext4_sblock *sb = &bg_ref->fs->sb;
struct ext4_bgroup *bg = bg_ref->block_group;
/* Load bitmap */
ext4_fsblk_t bitmap_block_addr = ext4_bg_get_inode_bitmap(bg, sb);
struct ext4_block b;
rc = ext4_trans_block_get_noread(bg_ref->fs->bdev, &b, bitmap_block_addr);
if (rc != EOK)
return rc;
/* Initialize all bitmap bits to zero */
uint32_t block_size = ext4_sb_get_block_size(sb);
uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
memset(b.data, 0, (inodes_per_group + 7) / 8);
uint32_t start_bit = inodes_per_group;
uint32_t end_bit = block_size * 8;
uint32_t i;
for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
ext4_bmap_bit_set(b.data, i);
if (i < end_bit)
memset(b.data + (i >> 3), 0xff, (end_bit - i) >> 3);
ext4_trans_set_block_dirty(b.buf);
ext4_ialloc_set_bitmap_csum(sb, bg, b.data);
bg_ref->dirty = true;
/* Save bitmap */
return ext4_block_set(bg_ref->fs->bdev, &b);
}
/**@brief Initialize i-node table in block group.
* @param bg_ref Reference to block group
* @return Error code
*/
static int ext4_fs_init_inode_table(struct ext4_block_group_ref *bg_ref)
{
struct ext4_sblock *sb = &bg_ref->fs->sb;
struct ext4_bgroup *bg = bg_ref->block_group;
uint32_t inode_size = ext4_get16(sb, inode_size);
uint32_t block_size = ext4_sb_get_block_size(sb);
uint32_t inodes_per_block = block_size / inode_size;
uint32_t inodes_in_group = ext4_inodes_in_group_cnt(sb, bg_ref->index);
uint32_t table_blocks = inodes_in_group / inodes_per_block;
ext4_fsblk_t fblock;
if (inodes_in_group % inodes_per_block)
table_blocks++;
/* Compute initialization bounds */
ext4_fsblk_t first_block = ext4_bg_get_inode_table_first_block(bg, sb);
ext4_fsblk_t last_block = first_block + table_blocks - 1;
/* Initialization of all itable blocks */
for (fblock = first_block; fblock <= last_block; ++fblock) {
struct ext4_block b;
int rc = ext4_trans_block_get_noread(bg_ref->fs->bdev, &b, fblock);
if (rc != EOK)
return rc;
memset(b.data, 0, block_size);
ext4_trans_set_block_dirty(b.buf);
rc = ext4_block_set(bg_ref->fs->bdev, &b);
if (rc != EOK)
return rc;
}
return EOK;
}
static ext4_fsblk_t ext4_fs_get_descriptor_block(struct ext4_sblock *s,
uint32_t bgid,
uint32_t dsc_per_block)
{
uint32_t first_meta_bg, dsc_id;
int has_super = 0;
dsc_id = bgid / dsc_per_block;
first_meta_bg = ext4_sb_first_meta_bg(s);
bool meta_bg = ext4_sb_feature_incom(s, EXT4_FINCOM_META_BG);
if (!meta_bg || dsc_id < first_meta_bg)
return ext4_get32(s, first_data_block) + dsc_id + 1;
if (ext4_sb_is_super_in_bg(s, bgid))
has_super = 1;
return (has_super + ext4_fs_first_bg_block_no(s, bgid));
}
/**@brief Compute checksum of block group descriptor.
* @param sb Superblock
* @param bgid Index of block group in the filesystem
* @param bg Block group to compute checksum for
* @return Checksum value
*/
static uint16_t ext4_fs_bg_checksum(struct ext4_sblock *sb, uint32_t bgid,
struct ext4_bgroup *bg)
{
/* If checksum not supported, 0 will be returned */
uint16_t crc = 0;
#if CONFIG_META_CSUM_ENABLE
/* Compute the checksum only if the filesystem supports it */
if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
/* Use metadata_csum algorithm instead */
uint32_t le32_bgid = to_le32(bgid);
uint32_t orig_checksum, checksum;
/* Preparation: temporarily set bg checksum to 0 */
orig_checksum = bg->checksum;
bg->checksum = 0;
/* First calculate crc32 checksum against fs uuid */
checksum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid,
sizeof(sb->uuid));
/* Then calculate crc32 checksum against bgid */
checksum = ext4_crc32c(checksum, &le32_bgid, sizeof(bgid));
/* Finally calculate crc32 checksum against block_group_desc */
checksum = ext4_crc32c(checksum, bg, ext4_sb_get_desc_size(sb));
bg->checksum = orig_checksum;
crc = checksum & 0xFFFF;
return crc;
}
#endif
if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_GDT_CSUM)) {
uint8_t *base = (uint8_t *)bg;
uint8_t *checksum = (uint8_t *)&bg->checksum;
uint32_t offset = (uint32_t)(checksum - base);
/* Convert block group index to little endian */
uint32_t group = to_le32(bgid);
/* Initialization */
crc = ext4_bg_crc16(~0, sb->uuid, sizeof(sb->uuid));
/* Include index of block group */
crc = ext4_bg_crc16(crc, (uint8_t *)&group, sizeof(group));
/* Compute crc from the first part (stop before checksum field)
*/
crc = ext4_bg_crc16(crc, (uint8_t *)bg, offset);
/* Skip checksum */
offset += sizeof(bg->checksum);
/* Checksum of the rest of block group descriptor */
if ((ext4_sb_feature_incom(sb, EXT4_FINCOM_64BIT)) &&
(offset < ext4_sb_get_desc_size(sb))) {
const uint8_t *start = ((uint8_t *)bg) + offset;
size_t len = ext4_sb_get_desc_size(sb) - offset;
crc = ext4_bg_crc16(crc, start, len);
}
}
return crc;
}
#if CONFIG_META_CSUM_ENABLE
static bool ext4_fs_verify_bg_csum(struct ext4_sblock *sb,
uint32_t bgid,
struct ext4_bgroup *bg)
{
if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
return true;
return ext4_fs_bg_checksum(sb, bgid, bg) == to_le16(bg->checksum);
}
#else
#define ext4_fs_verify_bg_csum(...) true
#endif
int ext4_fs_get_block_group_ref(struct ext4_fs *fs, uint32_t bgid,
struct ext4_block_group_ref *ref)
{
/* Compute number of descriptors, that fits in one data block */
uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
uint32_t dsc_cnt = block_size / ext4_sb_get_desc_size(&fs->sb);
/* Block group descriptor table starts at the next block after
* superblock */
uint64_t block_id = ext4_fs_get_descriptor_block(&fs->sb, bgid, dsc_cnt);
uint32_t offset = (bgid % dsc_cnt) * ext4_sb_get_desc_size(&fs->sb);
int rc = ext4_trans_block_get(fs->bdev, &ref->block, block_id);
if (rc != EOK)
return rc;
ref->block_group = (void *)(ref->block.data + offset);
ref->fs = fs;
ref->index = bgid;
ref->dirty = false;
struct ext4_bgroup *bg = ref->block_group;
if (!ext4_fs_verify_bg_csum(&fs->sb, bgid, bg)) {
ext4_dbg(DEBUG_FS,
DBG_WARN "Block group descriptor checksum failed."
"Block group index: %" PRIu32"\n",
bgid);
}
if (ext4_bg_has_flag(bg, EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
rc = ext4_fs_init_block_bitmap(ref);
if (rc != EOK) {
ext4_block_set(fs->bdev, &ref->block);
return rc;
}
ext4_bg_clear_flag(bg, EXT4_BLOCK_GROUP_BLOCK_UNINIT);
ref->dirty = true;
}
if (ext4_bg_has_flag(bg, EXT4_BLOCK_GROUP_INODE_UNINIT)) {
rc = ext4_fs_init_inode_bitmap(ref);
if (rc != EOK) {
ext4_block_set(ref->fs->bdev, &ref->block);
return rc;
}
ext4_bg_clear_flag(bg, EXT4_BLOCK_GROUP_INODE_UNINIT);
if (!ext4_bg_has_flag(bg, EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
rc = ext4_fs_init_inode_table(ref);
if (rc != EOK) {
ext4_block_set(fs->bdev, &ref->block);
return rc;
}
ext4_bg_set_flag(bg, EXT4_BLOCK_GROUP_ITABLE_ZEROED);
}
ref->dirty = true;
}
return EOK;
}
int ext4_fs_put_block_group_ref(struct ext4_block_group_ref *ref)
{
/* Check if reference modified */
if (ref->dirty) {
/* Compute new checksum of block group */
uint16_t cs;
cs = ext4_fs_bg_checksum(&ref->fs->sb, ref->index,
ref->block_group);
ref->block_group->checksum = to_le16(cs);
/* Mark block dirty for writing changes to physical device */
ext4_trans_set_block_dirty(ref->block.buf);
}
/* Put back block, that contains block group descriptor */
return ext4_block_set(ref->fs->bdev, &ref->block);
}
#if CONFIG_META_CSUM_ENABLE
static uint32_t ext4_fs_inode_checksum(struct ext4_inode_ref *inode_ref)
{
uint32_t checksum = 0;
struct ext4_sblock *sb = &inode_ref->fs->sb;
uint16_t inode_size = ext4_get16(sb, inode_size);
if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
uint32_t orig_checksum;
uint32_t ino_index = to_le32(inode_ref->index);
uint32_t ino_gen =
to_le32(ext4_inode_get_generation(inode_ref->inode));
/* Preparation: temporarily set bg checksum to 0 */
orig_checksum = ext4_inode_get_csum(sb, inode_ref->inode);
ext4_inode_set_csum(sb, inode_ref->inode, 0);
/* First calculate crc32 checksum against fs uuid */
checksum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid,
sizeof(sb->uuid));
/* Then calculate crc32 checksum against inode number
* and inode generation */
checksum = ext4_crc32c(checksum, &ino_index, sizeof(ino_index));
checksum = ext4_crc32c(checksum, &ino_gen, sizeof(ino_gen));
/* Finally calculate crc32 checksum against
* the entire inode */
checksum = ext4_crc32c(checksum, inode_ref->inode, inode_size);
ext4_inode_set_csum(sb, inode_ref->inode, orig_checksum);
/* If inode size is not large enough to hold the
* upper 16bit of the checksum */
if (inode_size == EXT4_GOOD_OLD_INODE_SIZE)
checksum &= 0xFFFF;
}
return checksum;
}
#else
#define ext4_fs_inode_checksum(...) 0
#endif
static void ext4_fs_set_inode_checksum(struct ext4_inode_ref *inode_ref)
{
struct ext4_sblock *sb = &inode_ref->fs->sb;
if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
return;
uint32_t csum = ext4_fs_inode_checksum(inode_ref);
ext4_inode_set_csum(sb, inode_ref->inode, csum);
}
#if CONFIG_META_CSUM_ENABLE
static bool ext4_fs_verify_inode_csum(struct ext4_inode_ref *inode_ref)
{
struct ext4_sblock *sb = &inode_ref->fs->sb;
if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
return true;
return ext4_inode_get_csum(sb, inode_ref->inode) ==
ext4_fs_inode_checksum(inode_ref);
}
#else
#define ext4_fs_verify_inode_csum(...) true
#endif
static int
__ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
struct ext4_inode_ref *ref,
bool initialized)
{
/* Compute number of i-nodes, that fits in one data block */
uint32_t inodes_per_group = ext4_get32(&fs->sb, inodes_per_group);
/*
* Inode numbers are 1-based, but it is simpler to work with 0-based
* when computing indices
*/
index -= 1;
uint32_t block_group = index / inodes_per_group;
uint32_t offset_in_group = index % inodes_per_group;
/* Load block group, where i-node is located */
struct ext4_block_group_ref bg_ref;
int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
if (rc != EOK) {
return rc;
}
/* Load block address, where i-node table is located */
ext4_fsblk_t inode_table_start =
ext4_bg_get_inode_table_first_block(bg_ref.block_group, &fs->sb);
/* Put back block group reference (not needed more) */
rc = ext4_fs_put_block_group_ref(&bg_ref);
if (rc != EOK) {
return rc;
}
/* Compute position of i-node in the block group */
uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
uint32_t byte_offset_in_group = offset_in_group * inode_size;
/* Compute block address */
ext4_fsblk_t block_id =
inode_table_start + (byte_offset_in_group / block_size);
rc = ext4_trans_block_get(fs->bdev, &ref->block, block_id);
if (rc != EOK) {
return rc;
}
/* Compute position of i-node in the data block */
uint32_t offset_in_block = byte_offset_in_group % block_size;
ref->inode = (struct ext4_inode *)(ref->block.data + offset_in_block);
/* We need to store the original value of index in the reference */
ref->index = index + 1;
ref->fs = fs;
ref->dirty = false;
if (initialized && !ext4_fs_verify_inode_csum(ref)) {
ext4_dbg(DEBUG_FS,
DBG_WARN "Inode checksum failed."
"Inode: %" PRIu32"\n",
ref->index);
}
return EOK;
}
int ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
struct ext4_inode_ref *ref)
{
return __ext4_fs_get_inode_ref(fs, index, ref, true);
}
int ext4_fs_put_inode_ref(struct ext4_inode_ref *ref)
{
/* Check if reference modified */
if (ref->dirty) {
/* Mark block dirty for writing changes to physical device */
ext4_fs_set_inode_checksum(ref);
ext4_trans_set_block_dirty(ref->block.buf);
}
/* Put back block, that contains i-node */
return ext4_block_set(ref->fs->bdev, &ref->block);
}
void ext4_fs_inode_blocks_init(struct ext4_fs *fs,
struct ext4_inode_ref *inode_ref)
{
struct ext4_inode *inode = inode_ref->inode;
/* Reset blocks array. For inode which is not directory or file, just
* fill in blocks with 0 */
switch (ext4_inode_type(&fs->sb, inode_ref->inode)) {
case EXT4_INODE_MODE_FILE:
case EXT4_INODE_MODE_DIRECTORY:
break;
default:
return;
}
#if CONFIG_EXTENT_ENABLE
/* Initialize extents if needed */
if (ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) {
ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
/* Initialize extent root header */
ext4_extent_tree_init(inode_ref);
}
inode_ref->dirty = true;
#endif
}
uint32_t ext4_fs_correspond_inode_mode(int filetype)
{
switch (filetype) {
case EXT4_DE_DIR:
return EXT4_INODE_MODE_DIRECTORY;
case EXT4_DE_REG_FILE:
return EXT4_INODE_MODE_FILE;
case EXT4_DE_SYMLINK:
return EXT4_INODE_MODE_SOFTLINK;
case EXT4_DE_CHRDEV:
return EXT4_INODE_MODE_CHARDEV;
case EXT4_DE_BLKDEV:
return EXT4_INODE_MODE_BLOCKDEV;
case EXT4_DE_FIFO:
return EXT4_INODE_MODE_FIFO;
case EXT4_DE_SOCK:
return EXT4_INODE_MODE_SOCKET;
}
/* FIXME: unsupported filetype */
return EXT4_INODE_MODE_FILE;
}
int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
int filetype)
{
/* Check if newly allocated i-node will be a directory */
bool is_dir;
uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
is_dir = (filetype == EXT4_DE_DIR);
/* Allocate inode by allocation algorithm */
uint32_t index;
int rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
if (rc != EOK)
return rc;
/* Load i-node from on-disk i-node table */
rc = __ext4_fs_get_inode_ref(fs, index, inode_ref, false);
if (rc != EOK) {
ext4_ialloc_free_inode(fs, index, is_dir);
return rc;
}
/* Initialize i-node */
struct ext4_inode *inode = inode_ref->inode;
memset(inode, 0, inode_size);
uint32_t mode;
if (is_dir) {
/*
* Default directory permissions to be compatible with other
* systems
* 0777 (octal) == rwxrwxrwx
*/
mode = 0777;
mode |= EXT4_INODE_MODE_DIRECTORY;
} else if (filetype == EXT4_DE_SYMLINK) {
/*
* Default symbolic link permissions to be compatible with other systems
* 0777 (octal) == rwxrwxrwx
*/
mode = 0777;
mode |= EXT4_INODE_MODE_SOFTLINK;
} else {
/*
* Default file permissions to be compatible with other systems
* 0666 (octal) == rw-rw-rw-
*/
mode = 0666;
mode |= ext4_fs_correspond_inode_mode(filetype);
}
ext4_inode_set_mode(&fs->sb, inode, mode);
ext4_inode_set_links_cnt(inode, 0);
ext4_inode_set_uid(inode, 0);
ext4_inode_set_gid(inode, 0);
ext4_inode_set_size(inode, 0);
ext4_inode_set_access_time(inode, 0);
ext4_inode_set_change_inode_time(inode, 0);
ext4_inode_set_modif_time(inode, 0);
ext4_inode_set_del_time(inode, 0);
ext4_inode_set_blocks_count(&fs->sb, inode, 0);
ext4_inode_set_flags(inode, 0);
ext4_inode_set_generation(inode, 0);
if (inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
uint16_t size = ext4_get16(&fs->sb, want_extra_isize);
ext4_inode_set_extra_isize(&fs->sb, inode, size);
}
memset(inode->blocks, 0, sizeof(inode->blocks));
inode_ref->dirty = true;
return EOK;
}
int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
{
struct ext4_fs *fs = inode_ref->fs;
uint32_t offset;
uint32_t suboff;
int rc;
#if CONFIG_EXTENT_ENABLE
/* For extents must be data block destroyed by other way */
if ((ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) &&
(ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
/* Data structures are released during truncate operation... */
goto finish;
}
#endif
/* Release all indirect (no data) blocks */
/* 1) Single indirect */
ext4_fsblk_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
if (fblock != 0) {
int rc = ext4_balloc_free_block(inode_ref, fblock);
if (rc != EOK)
return rc;
ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
}
uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
uint32_t count = block_size / sizeof(uint32_t);
struct ext4_block block;
/* 2) Double indirect */
fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
if (fblock != 0) {
int rc = ext4_trans_block_get(fs->bdev, &block, fblock);
if (rc != EOK)
return rc;
ext4_fsblk_t ind_block;
for (offset = 0; offset < count; ++offset) {
ind_block = to_le32(((uint32_t *)block.data)[offset]);
if (ind_block == 0)
continue;
rc = ext4_balloc_free_block(inode_ref, ind_block);
if (rc != EOK) {
ext4_block_set(fs->bdev, &block);
return rc;
}
}
ext4_block_set(fs->bdev, &block);
rc = ext4_balloc_free_block(inode_ref, fblock);
if (rc != EOK)
return rc;
ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
}
/* 3) Tripple indirect */
struct ext4_block subblock;
fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
if (fblock == 0)
goto finish;
rc = ext4_trans_block_get(fs->bdev, &block, fblock);
if (rc != EOK)
return rc;
ext4_fsblk_t ind_block;
for (offset = 0; offset < count; ++offset) {
ind_block = to_le32(((uint32_t *)block.data)[offset]);
if (ind_block == 0)
continue;
rc = ext4_trans_block_get(fs->bdev, &subblock,
ind_block);
if (rc != EOK) {
ext4_block_set(fs->bdev, &block);
return rc;
}
ext4_fsblk_t ind_subblk;
for (suboff = 0; suboff < count; ++suboff) {
ind_subblk = to_le32(((uint32_t *)subblock.data)[suboff]);
if (ind_subblk == 0)
continue;
rc = ext4_balloc_free_block(inode_ref, ind_subblk);
if (rc != EOK) {
ext4_block_set(fs->bdev, &subblock);
ext4_block_set(fs->bdev, &block);
return rc;
}
}
ext4_block_set(fs->bdev, &subblock);
rc = ext4_balloc_free_block(inode_ref,
ind_block);
if (rc != EOK) {
ext4_block_set(fs->bdev, &block);
return rc;
}
}
ext4_block_set(fs->bdev, &block);
rc = ext4_balloc_free_block(inode_ref, fblock);
if (rc != EOK)
return rc;
ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
finish:
/* Mark inode dirty for writing to the physical device */
inode_ref->dirty = true;
/* Free block with extended attributes if present */
ext4_fsblk_t xattr_block =
ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
if (xattr_block) {
int rc = ext4_balloc_free_block(inode_ref, xattr_block);
if (rc != EOK)
return rc;
ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0);
}
/* Free inode by allocator */
if (ext4_inode_is_type(&fs->sb, inode_ref->inode,
EXT4_INODE_MODE_DIRECTORY))
rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
else
rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
return rc;
}
/**@brief Release data block from i-node
* @param inode_ref I-node to release block from
* @param iblock Logical block to be released
* @return Error code
*/
static int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref,
ext4_lblk_t iblock)
{
ext4_fsblk_t fblock;
struct ext4_fs *fs = inode_ref->fs;
/* Extents are handled otherwise = there is not support in this function
*/
ext4_assert(!(
ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS) &&
(ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
struct ext4_inode *inode = inode_ref->inode;
/* Handle simple case when we are dealing with direct reference */
if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
fblock = ext4_inode_get_direct_block(inode, iblock);
/* Sparse file */
if (fblock == 0)
return EOK;
ext4_inode_set_direct_block(inode, iblock, 0);
return ext4_balloc_free_block(inode_ref, fblock);
}
/* Determine the indirection level needed to get the desired block */
unsigned int level = 0;
unsigned int i;
for (i = 1; i < 4; i++) {
if (iblock < fs->inode_block_limits[i]) {
level = i;
break;
}
}
if (level == 0)
return EIO;
/* Compute offsets for the topmost level */
uint32_t block_offset_in_level =
(uint32_t)(iblock - fs->inode_block_limits[level - 1]);
ext4_fsblk_t current_block =
ext4_inode_get_indirect_block(inode, level - 1);
uint32_t offset_in_block =
(uint32_t)(block_offset_in_level / fs->inode_blocks_per_level[level - 1]);
/*
* Navigate through other levels, until we find the block number
* or find null reference meaning we are dealing with sparse file
*/
struct ext4_block block;
while (level > 0) {
/* Sparse check */
if (current_block == 0)
return EOK;
int rc = ext4_trans_block_get(fs->bdev, &block, current_block);
if (rc != EOK)
return rc;
current_block =
to_le32(((uint32_t *)block.data)[offset_in_block]);
/* Set zero if physical data block address found */
if (level == 1) {
((uint32_t *)block.data)[offset_in_block] = to_le32(0);
ext4_trans_set_block_dirty(block.buf);
}
rc = ext4_block_set(fs->bdev, &block);
if (rc != EOK)
return rc;
level--;
/*
* If we are on the last level, break here as
* there is no next level to visit
*/
if (level == 0)
break;
/* Visit the next level */
block_offset_in_level %= fs->inode_blocks_per_level[level];
offset_in_block = (uint32_t)(block_offset_in_level /
fs->inode_blocks_per_level[level - 1]);
}
fblock = current_block;
if (fblock == 0)
return EOK;
/* Physical block is not referenced, it can be released */
return ext4_balloc_free_block(inode_ref, fblock);
}
int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref, uint64_t new_size)
{
struct ext4_sblock *sb = &inode_ref->fs->sb;
uint32_t i;
int r;
bool v;
/* Check flags, if i-node can be truncated */
if (!ext4_inode_can_truncate(sb, inode_ref->inode))
return EINVAL;
/* If sizes are equal, nothing has to be done. */
uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
if (old_size == new_size)
return EOK;
/* It's not supported to make the larger file by truncate operation */
if (old_size < new_size)
return EINVAL;
/* For symbolic link which is small enough */
v = ext4_inode_is_type(sb, inode_ref->inode, EXT4_INODE_MODE_SOFTLINK);
if (v && old_size < sizeof(inode_ref->inode->blocks) &&
!ext4_inode_get_blocks_count(sb, inode_ref->inode)) {
char *content = (char *)inode_ref->inode->blocks + new_size;
memset(content, 0,
sizeof(inode_ref->inode->blocks) - (uint32_t)new_size);
ext4_inode_set_size(inode_ref->inode, new_size);
inode_ref->dirty = true;
return EOK;
}
i = ext4_inode_type(sb, inode_ref->inode);
if (i == EXT4_INODE_MODE_CHARDEV ||
i == EXT4_INODE_MODE_BLOCKDEV ||
i == EXT4_INODE_MODE_SOCKET) {
inode_ref->inode->blocks[0] = 0;
inode_ref->inode->blocks[1] = 0;
inode_ref->dirty = true;
return EOK;
}
/* Compute how many blocks will be released */
uint32_t block_size = ext4_sb_get_block_size(sb);
uint32_t new_blocks_cnt = (uint32_t)((new_size + block_size - 1) / block_size);
uint32_t old_blocks_cnt = (uint32_t)((old_size + block_size - 1) / block_size);
uint32_t diff_blocks_cnt = old_blocks_cnt - new_blocks_cnt;
#if CONFIG_EXTENT_ENABLE
if ((ext4_sb_feature_incom(sb, EXT4_FINCOM_EXTENTS)) &&
(ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
/* Extents require special operation */
if (diff_blocks_cnt) {
r = ext4_extent_remove_space(inode_ref, new_blocks_cnt,
EXT_MAX_BLOCKS);
if (r != EOK)
return r;
}
} else
#endif
{
/* Release data blocks from the end of file */
/* Starting from 1 because of logical blocks are numbered from 0
*/
for (i = 0; i < diff_blocks_cnt; ++i) {
r = ext4_fs_release_inode_block(inode_ref,
new_blocks_cnt + i);
if (r != EOK)
return r;
}
}
/* Update i-node */
ext4_inode_set_size(inode_ref->inode, new_size);
inode_ref->dirty = true;
return EOK;
}
/**@brief Compute 'goal' for inode index
* @param inode_ref Reference to inode, to allocate block for
* @return goal
*/
ext4_fsblk_t ext4_fs_inode_to_goal_block(struct ext4_inode_ref *inode_ref)
{
uint32_t grp_inodes = ext4_get32(&inode_ref->fs->sb, inodes_per_group);
return (inode_ref->index - 1) / grp_inodes;
}
/**@brief Compute 'goal' for allocation algorithm (For blockmap).
* @param inode_ref Reference to inode, to allocate block for
* @param goal
* @return error code
*/
int ext4_fs_indirect_find_goal(struct ext4_inode_ref *inode_ref,
ext4_fsblk_t *goal)
{
int r;
struct ext4_sblock *sb = &inode_ref->fs->sb;
*goal = 0;
uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
uint32_t block_size = ext4_sb_get_block_size(sb);
uint32_t iblock_cnt = (uint32_t)(inode_size / block_size);
if (inode_size % block_size != 0)
iblock_cnt++;
/* If inode has some blocks, get last block address + 1 */
if (iblock_cnt > 0) {
r = ext4_fs_get_inode_dblk_idx(inode_ref, iblock_cnt - 1,
goal, false);
if (r != EOK)
return r;
if (*goal != 0) {
(*goal)++;
return r;
}
/* If goal == 0, sparse file -> continue */
}
/* Identify block group of inode */
uint32_t inodes_per_bg = ext4_get32(sb, inodes_per_group);
uint32_t block_group = (inode_ref->index - 1) / inodes_per_bg;
block_size = ext4_sb_get_block_size(sb);
/* Load block group reference */
struct ext4_block_group_ref bg_ref;
r = ext4_fs_get_block_group_ref(inode_ref->fs, block_group, &bg_ref);
if (r != EOK)
return r;
struct ext4_bgroup *bg = bg_ref.block_group;
/* Compute indexes */
uint32_t bg_count = ext4_block_group_cnt(sb);
ext4_fsblk_t itab_first_block = ext4_bg_get_inode_table_first_block(bg, sb);
uint16_t itab_item_size = ext4_get16(sb, inode_size);
uint32_t itab_bytes;
/* Check for last block group */
if (block_group < bg_count - 1) {
itab_bytes = inodes_per_bg * itab_item_size;
} else {
/* Last block group could be smaller */
uint32_t inodes_cnt = ext4_get32(sb, inodes_count);
itab_bytes = (inodes_cnt - ((bg_count - 1) * inodes_per_bg));
itab_bytes *= itab_item_size;
}
ext4_fsblk_t inode_table_blocks = itab_bytes / block_size;
if (itab_bytes % block_size)
inode_table_blocks++;
*goal = itab_first_block + inode_table_blocks;
return ext4_fs_put_block_group_ref(&bg_ref);
}
static int ext4_fs_get_inode_dblk_idx_internal(struct ext4_inode_ref *inode_ref,
ext4_lblk_t iblock, ext4_fsblk_t *fblock,
bool extent_create,
bool support_unwritten __unused)
{
struct ext4_fs *fs = inode_ref->fs;
/* For empty file is situation simple */
if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) {
*fblock = 0;
return EOK;
}
ext4_fsblk_t current_block;
(void)extent_create;
#if CONFIG_EXTENT_ENABLE
/* Handle i-node using extents */
if ((ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) &&
(ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
ext4_fsblk_t current_fsblk;
int rc = ext4_extent_get_blocks(inode_ref, iblock, 1,
¤t_fsblk, extent_create, NULL);
if (rc != EOK)
return rc;
current_block = current_fsblk;
*fblock = current_block;
ext4_assert(*fblock || support_unwritten);
return EOK;
}
#endif
struct ext4_inode *inode = inode_ref->inode;
/* Direct block are read directly from array in i-node structure */
if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
current_block =
ext4_inode_get_direct_block(inode, (uint32_t)iblock);
*fblock = current_block;
return EOK;
}
/* Determine indirection level of the target block */
unsigned int l = 0;
unsigned int i;
for (i = 1; i < 4; i++) {
if (iblock < fs->inode_block_limits[i]) {
l = i;
break;
}
}
if (l == 0)
return EIO;
/* Compute offsets for the topmost level */
uint32_t blk_off_in_lvl = (uint32_t)(iblock - fs->inode_block_limits[l - 1]);
current_block = ext4_inode_get_indirect_block(inode, l - 1);
uint32_t off_in_blk = (uint32_t)(blk_off_in_lvl / fs->inode_blocks_per_level[l - 1]);
/* Sparse file */
if (current_block == 0) {
*fblock = 0;
return EOK;
}
struct ext4_block block;
/*
* Navigate through other levels, until we find the block number
* or find null reference meaning we are dealing with sparse file
*/
while (l > 0) {
/* Load indirect block */
int rc = ext4_trans_block_get(fs->bdev, &block, current_block);
if (rc != EOK)
return rc;
/* Read block address from indirect block */
current_block =
to_le32(((uint32_t *)block.data)[off_in_blk]);
/* Put back indirect block untouched */
rc = ext4_block_set(fs->bdev, &block);
if (rc != EOK)
return rc;
/* Check for sparse file */
if (current_block == 0) {
*fblock = 0;
return EOK;
}
/* Jump to the next level */
l--;
/* Termination condition - we have address of data block loaded
*/
if (l == 0)
break;
/* Visit the next level */
blk_off_in_lvl %= fs->inode_blocks_per_level[l];
off_in_blk = (uint32_t)(blk_off_in_lvl / fs->inode_blocks_per_level[l - 1]);
}
*fblock = current_block;
return EOK;
}
int ext4_fs_get_inode_dblk_idx(struct ext4_inode_ref *inode_ref,
ext4_lblk_t iblock, ext4_fsblk_t *fblock,
bool support_unwritten)
{
return ext4_fs_get_inode_dblk_idx_internal(inode_ref, iblock, fblock,
false, support_unwritten);
}
int ext4_fs_init_inode_dblk_idx(struct ext4_inode_ref *inode_ref,
ext4_lblk_t iblock, ext4_fsblk_t *fblock)
{
return ext4_fs_get_inode_dblk_idx_internal(inode_ref, iblock, fblock,
true, true);
}
static int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
ext4_lblk_t iblock, ext4_fsblk_t fblock)
{
struct ext4_fs *fs = inode_ref->fs;
#if CONFIG_EXTENT_ENABLE
/* Handle inode using extents */
if ((ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) &&
(ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
/* Not reachable */
return ENOTSUP;
}
#endif
/* Handle simple case when we are dealing with direct reference */
if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
ext4_inode_set_direct_block(inode_ref->inode, (uint32_t)iblock,
(uint32_t)fblock);
inode_ref->dirty = true;
return EOK;
}
/* Determine the indirection level needed to get the desired block */
unsigned int l = 0;
unsigned int i;
for (i = 1; i < 4; i++) {
if (iblock < fs->inode_block_limits[i]) {
l = i;
break;
}
}
if (l == 0)
return EIO;
uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
/* Compute offsets for the topmost level */
uint32_t blk_off_in_lvl = (uint32_t)(iblock - fs->inode_block_limits[l - 1]);
ext4_fsblk_t current_block =
ext4_inode_get_indirect_block(inode_ref->inode, l - 1);
uint32_t off_in_blk = (uint32_t)(blk_off_in_lvl / fs->inode_blocks_per_level[l - 1]);
ext4_fsblk_t new_blk;
struct ext4_block block;
struct ext4_block new_block;
/* Is needed to allocate indirect block on the i-node level */
if (current_block == 0) {
/* Allocate new indirect block */
ext4_fsblk_t goal;
int rc = ext4_fs_indirect_find_goal(inode_ref, &goal);
if (rc != EOK)
return rc;
rc = ext4_balloc_alloc_block(inode_ref, goal, &new_blk);
if (rc != EOK)
return rc;
/* Update i-node */
ext4_inode_set_indirect_block(inode_ref->inode, l - 1,
(uint32_t)new_blk);
inode_ref->dirty = true;
/* Load newly allocated block */
rc = ext4_trans_block_get_noread(fs->bdev, &new_block, new_blk);
if (rc != EOK) {
ext4_balloc_free_block(inode_ref, new_blk);
return rc;
}
/* Initialize new block */
memset(new_block.data, 0, block_size);
ext4_trans_set_block_dirty(new_block.buf);
/* Put back the allocated block */
rc = ext4_block_set(fs->bdev, &new_block);
if (rc != EOK)
return rc;
current_block = new_blk;
}
/*
* Navigate through other levels, until we find the block number
* or find null reference meaning we are dealing with sparse file
*/
while (l > 0) {
int rc = ext4_trans_block_get(fs->bdev, &block, current_block);
if (rc != EOK)
return rc;
current_block = to_le32(((uint32_t *)block.data)[off_in_blk]);
if ((l > 1) && (current_block == 0)) {
ext4_fsblk_t goal;
rc = ext4_fs_indirect_find_goal(inode_ref, &goal);
if (rc != EOK) {
ext4_block_set(fs->bdev, &block);
return rc;
}
/* Allocate new block */
rc =
ext4_balloc_alloc_block(inode_ref, goal, &new_blk);
if (rc != EOK) {
ext4_block_set(fs->bdev, &block);
return rc;
}
/* Load newly allocated block */
rc = ext4_trans_block_get_noread(fs->bdev, &new_block,
new_blk);
if (rc != EOK) {
ext4_block_set(fs->bdev, &block);
return rc;
}
/* Initialize allocated block */
memset(new_block.data, 0, block_size);
ext4_trans_set_block_dirty(new_block.buf);
rc = ext4_block_set(fs->bdev, &new_block);
if (rc != EOK) {
ext4_block_set(fs->bdev, &block);
return rc;
}
/* Write block address to the parent */
uint32_t * p = (uint32_t * )block.data;
p[off_in_blk] = to_le32((uint32_t)new_blk);
ext4_trans_set_block_dirty(block.buf);
current_block = new_blk;
}
/* Will be finished, write the fblock address */
if (l == 1) {
uint32_t * p = (uint32_t * )block.data;
p[off_in_blk] = to_le32((uint32_t)fblock);
ext4_trans_set_block_dirty(block.buf);
}
rc = ext4_block_set(fs->bdev, &block);
if (rc != EOK)
return rc;
l--;
/*
* If we are on the last level, break here as
* there is no next level to visit
*/
if (l == 0)
break;
/* Visit the next level */
blk_off_in_lvl %= fs->inode_blocks_per_level[l];
off_in_blk = (uint32_t)(blk_off_in_lvl / fs->inode_blocks_per_level[l - 1]);
}
return EOK;
}
int ext4_fs_append_inode_dblk(struct ext4_inode_ref *inode_ref,
ext4_fsblk_t *fblock, ext4_lblk_t *iblock)
{
#if CONFIG_EXTENT_ENABLE
/* Handle extents separately */
if ((ext4_sb_feature_incom(&inode_ref->fs->sb, EXT4_FINCOM_EXTENTS)) &&
(ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
int rc;
ext4_fsblk_t current_fsblk;
struct ext4_sblock *sb = &inode_ref->fs->sb;
uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
uint32_t block_size = ext4_sb_get_block_size(sb);
*iblock = (uint32_t)((inode_size + block_size - 1) / block_size);
rc = ext4_extent_get_blocks(inode_ref, *iblock, 1,
¤t_fsblk, true, NULL);
if (rc != EOK)
return rc;
*fblock = current_fsblk;
ext4_assert(*fblock);
ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
inode_ref->dirty = true;
return rc;
}
#endif
struct ext4_sblock *sb = &inode_ref->fs->sb;
/* Compute next block index and allocate data block */
uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
uint32_t block_size = ext4_sb_get_block_size(sb);
/* Align size i-node size */
if ((inode_size % block_size) != 0)
inode_size += block_size - (inode_size % block_size);
/* Logical blocks are numbered from 0 */
uint32_t new_block_idx = (uint32_t)(inode_size / block_size);
/* Allocate new physical block */
ext4_fsblk_t goal, phys_block;
int rc = ext4_fs_indirect_find_goal(inode_ref, &goal);
if (rc != EOK)
return rc;
rc = ext4_balloc_alloc_block(inode_ref, goal, &phys_block);
if (rc != EOK)
return rc;
/* Add physical block address to the i-node */
rc = ext4_fs_set_inode_data_block_index(inode_ref, new_block_idx,
phys_block);
if (rc != EOK) {
ext4_balloc_free_block(inode_ref, phys_block);
return rc;
}
/* Update i-node */
ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
inode_ref->dirty = true;
*fblock = phys_block;
*iblock = new_block_idx;
return EOK;
}
void ext4_fs_inode_links_count_inc(struct ext4_inode_ref *inode_ref)
{
uint16_t link;
bool is_dx;
link = ext4_inode_get_links_cnt(inode_ref->inode);
link++;
ext4_inode_set_links_cnt(inode_ref->inode, link);
is_dx = ext4_sb_feature_com(&inode_ref->fs->sb, EXT4_FCOM_DIR_INDEX) &&
ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_INDEX);
if (is_dx && link > 1) {
if (link >= EXT4_LINK_MAX || link == 2) {
ext4_inode_set_links_cnt(inode_ref->inode, 1);
uint32_t v;
v = ext4_get32(&inode_ref->fs->sb, features_read_only);
v |= EXT4_FRO_COM_DIR_NLINK;
ext4_set32(&inode_ref->fs->sb, features_read_only, v);
}
}
}
void ext4_fs_inode_links_count_dec(struct ext4_inode_ref *inode_ref)
{
uint16_t links = ext4_inode_get_links_cnt(inode_ref->inode);
if (!ext4_inode_is_type(&inode_ref->fs->sb, inode_ref->inode,
EXT4_INODE_MODE_DIRECTORY)) {
if (links > 0)
ext4_inode_set_links_cnt(inode_ref->inode, links - 1);
return;
}
if (links > 2)
ext4_inode_set_links_cnt(inode_ref->inode, links - 1);
}
/**
* @}
*/
|