33df84721749233236ee431a89016468d6a11f73
[tpg/acess2.git] / KernelLand / Modules / Filesystems / Ext2 / dir.c
1 /*
2  * Acess2 Ext2 Driver
3  * - By John Hodge (thePowersGang)
4  *
5  * dir.c
6  * - Directory Handling
7  */
8 #define DEBUG   0
9 #define VERBOSE 0
10 #include "ext2_common.h"
11
12 // === MACROS ===
13 #define BLOCK_DIR_OFS(_data, _block)    (((Uint16*)(_data))[(_block)])
14
15 // === PROTOTYPES ===
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);
21
22 // === GLOBALS ===
23 tVFS_NodeType   gExt2_DirType = {
24         .TypeName = "ext2-dir",
25         .ReadDir = Ext2_ReadDir,
26         .FindDir = Ext2_FindDir,
27         .MkNod = Ext2_MkNod,
28         .Unlink = Ext2_Unlink,
29         .Link = Ext2_Link,
30         .Close = Ext2_CloseFile
31         };
32 tVFS_NodeType   gExt2_FileType = {
33         .TypeName = "ext2-file",
34         .Read = Ext2_Read,
35         .Write = Ext2_Write,
36         .Close = Ext2_CloseFile
37         };
38
39 // === CODE ===
40 /**
41  * \brief Reads a directory entry
42  * \param Node  Directory node
43  * \param Pos   Position of desired element
44  */
45 int Ext2_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX])
46 {
47         tExt2_Inode     inode;
48         tExt2_DirEnt    dirent;
49         Uint64  Base;   // Block's Base Address
50          int    block = 0;
51         Uint    ofs = 0;
52          int    entNum = 0;
53         tExt2_Disk      *disk = Node->ImplPtr;
54         Uint    size;
55         
56         ENTER("pNode iPos", Node, Pos);
57         
58         // Read directory's inode
59         Ext2_int_ReadInode(disk, Node->Inode, &inode);
60         size = inode.i_size;
61         
62         LOG("inode={.i_block[0]= 0x%x, .i_size=0x%x}", inode.i_block[0], inode.i_size);
63         
64         // Find Entry
65         // Get First Block
66         // - Do this ourselves as it is a simple operation
67         Base = inode.i_block[0] * disk->BlockSize;
68         // Scan directory
69         while(Pos -- && size > 0 && size <= inode.i_size)
70         {
71                 VFS_ReadAt( disk->FD, Base+ofs, sizeof(tExt2_DirEnt), &dirent);
72                 ofs += dirent.rec_len;
73                 size -= dirent.rec_len;
74                 entNum ++;
75                 
76                 if(ofs >= disk->BlockSize) {
77                         block ++;
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);
81                         }
82                         ofs = 0;
83                         Base = Ext2_int_GetBlockAddr( disk, inode.i_block, block );
84                         if( Base == 0 ) {
85                                 size = 0;
86                                 break;
87                         }
88                 }
89         }
90         
91         // Check for the end of the list
92         if(size <= 0 || size > inode.i_size) {
93                 LEAVE('i', -ENOENT);
94                 return -ENOENT;
95         }
96         
97         // Read Entry
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
102         
103         if( dirent.name_len == 0 ) {
104                 LEAVE('i', 1);
105                 return 1;
106         }
107         
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')) {
111                 LEAVE('i', 1);
112                 return 1;       // Skip
113         }
114         
115         LOG("Name '%s'", dirent.name);
116         strncpy(Dest, dirent.name, FILENAME_MAX);
117         LEAVE('i', 0);
118         return 0;
119 }
120
121 /**
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
126  */
127 tVFS_Node *Ext2_FindDir(tVFS_Node *Node, const char *Filename)
128 {
129         tExt2_Disk      *disk = Node->ImplPtr;
130         tExt2_Inode     inode;
131         tExt2_DirEnt    dirent;
132         Uint64  Base;   // Block's Base Address
133          int    block = 0;
134         Uint    ofs = 0;
135          int    entNum = 0;
136         Uint    size;
137          int    filenameLen = strlen(Filename);
138         
139         // Read directory's inode
140         Ext2_int_ReadInode(disk, Node->Inode, &inode);
141         size = inode.i_size;
142         
143         // Get First Block
144         // - Do this ourselves as it is a simple operation
145         Base = inode.i_block[0] * disk->BlockSize;
146         // Find File
147         while(size > 0)
148         {
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;
158                 entNum ++;
159                 
160                 // Check for end of block
161                 if(ofs >= disk->BlockSize) {
162                         block ++;
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);
166                         }
167                         ofs = 0;
168                         Base = Ext2_int_GetBlockAddr( disk, inode.i_block, block );
169                 }
170         }
171         
172         return NULL;
173 }
174
175 /**
176  * \fn int Ext2_MkNod(tVFS_Node *Parent, const char *Name, Uint Flags)
177  * \brief Create a new node
178  */
179 tVFS_Node *Ext2_MkNod(tVFS_Node *Parent, const char *Name, Uint Flags)
180 {
181         ENTER("pParent sName xFlags", Parent, Name, Flags);
182         
183         Uint64 inodeNum = Ext2_int_AllocateInode(Parent->ImplPtr, Parent->Inode);
184         if( inodeNum == 0 ) {
185                 LOG("Inode allocation failed");
186                 LEAVE_RET('n', NULL);
187         }
188         tVFS_Node *child = Ext2_int_CreateNode(Parent->ImplPtr, inodeNum);
189         if( !child ) {
190                 Ext2_int_DereferenceInode(Parent->ImplPtr, inodeNum);
191                 Log_Warning("Ext2", "Ext2_MkNod - Node creation failed");
192                 LEAVE_RET('n', NULL);
193         }
194
195         child->Flags = Flags & (VFS_FFLAG_DIRECTORY|VFS_FFLAG_SYMLINK|VFS_FFLAG_READONLY);
196         child->UID = Threads_GetUID();
197         child->GID = Threads_GetGID();
198         child->CTime =
199                 child->MTime =
200                 child->ATime =
201                 now();
202         child->ImplInt = 0;     // ImplInt is the link count
203         // TODO: Set up ACLs
204
205         int rv = Ext2_Link(Parent, Name, child);
206         if( rv ) {
207                 Ext2_CloseFile(child);
208                 return NULL;
209         }
210         LEAVE_RET('p', child);
211 }
212
213 /**
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
219  */
220 int Ext2_Unlink(tVFS_Node *Node, const char *OldName)
221 {
222         Log_Warning("Ext2", "TODO: Impliment Ext2_Unlink");
223         return 1;
224 }
225
226 /**
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
232  */
233 int Ext2_Link(tVFS_Node *Node, const char *Name, tVFS_Node *Child)
234 {       
235         tExt2_Disk      *disk = Node->ImplPtr;
236         tExt2_Inode     inode;
237         tExt2_DirEnt    *dirent;
238         tExt2_DirEnt    newEntry;
239         Uint64  base;   // Block's Base Address
240          int    block = 0, ofs = 0;
241         Uint    size;
242         void    *blockData;
243          int    bestMatch = -1;
244          int    bestSize=0, bestBlock=0, bestOfs=0, bestNeedsSplit=0;
245          int    nEntries;
246
247         ENTER("pNode sName pChild",
248                 Node, Name, Child);
249         
250         blockData = malloc(disk->BlockSize);
251         
252         // Read child inode (get's the file type)
253         Ext2_int_ReadInode(disk, Child->Inode, &inode);
254         
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);
261         
262         // Read directory's inode
263         Ext2_int_ReadInode(disk, Node->Inode, &inode);
264         size = inode.i_size;
265         
266         // Get a lock on the inode
267         //Ext2_int_LockInode(disk, Node->Inode);
268         Mutex_Acquire(&Node->Lock);
269
270 //      if( !Node->Data ) {
271 //      }
272
273         // Get First Block
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 );
277         block = 0;
278         nEntries = 0;
279         // Find File
280         while(size > 0)
281         {
282                 dirent = blockData + ofs;
283                 // Sanity Check the entry
284                 if(ofs + dirent->rec_len > disk->BlockSize) {
285                         Log_Warning("EXT2",
286                                 "Directory entry %i of inode 0x%x extends over a block boundary",
287                                 nEntries, (Uint)Node->Inode);
288                 }
289                 else
290                 {
291                         LOG("Entry %i: %x %i bytes", nEntries, dirent->type, dirent->rec_len);
292                         // Free entry
293                         if(dirent->type == 0)
294                         {
295                                 if( dirent->rec_len >= newEntry.rec_len
296                                  && (bestMatch == -1 || bestSize > dirent->rec_len) )
297                                 {
298                                         bestMatch = nEntries;
299                                         bestSize = dirent->rec_len;
300                                         bestBlock = block;
301                                         bestOfs = ofs;
302                                         bestNeedsSplit = 0;
303                                 }
304                         }
305                         // Non free - check name to avoid duplicates
306                         else
307                         {
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);
312                                         LEAVE('i', 1);
313                                         return 1;       // ERR_???
314                                 }
315                                 
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) )
319                                 {
320                                         bestMatch = nEntries;
321                                         bestSize = spare_space;
322                                         bestBlock = block;
323                                         bestOfs = ofs;
324                                         bestNeedsSplit = 1;
325                                 }
326                         }
327                 }
328                 
329                 // Increment the pointer
330                 nEntries ++;
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;
336                         block ++;
337                         ofs = 0;
338                         base = Ext2_int_GetBlockAddr(disk, inode.i_block, block);
339                         VFS_ReadAt( disk->FD, base, disk->BlockSize, blockData );
340                 }
341         }
342         
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;
348                 bestBlock = block;
349                 bestOfs = ofs;
350                 bestSize = newEntry.rec_len;
351                 bestNeedsSplit = 0;
352         }
353         // Check if a free slot was found
354         if( bestMatch >= 0 )
355         {
356                 // Read-Modify-Write
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
361                 if(bestNeedsSplit)
362                 {
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;
367                 }
368                 // Insert new file entry
369                 memcpy(dirent, &newEntry, newEntry.rec_len);
370                 // Create a new blank entry
371                 if( bestSize != newEntry.rec_len )
372                 {
373                         bestOfs += newEntry.rec_len;
374                         dirent = blockData + bestOfs;
375
376                         dirent->rec_len = bestSize - newEntry.rec_len;                  
377                         dirent->type = 0;
378                 }
379                 // Save changes
380                 VFS_WriteAt( disk->FD, base, disk->BlockSize, blockData );
381         }
382         else {
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 );
392         }
393
394         Child->ImplInt ++;
395         Child->Flags |= VFS_FFLAG_DIRTY;
396
397         //Ext2_int_UnlockInode(disk, Node->Inode);
398         Mutex_Release(&Node->Lock);
399         LEAVE('i', 0);
400         return 0;
401 }
402

UCC git Repository :: git.ucc.asn.au