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