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