3 * - By John Hodge (thePowersGang)
10 #include "ext2_common.h"
13 #define BLOCK_DIR_OFS(_data, _block) (((Uint16*)(_data))[(_block)])
16 int Ext2_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX]);
17 tVFS_Node *Ext2_FindDir(tVFS_Node *Node, const char *FileName, Uint Flags);
18 tVFS_Node *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);
23 tVFS_NodeType gExt2_DirType = {
24 .TypeName = "ext2-dir",
25 .ReadDir = Ext2_ReadDir,
26 .FindDir = Ext2_FindDir,
28 .Unlink = Ext2_Unlink,
30 .Close = Ext2_CloseFile
32 tVFS_NodeType gExt2_FileType = {
33 .TypeName = "ext2-file",
36 .Close = Ext2_CloseFile
41 * \brief Reads a directory entry
42 * \param Node Directory node
43 * \param Pos Position of desired element
45 int Ext2_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX])
49 Uint64 Base; // Block's Base Address
53 tExt2_Disk *disk = Node->ImplPtr;
56 ENTER("pNode iPos", Node, Pos);
58 // Read directory's inode
59 Ext2_int_ReadInode(disk, Node->Inode, &inode);
62 LOG("inode={.i_block[0]= 0x%x, .i_size=0x%x}", inode.i_block[0], inode.i_size);
66 // - Do this ourselves as it is a simple operation
67 Base = inode.i_block[0] * disk->BlockSize;
69 while(Pos -- && size > 0 && size <= inode.i_size)
71 VFS_ReadAt( disk->FD, Base+ofs, sizeof(tExt2_DirEnt), &dirent);
72 ofs += dirent.rec_len;
73 size -= dirent.rec_len;
76 if(ofs >= disk->BlockSize) {
78 if( ofs > disk->BlockSize ) {
79 Log_Warning("EXT2", "Directory Entry %i of inode %i extends over a block boundary, ignoring",
80 entNum-1, Node->Inode);
83 Base = Ext2_int_GetBlockAddr( disk, inode.i_block, block );
91 // Check for the end of the list
92 if(size <= 0 || size > inode.i_size) {
98 VFS_ReadAt( disk->FD, Base+ofs, sizeof(tExt2_DirEnt), &dirent );
99 LOG("dirent={.rec_len=%i,.inode=0x%x,.name_len=%i}",
100 dirent.rec_len, dirent.inode, dirent.name_len);
101 dirent.name[ dirent.name_len ] = '\0'; // Cap off string
103 if( dirent.name_len == 0 ) {
108 // Ignore . and .. (these are done in the VFS)
109 if( (dirent.name[0] == '.' && dirent.name[1] == '\0')
110 || (dirent.name[0] == '.' && dirent.name[1] == '.' && dirent.name[2]=='\0')) {
115 LOG("Name '%s'", dirent.name);
116 strncpy(Dest, dirent.name, FILENAME_MAX);
122 * \brief Gets information about a file
123 * \param Node Parent Node
124 * \param Filename Name of wanted file
125 * \return VFS Node of file
127 tVFS_Node *Ext2_FindDir(tVFS_Node *Node, const char *Filename, Uint Flags)
129 tExt2_Disk *disk = Node->ImplPtr;
132 Uint64 Base; // Block's Base Address
137 int filenameLen = strlen(Filename);
139 // Read directory's inode
140 Ext2_int_ReadInode(disk, Node->Inode, &inode);
144 // - Do this ourselves as it is a simple operation
145 Base = inode.i_block[0] * disk->BlockSize;
149 VFS_ReadAt( disk->FD, Base+ofs, sizeof(tExt2_DirEnt), &dirent);
150 // If it matches, create a node and return it
151 if(dirent.name_len == filenameLen && strncmp(dirent.name, Filename, filenameLen) == 0)
152 return Ext2_int_CreateNode( disk, dirent.inode );
153 // Increment pointers
154 ofs += dirent.rec_len;
155 size -= dirent.rec_len;
158 // Check for end of block
159 if(ofs >= disk->BlockSize) {
161 if( ofs > disk->BlockSize ) {
162 Log_Warning("EXT2", "Directory Entry %i of inode %i extends over a block boundary, ignoring",
163 entNum-1, Node->Inode);
166 Base = Ext2_int_GetBlockAddr( disk, inode.i_block, block );
174 * \fn int Ext2_MkNod(tVFS_Node *Parent, const char *Name, Uint Flags)
175 * \brief Create a new node
177 tVFS_Node *Ext2_MkNod(tVFS_Node *Parent, const char *Name, Uint Flags)
179 ENTER("pParent sName xFlags", Parent, Name, Flags);
181 Uint64 inodeNum = Ext2_int_AllocateInode(Parent->ImplPtr, Parent->Inode);
182 if( inodeNum == 0 ) {
183 LOG("Inode allocation failed");
184 LEAVE_RET('n', NULL);
186 tVFS_Node *child = Ext2_int_CreateNode(Parent->ImplPtr, inodeNum);
188 Ext2_int_DereferenceInode(Parent->ImplPtr, inodeNum);
189 Log_Warning("Ext2", "Ext2_MkNod - Node creation failed");
190 LEAVE_RET('n', NULL);
193 child->Flags = Flags & (VFS_FFLAG_DIRECTORY|VFS_FFLAG_SYMLINK|VFS_FFLAG_READONLY);
194 child->UID = Threads_GetUID();
195 child->GID = Threads_GetGID();
200 child->ImplInt = 0; // ImplInt is the link count
203 int rv = Ext2_Link(Parent, Name, child);
205 Ext2_CloseFile(child);
208 LEAVE_RET('p', child);
212 * \brief Rename a file
213 * \param Node This (directory) node
214 * \param OldName Old name of file
215 * \param NewName New name for file
216 * \return Boolean Failure - See ::tVFS_Node.Unlink for info
218 int Ext2_Unlink(tVFS_Node *Node, const char *OldName)
220 Log_Warning("Ext2", "TODO: Impliment Ext2_Unlink");
225 * \brief Links an existing node to a new name
226 * \param Parent Parent (directory) node
227 * \param Name New name for the node
228 * \param Node Node to link
229 * \return Boolean Failure - See ::tVFS_Node.Link for info
231 int Ext2_Link(tVFS_Node *Node, const char *Name, tVFS_Node *Child)
233 tExt2_Disk *disk = Node->ImplPtr;
235 tExt2_DirEnt *dirent;
236 tExt2_DirEnt newEntry;
237 Uint64 base; // Block's Base Address
238 int block = 0, ofs = 0;
242 int bestSize=0, bestBlock=0, bestOfs=0, bestNeedsSplit=0;
245 ENTER("pNode sName pChild",
248 blockData = malloc(disk->BlockSize);
250 // Read child inode (get's the file type)
251 Ext2_int_ReadInode(disk, Child->Inode, &inode);
253 // Create a stub entry
254 newEntry.inode = Child->Inode;
255 newEntry.name_len = strlen(Name);
256 newEntry.rec_len = ((newEntry.name_len+3)&~3) + EXT2_DIRENT_SIZE;
257 newEntry.type = inode.i_mode >> 12;
258 memcpy(newEntry.name, Name, newEntry.name_len);
260 // Read directory's inode
261 Ext2_int_ReadInode(disk, Node->Inode, &inode);
264 // Get a lock on the inode
265 //Ext2_int_LockInode(disk, Node->Inode);
266 Mutex_Acquire(&Node->Lock);
268 // if( !Node->Data ) {
272 // - Do this ourselves as it is a simple operation
273 base = inode.i_block[0] * disk->BlockSize;
274 VFS_ReadAt( disk->FD, base, disk->BlockSize, blockData );
280 dirent = blockData + ofs;
281 // Sanity Check the entry
282 if(ofs + dirent->rec_len > disk->BlockSize) {
284 "Directory entry %i of inode 0x%x extends over a block boundary",
285 nEntries, (Uint)Node->Inode);
289 LOG("Entry %i: %x %i bytes", nEntries, dirent->type, dirent->rec_len);
291 if(dirent->type == 0)
293 if( dirent->rec_len >= newEntry.rec_len
294 && (bestMatch == -1 || bestSize > dirent->rec_len) )
296 bestMatch = nEntries;
297 bestSize = dirent->rec_len;
303 // Non free - check name to avoid duplicates
306 LOG(" name='%.*s'", dirent->name_len, dirent->name);
307 if(strncmp(Name, dirent->name, dirent->name_len) == 0) {
308 //Ext2_int_UnlockInode(disk, Node->Inode);
309 Mutex_Release(&Node->Lock);
314 int spare_space = dirent->rec_len - (dirent->name_len + EXT2_DIRENT_SIZE);
315 if( spare_space > newEntry.rec_len
316 && (bestMatch == -1 || bestSize > spare_space) )
318 bestMatch = nEntries;
319 bestSize = spare_space;
327 // Increment the pointer
329 ofs += dirent->rec_len;
330 size -= dirent->rec_len;
331 if( size > 0 && ofs >= disk->BlockSize ) {
332 // Read the next block if needed
333 // BLOCK_DIR_OFS(Node->Data, block) = nEntries;
336 base = Ext2_int_GetBlockAddr(disk, inode.i_block, block);
337 VFS_ReadAt( disk->FD, base, disk->BlockSize, blockData );
341 LOG("bestMatch = %i", bestMatch);
342 // If EOF was reached with no space, check if we can fit one on the end
343 if( bestMatch < 0 && ofs + newEntry.rec_len < disk->BlockSize ) {
344 Node->Size += newEntry.rec_len;
345 Node->Flags |= VFS_FFLAG_DIRTY;
348 bestSize = newEntry.rec_len;
351 // Check if a free slot was found
355 base = Ext2_int_GetBlockAddr(disk, inode.i_block, bestBlock);
356 VFS_ReadAt( disk->FD, base, disk->BlockSize, blockData );
357 dirent = blockData + bestOfs;
358 // Shorten a pre-existing entry
361 dirent->rec_len = EXT2_DIRENT_SIZE + dirent->name_len;
362 bestOfs += dirent->rec_len;
363 //bestSize -= dirent->rec_len; // (not needed, bestSize is the spare space after)
364 dirent = blockData + bestOfs;
366 // Insert new file entry
367 memcpy(dirent, &newEntry, newEntry.rec_len);
368 // Create a new blank entry
369 if( bestSize != newEntry.rec_len )
371 bestOfs += newEntry.rec_len;
372 dirent = blockData + bestOfs;
374 dirent->rec_len = bestSize - newEntry.rec_len;
378 VFS_WriteAt( disk->FD, base, disk->BlockSize, blockData );
381 // Allocate block, Write
382 Uint32 newblock = Ext2_int_AllocateBlock(disk, base / disk->BlockSize);
383 Ext2_int_AppendBlock(disk, &inode, newblock);
384 base = newblock * disk->BlockSize;
385 Node->Size += newEntry.rec_len;
386 Node->Flags |= VFS_FFLAG_DIRTY;
387 memcpy(blockData, &newEntry, newEntry.rec_len);
388 memset(blockData + newEntry.rec_len, 0, disk->BlockSize - newEntry.rec_len);
389 VFS_WriteAt( disk->FD, base, disk->BlockSize, blockData );
393 Child->Flags |= VFS_FFLAG_DIRTY;
395 //Ext2_int_UnlockInode(disk, Node->Inode);
396 Mutex_Release(&Node->Lock);