1 |
786 |
skrzyp |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// fatfs_tcache.c
|
4 |
|
|
//
|
5 |
|
|
// FAT file system FAT table cache functions
|
6 |
|
|
//
|
7 |
|
|
//==========================================================================
|
8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 2003 Free Software Foundation, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
16 |
|
|
// version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
// for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use
|
28 |
|
|
// macros or inline functions from this file, or you compile this file
|
29 |
|
|
// and link it with other works to produce a work based on this file,
|
30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
// the GNU General Public License. However the source code for this file
|
32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
// General Public License v2.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
36 |
|
|
// on this file might be covered by the GNU General Public License.
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//==========================================================================
|
40 |
|
|
//#####DESCRIPTIONBEGIN####
|
41 |
|
|
//
|
42 |
|
|
// Author(s): savin
|
43 |
|
|
// Date: 2003-06-27
|
44 |
|
|
//
|
45 |
|
|
//####DESCRIPTIONEND####
|
46 |
|
|
//
|
47 |
|
|
//==========================================================================
|
48 |
|
|
|
49 |
|
|
#include <pkgconf/fs_fat.h>
|
50 |
|
|
#include <pkgconf/infra.h>
|
51 |
|
|
#include <cyg/kernel/kapi.h>
|
52 |
|
|
#include <cyg/infra/cyg_type.h>
|
53 |
|
|
#include <cyg/infra/cyg_ass.h>
|
54 |
|
|
#include <cyg/infra/cyg_trac.h>
|
55 |
|
|
#include <cyg/infra/diag.h>
|
56 |
|
|
#include <sys/types.h>
|
57 |
|
|
|
58 |
|
|
#include "fatfs.h"
|
59 |
|
|
|
60 |
|
|
//==========================================================================
|
61 |
|
|
// Defines & macros
|
62 |
|
|
|
63 |
|
|
#define TC_INC FATFS_FAT_TABLE_CACHE_INCREMENT
|
64 |
|
|
|
65 |
|
|
#ifdef CYGDBG_USE_ASSERTS
|
66 |
|
|
# define USE_ASSERTS 1
|
67 |
|
|
#endif
|
68 |
|
|
|
69 |
|
|
#ifdef FATFS_TRACE_FAT_TABLE_CACHE
|
70 |
|
|
# define TTC 1
|
71 |
|
|
#else
|
72 |
|
|
# define TTC 0
|
73 |
|
|
#endif
|
74 |
|
|
|
75 |
|
|
//==========================================================================
|
76 |
|
|
// Private functions
|
77 |
|
|
|
78 |
|
|
//--------------------------------------------------------------------------
|
79 |
|
|
// tcache_increment()
|
80 |
|
|
// Allocates or reallocates memory for FAT table cache.
|
81 |
|
|
// Returns true is allocation succeded.
|
82 |
|
|
|
83 |
|
|
static bool
|
84 |
|
|
tcache_increment(fatfs_disk_t *disk, fatfs_tcache_t *tcache)
|
85 |
|
|
{
|
86 |
|
|
CYG_TRACE2(TTC, "max_size=%d size=%d", tcache->max_size, tcache->size);
|
87 |
|
|
|
88 |
|
|
if (NULL == tcache->clusters)
|
89 |
|
|
{
|
90 |
|
|
tcache->clusters =
|
91 |
|
|
(cyg_uint32 *)cyg_mempool_var_try_alloc(disk->tcache_mpool_h,
|
92 |
|
|
TC_INC*sizeof(cyg_uint32));
|
93 |
|
|
|
94 |
|
|
if (NULL == tcache->clusters)
|
95 |
|
|
return false;
|
96 |
|
|
|
97 |
|
|
tcache->max_size = TC_INC;
|
98 |
|
|
}
|
99 |
|
|
else
|
100 |
|
|
{
|
101 |
|
|
cyg_uint32 *newc;
|
102 |
|
|
|
103 |
|
|
newc = (cyg_uint32 *)cyg_mempool_var_try_alloc(disk->tcache_mpool_h,
|
104 |
|
|
(tcache->max_size+TC_INC)*sizeof(cyg_uint32));
|
105 |
|
|
|
106 |
|
|
if (NULL == newc)
|
107 |
|
|
return false;
|
108 |
|
|
|
109 |
|
|
memcpy(newc, tcache->clusters, (tcache->max_size*sizeof(cyg_uint32)));
|
110 |
|
|
cyg_mempool_var_free(disk->tcache_mpool_h, tcache->clusters);
|
111 |
|
|
|
112 |
|
|
tcache->clusters = newc;
|
113 |
|
|
tcache->max_size += TC_INC;
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
CYG_TRACE2(TTC, "max_size=%d size=%d", tcache->max_size, tcache->size);
|
117 |
|
|
|
118 |
|
|
return true;
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
//==========================================================================
|
122 |
|
|
// Exported functions
|
123 |
|
|
|
124 |
|
|
//--------------------------------------------------------------------------
|
125 |
|
|
// fatfs_tcache_create()
|
126 |
|
|
// Creates FAT table caches memory pool.
|
127 |
|
|
|
128 |
|
|
int
|
129 |
|
|
fatfs_tcache_create(fatfs_disk_t *disk, cyg_uint32 mem_size)
|
130 |
|
|
{
|
131 |
|
|
disk->tcache_mem = (cyg_uint8 *)malloc(mem_size);
|
132 |
|
|
if (NULL == disk->tcache_mem)
|
133 |
|
|
return ENOMEM;
|
134 |
|
|
|
135 |
|
|
cyg_mempool_var_create(disk->tcache_mem, mem_size,
|
136 |
|
|
&disk->tcache_mpool_h, &disk->tcache_mpool);
|
137 |
|
|
return ENOERR;
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
//--------------------------------------------------------------------------
|
141 |
|
|
// fatfs_tcache_delete()
|
142 |
|
|
// Deletes FAT table caches memory pool.
|
143 |
|
|
|
144 |
|
|
void
|
145 |
|
|
fatfs_tcache_delete(fatfs_disk_t *disk)
|
146 |
|
|
{
|
147 |
|
|
cyg_mempool_var_delete(disk->tcache_mpool_h);
|
148 |
|
|
free(disk->tcache_mem);
|
149 |
|
|
}
|
150 |
|
|
|
151 |
|
|
//--------------------------------------------------------------------------
|
152 |
|
|
// fatfs_tcache_init()
|
153 |
|
|
// Initializes FAT table cache structure.
|
154 |
|
|
|
155 |
|
|
void
|
156 |
|
|
fatfs_tcache_init(fatfs_disk_t *disk, fatfs_tcache_t *tcache)
|
157 |
|
|
{
|
158 |
|
|
CYG_CHECK_DATA_PTRC(tcache);
|
159 |
|
|
|
160 |
|
|
tcache->clusters = NULL;
|
161 |
|
|
tcache->size = 0;
|
162 |
|
|
tcache->max_size = 0;
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
//--------------------------------------------------------------------------
|
166 |
|
|
// fatfs_tcache_flush()
|
167 |
|
|
// Frees given tcache.
|
168 |
|
|
|
169 |
|
|
void
|
170 |
|
|
fatfs_tcache_flush(fatfs_disk_t *disk, fatfs_tcache_t *tcache)
|
171 |
|
|
{
|
172 |
|
|
CYG_CHECK_DATA_PTRC(tcache);
|
173 |
|
|
|
174 |
|
|
if (tcache->clusters != NULL)
|
175 |
|
|
{
|
176 |
|
|
cyg_mempool_var_free(disk->tcache_mpool_h, tcache->clusters);
|
177 |
|
|
tcache->clusters = NULL;
|
178 |
|
|
tcache->size = 0;
|
179 |
|
|
tcache->max_size = 0;
|
180 |
|
|
}
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
//--------------------------------------------------------------------------
|
184 |
|
|
// fatfs_tcache_get()
|
185 |
|
|
// Gets the cluster from cache at given position.
|
186 |
|
|
// Returns true if cluster in cache.
|
187 |
|
|
|
188 |
|
|
bool
|
189 |
|
|
fatfs_tcache_get(fatfs_disk_t *disk,
|
190 |
|
|
fatfs_tcache_t *tcache,
|
191 |
|
|
cyg_uint32 num,
|
192 |
|
|
cyg_uint32 *cluster)
|
193 |
|
|
{
|
194 |
|
|
CYG_CHECK_DATA_PTRC(tcache);
|
195 |
|
|
CYG_TRACE2(TTC, "size=%d max_size=%d", tcache->size, tcache->max_size);
|
196 |
|
|
|
197 |
|
|
// Check if requested cluster is cached
|
198 |
|
|
if (num >= tcache->size)
|
199 |
|
|
{
|
200 |
|
|
CYG_TRACE1(TTC, "cluster num=%d not in tcache", num);
|
201 |
|
|
return false;
|
202 |
|
|
}
|
203 |
|
|
|
204 |
|
|
*cluster = tcache->clusters[num];
|
205 |
|
|
CYG_TRACE2(TTC, "got cluster=%d num=%d from tcache", *cluster, num);
|
206 |
|
|
return true;
|
207 |
|
|
}
|
208 |
|
|
|
209 |
|
|
//--------------------------------------------------------------------------
|
210 |
|
|
// fatfs_tcache_get()
|
211 |
|
|
// Gets last cluster in cache.
|
212 |
|
|
// Returns false if cache is empty.
|
213 |
|
|
|
214 |
|
|
bool
|
215 |
|
|
fatfs_tcache_get_last(fatfs_disk_t *disk,
|
216 |
|
|
fatfs_tcache_t *tcache,
|
217 |
|
|
cyg_uint32 *num,
|
218 |
|
|
cyg_uint32 *cluster)
|
219 |
|
|
{
|
220 |
|
|
CYG_CHECK_DATA_PTRC(tcache);
|
221 |
|
|
CYG_TRACE2(TTC, "size=%d max_size=%d", tcache->size, tcache->max_size);
|
222 |
|
|
|
223 |
|
|
// Check if we have something in cache
|
224 |
|
|
if (tcache->size == 0)
|
225 |
|
|
return false;
|
226 |
|
|
|
227 |
|
|
*num = tcache->size - 1;
|
228 |
|
|
*cluster = tcache->clusters[*num];
|
229 |
|
|
CYG_TRACE2(TTC, "got last cluster=%d num=%d from tcache", *cluster, *num);
|
230 |
|
|
return true;
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
//--------------------------------------------------------------------------
|
234 |
|
|
// fatfs_tcache_set()
|
235 |
|
|
// Sets given cluster in cache at given position.
|
236 |
|
|
// There can't be any holes in cache (ie in order to set cluster
|
237 |
|
|
// at position N all clusters from 0 to N-1 must be set before)
|
238 |
|
|
// Returns true if set succeded.
|
239 |
|
|
|
240 |
|
|
bool
|
241 |
|
|
fatfs_tcache_set(fatfs_disk_t *disk,
|
242 |
|
|
fatfs_tcache_t *tcache,
|
243 |
|
|
cyg_uint32 num,
|
244 |
|
|
cyg_uint32 cluster)
|
245 |
|
|
{
|
246 |
|
|
CYG_CHECK_DATA_PTRC(tcache);
|
247 |
|
|
CYG_TRACE4(TTC, "num=%d cluster=%d tcache size=%d max_size=%d",
|
248 |
|
|
num, cluster, tcache->size, tcache->max_size);
|
249 |
|
|
|
250 |
|
|
if (num > tcache->size)
|
251 |
|
|
{
|
252 |
|
|
CYG_TRACE0(TTC, "won't make a hole in tcache");
|
253 |
|
|
return false;
|
254 |
|
|
}
|
255 |
|
|
|
256 |
|
|
if (num < tcache->size)
|
257 |
|
|
{
|
258 |
|
|
CYG_TRACE2(TTC, "set cluster=%d num=%d in tcache", cluster, num);
|
259 |
|
|
tcache->clusters[num] = cluster;
|
260 |
|
|
return true;
|
261 |
|
|
}
|
262 |
|
|
|
263 |
|
|
// Here num is equal to size - check if we need to increment cache
|
264 |
|
|
if (tcache->size == tcache->max_size)
|
265 |
|
|
{
|
266 |
|
|
// If we can't get memory to increment the cache
|
267 |
|
|
// try to flush dead nodes fat table cache
|
268 |
|
|
if (!tcache_increment(disk, tcache))
|
269 |
|
|
{
|
270 |
|
|
fatfs_node_flush_dead_tcache(disk);
|
271 |
|
|
if (!tcache_increment(disk, tcache))
|
272 |
|
|
return false;
|
273 |
|
|
}
|
274 |
|
|
}
|
275 |
|
|
|
276 |
|
|
tcache->size++;
|
277 |
|
|
tcache->clusters[num] = cluster;
|
278 |
|
|
|
279 |
|
|
CYG_TRACE2(TTC, "added cluster=%d num=%d to tcache", cluster, num);
|
280 |
|
|
|
281 |
|
|
return true;
|
282 |
|
|
}
|
283 |
|
|
|
284 |
|
|
// -------------------------------------------------------------------------
|
285 |
|
|
// EOF fatfs_tcache.c
|