3 * Ext2 Driver Version 1
\r
7 * \brief Second Extended Filesystem Driver
\r
8 * \todo Implement file full write support
\r
12 #include "ext2_common.h"
\r
13 #include <modules.h>
\r
15 // === PROTOTYPES ===
\r
16 int Ext2_Install(char **Arguments);
\r
17 // Interface Functions
\r
18 tVFS_Node *Ext2_InitDevice(const char *Device, const char **Options);
\r
19 void Ext2_Unmount(tVFS_Node *Node);
\r
20 void Ext2_CloseFile(tVFS_Node *Node);
\r
22 int Ext2_int_GetInode(tVFS_Node *Node, tExt2_Inode *Inode);
\r
23 Uint64 Ext2_int_GetBlockAddr(tExt2_Disk *Disk, Uint32 *Blocks, int BlockNum);
\r
24 Uint32 Ext2_int_AllocateInode(tExt2_Disk *Disk, Uint32 Parent);
\r
25 void Ext2_int_UpdateSuperblock(tExt2_Disk *Disk);
\r
27 // === SEMI-GLOBALS ===
\r
28 MODULE_DEFINE(0, 0x5B /*v0.90*/, FS_Ext2, Ext2_Install, NULL);
\r
29 tExt2_Disk gExt2_disks[6];
\r
30 int giExt2_count = 0;
\r
31 tVFS_Driver gExt2_FSInfo = {
\r
32 "ext2", 0, Ext2_InitDevice, Ext2_Unmount, NULL
\r
37 * \fn int Ext2_Install(char **Arguments)
\r
38 * \brief Install the Ext2 Filesystem Driver
\r
40 int Ext2_Install(char **Arguments)
\r
42 VFS_AddDriver( &gExt2_FSInfo );
\r
43 return MODULE_ERR_OK;
\r
47 \brief Initializes a device to be read by by the driver
\r
48 \param Device String - Device to read from
\r
49 \param Options NULL Terminated array of option strings
\r
52 tVFS_Node *Ext2_InitDevice(const char *Device, const char **Options)
\r
57 tExt2_SuperBlock sb;
\r
60 ENTER("sDevice pOptions", Device, Options);
\r
63 fd = VFS_Open(Device, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE); //Open Device
\r
65 Log_Warning("EXT2", "Unable to open '%s'", Device);
\r
70 // Read Superblock at offset 1024
\r
71 VFS_ReadAt(fd, 1024, 1024, &sb); // Read Superblock
\r
73 // Sanity Check Magic value
\r
74 if(sb.s_magic != 0xEF53) {
\r
75 Log_Warning("EXT2", "Volume '%s' is not an EXT2 volume (0x%x != 0xEF53)",
\r
76 Device, sb.s_magic);
\r
83 groupCount = DivUp(sb.s_blocks_count, sb.s_blocks_per_group);
\r
84 LOG("groupCount = %i", groupCount);
\r
86 // Allocate Disk Information
\r
87 disk = malloc(sizeof(tExt2_Disk) + sizeof(tExt2_Group)*groupCount);
\r
89 Log_Warning("EXT2", "Unable to allocate disk structure");
\r
95 memcpy(&disk->SuperBlock, &sb, 1024);
\r
96 disk->GroupCount = groupCount;
\r
98 // Get an inode cache handle
\r
99 disk->CacheID = Inode_GetHandle();
\r
102 LOG("s_log_block_size = 0x%x", sb.s_log_block_size);
\r
103 disk->BlockSize = 1024 << sb.s_log_block_size;
\r
105 // Read Group Information
\r
108 sb.s_first_data_block * disk->BlockSize + 1024,
\r
109 sizeof(tExt2_Group)*groupCount,
\r
114 LOG("Block Group 0");
\r
115 LOG(".bg_block_bitmap = 0x%x", disk->Groups[0].bg_block_bitmap);
\r
116 LOG(".bg_inode_bitmap = 0x%x", disk->Groups[0].bg_inode_bitmap);
\r
117 LOG(".bg_inode_table = 0x%x", disk->Groups[0].bg_inode_table);
\r
118 LOG("Block Group 1");
\r
119 LOG(".bg_block_bitmap = 0x%x", disk->Groups[1].bg_block_bitmap);
\r
120 LOG(".bg_inode_bitmap = 0x%x", disk->Groups[1].bg_inode_bitmap);
\r
121 LOG(".bg_inode_table = 0x%x", disk->Groups[1].bg_inode_table);
\r
125 Ext2_int_ReadInode(disk, 2, &inode);
\r
127 // Create Root Node
\r
128 memset(&disk->RootNode, 0, sizeof(tVFS_Node));
\r
129 disk->RootNode.Inode = 2; // Root inode ID
\r
130 disk->RootNode.ImplPtr = disk; // Save disk pointer
\r
131 disk->RootNode.Size = -1; // Fill in later (on readdir)
\r
132 disk->RootNode.Flags = VFS_FFLAG_DIRECTORY;
\r
134 disk->RootNode.ReadDir = Ext2_ReadDir;
\r
135 disk->RootNode.FindDir = Ext2_FindDir;
\r
136 //disk->RootNode.Relink = Ext2_Relink;
\r
138 // Complete root node
\r
139 disk->RootNode.UID = inode.i_uid;
\r
140 disk->RootNode.GID = inode.i_gid;
\r
141 disk->RootNode.NumACLs = 1;
\r
142 disk->RootNode.ACLs = &gVFS_ACL_EveryoneRW;
\r
145 LOG("inode.i_size = 0x%x", inode.i_size);
\r
146 LOG("inode.i_block[0] = 0x%x", inode.i_block[0]);
\r
149 LEAVE('p', &disk->RootNode);
\r
150 return &disk->RootNode;
\r
154 * \fn void Ext2_Unmount(tVFS_Node *Node)
\r
155 * \brief Close a mounted device
\r
157 void Ext2_Unmount(tVFS_Node *Node)
\r
159 tExt2_Disk *disk = Node->ImplPtr;
\r
161 VFS_Close( disk->FD );
\r
162 Inode_ClearCache( disk->CacheID );
\r
163 memset(disk, 0, sizeof(tExt2_Disk)+disk->GroupCount*sizeof(tExt2_Group));
\r
168 * \fn void Ext2_CloseFile(tVFS_Node *Node)
\r
169 * \brief Close a file (Remove it from the cache)
\r
171 void Ext2_CloseFile(tVFS_Node *Node)
\r
173 tExt2_Disk *disk = Node->ImplPtr;
\r
174 Inode_UncacheNode(disk->CacheID, Node->Inode);
\r
178 //==================================
\r
179 //= INTERNAL FUNCTIONS =
\r
180 //==================================
\r
182 * \fn int Ext2_int_ReadInode(tExt2_Disk *Disk, Uint InodeId, tExt2_Inode *Inode)
\r
183 * \brief Read an inode into memory
\r
185 int Ext2_int_ReadInode(tExt2_Disk *Disk, Uint32 InodeId, tExt2_Inode *Inode)
\r
189 ENTER("pDisk iInodeId pInode", Disk, InodeId, Inode);
\r
191 if(InodeId == 0) return 0;
\r
193 InodeId --; // Inodes are numbered starting at 1
\r
195 group = InodeId / Disk->SuperBlock.s_inodes_per_group;
\r
196 subId = InodeId % Disk->SuperBlock.s_inodes_per_group;
\r
198 LOG("group=%i, subId = %i", group, subId);
\r
201 VFS_ReadAt(Disk->FD,
\r
202 Disk->Groups[group].bg_inode_table * Disk->BlockSize + sizeof(tExt2_Inode)*subId,
\r
203 sizeof(tExt2_Inode),
\r
211 * \brief Write a modified inode out to disk
\r
213 int Ext2_int_WriteInode(tExt2_Disk *Disk, Uint32 InodeId, tExt2_Inode *Inode)
\r
216 ENTER("pDisk iInodeId pInode", Disk, InodeId, Inode);
\r
223 InodeId --; // Inodes are numbered starting at 1
\r
225 group = InodeId / Disk->SuperBlock.s_inodes_per_group;
\r
226 subId = InodeId % Disk->SuperBlock.s_inodes_per_group;
\r
228 LOG("group=%i, subId = %i", group, subId);
\r
231 VFS_WriteAt(Disk->FD,
\r
232 Disk->Groups[group].bg_inode_table * Disk->BlockSize + sizeof(tExt2_Inode)*subId,
\r
233 sizeof(tExt2_Inode),
\r
242 * \fn Uint64 Ext2_int_GetBlockAddr(tExt2_Disk *Disk, Uint32 *Blocks, int BlockNum)
\r
243 * \brief Get the address of a block from an inode's list
\r
244 * \param Disk Disk information structure
\r
245 * \param Blocks Pointer to an inode's block list
\r
246 * \param BlockNum Block index in list
\r
248 Uint64 Ext2_int_GetBlockAddr(tExt2_Disk *Disk, Uint32 *Blocks, int BlockNum)
\r
251 int dwPerBlock = Disk->BlockSize / 4;
\r
255 return (Uint64)Blocks[BlockNum] * Disk->BlockSize;
\r
257 // Single Indirect Blocks
\r
258 iBlocks = malloc( Disk->BlockSize );
\r
259 VFS_ReadAt(Disk->FD, (Uint64)Blocks[12]*Disk->BlockSize, Disk->BlockSize, iBlocks);
\r
262 if(BlockNum < dwPerBlock)
\r
264 BlockNum = iBlocks[BlockNum];
\r
266 return (Uint64)BlockNum * Disk->BlockSize;
\r
269 BlockNum -= dwPerBlock;
\r
270 // Double Indirect Blocks
\r
271 if(BlockNum < dwPerBlock*dwPerBlock)
\r
273 VFS_ReadAt(Disk->FD, (Uint64)Blocks[13]*Disk->BlockSize, Disk->BlockSize, iBlocks);
\r
274 VFS_ReadAt(Disk->FD, (Uint64)iBlocks[BlockNum/dwPerBlock]*Disk->BlockSize, Disk->BlockSize, iBlocks);
\r
275 BlockNum = iBlocks[BlockNum%dwPerBlock];
\r
277 return (Uint64)BlockNum * Disk->BlockSize;
\r
280 BlockNum -= dwPerBlock*dwPerBlock;
\r
281 // Triple Indirect Blocks
\r
282 VFS_ReadAt(Disk->FD, (Uint64)Blocks[14]*Disk->BlockSize, Disk->BlockSize, iBlocks);
\r
283 VFS_ReadAt(Disk->FD, (Uint64)iBlocks[BlockNum/(dwPerBlock*dwPerBlock)]*Disk->BlockSize, Disk->BlockSize, iBlocks);
\r
284 VFS_ReadAt(Disk->FD, (Uint64)iBlocks[(BlockNum/dwPerBlock)%dwPerBlock]*Disk->BlockSize, Disk->BlockSize, iBlocks);
\r
285 BlockNum = iBlocks[BlockNum%dwPerBlock];
\r
287 return (Uint64)BlockNum * Disk->BlockSize;
\r
291 * \fn Uint32 Ext2_int_AllocateInode(tExt2_Disk *Disk, Uint32 Parent)
\r
292 * \brief Allocate an inode (from the current group preferably)
\r
293 * \param Disk EXT2 Disk Information Structure
\r
294 * \param Parent Inode ID of the parent (used to locate the child nearby)
\r
296 Uint32 Ext2_int_AllocateInode(tExt2_Disk *Disk, Uint32 Parent)
\r
298 // Uint block = (Parent - 1) / Disk->SuperBlock.s_inodes_per_group;
\r
299 Log_Warning("EXT2", "Ext2_int_AllocateInode is unimplemented");
\r
304 * \fn void Ext2_int_UpdateSuperblock(tExt2_Disk *Disk)
\r
305 * \brief Updates the superblock
\r
307 void Ext2_int_UpdateSuperblock(tExt2_Disk *Disk)
\r
309 int bpg = Disk->SuperBlock.s_blocks_per_group;
\r
310 int ngrp = Disk->SuperBlock.s_blocks_count / bpg;
\r
314 VFS_WriteAt(Disk->FD, 1024, 1024, &Disk->SuperBlock);
\r
317 // at Block Group 1, 3^n, 5^n, 7^n
\r
320 if(ngrp <= 1) return;
\r
321 VFS_WriteAt(Disk->FD, 1*bpg*Disk->BlockSize, 1024, &Disk->SuperBlock);
\r
323 #define INT_MAX (((long long int)1<<(sizeof(int)*8))-1)
\r
326 for( i = 3; i < ngrp && i < INT_MAX/3; i *= 3 )
\r
327 VFS_WriteAt(Disk->FD, i*bpg*Disk->BlockSize, 1024, &Disk->SuperBlock);
\r
330 for( i = 5; i < ngrp && i < INT_MAX/5; i *= 5 )
\r
331 VFS_WriteAt(Disk->FD, i*bpg*Disk->BlockSize, 1024, &Disk->SuperBlock);
\r
334 for( i = 7; i < ngrp && i < INT_MAX/7; i *= 7 )
\r
335 VFS_WriteAt(Disk->FD, i*bpg*Disk->BlockSize, 1024, &Disk->SuperBlock);
\r