Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 1 | -- Derived from GRUB -- GRand Unified Bootloader |
| 2 | -- Copyright (C) 1999, 2001, 2003 Free Software Foundation, Inc. |
| 3 | -- Copyright (C) 2023 secunet Security Networks AG |
| 4 | -- |
| 5 | -- This program is free software; you can redistribute it and/or modify |
| 6 | -- it under the terms of the GNU General Public License as published by |
| 7 | -- the Free Software Foundation; either version 2 of the License, or |
| 8 | -- (at your option) any later version. |
| 9 | |
Nico Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 10 | with Ada.Unchecked_Conversion; |
Nico Huber | 8ec45a1 | 2023-12-04 17:11:08 +0100 | [diff] [blame] | 11 | with System; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 12 | with Interfaces; |
Nico Huber | 8ec45a1 | 2023-12-04 17:11:08 +0100 | [diff] [blame] | 13 | with Interfaces.C; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 14 | |
Thomas Heijligen | 5c43abc | 2023-12-11 15:24:36 +0000 | [diff] [blame] | 15 | with FILO.Blockdev; |
| 16 | with FILO.FS.VFS; |
Nico Huber | 8ec45a1 | 2023-12-04 17:11:08 +0100 | [diff] [blame] | 17 | |
| 18 | use Interfaces.C; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 19 | |
Thomas Heijligen | 5c43abc | 2023-12-11 15:24:36 +0000 | [diff] [blame] | 20 | package body FILO.FS.Ext2 is |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 21 | |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 22 | function Is_Mounted (State : T) return Boolean is (State.S >= Mounted); |
| 23 | function Is_Open (State : T) return Boolean is (State.S = File_Opened); |
| 24 | |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 25 | -------------------------------------------------------------------------- |
| 26 | |
| 27 | SUPERBLOCK_SIZE : constant := 1024; |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 28 | SUPERBLOCK_MAGIC : constant := 16#ef53#; |
| 29 | OLD_REV : constant := 0; |
| 30 | DYNAMIC_REV : constant := 1; |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame] | 31 | |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 32 | FEATURE_INCOMPAT_EXTENTS : constant := 16#0040#; |
| 33 | FEATURE_INCOMPAT_64BIT : constant := 16#0080#; |
| 34 | |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 35 | EXT4_EXTENTS_FL : constant := 16#8_0000#; |
| 36 | |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 37 | procedure Mount |
| 38 | (State : in out T; |
| 39 | Part_Len : in Partition_Length; |
| 40 | Success : out Boolean) |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 41 | is |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 42 | Static : Mount_State renames State.Static; |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 43 | Super_Block : Buffer_Type (0 .. SUPERBLOCK_SIZE - 1) := (others => 0); |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 44 | begin |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 45 | if Part_Len < 2 * SUPERBLOCK_SIZE then |
| 46 | Success := False; |
| 47 | return; |
| 48 | end if; |
| 49 | |
Thomas Heijligen | 5c43abc | 2023-12-11 15:24:36 +0000 | [diff] [blame] | 50 | Blockdev.Read (Super_Block, 1 * SUPERBLOCK_SIZE, Success); |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 51 | if not Success then |
| 52 | return; |
| 53 | end if; |
| 54 | |
| 55 | if Read_LE16 (Super_Block, 14 * 4) /= 16#ef53# then |
| 56 | Success := False; |
| 57 | return; |
| 58 | end if; |
| 59 | |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 60 | Static.Part_Len := Part_Len; |
Nico Huber | 39f086c | 2024-01-09 17:43:06 +0100 | [diff] [blame^] | 61 | Static.First_Data_Block := Read_LE32 (Super_Block, 5 * 4); |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 62 | |
| 63 | declare |
| 64 | S_Log_Block_Size : constant Unsigned_32 := Read_LE32 (Super_Block, 6 * 4); |
| 65 | begin |
| 66 | if S_Log_Block_Size <= Unsigned_32 (Log_Block_Size'Last - 10) then |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 67 | Static.Block_Size_Bits := Log_Block_Size (S_Log_Block_Size + 10); |
| 68 | Static.Block_Size := 2 ** Log_Block_Size (S_Log_Block_Size + 10); |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 69 | else |
| 70 | Success := False; |
| 71 | return; |
| 72 | end if; |
| 73 | end; |
Nico Huber | ecafb8f | 2024-01-09 15:45:49 +0100 | [diff] [blame] | 74 | pragma Assert_And_Cut (Success); |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 75 | |
| 76 | declare |
| 77 | S_Inodes_Per_Group : constant Unsigned_32 := Read_LE32 (Super_Block, 10 * 4); |
| 78 | begin |
Nico Huber | 21d4202 | 2023-12-16 02:50:22 +0100 | [diff] [blame] | 79 | if S_Inodes_Per_Group in 2 .. Unsigned_32'Last then |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 80 | Static.Inodes_Per_Group := Inode_Index (S_Inodes_Per_Group); |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 81 | else |
| 82 | Success := False; |
| 83 | return; |
| 84 | end if; |
| 85 | end; |
Nico Huber | ecafb8f | 2024-01-09 15:45:49 +0100 | [diff] [blame] | 86 | pragma Assert_And_Cut (Success); |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 87 | |
| 88 | declare |
| 89 | S_Rev_Level : constant Unsigned_32 := Read_LE32 (Super_Block, 19 * 4); |
| 90 | begin |
| 91 | if S_Rev_Level >= DYNAMIC_REV then |
| 92 | declare |
| 93 | S_Inode_Size : constant Unsigned_16 := Read_LE16 (Super_Block, 22 * 4); |
| 94 | begin |
Nico Huber | ecafb8f | 2024-01-09 15:45:49 +0100 | [diff] [blame] | 95 | if Natural (S_Inode_Size) in Inode_Size and then |
| 96 | Natural (S_Inode_Size) <= Static.Block_Size |
| 97 | then |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 98 | Static.Inode_Size := Inode_Size (S_Inode_Size); |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 99 | else |
| 100 | Success := False; |
| 101 | return; |
| 102 | end if; |
Nico Huber | ecafb8f | 2024-01-09 15:45:49 +0100 | [diff] [blame] | 103 | pragma Assert (Static.Block_Size / Static.Inode_Size <= Natural (Inode_In_Block_Count'Last)); |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 104 | end; |
| 105 | else |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 106 | Static.Inode_Size := Inode_Size'First; |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 107 | end if; |
Nico Huber | ecafb8f | 2024-01-09 15:45:49 +0100 | [diff] [blame] | 108 | Static.Inodes_Per_Block := Inode_In_Block_Count (Static.Block_Size / Static.Inode_Size); |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 109 | end; |
Nico Huber | ecafb8f | 2024-01-09 15:45:49 +0100 | [diff] [blame] | 110 | pragma Assert_And_Cut (Success); |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 111 | |
| 112 | declare |
| 113 | S_Feature_Incompat : constant Unsigned_32 := Read_LE32 (Super_Block, 24 * 4); |
| 114 | begin |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 115 | Static.Feature_Extents := (S_Feature_Incompat and FEATURE_INCOMPAT_EXTENTS) /= 0; |
| 116 | Static.Feature_64Bit := (S_Feature_Incompat and FEATURE_INCOMPAT_64BIT) /= 0; |
| 117 | if Static.Feature_64Bit then |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 118 | declare |
| 119 | S_Desc_Size : constant Unsigned_16 := Read_LE16 (Super_Block, 63 * 4 + 2); |
| 120 | begin |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame] | 121 | if Natural (S_Desc_Size) in Desc_Size and |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 122 | Natural (S_Desc_Size) <= Static.Block_Size and |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame] | 123 | Is_Power_Of_2 (S_Desc_Size) |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 124 | then |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 125 | Static.Desc_Size := Desc_Size (S_Desc_Size); |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 126 | else |
| 127 | Success := False; |
| 128 | return; |
| 129 | end if; |
| 130 | end; |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 131 | Static.Feature_64Bit := Static.Feature_64Bit and Static.Desc_Size >= 64; |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 132 | else |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 133 | Static.Desc_Size := Desc_Size'First; |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 134 | end if; |
Nico Huber | ecafb8f | 2024-01-09 15:45:49 +0100 | [diff] [blame] | 135 | Static.Desc_Per_Block := Group_Index (Static.Block_Size / Static.Desc_Size); |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 136 | end; |
Nico Huber | ecafb8f | 2024-01-09 15:45:49 +0100 | [diff] [blame] | 137 | pragma Assert_And_Cut (Success); |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 138 | |
| 139 | State.S := Mounted; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 140 | end Mount; |
| 141 | |
Nico Huber | f5d99d0 | 2023-12-12 13:42:55 +0100 | [diff] [blame] | 142 | procedure Read_FSBlock |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame] | 143 | (Buf : in out Buffer_Type; |
Nico Huber | f5d99d0 | 2023-12-12 13:42:55 +0100 | [diff] [blame] | 144 | FSBlock : in FSBlock_Offset; |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame] | 145 | Part_Len : in Partition_Length; |
Nico Huber | f5d99d0 | 2023-12-12 13:42:55 +0100 | [diff] [blame] | 146 | Success : out Boolean) |
| 147 | with |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame] | 148 | Pre => Buf'Length in Block_Size |
Nico Huber | f5d99d0 | 2023-12-12 13:42:55 +0100 | [diff] [blame] | 149 | is |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame] | 150 | FSBlock_64 : constant Integer_64 := Integer_64 (FSBlock); |
| 151 | Block_Size : constant Integer_64 := Integer_64 (Buf'Length); |
| 152 | Max_Block_Offset : constant Integer_64 := Integer_64 (Part_Len) / Block_Size - 1; |
Nico Huber | f5d99d0 | 2023-12-12 13:42:55 +0100 | [diff] [blame] | 153 | begin |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame] | 154 | if FSBlock_64 > Max_Block_Offset then |
Nico Huber | f5d99d0 | 2023-12-12 13:42:55 +0100 | [diff] [blame] | 155 | Success := False; |
| 156 | return; |
| 157 | end if; |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame] | 158 | Blockdev.Read (Buf, Blockdev_Length (FSBlock_64 * Block_Size), Success); |
Nico Huber | f5d99d0 | 2023-12-12 13:42:55 +0100 | [diff] [blame] | 159 | end Read_FSBlock; |
| 160 | |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 161 | procedure Cache_FSBlock |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 162 | (Static : in Mount_State; |
| 163 | Cache : in out Block_Cache; |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 164 | Phys : in FSBlock_Offset; |
| 165 | Level : in Block_Cache_Index; |
Nico Huber | 57dfbfb | 2023-12-13 23:24:16 +0100 | [diff] [blame] | 166 | Label : in Cache_Label; |
| 167 | Logical : in Boolean := True; |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 168 | Cache_Start : out Max_Block_Index; |
| 169 | Cache_End : out Max_Block_Index; |
| 170 | Success : out Boolean) |
| 171 | with |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 172 | Post => Cache_End = Cache_Start + Static.Block_Size - 1 |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 173 | is |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 174 | -- Limit cache usage depending on block size: |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 175 | Max_Level : constant Block_Cache_Index := Block_Size'Last / Static.Block_Size - 1; |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame] | 176 | Cache_Level : constant Block_Cache_Index := Block_Cache_Index'Min (Level, Max_Level); |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 177 | begin |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 178 | Cache_Start := Cache_Level * Static.Block_Size; |
| 179 | Cache_End := Cache_Start + Static.Block_Size - 1; |
| 180 | if Cache.Logical (Cache_Level) = Logical and |
| 181 | Cache.Label (Cache_Level) = Label |
Nico Huber | 57dfbfb | 2023-12-13 23:24:16 +0100 | [diff] [blame] | 182 | then |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 183 | Success := True; |
| 184 | else |
| 185 | Read_FSBlock |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 186 | (Buf => Cache.Buffer (Cache_Start .. Cache_End), |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 187 | FSBlock => Phys, |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 188 | Part_Len => Static.Part_Len, |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 189 | Success => Success); |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 190 | Cache.Logical (Cache_Level) := Logical; -- FIXME: Level needs to be part of Label |
| 191 | Cache.Label (Cache_Level) := Label; |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 192 | end if; |
| 193 | end Cache_FSBlock; |
| 194 | |
Nico Huber | 57dfbfb | 2023-12-13 23:24:16 +0100 | [diff] [blame] | 195 | procedure Cache_FSBlock |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 196 | (Static : in Mount_State; |
| 197 | Cache : in out Block_Cache; |
Nico Huber | 57dfbfb | 2023-12-13 23:24:16 +0100 | [diff] [blame] | 198 | Phys : in FSBlock_Offset; |
| 199 | Level : in Block_Cache_Index; |
| 200 | Cache_Start : out Max_Block_Index; |
| 201 | Cache_End : out Max_Block_Index; |
| 202 | Success : out Boolean) |
| 203 | with |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 204 | Post => Cache_End = Cache_Start + Static.Block_Size - 1 |
Nico Huber | 57dfbfb | 2023-12-13 23:24:16 +0100 | [diff] [blame] | 205 | is |
| 206 | begin |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 207 | Cache_FSBlock (Static, Cache, Phys, Level, Cache_Label (Phys), |
Nico Huber | 57dfbfb | 2023-12-13 23:24:16 +0100 | [diff] [blame] | 208 | False, Cache_Start, Cache_End, Success); |
| 209 | end Cache_FSBlock; |
| 210 | |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 211 | procedure Reset_Cache_Logical (Cache : in out Block_Cache) is |
Nico Huber | 57dfbfb | 2023-12-13 23:24:16 +0100 | [diff] [blame] | 212 | begin |
| 213 | for I in Block_Cache_Index loop |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 214 | if Cache.Logical (I) then |
| 215 | Cache.Logical (I) := False; |
| 216 | Cache.Label (I) := 0; |
Nico Huber | 57dfbfb | 2023-12-13 23:24:16 +0100 | [diff] [blame] | 217 | end if; |
| 218 | end loop; |
| 219 | end Reset_Cache_Logical; |
| 220 | |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 221 | procedure Ext2_Block_Map |
| 222 | (State : in out T; |
| 223 | Logical : in FSBlock_Logical; |
| 224 | Physical : out FSBlock_Offset; |
| 225 | Success : out Boolean) |
| 226 | is |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 227 | Static : constant Mount_State := State.Static; |
| 228 | |
Nico Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 229 | Direct_Blocks : constant := 12; |
| 230 | type Direct_Blocks_Array is array (Natural range 0 .. Direct_Blocks - 1) of Unsigned_32; |
| 231 | type Inode_Blocks is record |
| 232 | Direct_Blocks : Direct_Blocks_Array; |
| 233 | Indirect_Block : Unsigned_32; |
| 234 | Double_Indirect : Unsigned_32; |
| 235 | Triple_Indirect : Unsigned_32; |
| 236 | end record |
Nico Huber | 5a042fd | 2024-01-08 15:54:57 +0100 | [diff] [blame] | 237 | with Object_Size => Inode_Extents'Length * 8; |
Nico Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 238 | |
| 239 | function I_Blocks is new Ada.Unchecked_Conversion (Inode_Extents, Inode_Blocks); |
Nico Huber | 7403a54 | 2023-12-15 23:13:47 +0100 | [diff] [blame] | 240 | function I_Blocks (State : T) return Inode_Blocks is (I_Blocks (State.Inode.Inline)); |
Nico Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 241 | |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 242 | Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (Static.Block_Size / 4); |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame] | 243 | Max_Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (Block_Size'Last / 4); |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 244 | type Addr_In_Block_Range is range 0 .. Max_Addr_Per_Block - 1; |
| 245 | |
| 246 | procedure Indirect_Block_Lookup |
| 247 | (Indirect_Block_Phys : in FSBlock_Offset; |
| 248 | Addr_In_Block : in Addr_In_Block_Range; |
| 249 | Level : in Block_Cache_Index; |
| 250 | Logical_Off : in FSBlock_Logical; |
| 251 | Next_Physical : out FSBlock_Offset; |
| 252 | Success : out Boolean) |
| 253 | with |
Nico Huber | 5a042fd | 2024-01-08 15:54:57 +0100 | [diff] [blame] | 254 | Pre => |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 255 | Addr_Per_Block = FSBlock_Logical (Static.Block_Size / 4) and |
Nico Huber | 5a042fd | 2024-01-08 15:54:57 +0100 | [diff] [blame] | 256 | FSBlock_Logical (Addr_In_Block) < Addr_Per_Block |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 257 | is |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 258 | Cache_Start, Cache_End : Max_Block_Index; |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 259 | begin |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 260 | Cache_FSBlock |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 261 | (Static => Static, |
| 262 | Cache => State.Cache, |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 263 | Phys => Indirect_Block_Phys, |
| 264 | Level => Level, |
Nico Huber | 57dfbfb | 2023-12-13 23:24:16 +0100 | [diff] [blame] | 265 | Label => Cache_Label (Logical_Off), |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 266 | Cache_Start => Cache_Start, |
| 267 | Cache_End => Cache_End, |
| 268 | Success => Success); |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 269 | Next_Physical := FSBlock_Offset (Read_LE32 |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 270 | (Buf => State.Cache.Buffer (Cache_Start .. Cache_End), |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 271 | Off => Natural (Addr_In_Block) * 4)); |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 272 | end Indirect_Block_Lookup; |
| 273 | |
| 274 | Logical_Rest : FSBlock_Logical := Logical; |
Nico Huber | 5a042fd | 2024-01-08 15:54:57 +0100 | [diff] [blame] | 275 | |
| 276 | pragma Assert (Addr_Per_Block <= 2 ** 14); |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 277 | begin |
| 278 | if Logical_Rest < Direct_Blocks then |
Nico Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 279 | Physical := FSBlock_Offset (I_Blocks (State).Direct_Blocks (Natural (Logical))); |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 280 | Success := True; |
| 281 | return; |
| 282 | end if; |
| 283 | |
| 284 | Logical_Rest := Logical_Rest - Direct_Blocks; |
| 285 | if Logical_Rest < Addr_Per_Block then |
| 286 | Indirect_Block_Lookup |
Nico Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 287 | (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Indirect_Block), |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 288 | Addr_In_Block => Addr_In_Block_Range (Logical_Rest), |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame] | 289 | Level => 0, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 290 | Logical_Off => Logical - Logical_Rest, |
| 291 | Next_Physical => Physical, |
| 292 | Success => Success); |
| 293 | return; |
| 294 | end if; |
| 295 | |
| 296 | Logical_Rest := Logical_Rest - Addr_Per_Block; |
| 297 | if Logical_Rest < Addr_Per_Block ** 2 then |
| 298 | Indirect_Block_Lookup |
Nico Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 299 | (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Double_Indirect), |
Nico Huber | 5a042fd | 2024-01-08 15:54:57 +0100 | [diff] [blame] | 300 | Addr_In_Block => Addr_In_Block_Range ((Logical_Rest / Addr_Per_Block) mod Addr_Per_Block), |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame] | 301 | Level => 1, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 302 | Logical_Off => Logical - Logical_Rest, |
| 303 | Next_Physical => Physical, |
| 304 | Success => Success); |
| 305 | if not Success then |
| 306 | return; |
| 307 | end if; |
| 308 | |
| 309 | Indirect_Block_Lookup |
| 310 | (Indirect_Block_Phys => Physical, |
| 311 | Addr_In_Block => Addr_In_Block_Range (Logical_Rest mod Addr_Per_Block), |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame] | 312 | Level => 0, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 313 | Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block), |
| 314 | Next_Physical => Physical, |
| 315 | Success => Success); |
| 316 | return; |
| 317 | end if; |
| 318 | |
| 319 | Logical_Rest := Logical_Rest - Addr_Per_Block ** 2; |
| 320 | if Logical_Rest < Addr_Per_Block ** 3 then |
| 321 | Indirect_Block_Lookup |
Nico Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 322 | (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Triple_Indirect), |
Nico Huber | 5a042fd | 2024-01-08 15:54:57 +0100 | [diff] [blame] | 323 | Addr_In_Block => Addr_In_Block_Range ((Logical_Rest / Addr_Per_Block ** 2) mod Addr_Per_Block), |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame] | 324 | Level => 2, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 325 | Logical_Off => Logical - Logical_Rest, |
| 326 | Next_Physical => Physical, |
| 327 | Success => Success); |
| 328 | if not Success then |
| 329 | return; |
| 330 | end if; |
| 331 | |
| 332 | Indirect_Block_Lookup |
| 333 | (Indirect_Block_Phys => Physical, |
Nico Huber | 5a042fd | 2024-01-08 15:54:57 +0100 | [diff] [blame] | 334 | Addr_In_Block => Addr_In_Block_Range ((Logical_Rest / Addr_Per_Block) mod Addr_Per_Block), |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame] | 335 | Level => 1, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 336 | Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block ** 2), |
| 337 | Next_Physical => Physical, |
| 338 | Success => Success); |
| 339 | if not Success then |
| 340 | return; |
| 341 | end if; |
| 342 | |
| 343 | Indirect_Block_Lookup |
| 344 | (Indirect_Block_Phys => Physical, |
| 345 | Addr_In_Block => Addr_In_Block_Range (Logical_Rest mod Addr_Per_Block), |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame] | 346 | Level => 0, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 347 | Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block), |
| 348 | Next_Physical => Physical, |
| 349 | Success => Success); |
| 350 | return; |
| 351 | end if; |
| 352 | |
| 353 | -- Logical address was just too high. |
Nico Huber | 5a042fd | 2024-01-08 15:54:57 +0100 | [diff] [blame] | 354 | Physical := 0; |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 355 | Success := False; |
| 356 | end Ext2_Block_Map; |
| 357 | |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 358 | procedure Extent_Block_Map |
| 359 | (State : in out T; |
| 360 | Logical : in FSBlock_Logical; |
| 361 | Physical : out FSBlock_Offset; |
| 362 | Success : out Boolean) |
| 363 | is |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 364 | Cache : Cache_Buffer renames State.Cache.Buffer; |
| 365 | |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 366 | -- Extent blocks always start with a 12B header and contain 12B entries. |
| 367 | -- Every entry starts with the number of the first logical block it |
| 368 | -- covers. Entries are sorted by this number. |
| 369 | -- Depth > 0 blocks have index entries, referencing further extent blocks. |
| 370 | -- Depth = 0 blocks have extent entries, referencing a contiguous range |
| 371 | -- of data blocks. |
| 372 | -- |
| 373 | -- +-----------------+ |
| 374 | -- .-> | Hdr depth=0 | |
| 375 | -- | +-----------------+ |
| 376 | -- | | Extent 0.. 12 | |
| 377 | -- +-------------+ | +-----------------+ |
| 378 | -- | Hdr depth=1 | | | Extent 13.. 13 | |
| 379 | -- +-------------+ | +-----------------+ |
| 380 | -- | Index 0 | --' | Extent 14..122 | |
| 381 | -- +-------------+ +-----------------+ |
| 382 | -- | Index 123 | --. |
| 383 | -- +-------------+ | +-----------------+ |
| 384 | -- `-> | Hdr depth=0 | |
| 385 | -- +-----------------+ |
| 386 | -- | Extent 123..125 | |
| 387 | -- +-----------------+ |
| 388 | -- | Extent 126..234 | |
| 389 | -- +-----------------+ |
| 390 | -- |
| 391 | |
| 392 | Extent_Header_Size : constant := 12; |
| 393 | Extent_Header_Magic : constant := 16#f03a#; |
| 394 | subtype Extent_Off is Natural range 0 .. Extent_Header_Size; |
| 395 | subtype Extent_Idx is Natural range 1 .. (Max_Block_Index'Last + 1) / Extent_Header_Size - 1; |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 396 | Dynamic_Max_Index : constant Extent_Idx := State.Static.Block_Size / Extent_Header_Size - 1; |
Nico Huber | 96a0b0e | 2024-01-08 15:57:09 +0100 | [diff] [blame] | 397 | subtype Extent_Depth is Natural range 0 .. 32; |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 398 | |
| 399 | function Extent_Byte_Offset (Idx : Extent_Idx; Off : Extent_Off) return Natural |
| 400 | is |
| 401 | (Idx * Extent_Header_Size + Off); |
| 402 | |
| 403 | function Header_Magic (Buf : Buffer_Type) return Unsigned_16 |
| 404 | is |
| 405 | (Read_LE16 (Buf, 0)) |
| 406 | with |
| 407 | Pre => Buf'Length >= 2; |
| 408 | |
| 409 | function Header_Entries (Buf : Buffer_Type) return Natural |
| 410 | is |
| 411 | (Natural (Read_LE16 (Buf, 2))) |
| 412 | with |
| 413 | Pre => Buf'Length >= 4; |
| 414 | |
| 415 | function Header_Depth (Buf : Buffer_Type) return Natural |
| 416 | is |
| 417 | (Natural (Read_LE16 (Buf, 6))) |
| 418 | with |
| 419 | Pre => Buf'Length >= 8; |
| 420 | |
| 421 | function Index_Logical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical |
| 422 | is |
| 423 | (FSBlock_Logical (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 0)))) |
| 424 | with |
| 425 | Pre => Buf'Length >= Extent_Byte_Offset (Idx, 4); |
| 426 | |
| 427 | function Index_Physical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Offset |
| 428 | is |
| 429 | (FSBlock_Offset |
| 430 | (Shift_Left (Unsigned_64 (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 8))), 32) or |
| 431 | Unsigned_64 (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 4))))) |
| 432 | with |
| 433 | Pre => Buf'Length >= Extent_Byte_Offset (Idx, 10); |
| 434 | |
| 435 | function Extent_Logical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical |
| 436 | renames Index_Logical; |
| 437 | |
| 438 | function Extent_Length (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical |
| 439 | is |
| 440 | (FSBlock_Logical (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 4)))) |
| 441 | with |
| 442 | Pre => Buf'Length >= Extent_Byte_Offset (Idx, 6); |
| 443 | |
| 444 | function Extent_Physical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Offset |
| 445 | is |
| 446 | (FSBlock_Offset |
| 447 | (Shift_Left (Unsigned_64 (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 6))), 32) or |
| 448 | Unsigned_64 (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 8))))) |
| 449 | with |
| 450 | Pre => Buf'Length >= Extent_Byte_Offset (Idx, 12); |
| 451 | |
| 452 | function Bin_Search (Buf : Buffer_Type; Refs : Extent_Idx) return Extent_Idx |
| 453 | with |
Nico Huber | 6710c0e | 2024-01-08 15:56:24 +0100 | [diff] [blame] | 454 | Pre => |
| 455 | Buf'Length in 2 * Extent_Header_Size .. Max_Block_Index'Last and then |
| 456 | Refs in 1 .. Extent_Idx (Buf'Length / Extent_Header_Size - 1) and then |
| 457 | Extent_Logical (Buf, 1) <= Logical, |
| 458 | Post => |
| 459 | Bin_Search'Result in 1 .. Refs and |
| 460 | Extent_Logical (Buf, Bin_Search'Result) <= Logical |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 461 | is |
Nico Huber | 6710c0e | 2024-01-08 15:56:24 +0100 | [diff] [blame] | 462 | Left : Positive := 2; |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 463 | Right : Extent_Idx := Refs; |
| 464 | begin |
| 465 | while Left <= Right loop |
| 466 | declare |
| 467 | Mid : constant Extent_Idx := (Left + Right) / 2; |
| 468 | Ext_Logical : constant FSBlock_Logical := Extent_Logical (Buf, Mid); |
| 469 | begin |
| 470 | if Logical < Ext_Logical then |
| 471 | Right := Mid - 1; |
| 472 | else |
| 473 | Left := Mid + 1; |
| 474 | end if; |
Nico Huber | 6710c0e | 2024-01-08 15:56:24 +0100 | [diff] [blame] | 475 | pragma Loop_Invariant |
| 476 | (Right <= Refs and then |
| 477 | Left in 2 .. Refs + 1 and then |
| 478 | Extent_Logical (Buf, Left - 1) <= Logical); |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 479 | end; |
| 480 | end loop; |
| 481 | return Left - 1; |
| 482 | end Bin_Search; |
| 483 | |
| 484 | procedure Next_Ref |
| 485 | (Current : in FSBlock_Offset; |
| 486 | Logical_Off : in FSBlock_Logical; |
Nico Huber | 96a0b0e | 2024-01-08 15:57:09 +0100 | [diff] [blame] | 487 | Depth : in Extent_Depth; |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 488 | Next : out Extent_Idx; |
| 489 | Cache_Start : out Max_Block_Index; |
| 490 | Cache_End : out Max_Block_Index; |
| 491 | Success : out Boolean) |
| 492 | with |
Nico Huber | 1db9193 | 2024-01-09 16:58:26 +0100 | [diff] [blame] | 493 | Pre => |
| 494 | Logical_Off <= Logical and |
| 495 | Dynamic_Max_Index = State.Static.Block_Size / Extent_Header_Size - 1, |
| 496 | Post => |
| 497 | State.Static = State.Static'Old and |
| 498 | (if Success then |
| 499 | Next <= Dynamic_Max_Index and then |
| 500 | Cache_End = Cache_Start + State.Static.Block_Size - 1 and then |
| 501 | Extent_Logical (Cache (Cache_Start .. Cache_End), Next) <= Logical) |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 502 | is |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 503 | begin |
| 504 | Cache_FSBlock |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 505 | (Static => State.Static, |
| 506 | Cache => State.Cache, |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 507 | Phys => Current, |
| 508 | Level => Depth, |
Nico Huber | 57dfbfb | 2023-12-13 23:24:16 +0100 | [diff] [blame] | 509 | Label => Cache_Label (Logical_Off), |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 510 | Cache_Start => Cache_Start, |
| 511 | Cache_End => Cache_End, |
| 512 | Success => Success); |
| 513 | if not Success then |
| 514 | Next := 1; |
| 515 | return; |
| 516 | end if; |
| 517 | |
| 518 | declare |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 519 | Hdr_Magic : constant Unsigned_16 := Header_Magic (Cache (Cache_Start .. Cache_End)); |
| 520 | Hdr_Entries : constant Natural := Header_Entries (Cache (Cache_Start .. Cache_End)); |
| 521 | Hdr_Depth : constant Natural := Header_Depth (Cache (Cache_Start .. Cache_End)); |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 522 | First_Logical : constant FSBlock_Logical := |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 523 | Extent_Logical (Cache (Cache_Start .. Cache_End), 1); |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 524 | begin |
| 525 | Success := Success and then |
| 526 | Hdr_Magic = Extent_Header_Magic and then |
| 527 | Hdr_Depth = Depth and then |
Nico Huber | 96a0b0e | 2024-01-08 15:57:09 +0100 | [diff] [blame] | 528 | Hdr_Entries in Extent_Idx and then |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 529 | Hdr_Entries <= Dynamic_Max_Index and then |
| 530 | First_Logical = Logical_Off; |
| 531 | if not Success then |
| 532 | Next := 1; |
| 533 | else |
Nico Huber | 1db9193 | 2024-01-09 16:58:26 +0100 | [diff] [blame] | 534 | pragma Assert (Cache_End - Cache_Start + 1 = State.Static.Block_Size); |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 535 | Next := Bin_Search (Cache (Cache_Start .. Cache_End), Hdr_Entries); |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 536 | end if; |
| 537 | end; |
| 538 | end Next_Ref; |
| 539 | |
Nico Huber | 7403a54 | 2023-12-15 23:13:47 +0100 | [diff] [blame] | 540 | Inline_Extents : Ext2.Inode_Extents renames State.Inode.Inline; |
| 541 | Inode_Magic : constant Unsigned_16 := Header_Magic (Inline_Extents); |
| 542 | Inode_Entries : constant Natural := Header_Entries (Inline_Extents); |
| 543 | First_Logical : constant FSBlock_Logical := Extent_Logical (Inline_Extents, 1); |
| 544 | Depth : Natural := Header_Depth (Inline_Extents); |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 545 | |
| 546 | Cache_Start, Cache_End : Max_Block_Index; |
| 547 | Logical_Off, Length : FSBlock_Logical; |
| 548 | Idx : Extent_Idx; |
| 549 | begin |
| 550 | Success := |
| 551 | Inode_Magic = Extent_Header_Magic and then |
| 552 | Inode_Entries > 0 and then |
Nico Huber | 7403a54 | 2023-12-15 23:13:47 +0100 | [diff] [blame] | 553 | Inode_Entries < Inline_Extents'Length / Extent_Header_Size and then |
Nico Huber | 96a0b0e | 2024-01-08 15:57:09 +0100 | [diff] [blame] | 554 | First_Logical <= Logical and then |
| 555 | Depth in Extent_Depth; |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 556 | if not Success then |
| 557 | Physical := 0; |
| 558 | return; |
| 559 | end if; |
| 560 | |
Nico Huber | 7403a54 | 2023-12-15 23:13:47 +0100 | [diff] [blame] | 561 | Idx := Bin_Search (Inline_Extents, Inode_Entries); |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 562 | if Depth = 0 then |
Nico Huber | 7403a54 | 2023-12-15 23:13:47 +0100 | [diff] [blame] | 563 | Physical := Extent_Physical (Inline_Extents, Idx); |
| 564 | Logical_Off := Extent_Logical (Inline_Extents, Idx); |
| 565 | Length := Extent_Length (Inline_Extents, Idx); |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 566 | else |
Nico Huber | 7403a54 | 2023-12-15 23:13:47 +0100 | [diff] [blame] | 567 | Physical := Index_Physical (Inline_Extents, Idx); |
| 568 | Logical_Off := Index_Logical (Inline_Extents, Idx); |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 569 | loop |
Nico Huber | 96a0b0e | 2024-01-08 15:57:09 +0100 | [diff] [blame] | 570 | pragma Loop_Invariant |
Nico Huber | 1db9193 | 2024-01-09 16:58:26 +0100 | [diff] [blame] | 571 | (State.Static = State.Static'Loop_Entry and then |
| 572 | Depth > 0 and then |
Nico Huber | 96a0b0e | 2024-01-08 15:57:09 +0100 | [diff] [blame] | 573 | Depth in Extent_Depth and then |
| 574 | Logical_Off <= Logical); |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 575 | Depth := Depth - 1; |
| 576 | Next_Ref |
| 577 | (Current => Physical, |
| 578 | Logical_Off => Logical_Off, |
| 579 | Depth => Depth, |
| 580 | Next => Idx, |
| 581 | Cache_Start => Cache_Start, |
| 582 | Cache_End => Cache_End, |
| 583 | Success => Success); |
| 584 | if not Success then |
| 585 | return; |
| 586 | end if; |
| 587 | |
| 588 | exit when Depth = 0; |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 589 | Physical := Index_Physical (Cache (Cache_Start .. Cache_End), Idx); |
| 590 | Logical_Off := Index_Logical (Cache (Cache_Start .. Cache_End), Idx); |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 591 | end loop; |
| 592 | |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 593 | Physical := Extent_Physical (Cache (Cache_Start .. Cache_End), Idx); |
| 594 | Logical_Off := Extent_Logical (Cache (Cache_Start .. Cache_End), Idx); |
| 595 | Length := Extent_Length (Cache (Cache_Start .. Cache_End), Idx); |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 596 | end if; |
| 597 | |
| 598 | Success := |
| 599 | Length > 0 and then |
Nico Huber | c4c7a5e | 2023-12-14 00:08:56 +0100 | [diff] [blame] | 600 | Logical_Off <= FSBlock_Logical'Last - Length and then |
Nico Huber | 1db9193 | 2024-01-09 16:58:26 +0100 | [diff] [blame] | 601 | Logical < Logical_Off + Length and then |
| 602 | FSBlock_Offset (Logical - Logical_Off) <= FSBlock_Offset'Last - Physical; |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 603 | if Success then |
| 604 | Physical := Physical + FSBlock_Offset (Logical - Logical_Off); |
| 605 | end if; |
| 606 | end Extent_Block_Map; |
| 607 | |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 608 | procedure Open |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 609 | (State : in out T; |
| 610 | Inode : in Inode_Index; |
| 611 | Success : out Boolean) |
| 612 | with |
| 613 | Pre => Is_Mounted (State) and not Is_Open (State), |
| 614 | Post => Success = Is_Open (State) |
| 615 | is |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 616 | Static : Mount_State renames State.Static; |
| 617 | Cache : Cache_Buffer renames State.Cache.Buffer; |
| 618 | |
Nico Huber | 022e226 | 2023-12-15 23:15:17 +0100 | [diff] [blame] | 619 | ------------------------ |
| 620 | -- Group deserialization |
| 621 | |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 622 | subtype Group_Off is Natural range 0 .. Desc_Size'Last; |
| 623 | function Group_Byte_Offset (Idx : Group_Index; Off : Group_Off) return Natural |
| 624 | is |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 625 | (Natural (Idx) * Static.Desc_Size + Off); |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 626 | |
| 627 | function Group_Inode_Table (Buf : Buffer_Type; Idx : Group_Index) return FSBlock_Offset |
| 628 | is |
| 629 | (FSBlock_Offset ( |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 630 | (if Static.Feature_64Bit |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 631 | then Shift_Left (Unsigned_64 (Read_LE32 (Buf, Group_Byte_Offset (Idx, 40))), 32) |
| 632 | else 0) |
| 633 | or |
| 634 | Unsigned_64 (Read_LE32 (Buf, Group_Byte_Offset (Idx, 8))))) |
| 635 | with |
| 636 | Pre => Buf'Length >= Group_Byte_Offset (Idx, 44); |
| 637 | |
Nico Huber | 022e226 | 2023-12-15 23:15:17 +0100 | [diff] [blame] | 638 | ------------------------ |
| 639 | -- Inode deserialization |
| 640 | |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 641 | subtype Inode_Off is Natural range 0 .. Inode_Size'Last; |
| 642 | function Inode_Byte_Offset (Idx : Inode_Index; Off : Inode_Off) return Natural |
| 643 | is |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 644 | (Natural (Idx) * Static.Inode_Size + Off); |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 645 | |
Nico Huber | 022e226 | 2023-12-15 23:15:17 +0100 | [diff] [blame] | 646 | function Inode_Mode (Buf : Buffer_Type; Idx : Inode_Index) return Unsigned_16 |
| 647 | is |
| 648 | (Read_LE16 (Buf, Inode_Byte_Offset (Idx, 0))) |
| 649 | with |
| 650 | Pre => Buf'Length >= Inode_Byte_Offset (Idx, 2); |
| 651 | |
| 652 | function Inode_Size (Buf : Buffer_Type; Idx : Inode_Index; Mode : Inode_Type) return Unsigned_64 |
| 653 | is |
| 654 | ((if Mode = Regular |
| 655 | then Shift_Left (Unsigned_64 (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 108))), 32) |
| 656 | else 0) or |
| 657 | Unsigned_64 (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 4)))) |
| 658 | with |
| 659 | Pre => Buf'Length >= Inode_Byte_Offset (Idx, 112); |
| 660 | |
| 661 | function Inode_Blocks (Buf : Buffer_Type; Idx : Inode_Index) return Unsigned_32 |
| 662 | is |
| 663 | (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 28))) |
| 664 | with |
| 665 | Pre => Buf'Length >= Inode_Byte_Offset (Idx, 32); |
| 666 | |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 667 | function Inode_Flags (Buf : Buffer_Type; Idx : Inode_Index) return Unsigned_32 |
| 668 | is |
| 669 | (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 32))) |
| 670 | with |
| 671 | Pre => Buf'Length >= Inode_Byte_Offset (Idx, 36); |
| 672 | |
Nico Huber | 022e226 | 2023-12-15 23:15:17 +0100 | [diff] [blame] | 673 | function Inode_File_ACL (Buf : Buffer_Type; Idx : Inode_Index) return Unsigned_32 |
| 674 | is |
| 675 | (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 104))) |
| 676 | with |
| 677 | Pre => Buf'Length >= Inode_Byte_Offset (Idx, 108); |
| 678 | |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 679 | Group : constant Group_Index := Group_Index ((Inode - 1) / Static.Inodes_Per_Group); |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 680 | Desc_Block : constant FSBlock_Offset := |
Nico Huber | 39f086c | 2024-01-09 17:43:06 +0100 | [diff] [blame^] | 681 | 1 + FSBlock_Offset (Static.First_Data_Block) + FSBlock_Offset (Group / Static.Desc_Per_Block); |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 682 | Cache_Start, Cache_End : Max_Block_Index; |
| 683 | begin |
| 684 | Cache_FSBlock |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 685 | (Static => Static, |
| 686 | Cache => State.Cache, |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 687 | Phys => Desc_Block, |
Nico Huber | ecafb8f | 2024-01-09 15:45:49 +0100 | [diff] [blame] | 688 | Level => Natural'Min (Natural (Group / Static.Desc_Per_Block), Block_Cache_Index'Last), |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 689 | Cache_Start => Cache_Start, |
| 690 | Cache_End => Cache_End, |
| 691 | Success => Success); |
| 692 | if not Success then |
| 693 | return; |
| 694 | end if; |
| 695 | |
| 696 | declare |
Nico Huber | ecafb8f | 2024-01-09 15:45:49 +0100 | [diff] [blame] | 697 | Desc : constant Desc_In_Block_Index := Desc_In_Block_Index (Group mod Static.Desc_Per_Block); |
| 698 | Inode_In_Group : constant Inode_In_Group_Index := |
| 699 | Inode_In_Group_Index ((Inode - 1) mod Static.Inodes_Per_Group); |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 700 | Inode_Block : constant FSBlock_Offset := |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 701 | Group_Inode_Table (Cache (Cache_Start .. Cache_End), Group) + |
Nico Huber | ecafb8f | 2024-01-09 15:45:49 +0100 | [diff] [blame] | 702 | FSBlock_Offset (Inode_In_Group / Static.Inodes_Per_Block); |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 703 | begin |
| 704 | Cache_FSBlock |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 705 | (Static => Static, |
| 706 | Cache => State.Cache, |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 707 | Phys => Inode_Block, |
| 708 | Level => Block_Cache_Index'Last, |
| 709 | Cache_Start => Cache_Start, |
| 710 | Cache_End => Cache_End, |
| 711 | Success => Success); |
| 712 | if not Success then |
| 713 | return; |
| 714 | end if; |
| 715 | |
| 716 | declare |
Nico Huber | 022e226 | 2023-12-15 23:15:17 +0100 | [diff] [blame] | 717 | S_IFMT : constant := 8#170000#; |
| 718 | S_IFDIR : constant := 8#040000#; |
| 719 | S_IFREG : constant := 8#100000#; |
| 720 | S_IFLNK : constant := 8#120000#; |
| 721 | |
Nico Huber | ecafb8f | 2024-01-09 15:45:49 +0100 | [diff] [blame] | 722 | Inode_In_Block : constant Inode_Index := Inode_Index (Inode_In_Group mod Static.Inodes_Per_Block); |
Nico Huber | 022e226 | 2023-12-15 23:15:17 +0100 | [diff] [blame] | 723 | I_Mode : constant Unsigned_16 := |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 724 | Inode_Mode (Cache (Cache_Start .. Cache_End), Inode_In_Block); |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 725 | I_Flags : constant Unsigned_32 := |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 726 | Inode_Flags (Cache (Cache_Start .. Cache_End), Inode_In_Block); |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 727 | begin |
Nico Huber | 022e226 | 2023-12-15 23:15:17 +0100 | [diff] [blame] | 728 | case I_Mode and S_IFMT is |
| 729 | when S_IFDIR => State.Inode.Mode := Dir; |
| 730 | when S_IFREG => State.Inode.Mode := Regular; |
| 731 | when S_IFLNK => State.Inode.Mode := Link; |
| 732 | when others => Success := False; return; |
| 733 | end case; |
| 734 | if State.Inode.Mode = Link then |
| 735 | declare |
| 736 | I_File_ACL : constant Unsigned_32 := Inode_File_ACL ( |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 737 | Cache (Cache_Start .. Cache_End), Inode_In_Block); |
Nico Huber | 022e226 | 2023-12-15 23:15:17 +0100 | [diff] [blame] | 738 | I_Blocks : constant Unsigned_32 := Inode_Blocks ( |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 739 | Cache (Cache_Start .. Cache_End), Inode_In_Block); |
Nico Huber | 022e226 | 2023-12-15 23:15:17 +0100 | [diff] [blame] | 740 | begin |
| 741 | if (I_File_ACL = 0 and I_Blocks = 0) or |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 742 | (I_File_ACL /= 0 and I_Blocks = 2 ** (Static.Block_Size_Bits - 9)) |
Nico Huber | 022e226 | 2023-12-15 23:15:17 +0100 | [diff] [blame] | 743 | then |
| 744 | State.Inode.Mode := Fast_Link; |
| 745 | end if; |
| 746 | end; |
| 747 | end if; |
| 748 | declare |
| 749 | I_Size : constant Unsigned_64 := Inode_Size ( |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 750 | Cache (Cache_Start .. Cache_End), Inode_In_Block, State.Inode.Mode); |
Nico Huber | 022e226 | 2023-12-15 23:15:17 +0100 | [diff] [blame] | 751 | begin |
| 752 | if I_Size <= Unsigned_64 (Inode_Length'Last) then |
| 753 | State.Inode.Size := Inode_Length (I_Size); |
| 754 | else |
| 755 | Success := False; |
Nico Huber | ecafb8f | 2024-01-09 15:45:49 +0100 | [diff] [blame] | 756 | return; |
Nico Huber | 022e226 | 2023-12-15 23:15:17 +0100 | [diff] [blame] | 757 | end if; |
| 758 | end; |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 759 | State.Inode.Use_Extents := Static.Feature_Extents and (I_Flags and EXT4_EXTENTS_FL) /= 0; |
| 760 | State.Inode.Inline := Cache (Cache_Start + 40 .. Cache_Start + 100 - 1); |
Nico Huber | 21d4202 | 2023-12-16 02:50:22 +0100 | [diff] [blame] | 761 | State.Inode.I := Inode; |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 762 | Reset_Cache_Logical (State.Cache); |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 763 | State.S := File_Opened; |
| 764 | end; |
| 765 | end; |
| 766 | end Open; |
| 767 | |
| 768 | procedure Open |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 769 | (State : in out T; |
| 770 | File_Len : out File_Length; |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 771 | File_Type : out FS.File_Type; |
| 772 | File_Name : in String; |
| 773 | In_Root : in Boolean; |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 774 | Success : out Boolean) |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 775 | is |
Nico Huber | 21d4202 | 2023-12-16 02:50:22 +0100 | [diff] [blame] | 776 | File_Name_Max : constant := 255; |
| 777 | Root_Inode : constant := 2; |
| 778 | |
| 779 | function Str_Buf_Equal (Str : String; Buf : Buffer_Type) return Boolean is |
| 780 | begin |
| 781 | for I in Str'Range loop |
| 782 | if Character'Pos (Str (I)) /= Buf (Buf'First + (I - Str'First)) then |
| 783 | return False; |
| 784 | end if; |
| 785 | end loop; |
| 786 | return True; |
| 787 | end Str_Buf_Equal; |
| 788 | |
| 789 | File_Inode : Inode_Index; |
| 790 | File_Pos : File_Length; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 791 | begin |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 792 | File_Len := 0; |
Nico Huber | 21d4202 | 2023-12-16 02:50:22 +0100 | [diff] [blame] | 793 | File_Type := FS.File_Type'First; |
| 794 | |
| 795 | if File_Name'Length > File_Name_Max then |
| 796 | Success := False; |
| 797 | return; |
| 798 | end if; |
| 799 | |
| 800 | -- Ensure dir is opened: |
| 801 | -- |
| 802 | if State.S = File_Opened then |
| 803 | if State.Cur_Dir /= State.Inode.I then |
| 804 | Success := False; |
| 805 | return; |
| 806 | end if; |
| 807 | else |
| 808 | if In_Root then |
| 809 | State.Cur_Dir := Root_Inode; |
| 810 | end if; |
| 811 | Open (State, State.Cur_Dir, Success); |
| 812 | if not Success then |
| 813 | return; |
| 814 | end if; |
| 815 | end if; |
| 816 | |
| 817 | -- Lookup file in opened dir: |
| 818 | -- |
| 819 | File_Pos := 0; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 820 | Success := False; |
Nico Huber | 21d4202 | 2023-12-16 02:50:22 +0100 | [diff] [blame] | 821 | while Unsigned_64 (File_Pos) < Unsigned_64 (State.Inode.Size) loop |
| 822 | declare |
| 823 | Dir_Entry_Header_Length : constant := 4 + 2 + 1 + 1; |
| 824 | subtype Dir_Entry_Index is Natural |
| 825 | range 0 .. Dir_Entry_Header_Length + File_Name_Max - 1; |
| 826 | Dir_Entry : Buffer_Type (Dir_Entry_Index); |
| 827 | Entry_File_Pos : File_Offset := File_Pos; |
| 828 | Len : Natural; |
| 829 | begin |
| 830 | Read |
| 831 | (State => State, |
| 832 | File_Pos => Entry_File_Pos, |
| 833 | Buf => Dir_Entry (0 .. 7), |
| 834 | Len => Len); |
| 835 | if Len < Dir_Entry_Header_Length then |
| 836 | return; |
| 837 | end if; |
| 838 | |
| 839 | -- Only check filenames of exact same length |
| 840 | if Read_LE32 (Dir_Entry, 0) > Root_Inode and then |
| 841 | File_Name'Length = Natural (Dir_Entry (6)) |
| 842 | then |
| 843 | Read |
| 844 | (State => State, |
| 845 | File_Pos => Entry_File_Pos, |
| 846 | Buf => Dir_Entry (8 .. 8 + File_Name'Length - 1), |
| 847 | Len => Len); |
| 848 | if Len < File_Name'Length then |
| 849 | return; |
| 850 | end if; |
| 851 | |
| 852 | File_Inode := Inode_Index (Read_LE32 (Dir_Entry, 0)); |
| 853 | Success := Str_Buf_Equal (File_Name, Dir_Entry (8 .. 8 + File_Name'Length - 1)); |
| 854 | exit when Success; |
| 855 | end if; |
| 856 | |
| 857 | File_Pos := File_Pos + File_Length (Read_LE16 (Dir_Entry, 4)); |
| 858 | end; |
| 859 | end loop; |
| 860 | |
| 861 | if Success then |
| 862 | Open (State, File_Inode, Success); |
| 863 | if not Success then |
| 864 | return; |
| 865 | end if; |
| 866 | |
| 867 | if State.Inode.Mode = Dir then |
| 868 | State.Cur_Dir := File_Inode; |
| 869 | end if; |
| 870 | end if; |
| 871 | |
| 872 | Success := Unsigned_64 (State.Inode.Size) <= Unsigned_64 (File_Length'Last); |
| 873 | if Success then |
| 874 | File_Len := File_Length (State.Inode.Size); |
| 875 | File_Type := (case State.Inode.Mode is |
| 876 | when Dir => FS.Dir, |
| 877 | when Regular => FS.Regular, |
| 878 | when Link .. Fast_Link => FS.Link); |
| 879 | else |
| 880 | Close (State); |
| 881 | end if; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 882 | end Open; |
| 883 | |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 884 | procedure Close (State : in out T) is |
| 885 | begin |
| 886 | State.S := Mounted; |
| 887 | end Close; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 888 | |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 889 | procedure Read |
| 890 | (State : in out T; |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 891 | File_Pos : in out File_Offset; |
| 892 | Buf : out Buffer_Type; |
| 893 | Len : out Natural) |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 894 | is |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 895 | Static : Mount_State renames State.Static; |
| 896 | Cache : Cache_Buffer renames State.Cache.Buffer; |
| 897 | |
Nico Huber | d3644be | 2023-12-16 01:43:00 +0100 | [diff] [blame] | 898 | Pos : Natural; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 899 | begin |
Nico Huber | d3644be | 2023-12-16 01:43:00 +0100 | [diff] [blame] | 900 | if State.Inode.Mode = Fast_Link then |
Nico Huber | 549a1b8 | 2023-12-17 01:51:59 +0100 | [diff] [blame] | 901 | if State.Inode.Size > Inode_Length (State.Inode.Inline'Length) or |
| 902 | Inode_Length (File_Pos) >= State.Inode.Size then |
Nico Huber | d3644be | 2023-12-16 01:43:00 +0100 | [diff] [blame] | 903 | Len := 0; |
| 904 | else |
Nico Huber | 549a1b8 | 2023-12-17 01:51:59 +0100 | [diff] [blame] | 905 | Len := Natural'Min (Buf'Length, Natural (State.Inode.Size) - Natural (File_Pos)); |
Nico Huber | d3644be | 2023-12-16 01:43:00 +0100 | [diff] [blame] | 906 | end if; |
| 907 | Buf (Buf'First .. Buf'First + Len - 1) := |
| 908 | State.Inode.Inline (Natural (File_Pos) .. Natural (File_Pos) + Len - 1); |
| 909 | Buf (Buf'First + Len .. Buf'Last) := (others => 16#00#); |
| 910 | File_Pos := File_Pos + File_Length (Len); |
| 911 | return; |
| 912 | end if; |
| 913 | |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 914 | Len := 0; |
Nico Huber | d3644be | 2023-12-16 01:43:00 +0100 | [diff] [blame] | 915 | Pos := Buf'First; |
| 916 | while Pos <= Buf'Last and Inode_Length (File_Pos) < State.Inode.Size loop |
| 917 | declare |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 918 | In_Block : constant Max_Block_Index := Natural (File_Pos) mod Static.Block_Size; |
| 919 | Logical : constant FSBlock_Logical := FSBlock_Logical (File_Pos / File_Offset (Static.Block_Size)); |
| 920 | In_Block_Space : constant Natural := Natural (Static.Block_Size) - In_Block; |
Nico Huber | 549a1b8 | 2023-12-17 01:51:59 +0100 | [diff] [blame] | 921 | In_File_Space : constant Inode_Length := State.Inode.Size - Inode_Length (File_Pos); |
Nico Huber | d3644be | 2023-12-16 01:43:00 +0100 | [diff] [blame] | 922 | In_Buf_Space : constant Natural := Buf'Last - Pos + 1; |
| 923 | Len_Here : Natural; |
| 924 | begin |
| 925 | Len_Here := In_Block_Space; |
Nico Huber | 549a1b8 | 2023-12-17 01:51:59 +0100 | [diff] [blame] | 926 | if In_File_Space < Inode_Length (Len_Here) then |
| 927 | Len_Here := Natural (In_File_Space); |
Nico Huber | d3644be | 2023-12-16 01:43:00 +0100 | [diff] [blame] | 928 | end if; |
| 929 | if In_Buf_Space < Len_Here then |
| 930 | Len_Here := In_Buf_Space; |
| 931 | end if; |
| 932 | |
| 933 | declare |
| 934 | Last : constant Index_Type := Pos + Len_Here - 1; |
| 935 | Cache_Start, Cache_End : Max_Block_Index; |
| 936 | Physical : FSBlock_Offset; |
| 937 | Success : Boolean; |
| 938 | begin |
| 939 | if State.Inode.Use_Extents then |
| 940 | Extent_Block_Map (State, Logical, Physical, Success); |
| 941 | else |
| 942 | Ext2_Block_Map (State, Logical, Physical, Success); |
| 943 | end if; |
| 944 | if Success then |
| 945 | Cache_FSBlock |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 946 | (Static => Static, |
| 947 | Cache => State.Cache, |
Nico Huber | d3644be | 2023-12-16 01:43:00 +0100 | [diff] [blame] | 948 | Phys => Physical, |
| 949 | Level => Block_Cache_Index'Last, |
| 950 | Cache_Start => Cache_Start, |
| 951 | Cache_End => Cache_End, |
| 952 | Success => Success); |
| 953 | end if; |
| 954 | exit when not Success; |
| 955 | |
Nico Huber | 52f4c8d | 2024-01-09 13:57:33 +0100 | [diff] [blame] | 956 | Buf (Pos .. Last) := Cache ( |
Nico Huber | d3644be | 2023-12-16 01:43:00 +0100 | [diff] [blame] | 957 | Cache_Start + In_Block .. Cache_Start + In_Block + Len_Here - 1); |
| 958 | File_Pos := File_Pos + File_Length (Len_Here); |
| 959 | Pos := Pos + Len_Here; |
| 960 | Len := Len + Len_Here; |
| 961 | end; |
| 962 | end; |
| 963 | end loop; |
| 964 | Buf (Pos .. Buf'Last) := (others => 16#00#); |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 965 | end Read; |
| 966 | |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 967 | -------------------------------------------------------------------------- |
| 968 | |
| 969 | package C is new VFS (T => T, Initial => (S => Unmounted, others => <>)); |
Nico Huber | 8ec45a1 | 2023-12-04 17:11:08 +0100 | [diff] [blame] | 970 | |
| 971 | function C_Mount return int |
| 972 | with |
| 973 | Export, |
| 974 | Convention => C, |
| 975 | External_Name => "ext2fs_mount"; |
| 976 | function C_Mount return int |
| 977 | with |
| 978 | SPARK_Mode => Off |
| 979 | is |
| 980 | begin |
| 981 | return C.C_Mount; |
| 982 | end C_Mount; |
| 983 | |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 984 | function C_Open (File_Path : System.Address) return int |
Nico Huber | 8ec45a1 | 2023-12-04 17:11:08 +0100 | [diff] [blame] | 985 | with |
| 986 | Export, |
| 987 | Convention => C, |
| 988 | External_Name => "ext2fs_dir"; |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 989 | function C_Open (File_Path : System.Address) return int |
Nico Huber | 8ec45a1 | 2023-12-04 17:11:08 +0100 | [diff] [blame] | 990 | with |
| 991 | SPARK_Mode => Off |
| 992 | is |
| 993 | begin |
| 994 | return C.C_Open (File_Path); |
| 995 | end C_Open; |
| 996 | |
| 997 | function C_Read (Buf : System.Address; Len : int) return int |
| 998 | with |
| 999 | Export, |
| 1000 | Convention => C, |
| 1001 | External_Name => "ext2fs_read"; |
| 1002 | function C_Read (Buf : System.Address; Len : int) return int |
| 1003 | with |
| 1004 | SPARK_Mode => Off |
| 1005 | is |
| 1006 | begin |
| 1007 | return C.C_Read (Buf, Len); |
| 1008 | end C_Read; |
| 1009 | |
Thomas Heijligen | 5c43abc | 2023-12-11 15:24:36 +0000 | [diff] [blame] | 1010 | end FILO.FS.Ext2; |