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])
47 tExt2_Inode *inode = (void*)(Node+1);
49 Uint64 Base; // Block's Base Address
53 tExt2_Disk *disk = Node->ImplPtr;
56 ENTER("pNode iPos", Node, Pos);
58 // Read directory's inode
61 LOG("inode={.i_block[0]= 0x%x, .i_size=0x%x}", inode->i_block[0], inode->i_size);
65 // - Do this ourselves as it is a simple operation
66 Base = inode->i_block[0] * disk->BlockSize;
68 while(Pos -- && size > 0 && size <= inode->i_size)
70 VFS_ReadAt( disk->FD, Base+ofs, sizeof(tExt2_DirEnt), &dirent);
71 ofs += dirent.rec_len;
72 size -= dirent.rec_len;
75 if(ofs >= disk->BlockSize) {
77 if( ofs > disk->BlockSize ) {
78 Log_Warning("EXT2", "Directory Entry %i of inode %i extends over a block boundary, ignoring",
79 entNum-1, Node->Inode);
82 Base = Ext2_int_GetBlockAddr( disk, inode->i_block, block );
90 // Check for the end of the list
91 if(size <= 0 || size > inode->i_size) {
97 VFS_ReadAt( disk->FD, Base+ofs, sizeof(tExt2_DirEnt), &dirent );
98 LOG("dirent={.rec_len=%i,.inode=0x%x,.name_len=%i}",
99 dirent.rec_len, dirent.inode, dirent.name_len);
100 dirent.name[ dirent.name_len ] = '\0'; // Cap off string
102 if( dirent.name_len == 0 ) {
107 // Ignore . and .. (these are done in the VFS)
108 if( (dirent.name[0] == '.' && dirent.name[1] == '\0')
109 || (dirent.name[0] == '.' && dirent.name[1] == '.' && dirent.name[2]=='\0')) {
114 LOG("Name '%s'", dirent.name);
115 strncpy(Dest, dirent.name, FILENAME_MAX);
121 * \brief Gets information about a file
122 * \param Node Parent Node
123 * \param Filename Name of wanted file
124 * \return VFS Node of file
126 tVFS_Node *Ext2_FindDir(tVFS_Node *Node, const char *Filename, Uint Flags)
128 tExt2_Disk *disk = Node->ImplPtr;
129 tExt2_Inode *inode = (void*)(Node+1);
131 Uint64 Base; // Block's Base Address
136 int filenameLen = strlen(Filename);
138 // Read directory's inode
139 size = inode->i_size;
142 // - Do this ourselves as it is a simple operation
143 Base = inode->i_block[0] * disk->BlockSize;
147 VFS_ReadAt( disk->FD, Base+ofs, sizeof(tExt2_DirEnt), &dirent);
148 // If it matches, create a node and return it
149 if(dirent.name_len == filenameLen && strncmp(dirent.name, Filename, filenameLen) == 0)
150 return Ext2_int_CreateNode( disk, dirent.inode );
151 // Increment pointers
152 ofs += dirent.rec_len;
153 size -= dirent.rec_len;
156 // Check for end of block
157 if(ofs >= disk->BlockSize) {
159 if( ofs > disk->BlockSize ) {
160 Log_Warning("EXT2", "Directory Entry %i of inode %i extends over a block boundary, ignoring",
161 entNum-1, Node->Inode);
164 Base = Ext2_int_GetBlockAddr( disk, inode->i_block, block );
172 * \fn int Ext2_MkNod(tVFS_Node *Parent, const char *Name, Uint Flags)
173 * \brief Create a new node
175 tVFS_Node *Ext2_MkNod(tVFS_Node *Parent, const char *Name, Uint Flags)
177 ENTER("pParent sName xFlags", Parent, Name, Flags);
179 Uint64 inodeNum = Ext2_int_AllocateInode(Parent->ImplPtr, Parent->Inode);
180 if( inodeNum == 0 ) {
181 LOG("Inode allocation failed");
182 LEAVE_RET('n', NULL);
184 tVFS_Node *child = Ext2_int_CreateNode(Parent->ImplPtr, inodeNum);
186 Ext2_int_DereferenceInode(Parent->ImplPtr, inodeNum);
187 Log_Warning("Ext2", "Ext2_MkNod - Node creation failed");
188 LEAVE_RET('n', NULL);
191 child->Flags = Flags & (VFS_FFLAG_DIRECTORY|VFS_FFLAG_SYMLINK|VFS_FFLAG_READONLY);
192 child->UID = Threads_GetUID();
193 child->GID = Threads_GetGID();
198 child->ImplInt = 0; // ImplInt is the link count
201 int rv = Ext2_Link(Parent, Name, child);
203 Ext2_CloseFile(child);
206 LEAVE_RET('p', child);
210 * \brief Rename a file
211 * \param Node This (directory) node
212 * \param OldName Old name of file
213 * \param NewName New name for file
214 * \return Boolean Failure - See ::tVFS_Node.Unlink for info
216 int Ext2_Unlink(tVFS_Node *Node, const char *OldName)
218 Log_Warning("Ext2", "TODO: Impliment Ext2_Unlink");
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)
231 tExt2_Disk *disk = Node->ImplPtr;
232 tExt2_Inode *inode = (void*)(Node+1);
233 tExt2_DirEnt *dirent;
234 tExt2_DirEnt newEntry;
235 Uint64 base; // Block's Base Address
236 int block = 0, ofs = 0;
239 int bestSize=0, bestBlock=0, bestOfs=0, bestNeedsSplit=0;
242 ENTER("pNode sName pChild",
245 void *blockData = malloc(disk->BlockSize);
247 // Create a stub entry
248 newEntry.inode = Child->Inode;
249 newEntry.name_len = strlen(Name);
250 newEntry.rec_len = ((newEntry.name_len+3)&~3) + EXT2_DIRENT_SIZE;
251 newEntry.type = inode->i_mode >> 12;
252 memcpy(newEntry.name, Name, newEntry.name_len);
254 // Read directory's inode
255 size = inode->i_size;
257 // Get a lock on the inode
258 //Ext2_int_LockInode(disk, Node->Inode);
259 Mutex_Acquire(&Node->Lock);
261 // if( !Node->Data ) {
265 // - Do this ourselves as it is a simple operation
266 base = inode->i_block[0] * disk->BlockSize;
267 VFS_ReadAt( disk->FD, base, disk->BlockSize, blockData );
273 dirent = blockData + ofs;
274 // Sanity Check the entry
275 if(ofs + dirent->rec_len > disk->BlockSize) {
277 "Directory entry %i of inode 0x%x extends over a block boundary",
278 nEntries, (Uint)Node->Inode);
282 LOG("Entry %i: %x %i bytes", nEntries, dirent->type, dirent->rec_len);
284 if(dirent->type == 0)
286 if( dirent->rec_len >= newEntry.rec_len
287 && (bestMatch == -1 || bestSize > dirent->rec_len) )
289 bestMatch = nEntries;
290 bestSize = dirent->rec_len;
296 // Non free - check name to avoid duplicates
299 LOG(" name='%.*s'", dirent->name_len, dirent->name);
300 if(strncmp(Name, dirent->name, dirent->name_len) == 0) {
301 //Ext2_int_UnlockInode(disk, Node->Inode);
305 int spare_space = dirent->rec_len - (dirent->name_len + EXT2_DIRENT_SIZE);
306 if( spare_space > newEntry.rec_len
307 && (bestMatch == -1 || bestSize > spare_space) )
309 bestMatch = nEntries;
310 bestSize = spare_space;
318 // Increment the pointer
320 ofs += dirent->rec_len;
321 size -= dirent->rec_len;
322 if( size > 0 && ofs >= disk->BlockSize ) {
323 // Read the next block if needed
324 // BLOCK_DIR_OFS(Node->Data, block) = nEntries;
327 base = Ext2_int_GetBlockAddr(disk, inode->i_block, block);
328 VFS_ReadAt( disk->FD, base, disk->BlockSize, blockData );
332 LOG("bestMatch = %i", bestMatch);
333 // If EOF was reached with no space, check if we can fit one on the end
334 if( bestMatch < 0 && ofs + newEntry.rec_len < disk->BlockSize ) {
335 Node->Size += newEntry.rec_len;
336 Node->Flags |= VFS_FFLAG_DIRTY;
339 bestSize = newEntry.rec_len;
342 // Check if a free slot was found
346 base = Ext2_int_GetBlockAddr(disk, inode->i_block, bestBlock);
347 VFS_ReadAt( disk->FD, base, disk->BlockSize, blockData );
348 dirent = blockData + bestOfs;
349 // Shorten a pre-existing entry
352 dirent->rec_len = EXT2_DIRENT_SIZE + dirent->name_len;
353 bestOfs += dirent->rec_len;
354 //bestSize -= dirent->rec_len; // (not needed, bestSize is the spare space after)
355 dirent = blockData + bestOfs;
357 // Insert new file entry
358 memcpy(dirent, &newEntry, newEntry.rec_len);
359 // Create a new blank entry
360 if( bestSize != newEntry.rec_len )
362 bestOfs += newEntry.rec_len;
363 dirent = blockData + bestOfs;
365 dirent->rec_len = bestSize - newEntry.rec_len;
369 VFS_WriteAt( disk->FD, base, disk->BlockSize, blockData );
372 // Allocate block, Write
373 Uint32 newblock = Ext2_int_AllocateBlock(disk, base / disk->BlockSize);
374 Ext2_int_AppendBlock(Node, inode, newblock);
375 base = newblock * disk->BlockSize;
376 Node->Size += newEntry.rec_len;
377 Node->Flags |= VFS_FFLAG_DIRTY;
378 memcpy(blockData, &newEntry, newEntry.rec_len);
379 memset(blockData + newEntry.rec_len, 0, disk->BlockSize - newEntry.rec_len);
380 VFS_WriteAt( disk->FD, base, disk->BlockSize, blockData );
384 Child->Flags |= VFS_FFLAG_DIRTY;
386 //Ext2_int_UnlockInode(disk, Node->Inode);
388 Mutex_Release(&Node->Lock);
393 Mutex_Release(&Node->Lock);