3 * - By John Hodge (thePowersGang)
10 #include "ext2_common.h"
13 #define BLOCK_DIR_OFS(_data, _block) ((Uint16*)(_data)[(_block)])
16 char *Ext2_ReadDir(tVFS_Node *Node, int Pos);
17 tVFS_Node *Ext2_FindDir(tVFS_Node *Node, const char *FileName);
18 int Ext2_MkNod(tVFS_Node *Node, const char *Name, Uint Flags);
19 int Ext2_Unlink(tVFS_Node *Node, const char *OldName);
20 int Ext2_Link(tVFS_Node *Parent, const char *Name, tVFS_Node *Node);
22 tVFS_Node *Ext2_int_CreateNode(tExt2_Disk *Disk, Uint InodeId);
25 tVFS_NodeType gExt2_DirType = {
26 .TypeName = "ext2-dir",
27 .ReadDir = Ext2_ReadDir,
28 .FindDir = Ext2_FindDir,
30 .Unlink = Ext2_Unlink,
32 .Close = Ext2_CloseFile
34 tVFS_NodeType gExt2_FileType = {
35 .TypeName = "ext2-file",
38 .Close = Ext2_CloseFile
43 * \brief Reads a directory entry
44 * \param Node Directory node
45 * \param Pos Position of desired element
47 char *Ext2_ReadDir(tVFS_Node *Node, int Pos)
51 Uint64 Base; // Block's Base Address
55 tExt2_Disk *disk = Node->ImplPtr;
58 ENTER("pNode iPos", Node, Pos);
60 // Read directory's inode
61 Ext2_int_ReadInode(disk, Node->Inode, &inode);
64 LOG("inode={.i_block[0]= 0x%x, .i_size=0x%x}", inode.i_block[0], inode.i_size);
68 // - Do this ourselves as it is a simple operation
69 Base = inode.i_block[0] * disk->BlockSize;
71 while(Pos -- && size > 0 && size <= inode.i_size)
73 VFS_ReadAt( disk->FD, Base+ofs, sizeof(tExt2_DirEnt), &dirent);
74 ofs += dirent.rec_len;
75 size -= dirent.rec_len;
78 if(ofs >= disk->BlockSize) {
80 if( ofs > disk->BlockSize ) {
81 Log_Warning("EXT2", "Directory Entry %i of inode %i extends over a block boundary, ignoring",
82 entNum-1, Node->Inode);
85 Base = Ext2_int_GetBlockAddr( disk, inode.i_block, block );
93 // Check for the end of the list
94 if(size <= 0 || size > inode.i_size) {
100 VFS_ReadAt( disk->FD, Base+ofs, sizeof(tExt2_DirEnt), &dirent );
101 LOG("dirent={.rec_len=%i,.inode=0x%x,.name_len=%i}",
102 dirent.rec_len, dirent.inode, dirent.name_len);
103 dirent.name[ dirent.name_len ] = '\0'; // Cap off string
105 if( dirent.name_len == 0 ) {
106 LEAVE('p', VFS_SKIP);
110 // Ignore . and .. (these are done in the VFS)
111 if( (dirent.name[0] == '.' && dirent.name[1] == '\0')
112 || (dirent.name[0] == '.' && dirent.name[1] == '.' && dirent.name[2]=='\0')) {
113 LEAVE('p', VFS_SKIP);
114 return VFS_SKIP; // Skip
117 LEAVE('s', dirent.name);
119 return strdup(dirent.name);
123 * \brief Gets information about a file
124 * \param Node Parent Node
125 * \param Filename Name of wanted file
126 * \return VFS Node of file
128 tVFS_Node *Ext2_FindDir(tVFS_Node *Node, const char *Filename)
130 tExt2_Disk *disk = Node->ImplPtr;
133 Uint64 Base; // Block's Base Address
138 int filenameLen = strlen(Filename);
140 // Read directory's inode
141 Ext2_int_ReadInode(disk, Node->Inode, &inode);
145 // - Do this ourselves as it is a simple operation
146 Base = inode.i_block[0] * disk->BlockSize;
150 VFS_ReadAt( disk->FD, Base+ofs, sizeof(tExt2_DirEnt), &dirent);
151 // TODO: Possible overrun if name_len == 255?
152 dirent.name[ dirent.name_len ] = '\0'; // Cap off string
153 // If it matches, create a node and return it
154 if(dirent.name_len == filenameLen && strcmp(dirent.name, Filename) == 0)
155 return Ext2_int_CreateNode( disk, dirent.inode );
156 // Increment pointers
157 ofs += dirent.rec_len;
158 size -= dirent.rec_len;
161 // Check for end of block
162 if(ofs >= disk->BlockSize) {
164 if( ofs > disk->BlockSize ) {
165 Log_Warning("EXT2", "Directory Entry %i of inode %i extends over a block boundary, ignoring",
166 entNum-1, Node->Inode);
169 Base = Ext2_int_GetBlockAddr( disk, inode.i_block, block );
177 * \fn int Ext2_MkNod(tVFS_Node *Parent, const char *Name, Uint Flags)
178 * \brief Create a new node
180 int Ext2_MkNod(tVFS_Node *Parent, const char *Name, Uint Flags)
182 ENTER("pParent sName xFlags", Parent, Name, Flags);
184 Uint64 inodeNum = Ext2_int_AllocateInode(Parent->ImplPtr, Parent->Inode);
185 if( inodeNum == 0 ) {
188 tVFS_Node *child = Ext2_int_CreateNode(Parent->ImplPtr, inodeNum);
190 Ext2_int_DereferenceInode(Parent->ImplPtr, inodeNum);
194 child->Flags = Flags & (VFS_FFLAG_DIRECTORY|VFS_FFLAG_SYMLINK|VFS_FFLAG_READONLY);
195 child->UID = Threads_GetUID();
196 child->GID = Threads_GetGID();
201 child->ImplInt = 0; // ImplInt is the link count
204 int rv = Ext2_Link(Parent, Name, child);
205 child->Type->Close(child);
211 * \brief Rename a file
212 * \param Node This (directory) node
213 * \param OldName Old name of file
214 * \param NewName New name for file
215 * \return Boolean Failure - See ::tVFS_Node.Unlink for info
217 int Ext2_Unlink(tVFS_Node *Node, const char *OldName)
223 * \brief Links an existing node to a new name
224 * \param Parent Parent (directory) node
225 * \param Name New name for the node
226 * \param Node Node to link
227 * \return Boolean Failure - See ::tVFS_Node.Link for info
229 int Ext2_Link(tVFS_Node *Node, const char *Name, tVFS_Node *Child)
232 tExt2_Disk *disk = Node->ImplPtr;
235 tExt2_DirEnt newEntry;
236 Uint64 Base; // Block's Base Address
237 int block = 0, ofs = 0;
240 int bestMatch = -1, bestSize, bestBlock, bestOfs;
243 blockData = malloc(disk->BlockSize);
245 // Read child inode (get's the file type)
246 Ext2_int_ReadInode(disk, Child->Inode, &inode);
248 // Create a stub entry
249 newEntry.inode = Child->Inode;
250 newEntry.name_len = strlen(Name);
251 newEntry.rec_len = (newEntry.name_len+3+8)&~3;
252 newEntry.type = inode.i_mode >> 12;
253 memcpy(newEntry.name, Name, newEntry.name_len);
255 // Read directory's inode
256 Ext2_int_ReadInode(disk, Node->Inode, &inode);
259 // Get a lock on the inode
260 Ext2_int_LockInode(disk, Node->Inode);
263 // - Do this ourselves as it is a simple operation
264 base = inode.i_block[0] * disk->BlockSize;
265 VFS_ReadAt( disk->FD, base, disk->BlockSize, blockData );
270 dirent = blockData + ofs;
271 // Sanity Check the entry
272 if(ofs + dirent->rec_len > disk->BlockSize) {
274 "Directory entry %i of inode 0x%x extends over a block boundary",
275 nEntries, (Uint)Node->Inode);
280 if(dirent->type == 0) {
281 if( dirent->rec_len >= newEntry.rec_len
282 && (bestMatch == -1 || bestSize > dirent->rec_len) )
284 bestMatch = nEntries;
285 bestSize = dirent->rec_len;
290 // Non free - check name to avoid duplicates
292 if(strncmp(Name, dirent->name, dirent->name_len) == 0) {
293 Ext2_int_UnlockInode(disk, Node->Inode);
299 // Increment the pointer
301 ofs += dirent->rec_len;
302 if( ofs >= disk->BlockSize ) {
303 // Read the next block if needed
304 BLOCK_DIR_OFS(Node->Data, block) = nEntries;
307 base = Ext2_int_GetBlockAddr(disk, inode.i_block, block);
308 VFS_ReadAt( disk->FD, base, disk->BlockSize, blockData );
312 // Check if a free slot was found
313 if( bestMatch >= 0 ) {
315 bestBlock = Ext2_int_GetBlockAddr(disk, inode.i_block, bestBlock);
317 bestMatch = BLOCK_DIR_OFS(Node->Data, bestBlock);
318 VFS_ReadAt( disk->FD, base, disk->BlockSize, blockData );
319 dirent = blockData + bestOfs;
320 memcpy(dirent, newEntry, newEntry.rec_len);
321 VFS_WriteAt( disk->FD, base, disk->BlockSize, blockData );
324 // Allocate block, Write
325 block = Ext2_int_AllocateBlock(Disk, block);
326 Log_Warning("EXT2", "");
329 Ext2_int_UnlockInode(disk, Node->Inode);
336 // ---- INTERNAL FUNCTIONS ----
338 * \fn vfs_node *Ext2_int_CreateNode(tExt2_Disk *Disk, Uint InodeID)
339 * \brief Create a new VFS Node
341 tVFS_Node *Ext2_int_CreateNode(tExt2_Disk *Disk, Uint InodeID)
347 if( !Ext2_int_ReadInode(Disk, InodeID, &inode) )
350 if( (tmpNode = Inode_GetCache(Disk->CacheID, InodeID)) )
353 memset(&retNode, 0, sizeof(retNode));
356 retNode.Inode = InodeID;
357 retNode.ImplPtr = Disk;
360 retNode.Size = inode.i_size;
362 // Set Access Permissions
363 retNode.UID = inode.i_uid;
364 retNode.GID = inode.i_gid;
366 retNode.ACLs = VFS_UnixToAcessACL(inode.i_mode & 0777, inode.i_uid, inode.i_gid);
368 // Set Function Pointers
369 retNode.Type = &gExt2_FileType;
371 switch(inode.i_mode & EXT2_S_IFMT)
375 retNode.Flags = VFS_FFLAG_SYMLINK;
380 retNode.Size |= (Uint64)inode.i_dir_acl << 32;
384 retNode.Type = &gExt2_DirType;
385 retNode.Flags = VFS_FFLAG_DIRECTORY;
386 retNode.Data = calloc( sizeof(Uint16), DivUp(retNode.Size, Disk->BlockSize) );
388 // Unknown, Write protect it to be safe
390 retNode.Flags = VFS_FFLAG_READONLY;
395 retNode.ATime = inode.i_atime * 1000;
396 retNode.MTime = inode.i_mtime * 1000;
397 retNode.CTime = inode.i_ctime * 1000;
399 // Save in node cache and return saved node
400 return Inode_CacheNode(Disk->CacheID, &retNode);