a9cdbd689a425a87c377341e1224c19d766fd399
[tpg/acess2.git] / KernelLand / Modules / Filesystems / FAT / nodecache.c
1 /*
2  * Acess2 FAT12/16/32 Driver
3  * - By John Hodge (thePowersGang)
4  *
5  * nodecache.c
6  * - FAT-Specific node caching
7  */
8 #include <acess.h>
9 #include <vfs.h>
10 #include "common.h"
11
12 // === PROTOTYPES ===
13 extern tVFS_Node        *FAT_int_CacheNode(tFAT_VolInfo *Disk, const tVFS_Node *Node);
14
15 // === CODE ===
16 tTime FAT_int_GetAcessTimestamp(Uint16 Date, Uint16 Time, Uint8 MS)
17 {
18         return MS * 10 + timestamp(
19                 // Seconds         Minutes              Hours
20                 (Time & 0x1F) * 2, (Time >> 5) & 0x3F, (Time >> 11) & 0x1F,
21                 // Day             Month                    Year
22                 (Date & 0x1F) - 1, ((Date >> 5) & 0xF) - 1, 1980 + ((Date >> 9) & 0xFF)
23                 );
24 }
25
26 void FAT_int_GetFATTimestamp(tTime AcessTimestamp, Uint16 *Date, Uint16 *Time, Uint8 *MS)
27 {
28          int    y, m, d;
29          int    h, min, s, ms;
30         format_date(AcessTimestamp, &y, &m, &d, &h, &min, &s, &ms);
31         if(Date)
32                 *Date = (d + 1) | ((m + 1) << 5) | ((y - 1980) << 9);
33         if(Time)
34                 *Time = (s / 2) | (min << 5) | (h << 11);
35         if(MS)
36                 *MS = (ms / 10) + (s & 1) * 100;
37 }
38
39 /**
40  * \brief Creates a tVFS_Node structure for a given file entry
41  * \param Parent        Parent directory VFS node
42  * \param Entry File table entry for the new node
43  */
44 tVFS_Node *FAT_int_CreateNode(tVFS_Node *Parent, fat_filetable *Entry)
45 {
46         tVFS_Node       node;
47         tVFS_Node       *ret;
48         tFAT_VolInfo    *disk = Parent->ImplPtr;
49
50         ENTER("pParent pEntry", Parent, Entry);
51         LOG("disk = %p", disk);
52         
53         if( (ret = FAT_int_GetNode(disk, Entry->cluster | (Entry->clusterHi<<16))) ) {
54                 LEAVE('p', ret);
55                 return ret;
56         }
57
58         memset(&node, 0, sizeof(tVFS_Node));
59         
60         // Set Other Data
61         // 0-27: Cluster, 32-59: Parent Cluster
62         node.Inode = Entry->cluster | (Entry->clusterHi<<16) | (Parent->Inode << 32);
63         LOG("node.Inode = %llx", node.Inode);
64         node.ImplInt = 0;
65         // Disk Pointer
66         node.ImplPtr = disk;
67         node.Size = Entry->size;
68         LOG("Entry->size = %i", Entry->size);
69         // root:root
70         node.UID = 0;   node.GID = 0;
71         node.NumACLs = 1;
72         
73         node.Flags = 0;
74         if(Entry->attrib & ATTR_DIRECTORY)      node.Flags |= VFS_FFLAG_DIRECTORY;
75         if(Entry->attrib & ATTR_READONLY) {
76                 node.Flags |= VFS_FFLAG_READONLY;
77                 node.ACLs = &gVFS_ACL_EveryoneRX;       // R-XR-XR-X
78         }
79         else {
80                 node.ACLs = &gVFS_ACL_EveryoneRWX;      // RWXRWXRWX
81         }
82         
83         // Create timestamps
84         node.CTime = FAT_int_GetAcessTimestamp(Entry->cdate, Entry->ctime, Entry->ctimems);
85         node.MTime = FAT_int_GetAcessTimestamp(Entry->mdate, Entry->mtime, 0);
86         node.ATime = FAT_int_GetAcessTimestamp(Entry->adate, 0, 0);
87         
88         // Set pointers
89         if(node.Flags & VFS_FFLAG_DIRECTORY) {
90                 //Log_Debug("FAT", "Directory %08x has size 0x%x", node.Inode, node.Size);
91                 node.Type = &gFAT_DirType;      
92                 node.Size = -1;
93         }
94         else {
95                 node.Type = &gFAT_FileType;
96         }
97
98         // TODO: Cache node     
99         ret = FAT_int_CacheNode(disk, &node);
100         LEAVE('p', ret);
101         return ret;
102 }
103
104 tVFS_Node *FAT_int_CreateIncompleteDirNode(tFAT_VolInfo *Disk, Uint32 Cluster)
105 {
106         if( Cluster == Disk->rootOffset )
107                 return &Disk->rootNode;
108         
109         // If the directory isn't in the cache, what do?
110         // - we want to lock it such that we don't collide, but don't want to put crap data in the cache
111         // - Put a temp node in with a flag that indicates it's incomplete?
112         
113         Mutex_Acquire(&Disk->lNodeCache);
114         tFAT_CachedNode *cnode;
115
116         for(cnode = Disk->NodeCache; cnode; cnode = cnode->Next)
117         {
118                 if( (cnode->Node.Inode & 0xFFFFFFFF) == Cluster ) {
119                         cnode->Node.ReferenceCount ++;
120                         Mutex_Release(&Disk->lNodeCache);
121                         return &cnode->Node;
122                 }
123         }       
124
125         // Create a temporary node?
126
127         Mutex_Release(&Disk->lNodeCache);
128         return NULL;
129 }
130
131 tVFS_Node *FAT_int_GetNode(tFAT_VolInfo *Disk, Uint32 Cluster)
132 {
133         if( Cluster == Disk->rootOffset )
134                 return &Disk->rootNode;
135         Mutex_Acquire(&Disk->lNodeCache);
136         tFAT_CachedNode *cnode;
137
138         for(cnode = Disk->NodeCache; cnode; cnode = cnode->Next)
139         {
140                 if( (cnode->Node.Inode & 0xFFFFFFFF) == Cluster ) {
141                         cnode->Node.ReferenceCount ++;
142                         Mutex_Release(&Disk->lNodeCache);
143                         return &cnode->Node;
144                 }
145         }       
146
147         Mutex_Release(&Disk->lNodeCache);
148         return NULL;
149 }
150
151 tVFS_Node *FAT_int_CacheNode(tFAT_VolInfo *Disk, const tVFS_Node *Node)
152 {
153         tFAT_CachedNode *cnode, *prev = NULL;
154         Mutex_Acquire(&Disk->lNodeCache);
155         
156         for(cnode = Disk->NodeCache; cnode; prev = cnode, cnode = cnode->Next )
157         {
158                 if( cnode->Node.Inode == Node->Inode ) {
159                         cnode->Node.ReferenceCount ++;
160                         Mutex_Release(&Disk->lNodeCache);
161                         return &cnode->Node;
162                 }
163         }
164         
165         cnode = malloc(sizeof(tFAT_CachedNode));
166         cnode->Next = NULL;
167         memcpy(&cnode->Node, Node, sizeof(tVFS_Node));
168         cnode->Node.ReferenceCount = 1;
169         
170         if( prev )
171                 prev->Next = cnode;
172         else
173                 Disk->NodeCache = cnode;
174         
175         Mutex_Release(&Disk->lNodeCache);
176         return &cnode->Node;
177 }
178
179 int FAT_int_DerefNode(tVFS_Node *Node)
180 {
181         tFAT_VolInfo    *Disk = Node->ImplPtr;
182         tFAT_CachedNode *cnode, *prev = NULL;
183          int    bFreed = 0;
184
185         if( Node == &Disk->rootNode )
186                 return 0;
187
188         Mutex_Acquire(&Disk->lNodeCache);
189         Node->ReferenceCount --;
190         for(cnode = Disk->NodeCache; cnode; prev = cnode, cnode = cnode->Next )
191         {
192                 if(Node == &cnode->Node) {
193                         if(prev)
194                                 prev->Next = cnode->Next;
195                         else
196                                 Disk->NodeCache = cnode->Next;
197                         break;
198                 }
199         }
200         if(Node->ReferenceCount == 0 && cnode) {
201                 // Already out of the list :)
202                 free(cnode->Node.Data);
203                 free(cnode);
204                 bFreed = 1;
205         }
206         Mutex_Release(&Disk->lNodeCache);
207         if( !cnode ) {
208                 // Not here?
209                 return -1;
210         }
211         
212         return bFreed;
213 }
214
215 void FAT_int_ClearNodeCache(tFAT_VolInfo *Disk)
216 {
217         // TODO: In theory when this is called, all handles will be closed
218 }

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