Kernel - Added 'Flags' param to VFS Read/Write/FindDir
[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, 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);
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, Uint Flags)
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                 // 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;
156                 entNum ++;
157                 
158                 // Check for end of block
159                 if(ofs >= disk->BlockSize) {
160                         block ++;
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);
164                         }
165                         ofs = 0;
166                         Base = Ext2_int_GetBlockAddr( disk, inode.i_block, block );
167                 }
168         }
169         
170         return NULL;
171 }
172
173 /**
174  * \fn int Ext2_MkNod(tVFS_Node *Parent, const char *Name, Uint Flags)
175  * \brief Create a new node
176  */
177 tVFS_Node *Ext2_MkNod(tVFS_Node *Parent, const char *Name, Uint Flags)
178 {
179         ENTER("pParent sName xFlags", Parent, Name, Flags);
180         
181         Uint64 inodeNum = Ext2_int_AllocateInode(Parent->ImplPtr, Parent->Inode);
182         if( inodeNum == 0 ) {
183                 LOG("Inode allocation failed");
184                 LEAVE_RET('n', NULL);
185         }
186         tVFS_Node *child = Ext2_int_CreateNode(Parent->ImplPtr, inodeNum);
187         if( !child ) {
188                 Ext2_int_DereferenceInode(Parent->ImplPtr, inodeNum);
189                 Log_Warning("Ext2", "Ext2_MkNod - Node creation failed");
190                 LEAVE_RET('n', NULL);
191         }
192
193         child->Flags = Flags & (VFS_FFLAG_DIRECTORY|VFS_FFLAG_SYMLINK|VFS_FFLAG_READONLY);
194         child->UID = Threads_GetUID();
195         child->GID = Threads_GetGID();
196         child->CTime =
197                 child->MTime =
198                 child->ATime =
199                 now();
200         child->ImplInt = 0;     // ImplInt is the link count
201         // TODO: Set up ACLs
202
203         int rv = Ext2_Link(Parent, Name, child);
204         if( rv ) {
205                 Ext2_CloseFile(child);
206                 return NULL;
207         }
208         LEAVE_RET('p', child);
209 }
210
211 /**
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
217  */
218 int Ext2_Unlink(tVFS_Node *Node, const char *OldName)
219 {
220         Log_Warning("Ext2", "TODO: Impliment Ext2_Unlink");
221         return 1;
222 }
223
224 /**
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
230  */
231 int Ext2_Link(tVFS_Node *Node, const char *Name, tVFS_Node *Child)
232 {       
233         tExt2_Disk      *disk = Node->ImplPtr;
234         tExt2_Inode     inode;
235         tExt2_DirEnt    *dirent;
236         tExt2_DirEnt    newEntry;
237         Uint64  base;   // Block's Base Address
238          int    block = 0, ofs = 0;
239         Uint    size;
240         void    *blockData;
241          int    bestMatch = -1;
242          int    bestSize=0, bestBlock=0, bestOfs=0, bestNeedsSplit=0;
243          int    nEntries;
244
245         ENTER("pNode sName pChild",
246                 Node, Name, Child);
247         
248         blockData = malloc(disk->BlockSize);
249         
250         // Read child inode (get's the file type)
251         Ext2_int_ReadInode(disk, Child->Inode, &inode);
252         
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);
259         
260         // Read directory's inode
261         Ext2_int_ReadInode(disk, Node->Inode, &inode);
262         size = inode.i_size;
263         
264         // Get a lock on the inode
265         //Ext2_int_LockInode(disk, Node->Inode);
266         Mutex_Acquire(&Node->Lock);
267
268 //      if( !Node->Data ) {
269 //      }
270
271         // Get First Block
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 );
275         block = 0;
276         nEntries = 0;
277         // Find File
278         while(size > 0)
279         {
280                 dirent = blockData + ofs;
281                 // Sanity Check the entry
282                 if(ofs + dirent->rec_len > disk->BlockSize) {
283                         Log_Warning("EXT2",
284                                 "Directory entry %i of inode 0x%x extends over a block boundary",
285                                 nEntries, (Uint)Node->Inode);
286                 }
287                 else
288                 {
289                         LOG("Entry %i: %x %i bytes", nEntries, dirent->type, dirent->rec_len);
290                         // Free entry
291                         if(dirent->type == 0)
292                         {
293                                 if( dirent->rec_len >= newEntry.rec_len
294                                  && (bestMatch == -1 || bestSize > dirent->rec_len) )
295                                 {
296                                         bestMatch = nEntries;
297                                         bestSize = dirent->rec_len;
298                                         bestBlock = block;
299                                         bestOfs = ofs;
300                                         bestNeedsSplit = 0;
301                                 }
302                         }
303                         // Non free - check name to avoid duplicates
304                         else
305                         {
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);
310                                         LEAVE('i', 1);
311                                         return 1;       // ERR_???
312                                 }
313                                 
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) )
317                                 {
318                                         bestMatch = nEntries;
319                                         bestSize = spare_space;
320                                         bestBlock = block;
321                                         bestOfs = ofs;
322                                         bestNeedsSplit = 1;
323                                 }
324                         }
325                 }
326                 
327                 // Increment the pointer
328                 nEntries ++;
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;
334                         block ++;
335                         ofs = 0;
336                         base = Ext2_int_GetBlockAddr(disk, inode.i_block, block);
337                         VFS_ReadAt( disk->FD, base, disk->BlockSize, blockData );
338                 }
339         }
340         
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;
346                 bestBlock = block;
347                 bestOfs = ofs;
348                 bestSize = newEntry.rec_len;
349                 bestNeedsSplit = 0;
350         }
351         // Check if a free slot was found
352         if( bestMatch >= 0 )
353         {
354                 // Read-Modify-Write
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
359                 if(bestNeedsSplit)
360                 {
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;
365                 }
366                 // Insert new file entry
367                 memcpy(dirent, &newEntry, newEntry.rec_len);
368                 // Create a new blank entry
369                 if( bestSize != newEntry.rec_len )
370                 {
371                         bestOfs += newEntry.rec_len;
372                         dirent = blockData + bestOfs;
373
374                         dirent->rec_len = bestSize - newEntry.rec_len;                  
375                         dirent->type = 0;
376                 }
377                 // Save changes
378                 VFS_WriteAt( disk->FD, base, disk->BlockSize, blockData );
379         }
380         else {
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 );
390         }
391
392         Child->ImplInt ++;
393         Child->Flags |= VFS_FFLAG_DIRTY;
394
395         //Ext2_int_UnlockInode(disk, Node->Inode);
396         Mutex_Release(&Node->Lock);
397         LEAVE('i', 0);
398         return 0;
399 }
400

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