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);
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)
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 // TODO: Possible overrun if name_len == 255?
151 dirent.name[ dirent.name_len ] = '\0'; // Cap off string
152 // If it matches, create a node and return it
153 if(dirent.name_len == filenameLen && strcmp(dirent.name, Filename) == 0)
154 return Ext2_int_CreateNode( disk, dirent.inode );
155 // Increment pointers
156 ofs += dirent.rec_len;
157 size -= dirent.rec_len;
160 // Check for end of block
161 if(ofs >= disk->BlockSize) {
163 if( ofs > disk->BlockSize ) {
164 Log_Warning("EXT2", "Directory Entry %i of inode %i extends over a block boundary, ignoring",
165 entNum-1, Node->Inode);
168 Base = Ext2_int_GetBlockAddr( disk, inode.i_block, block );
176 * \fn int Ext2_MkNod(tVFS_Node *Parent, const char *Name, Uint Flags)
177 * \brief Create a new node
179 tVFS_Node *Ext2_MkNod(tVFS_Node *Parent, const char *Name, Uint Flags)
181 ENTER("pParent sName xFlags", Parent, Name, Flags);
183 Uint64 inodeNum = Ext2_int_AllocateInode(Parent->ImplPtr, Parent->Inode);
184 if( inodeNum == 0 ) {
185 LOG("Inode allocation failed");
186 LEAVE_RET('n', NULL);
188 tVFS_Node *child = Ext2_int_CreateNode(Parent->ImplPtr, inodeNum);
190 Ext2_int_DereferenceInode(Parent->ImplPtr, inodeNum);
191 Log_Warning("Ext2", "Ext2_MkNod - Node creation failed");
192 LEAVE_RET('n', NULL);
195 child->Flags = Flags & (VFS_FFLAG_DIRECTORY|VFS_FFLAG_SYMLINK|VFS_FFLAG_READONLY);
196 child->UID = Threads_GetUID();
197 child->GID = Threads_GetGID();
202 child->ImplInt = 0; // ImplInt is the link count
205 int rv = Ext2_Link(Parent, Name, child);
207 Ext2_CloseFile(child);
210 LEAVE_RET('p', child);
214 * \brief Rename a file
215 * \param Node This (directory) node
216 * \param OldName Old name of file
217 * \param NewName New name for file
218 * \return Boolean Failure - See ::tVFS_Node.Unlink for info
220 int Ext2_Unlink(tVFS_Node *Node, const char *OldName)
222 Log_Warning("Ext2", "TODO: Impliment Ext2_Unlink");
227 * \brief Links an existing node to a new name
228 * \param Parent Parent (directory) node
229 * \param Name New name for the node
230 * \param Node Node to link
231 * \return Boolean Failure - See ::tVFS_Node.Link for info
233 int Ext2_Link(tVFS_Node *Node, const char *Name, tVFS_Node *Child)
235 tExt2_Disk *disk = Node->ImplPtr;
237 tExt2_DirEnt *dirent;
238 tExt2_DirEnt newEntry;
239 Uint64 base; // Block's Base Address
240 int block = 0, ofs = 0;
244 int bestSize=0, bestBlock=0, bestOfs=0, bestNeedsSplit=0;
247 ENTER("pNode sName pChild",
250 blockData = malloc(disk->BlockSize);
252 // Read child inode (get's the file type)
253 Ext2_int_ReadInode(disk, Child->Inode, &inode);
255 // Create a stub entry
256 newEntry.inode = Child->Inode;
257 newEntry.name_len = strlen(Name);
258 newEntry.rec_len = ((newEntry.name_len+3)&~3) + EXT2_DIRENT_SIZE;
259 newEntry.type = inode.i_mode >> 12;
260 memcpy(newEntry.name, Name, newEntry.name_len);
262 // Read directory's inode
263 Ext2_int_ReadInode(disk, Node->Inode, &inode);
266 // Get a lock on the inode
267 //Ext2_int_LockInode(disk, Node->Inode);
268 Mutex_Acquire(&Node->Lock);
270 // if( !Node->Data ) {
274 // - Do this ourselves as it is a simple operation
275 base = inode.i_block[0] * disk->BlockSize;
276 VFS_ReadAt( disk->FD, base, disk->BlockSize, blockData );
282 dirent = blockData + ofs;
283 // Sanity Check the entry
284 if(ofs + dirent->rec_len > disk->BlockSize) {
286 "Directory entry %i of inode 0x%x extends over a block boundary",
287 nEntries, (Uint)Node->Inode);
291 LOG("Entry %i: %x %i bytes", nEntries, dirent->type, dirent->rec_len);
293 if(dirent->type == 0)
295 if( dirent->rec_len >= newEntry.rec_len
296 && (bestMatch == -1 || bestSize > dirent->rec_len) )
298 bestMatch = nEntries;
299 bestSize = dirent->rec_len;
305 // Non free - check name to avoid duplicates
308 LOG(" name='%.*s'", dirent->name_len, dirent->name);
309 if(strncmp(Name, dirent->name, dirent->name_len) == 0) {
310 //Ext2_int_UnlockInode(disk, Node->Inode);
311 Mutex_Release(&Node->Lock);
316 int spare_space = dirent->rec_len - (dirent->name_len + EXT2_DIRENT_SIZE);
317 if( spare_space > newEntry.rec_len
318 && (bestMatch == -1 || bestSize > spare_space) )
320 bestMatch = nEntries;
321 bestSize = spare_space;
329 // Increment the pointer
331 ofs += dirent->rec_len;
332 size -= dirent->rec_len;
333 if( size > 0 && ofs >= disk->BlockSize ) {
334 // Read the next block if needed
335 // BLOCK_DIR_OFS(Node->Data, block) = nEntries;
338 base = Ext2_int_GetBlockAddr(disk, inode.i_block, block);
339 VFS_ReadAt( disk->FD, base, disk->BlockSize, blockData );
343 LOG("bestMatch = %i", bestMatch);
344 // If EOF was reached with no space, check if we can fit one on the end
345 if( bestMatch < 0 && ofs + newEntry.rec_len < disk->BlockSize ) {
346 Node->Size += newEntry.rec_len;
347 Node->Flags |= VFS_FFLAG_DIRTY;
350 bestSize = newEntry.rec_len;
353 // Check if a free slot was found
357 base = Ext2_int_GetBlockAddr(disk, inode.i_block, bestBlock);
358 VFS_ReadAt( disk->FD, base, disk->BlockSize, blockData );
359 dirent = blockData + bestOfs;
360 // Shorten a pre-existing entry
363 dirent->rec_len = EXT2_DIRENT_SIZE + dirent->name_len;
364 bestOfs += dirent->rec_len;
365 //bestSize -= dirent->rec_len; // (not needed, bestSize is the spare space after)
366 dirent = blockData + bestOfs;
368 // Insert new file entry
369 memcpy(dirent, &newEntry, newEntry.rec_len);
370 // Create a new blank entry
371 if( bestSize != newEntry.rec_len )
373 bestOfs += newEntry.rec_len;
374 dirent = blockData + bestOfs;
376 dirent->rec_len = bestSize - newEntry.rec_len;
380 VFS_WriteAt( disk->FD, base, disk->BlockSize, blockData );
383 // Allocate block, Write
384 Uint32 newblock = Ext2_int_AllocateBlock(disk, base / disk->BlockSize);
385 Ext2_int_AppendBlock(disk, &inode, newblock);
386 base = newblock * disk->BlockSize;
387 Node->Size += newEntry.rec_len;
388 Node->Flags |= VFS_FFLAG_DIRTY;
389 memcpy(blockData, &newEntry, newEntry.rec_len);
390 memset(blockData + newEntry.rec_len, 0, disk->BlockSize - newEntry.rec_len);
391 VFS_WriteAt( disk->FD, base, disk->BlockSize, blockData );
395 Child->Flags |= VFS_FFLAG_DIRTY;
397 //Ext2_int_UnlockInode(disk, Node->Inode);
398 Mutex_Release(&Node->Lock);