]>
Commit | Line | Data |
---|---|---|
cae7420b DB |
1 | /** @file\r |
2 | Functions for directory cache operation.\r | |
b9ec9330 | 3 | \r |
6163cc98 | 4 | Copyright (c) 2005, Intel Corporation. All rights reserved.<BR>\r |
eb6cb4ce | 5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
b9ec9330 QH |
6 | \r |
7 | \r | |
cae7420b | 8 | **/\r |
b9ec9330 | 9 | \r |
cae7420b | 10 | #include "Fat.h"\r |
b9ec9330 | 11 | \r |
cae7420b | 12 | /**\r |
b9ec9330 | 13 | \r |
cae7420b | 14 | Free the directory structure and release the memory.\r |
b9ec9330 | 15 | \r |
cae7420b | 16 | @param ODir - The directory to be freed.\r |
b9ec9330 | 17 | \r |
cae7420b | 18 | **/\r |
b9ec9330 QH |
19 | STATIC\r |
20 | VOID\r | |
21 | FatFreeODir (\r | |
bcdcc416 | 22 | IN FAT_ODIR *ODir\r |
b9ec9330 | 23 | )\r |
b9ec9330 QH |
24 | {\r |
25 | FAT_DIRENT *DirEnt;\r | |
26 | \r | |
27 | //\r | |
28 | // Release Directory Entry Nodes\r | |
29 | //\r | |
30 | while (!IsListEmpty (&ODir->ChildList)) {\r | |
31 | DirEnt = DIRENT_FROM_LINK (ODir->ChildList.ForwardLink);\r | |
32 | RemoveEntryList (&DirEnt->Link);\r | |
33 | //\r | |
34 | // Make sure the OFile has been closed\r | |
35 | //\r | |
36 | ASSERT (DirEnt->OFile == NULL);\r | |
37 | FatFreeDirEnt (DirEnt);\r | |
38 | }\r | |
39 | \r | |
40 | FreePool (ODir);\r | |
41 | }\r | |
42 | \r | |
cae7420b DB |
43 | /**\r |
44 | \r | |
45 | Allocate the directory structure.\r | |
46 | \r | |
47 | @param OFile - The corresponding OFile.\r | |
48 | \r | |
49 | **/\r | |
b9ec9330 QH |
50 | STATIC\r |
51 | FAT_ODIR *\r | |
52 | FatAllocateODir (\r | |
bcdcc416 | 53 | IN FAT_OFILE *OFile\r |
b9ec9330 | 54 | )\r |
b9ec9330 QH |
55 | {\r |
56 | FAT_ODIR *ODir;\r | |
57 | \r | |
58 | ODir = AllocateZeroPool (sizeof (FAT_ODIR));\r | |
59 | if (ODir != NULL) {\r | |
60 | //\r | |
61 | // Initialize the directory entry list\r | |
62 | //\r | |
63 | ODir->Signature = FAT_ODIR_SIGNATURE;\r | |
64 | InitializeListHead (&ODir->ChildList);\r | |
65 | ODir->CurrentCursor = &ODir->ChildList;\r | |
66 | }\r | |
67 | \r | |
68 | return ODir;\r | |
69 | }\r | |
70 | \r | |
cae7420b | 71 | /**\r |
b9ec9330 QH |
72 | \r |
73 | Discard the directory structure when an OFile will be freed.\r | |
74 | Volume will cache this directory if the OFile does not represent a deleted file.\r | |
75 | \r | |
cae7420b | 76 | @param OFile - The OFile whose directory structure is to be discarded.\r |
b9ec9330 | 77 | \r |
cae7420b DB |
78 | **/\r |
79 | VOID\r | |
80 | FatDiscardODir (\r | |
bcdcc416 | 81 | IN FAT_OFILE *OFile\r |
cae7420b | 82 | )\r |
b9ec9330 QH |
83 | {\r |
84 | FAT_ODIR *ODir;\r | |
85 | FAT_VOLUME *Volume;\r | |
86 | \r | |
bcdcc416 MK |
87 | Volume = OFile->Volume;\r |
88 | ODir = OFile->ODir;\r | |
b9ec9330 QH |
89 | if (!OFile->DirEnt->Invalid) {\r |
90 | //\r | |
91 | // If OFile does not represent a deleted file, then we will cache the directory\r | |
92 | // We use OFile's first cluster as the directory's tag\r | |
93 | //\r | |
94 | ODir->DirCacheTag = OFile->FileCluster;\r | |
95 | InsertHeadList (&Volume->DirCacheList, &ODir->DirCacheLink);\r | |
96 | if (Volume->DirCacheCount == FAT_MAX_DIR_CACHE_COUNT) {\r | |
97 | //\r | |
98 | // Replace the least recent used directory\r | |
99 | //\r | |
100 | ODir = ODIR_FROM_DIRCACHELINK (Volume->DirCacheList.BackLink);\r | |
101 | RemoveEntryList (&ODir->DirCacheLink);\r | |
102 | } else {\r | |
103 | //\r | |
104 | // No need to find a replace\r | |
105 | //\r | |
106 | Volume->DirCacheCount++;\r | |
107 | ODir = NULL;\r | |
108 | }\r | |
109 | }\r | |
bcdcc416 | 110 | \r |
b9ec9330 QH |
111 | //\r |
112 | // Release ODir Structure\r | |
113 | //\r | |
114 | if (ODir != NULL) {\r | |
115 | FatFreeODir (ODir);\r | |
116 | }\r | |
117 | }\r | |
118 | \r | |
cae7420b | 119 | /**\r |
b9ec9330 | 120 | \r |
b9ec9330 QH |
121 | \r |
122 | Request the directory structure when an OFile is newly generated.\r | |
123 | If the directory structure is cached by volume, then just return this directory;\r | |
124 | Otherwise, allocate a new one for OFile.\r | |
125 | \r | |
cae7420b | 126 | @param OFile - The OFile which requests directory structure.\r |
b9ec9330 | 127 | \r |
cae7420b DB |
128 | **/\r |
129 | VOID\r | |
130 | FatRequestODir (\r | |
bcdcc416 | 131 | IN FAT_OFILE *OFile\r |
cae7420b | 132 | )\r |
b9ec9330 | 133 | {\r |
bcdcc416 MK |
134 | UINTN DirCacheTag;\r |
135 | FAT_VOLUME *Volume;\r | |
136 | FAT_ODIR *ODir;\r | |
137 | FAT_ODIR *CurrentODir;\r | |
138 | LIST_ENTRY *CurrentODirLink;\r | |
b9ec9330 QH |
139 | \r |
140 | Volume = OFile->Volume;\r | |
141 | ODir = NULL;\r | |
142 | DirCacheTag = OFile->FileCluster;\r | |
143 | for (CurrentODirLink = Volume->DirCacheList.ForwardLink;\r | |
144 | CurrentODirLink != &Volume->DirCacheList;\r | |
145 | CurrentODirLink = CurrentODirLink->ForwardLink\r | |
bcdcc416 MK |
146 | )\r |
147 | {\r | |
b9ec9330 QH |
148 | CurrentODir = ODIR_FROM_DIRCACHELINK (CurrentODirLink);\r |
149 | if (CurrentODir->DirCacheTag == DirCacheTag) {\r | |
150 | RemoveEntryList (&CurrentODir->DirCacheLink);\r | |
151 | Volume->DirCacheCount--;\r | |
152 | ODir = CurrentODir;\r | |
153 | break;\r | |
154 | }\r | |
155 | }\r | |
156 | \r | |
157 | if (ODir == NULL) {\r | |
158 | //\r | |
159 | // This directory is not cached, then allocate a new one\r | |
160 | //\r | |
161 | ODir = FatAllocateODir (OFile);\r | |
162 | }\r | |
163 | \r | |
164 | OFile->ODir = ODir;\r | |
165 | }\r | |
166 | \r | |
cae7420b | 167 | /**\r |
b9ec9330 QH |
168 | \r |
169 | Clean up all the cached directory structures when the volume is going to be abandoned.\r | |
170 | \r | |
cae7420b | 171 | @param Volume - FAT file system volume.\r |
b9ec9330 | 172 | \r |
cae7420b DB |
173 | **/\r |
174 | VOID\r | |
175 | FatCleanupODirCache (\r | |
bcdcc416 | 176 | IN FAT_VOLUME *Volume\r |
cae7420b | 177 | )\r |
b9ec9330 QH |
178 | {\r |
179 | FAT_ODIR *ODir;\r | |
bcdcc416 | 180 | \r |
b9ec9330 QH |
181 | while (Volume->DirCacheCount > 0) {\r |
182 | ODir = ODIR_FROM_DIRCACHELINK (Volume->DirCacheList.BackLink);\r | |
183 | RemoveEntryList (&ODir->DirCacheLink);\r | |
184 | FatFreeODir (ODir);\r | |
185 | Volume->DirCacheCount--;\r | |
186 | }\r | |
187 | }\r |