3 * - By John Hodge (thePowersGang)
\r
9 #define VERSION VER2(0,90)
\r
10 #include "ext2_common.h"
\r
11 #include <modules.h>
\r
14 extern tVFS_NodeType gExt2_DirType;
\r
16 // === PROTOTYPES ===
\r
17 int Ext2_Install(char **Arguments);
\r
18 int Ext2_Cleanup(void);
\r
19 // - Interface Functions
\r
20 tVFS_Node *Ext2_InitDevice(const char *Device, const char **Options);
\r
21 void Ext2_Unmount(tVFS_Node *Node);
\r
22 void Ext2_CloseFile(tVFS_Node *Node);
\r
23 // - Internal Helpers
\r
24 int Ext2_int_GetInode(tVFS_Node *Node, tExt2_Inode *Inode);
\r
25 Uint64 Ext2_int_GetBlockAddr(tExt2_Disk *Disk, Uint32 *Blocks, int BlockNum);
\r
26 Uint32 Ext2_int_AllocateInode(tExt2_Disk *Disk, Uint32 Parent);
\r
27 void Ext2_int_UpdateSuperblock(tExt2_Disk *Disk);
\r
29 // === SEMI-GLOBALS ===
\r
30 MODULE_DEFINE(0, VERSION, FS_Ext2, Ext2_Install, Ext2_Cleanup);
\r
31 tExt2_Disk gExt2_disks[6];
\r
32 int giExt2_count = 0;
\r
33 tVFS_Driver gExt2_FSInfo = {
\r
34 "ext2", 0, Ext2_InitDevice, Ext2_Unmount, NULL
\r
39 * \fn int Ext2_Install(char **Arguments)
\r
40 * \brief Install the Ext2 Filesystem Driver
\r
42 int Ext2_Install(char **Arguments)
\r
44 VFS_AddDriver( &gExt2_FSInfo );
\r
45 return MODULE_ERR_OK;
\r
49 * \brief Clean up driver state before unload
\r
51 int Ext2_Cleanup(void)
\r
57 \brief Initializes a device to be read by by the driver
\r
58 \param Device String - Device to read from
\r
59 \param Options NULL Terminated array of option strings
\r
62 tVFS_Node *Ext2_InitDevice(const char *Device, const char **Options)
\r
67 tExt2_SuperBlock sb;
\r
70 ENTER("sDevice pOptions", Device, Options);
\r
73 fd = VFS_Open(Device, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE); //Open Device
\r
75 Log_Warning("EXT2", "Unable to open '%s'", Device);
\r
80 // Read Superblock at offset 1024
\r
81 VFS_ReadAt(fd, 1024, 1024, &sb); // Read Superblock
\r
83 // Sanity Check Magic value
\r
84 if(sb.s_magic != 0xEF53) {
\r
85 Log_Warning("EXT2", "Volume '%s' is not an EXT2 volume (0x%x != 0xEF53)",
\r
86 Device, sb.s_magic);
\r
93 groupCount = DivUp(sb.s_blocks_count, sb.s_blocks_per_group);
\r
94 LOG("groupCount = %i", groupCount);
\r
96 // Allocate Disk Information
\r
97 disk = malloc(sizeof(tExt2_Disk) + sizeof(tExt2_Group)*groupCount);
\r
99 Log_Warning("EXT2", "Unable to allocate disk structure");
\r
105 memcpy(&disk->SuperBlock, &sb, 1024);
\r
106 disk->GroupCount = groupCount;
\r
108 // Get an inode cache handle
\r
109 disk->CacheID = Inode_GetHandle();
\r
112 disk->BlockSize = 1024 << sb.s_log_block_size;
\r
113 LOG("Disk->BlockSie = 0x%x (1024 << %i)", disk->BlockSize, sb.s_log_block_size);
\r
115 // Read Group Information
\r
116 LOG("sb,s_first_data_block = %x", sb.s_first_data_block);
\r
119 sb.s_first_data_block * disk->BlockSize + 1024,
\r
120 sizeof(tExt2_Group)*groupCount,
\r
124 LOG("Block Group 0");
\r
125 LOG(".bg_block_bitmap = 0x%x", disk->Groups[0].bg_block_bitmap);
\r
126 LOG(".bg_inode_bitmap = 0x%x", disk->Groups[0].bg_inode_bitmap);
\r
127 LOG(".bg_inode_table = 0x%x", disk->Groups[0].bg_inode_table);
\r
128 LOG("Block Group 1");
\r
129 LOG(".bg_block_bitmap = 0x%x", disk->Groups[1].bg_block_bitmap);
\r
130 LOG(".bg_inode_bitmap = 0x%x", disk->Groups[1].bg_inode_bitmap);
\r
131 LOG(".bg_inode_table = 0x%x", disk->Groups[1].bg_inode_table);
\r
134 Ext2_int_ReadInode(disk, 2, &inode);
\r
136 // Create Root Node
\r
137 memset(&disk->RootNode, 0, sizeof(tVFS_Node));
\r
138 disk->RootNode.Inode = 2; // Root inode ID
\r
139 disk->RootNode.ImplPtr = disk; // Save disk pointer
\r
140 disk->RootNode.Size = -1; // Fill in later (on readdir)
\r
141 disk->RootNode.Flags = VFS_FFLAG_DIRECTORY;
\r
143 disk->RootNode.Type = &gExt2_DirType;
\r
145 // Complete root node
\r
146 disk->RootNode.UID = inode.i_uid;
\r
147 disk->RootNode.GID = inode.i_gid;
\r
148 disk->RootNode.NumACLs = 1;
\r
149 disk->RootNode.ACLs = &gVFS_ACL_EveryoneRW;
\r
152 LOG("inode.i_size = 0x%x", inode.i_size);
\r
153 LOG("inode.i_block[0] = 0x%x", inode.i_block[0]);
\r
156 LEAVE('p', &disk->RootNode);
\r
157 return &disk->RootNode;
\r
161 * \fn void Ext2_Unmount(tVFS_Node *Node)
\r
162 * \brief Close a mounted device
\r
164 void Ext2_Unmount(tVFS_Node *Node)
\r
166 tExt2_Disk *disk = Node->ImplPtr;
\r
168 VFS_Close( disk->FD );
\r
169 Inode_ClearCache( disk->CacheID );
\r
170 memset(disk, 0, sizeof(tExt2_Disk)+disk->GroupCount*sizeof(tExt2_Group));
\r
175 * \fn void Ext2_CloseFile(tVFS_Node *Node)
\r
176 * \brief Close a file (Remove it from the cache)
\r
178 void Ext2_CloseFile(tVFS_Node *Node)
\r
180 tExt2_Disk *disk = Node->ImplPtr;
\r
181 Inode_UncacheNode(disk->CacheID, Node->Inode);
\r
185 //==================================
\r
186 //= INTERNAL FUNCTIONS =
\r
187 //==================================
\r
189 * \fn int Ext2_int_ReadInode(tExt2_Disk *Disk, Uint InodeId, tExt2_Inode *Inode)
\r
190 * \brief Read an inode into memory
\r
192 int Ext2_int_ReadInode(tExt2_Disk *Disk, Uint32 InodeId, tExt2_Inode *Inode)
\r
196 ENTER("pDisk iInodeId pInode", Disk, InodeId, Inode);
\r
198 if(InodeId == 0) return 0;
\r
200 InodeId --; // Inodes are numbered starting at 1
\r
202 group = InodeId / Disk->SuperBlock.s_inodes_per_group;
\r
203 subId = InodeId % Disk->SuperBlock.s_inodes_per_group;
\r
205 LOG("group=%i, subId = %i", group, subId);
\r
208 VFS_ReadAt(Disk->FD,
\r
209 Disk->Groups[group].bg_inode_table * Disk->BlockSize + sizeof(tExt2_Inode)*subId,
\r
210 sizeof(tExt2_Inode),
\r
218 * \brief Write a modified inode out to disk
\r
220 int Ext2_int_WriteInode(tExt2_Disk *Disk, Uint32 InodeId, tExt2_Inode *Inode)
\r
223 ENTER("pDisk iInodeId pInode", Disk, InodeId, Inode);
\r
230 InodeId --; // Inodes are numbered starting at 1
\r
232 group = InodeId / Disk->SuperBlock.s_inodes_per_group;
\r
233 subId = InodeId % Disk->SuperBlock.s_inodes_per_group;
\r
235 LOG("group=%i, subId = %i", group, subId);
\r
238 VFS_WriteAt(Disk->FD,
\r
239 Disk->Groups[group].bg_inode_table * Disk->BlockSize + sizeof(tExt2_Inode)*subId,
\r
240 sizeof(tExt2_Inode),
\r
249 * \fn Uint64 Ext2_int_GetBlockAddr(tExt2_Disk *Disk, Uint32 *Blocks, int BlockNum)
\r
250 * \brief Get the address of a block from an inode's list
\r
251 * \param Disk Disk information structure
\r
252 * \param Blocks Pointer to an inode's block list
\r
253 * \param BlockNum Block index in list
\r
255 Uint64 Ext2_int_GetBlockAddr(tExt2_Disk *Disk, Uint32 *Blocks, int BlockNum)
\r
258 int dwPerBlock = Disk->BlockSize / 4;
\r
262 return (Uint64)Blocks[BlockNum] * Disk->BlockSize;
\r
264 // Single Indirect Blocks
\r
265 iBlocks = malloc( Disk->BlockSize );
\r
266 VFS_ReadAt(Disk->FD, (Uint64)Blocks[12]*Disk->BlockSize, Disk->BlockSize, iBlocks);
\r
269 if(BlockNum < dwPerBlock)
\r
271 BlockNum = iBlocks[BlockNum];
\r
273 return (Uint64)BlockNum * Disk->BlockSize;
\r
276 BlockNum -= dwPerBlock;
\r
277 // Double Indirect Blocks
\r
278 if(BlockNum < dwPerBlock*dwPerBlock)
\r
280 VFS_ReadAt(Disk->FD, (Uint64)Blocks[13]*Disk->BlockSize, Disk->BlockSize, iBlocks);
\r
281 VFS_ReadAt(Disk->FD, (Uint64)iBlocks[BlockNum/dwPerBlock]*Disk->BlockSize, Disk->BlockSize, iBlocks);
\r
282 BlockNum = iBlocks[BlockNum%dwPerBlock];
\r
284 return (Uint64)BlockNum * Disk->BlockSize;
\r
287 BlockNum -= dwPerBlock*dwPerBlock;
\r
288 // Triple Indirect Blocks
\r
289 VFS_ReadAt(Disk->FD, (Uint64)Blocks[14]*Disk->BlockSize, Disk->BlockSize, iBlocks);
\r
290 VFS_ReadAt(Disk->FD, (Uint64)iBlocks[BlockNum/(dwPerBlock*dwPerBlock)]*Disk->BlockSize, Disk->BlockSize, iBlocks);
\r
291 VFS_ReadAt(Disk->FD, (Uint64)iBlocks[(BlockNum/dwPerBlock)%dwPerBlock]*Disk->BlockSize, Disk->BlockSize, iBlocks);
\r
292 BlockNum = iBlocks[BlockNum%dwPerBlock];
\r
294 return (Uint64)BlockNum * Disk->BlockSize;
\r
298 * \fn Uint32 Ext2_int_AllocateInode(tExt2_Disk *Disk, Uint32 Parent)
\r
299 * \brief Allocate an inode (from the current group preferably)
\r
300 * \param Disk EXT2 Disk Information Structure
\r
301 * \param Parent Inode ID of the parent (used to locate the child nearby)
\r
303 Uint32 Ext2_int_AllocateInode(tExt2_Disk *Disk, Uint32 Parent)
\r
305 // Uint block = (Parent - 1) / Disk->SuperBlock.s_inodes_per_group;
\r
306 Log_Warning("EXT2", "Ext2_int_AllocateInode is unimplemented");
\r
311 * \fn void Ext2_int_UpdateSuperblock(tExt2_Disk *Disk)
\r
312 * \brief Updates the superblock
\r
314 void Ext2_int_UpdateSuperblock(tExt2_Disk *Disk)
\r
316 int bpg = Disk->SuperBlock.s_blocks_per_group;
\r
317 int ngrp = Disk->SuperBlock.s_blocks_count / bpg;
\r
321 VFS_WriteAt(Disk->FD, 1024, 1024, &Disk->SuperBlock);
\r
324 // at Block Group 1, 3^n, 5^n, 7^n
\r
327 if(ngrp <= 1) return;
\r
328 VFS_WriteAt(Disk->FD, 1*bpg*Disk->BlockSize, 1024, &Disk->SuperBlock);
\r
330 #define INT_MAX (((long long int)1<<(sizeof(int)*8))-1)
\r
333 for( i = 3; i < ngrp && i < INT_MAX/3; i *= 3 )
\r
334 VFS_WriteAt(Disk->FD, i*bpg*Disk->BlockSize, 1024, &Disk->SuperBlock);
\r
337 for( i = 5; i < ngrp && i < INT_MAX/5; i *= 5 )
\r
338 VFS_WriteAt(Disk->FD, i*bpg*Disk->BlockSize, 1024, &Disk->SuperBlock);
\r
341 for( i = 7; i < ngrp && i < INT_MAX/7; i *= 7 )
\r
342 VFS_WriteAt(Disk->FD, i*bpg*Disk->BlockSize, 1024, &Disk->SuperBlock);
\r