1 | ////////////////////////////////////////////////////////////////////////////////// |
---|
2 | // Date : 01/06/2015 |
---|
3 | // Authors : Alain Greiner |
---|
4 | // Copyright (c) UPMC-LIP6 |
---|
5 | ////////////////////////////////////////////////////////////////////////////////// |
---|
6 | // The fat32.h and fat32.c files define a library of access functions |
---|
7 | // to a FAT32 disk on a block device. It is intended to be used by both |
---|
8 | // the boot code and the kernel code. |
---|
9 | ////////////////////////////////////////////////////////////////////////////////// |
---|
10 | // Implementation notes: |
---|
11 | // 1. the "lba" (Logical Block Address) is the physical sector index on |
---|
12 | // the block device. The physical sector size is supposed to be 512 bytes. |
---|
13 | // 2. the "cluster" variable is actually a cluster index. A cluster contains |
---|
14 | // 8 sectors (4K bytes) and the cluster index is a 32 bits word. |
---|
15 | // 3. Each file or directory referenced by the software is represented |
---|
16 | // by an "inode". The set of "inodes" is organised as a tree, that is |
---|
17 | // a sub-tree of the complete file system existing on the block device. |
---|
18 | // 4. A given file can be referenced by several software tasks, and each task |
---|
19 | // will use a private handler, called a "file descriptor", allocated by the OS |
---|
20 | // when the task open the file, that is organised as an indexed array. |
---|
21 | // 5. This FAT32 library implements (N+1) caches : one private "File_ Cache" |
---|
22 | // for each referenced file or directory, and a specific "Fat_Cache" for |
---|
23 | // the FAT itself. Each cache contain a variable number of clusters that are |
---|
24 | // dynamically allocated when they are accessed, and organised as a 64-Tree. |
---|
25 | ////////////////////////////////////////////////////////////////////////////////// |
---|
26 | // General Debug Policy: |
---|
27 | // The global variable GIET_DEBUG_FAT is defined in the giet_config.h file. |
---|
28 | // The debug is activated if (proctime > GIET_DEBUG_FAT) && (GIET_DEBUG_FAT != 0) |
---|
29 | // The GIET_DEBUG_FAT bit 0 defines the level of debug: |
---|
30 | // if (GIET_DEBUG_FAT & 0x1) => detailed debug |
---|
31 | // else => external functions only |
---|
32 | ////////////////////////////////////////////////////////////////////////////////// |
---|
33 | |
---|
34 | #include <giet_config.h> |
---|
35 | #include <hard_config.h> |
---|
36 | #include <fat32.h> |
---|
37 | #include <utils.h> |
---|
38 | #include <vmem.h> |
---|
39 | #include <kernel_malloc.h> |
---|
40 | #include <bdv_driver.h> |
---|
41 | #include <hba_driver.h> |
---|
42 | #include <sdc_driver.h> |
---|
43 | #include <rdk_driver.h> |
---|
44 | #include <mmc_driver.h> |
---|
45 | #include <tty0.h> |
---|
46 | |
---|
47 | ////////////////////////////////////////////////////////////////////////////////// |
---|
48 | // Global variables |
---|
49 | ////////////////////////////////////////////////////////////////////////////////// |
---|
50 | |
---|
51 | // Fat-Descriptor |
---|
52 | __attribute__((section(".kdata"))) |
---|
53 | fat_desc_t _fat __attribute__((aligned(64))); |
---|
54 | |
---|
55 | // buffer used by boot code as a simple cache when scanning FAT |
---|
56 | __attribute__((section(".kdata"))) |
---|
57 | unsigned char _fat_buffer_fat[4096] __attribute__((aligned(64))); |
---|
58 | |
---|
59 | // buffer used by boot code as a simple cache when scanning a directory in DATA region |
---|
60 | __attribute__((section(".kdata"))) |
---|
61 | unsigned char _fat_buffer_data[4096] __attribute__((aligned(64))); |
---|
62 | |
---|
63 | // lba of cluster in fat_buffer_fat |
---|
64 | __attribute__((section(".kdata"))) |
---|
65 | unsigned int _fat_buffer_fat_lba; |
---|
66 | |
---|
67 | // lba of cluster in fat_buffer_data |
---|
68 | __attribute__((section(".kdata"))) |
---|
69 | unsigned int _fat_buffer_data_lba; |
---|
70 | |
---|
71 | ////////////////////////////////////////////////////////////////////////////////// |
---|
72 | ////////////////////////////////////////////////////////////////////////////////// |
---|
73 | // Static functions declaration |
---|
74 | ////////////////////////////////////////////////////////////////////////////////// |
---|
75 | ////////////////////////////////////////////////////////////////////////////////// |
---|
76 | |
---|
77 | |
---|
78 | /////////////////////////////////////////////////////////////////////////////////// |
---|
79 | // This debug function displays the content of a 512 bytes buffer "buf", |
---|
80 | // with an identifier defined by the "string" and "block_id" arguments. |
---|
81 | /////////////////////////////////////////////////////////////////////////////////// |
---|
82 | |
---|
83 | #if GIET_DEBUG_FAT |
---|
84 | static void _display_one_block( unsigned char* buf, |
---|
85 | char* string, |
---|
86 | unsigned int block_id ); |
---|
87 | #endif |
---|
88 | |
---|
89 | ////////////////////////////////////////////////////////////////////////////////// |
---|
90 | // This debug function displays the FAT descriptor. |
---|
91 | ////////////////////////////////////////////////////////////////////////////////// |
---|
92 | |
---|
93 | #if GIET_DEBUG_FAT |
---|
94 | static void _display_fat_descriptor(); |
---|
95 | #endif |
---|
96 | |
---|
97 | ///////////////////////////////////////////////////////////////////////////////// |
---|
98 | // This debug function displays the sequence of clusters allocated to a |
---|
99 | // file (or directory) identified by the "inode" argument. |
---|
100 | ///////////////////////////////////////////////////////////////////////////////// |
---|
101 | |
---|
102 | #if GIET_DEBUG_FAT |
---|
103 | static void _display_clusters_list( fat_inode_t* inode ); |
---|
104 | #endif |
---|
105 | |
---|
106 | ///////////////////////////////////////////////////////////////////////////////// |
---|
107 | // The following function transfers one or several blocks between the device |
---|
108 | // and a memory buffer identified by a virtual address. |
---|
109 | // It computes the memory buffer physical address, and calls the proper |
---|
110 | // IOC driver depending on the subtype (BDV / HBA / SDC / SPI / RDK). |
---|
111 | // The use_irq argument allows to activate the descheduling mode, |
---|
112 | // if it supported by the IOC driver subtype |
---|
113 | // It returns O in case of success. |
---|
114 | // It returns -1 in case of error. |
---|
115 | ///////////////////////////////////////////////////////////////////////////////// |
---|
116 | |
---|
117 | static int _fat_ioc_access( unsigned int use_irq, |
---|
118 | unsigned int to_mem, |
---|
119 | unsigned int lba, |
---|
120 | unsigned int buf_vaddr, |
---|
121 | unsigned int count ); |
---|
122 | |
---|
123 | ////////////////////////////////////////////////////////////////////////////////// |
---|
124 | // The following function returns in the "desc" argument a pointer on a buffer |
---|
125 | // descriptor contained in a File_Cache, or in the Fat_Cache. |
---|
126 | // The searched buffer is idenfified by the "inode" and "cluster_id" arguments. |
---|
127 | // If the "inode" pointer is not NULL, the searched cache is a File-Cache. |
---|
128 | // If the "inode" pointer is NULL, the searched cache is the Fat-Cache, |
---|
129 | // The "cluster_id" argument is the buffer index in the file (or in the FAT). |
---|
130 | // In case of miss, it allocate a 4 Kbytes buffer and a cluster descriptor |
---|
131 | // from the local kernel heap, and calls the _fat_ioc_access() function to load |
---|
132 | // the missing cluster from the block device. |
---|
133 | // It returns O in case of success. |
---|
134 | // It returns 1 in case of error. |
---|
135 | ////////////////////////////////////////////////////////////////////////////////// |
---|
136 | |
---|
137 | static unsigned int _get_buffer_from_cache( fat_inode_t* inode, |
---|
138 | unsigned int cluster_id, |
---|
139 | fat_cache_desc_t** desc ); |
---|
140 | |
---|
141 | //////////////////////////////////////////////////////////////////////////////// |
---|
142 | // This function extract a (partial) name from a LFN directory entry. |
---|
143 | //////////////////////////////////////////////////////////////////////////////// |
---|
144 | |
---|
145 | static void _get_name_from_long( unsigned char* buffer, |
---|
146 | char* name ); |
---|
147 | |
---|
148 | //////////////////////////////////////////////////////////////////////////////// |
---|
149 | // The following function extract a name from a NORMAL directory entry. |
---|
150 | //////////////////////////////////////////////////////////////////////////////// |
---|
151 | |
---|
152 | static void _get_name_from_short( unsigned char* buffer, |
---|
153 | char* name ); |
---|
154 | |
---|
155 | ////////////////////////////////////////////////////////////////////////////////// |
---|
156 | // This function returns the number of levels of a File-Cache (or Fat-Cache) |
---|
157 | // from the size of the file (or FAT). |
---|
158 | ////////////////////////////////////////////////////////////////////////////////// |
---|
159 | |
---|
160 | static inline unsigned int _get_levels_from_size( unsigned int size ); |
---|
161 | |
---|
162 | /////////////////////////////////////////////////////////////////////////////////// |
---|
163 | // The following function analyses the "pathname" argument, from the character |
---|
164 | // defined by the "nb_read" argument. |
---|
165 | // It copies the found name in the "name" buffer (without '/'), |
---|
166 | // and updates the "nb_read" argument. |
---|
167 | // It returns 0 if success. |
---|
168 | // It returns 1 if one name length > NAME_MAX_SIZE characters |
---|
169 | /////////////////////////////////////////////////////////////////////////////////// |
---|
170 | |
---|
171 | static unsigned int _get_name_from_path( char* pathname, |
---|
172 | char* name, |
---|
173 | unsigned int* nb_read ); |
---|
174 | |
---|
175 | //////////////////////////////////////////////////////////////////////////////// |
---|
176 | // The following function scan the "pathname" argument, and copies in the |
---|
177 | // "name" buffer the last name in path (leaf name). |
---|
178 | // It returns 0 if success. |
---|
179 | // It returns 1 if one name length > NAME_MAX_SIZE characters |
---|
180 | //////////////////////////////////////////////////////////////////////////////// |
---|
181 | static unsigned int _get_last_name( char* pathname, |
---|
182 | char* name ); |
---|
183 | |
---|
184 | ////////////////////////////////////////////////////////////////////////////////// |
---|
185 | // The following function access the Fat-Cache and returns in the "value" |
---|
186 | // argument the content of the FAT slot identified by the "cluster" argument. |
---|
187 | // It loads the missing cluster from block device into cache in case of miss. |
---|
188 | // It returns 0 if success. |
---|
189 | // It returns 1 if error. |
---|
190 | ////////////////////////////////////////////////////////////////////////////////// |
---|
191 | |
---|
192 | static unsigned int _get_fat_entry( unsigned int cluster, |
---|
193 | unsigned int* value ); |
---|
194 | |
---|
195 | ////////////////////////////////////////////////////////////////////////////////// |
---|
196 | // The following function writes a new "value" in the Fat-Cache, in the slot |
---|
197 | // identified by the "cluster" argument. |
---|
198 | // It loads the missing cluster from block device into cache in case of miss. |
---|
199 | // It returns 0 if success, |
---|
200 | // It returns 1 if error. |
---|
201 | ////////////////////////////////////////////////////////////////////////////////// |
---|
202 | |
---|
203 | static unsigned int _set_fat_entry( unsigned int cluster, |
---|
204 | unsigned int value ); |
---|
205 | |
---|
206 | ////////////////////////////////////////////////////////////////////////////////// |
---|
207 | // The following function introduces the inode identified by the "child" argument, |
---|
208 | // as a new child of the "parent" inode in the Inode-Tree. |
---|
209 | // All checking are supposed to be done by the caller. |
---|
210 | // Nor the File-Cache, neither the block device are modified. |
---|
211 | ////////////////////////////////////////////////////////////////////////////////// |
---|
212 | |
---|
213 | static void _add_inode_in_tree( fat_inode_t* child, |
---|
214 | fat_inode_t* parent ); |
---|
215 | |
---|
216 | ////////////////////////////////////////////////////////////////////////////////// |
---|
217 | // The following function removes one inode identified by the "inode" argument |
---|
218 | // from the Inode-Tree. All checking are supposed to be done by the caller. |
---|
219 | // Nor the File-Cache, neither the block device are modified. |
---|
220 | ////////////////////////////////////////////////////////////////////////////////// |
---|
221 | |
---|
222 | static void _remove_inode_from_tree( fat_inode_t* inode ); |
---|
223 | |
---|
224 | ////////////////////////////////////////////////////////////////////////////////// |
---|
225 | // This recursive function scan one File-Cache (or Fat-Cache) from root to leaves, |
---|
226 | // to writes all dirty clusters to block device, and reset the dirty bits. |
---|
227 | // The cache is identified by the "root" an "levels" arguments. |
---|
228 | // The "string" argument is only used for debug : inode name or Fat. |
---|
229 | // It returns 0 if success. |
---|
230 | // It returns 1 if error. |
---|
231 | ////////////////////////////////////////////////////////////////////////////////// |
---|
232 | |
---|
233 | static unsigned int _update_device_from_cache( unsigned int levels, |
---|
234 | fat_cache_node_t* root, |
---|
235 | char* string ); |
---|
236 | |
---|
237 | ////////////////////////////////////////////////////////////////////////////////// |
---|
238 | // The following function access directly the FS_INFO block on the block device, |
---|
239 | // to update the "first_free_cluster" and "free_clusters_number" values, |
---|
240 | // using only the Fat-Descriptor single block buffer. |
---|
241 | // It return 0 in case of success. |
---|
242 | // It return 1 in case of error. |
---|
243 | ////////////////////////////////////////////////////////////////////////////////// |
---|
244 | |
---|
245 | static unsigned int _update_fs_info(); |
---|
246 | |
---|
247 | ////////////////////////////////////////////////////////////////////////////// |
---|
248 | // The following function read a data field (from one to four bytes) |
---|
249 | // from an unsigned char[] buffer, taking endianness into account. |
---|
250 | // The analysed field is defined by the "offset" and "size" arguments. |
---|
251 | ////////////////////////////////////////////////////////////////////////////// |
---|
252 | |
---|
253 | static unsigned int _read_entry( unsigned int offset, |
---|
254 | unsigned int size, |
---|
255 | unsigned char* buffer, |
---|
256 | unsigned int little_indian ); |
---|
257 | |
---|
258 | ////////////////////////////////////////////////////////////////////////////////// |
---|
259 | // The following function returns the lba of first sector in DATA region |
---|
260 | // from the cluster index. The cluster index must be larger than 2. |
---|
261 | ////////////////////////////////////////////////////////////////////////////////// |
---|
262 | |
---|
263 | static unsigned int _cluster_to_lba( unsigned int cluster ); |
---|
264 | |
---|
265 | ////////////////////////////////////////////////////////////////////////////////// |
---|
266 | // The following function returns in the "nb_entries" argument the number of files |
---|
267 | // (or directories) contained in a directory identified by the "inode " pointer. |
---|
268 | // It returns 0 if success. |
---|
269 | // It returns 1 if error. |
---|
270 | ////////////////////////////////////////////////////////////////////////////////// |
---|
271 | |
---|
272 | static unsigned int _get_nb_entries( fat_inode_t* inode, |
---|
273 | unsigned int* nb_entries ); |
---|
274 | |
---|
275 | ////////////////////////////////////////////////////////////////////////////////// |
---|
276 | // The following function search in the directory identified by the "parent" |
---|
277 | // inode pointer a child (file or directory) identified by its "name". |
---|
278 | // It returns in the "inode" argument the searched child inode pointer. |
---|
279 | // If the searched name is not found in the Inode-Tree, the function access |
---|
280 | // the "file_cache" associated to the parent directory. |
---|
281 | // If the child exist on block device, the Inode-Tree is updated, and |
---|
282 | // a success code is returned. |
---|
283 | // If the file/dir does not exist on block device, a error code is returned. |
---|
284 | // It returns 0 if inode found. |
---|
285 | // It returns 1 if inode not found. |
---|
286 | // It returns 2 if error in cache access. |
---|
287 | ////////////////////////////////////////////////////////////////////////////////// |
---|
288 | |
---|
289 | static unsigned int _get_child_from_parent( fat_inode_t* parent, |
---|
290 | char* name, |
---|
291 | fat_inode_t** inode ); |
---|
292 | |
---|
293 | ///////////////////////////////////////////////////////////////////////////////// |
---|
294 | // For a file (or a directory) identified by the "pathname" argument, the |
---|
295 | // following function returns in the "inode" argument the inode pointer |
---|
296 | // associated to the searched file (or directory), with code (0). |
---|
297 | // If the searched file (or directory) is not found, but the parent directory |
---|
298 | // is found, it returns in the "inode" argument the pointer on the parent inode, |
---|
299 | // with code (1). Finally, code (2) and code (3) are error codes. |
---|
300 | // Both the Inode-Tree and the involved Cache-Files are updated from the block |
---|
301 | // device in case of miss on one inode during the search in path. |
---|
302 | // Neither the Fat-Cache, nor the block device are updated. |
---|
303 | // It returns 0 if searched inode found |
---|
304 | // It returns 1 if searched inode not found but parent directory found |
---|
305 | // It returns 2 if searched inode not found and parent directory not found |
---|
306 | // It returns 3 if one name too long |
---|
307 | ///////////////////////////////////////////////////////////////////////////////// |
---|
308 | |
---|
309 | static unsigned int _get_inode_from_path( char* pathname, |
---|
310 | fat_inode_t** inode ); |
---|
311 | |
---|
312 | ////////////////////////////////////////////////////////////////////////////////// |
---|
313 | // This function computes the length and the number of LFN entries required |
---|
314 | // to store a node name in the "length" and "nb_lfn" arguments. |
---|
315 | // Short name (less than 13 characters) require 1 LFN entry. |
---|
316 | // Medium names (from 14 to 26 characters require 2 LFN entries. |
---|
317 | // Large names (up to 31 characters) require 3 LFN entries. |
---|
318 | // It returns 0 if success. |
---|
319 | // It returns 1 if length larger than 31 characters. |
---|
320 | ////////////////////////////////////////////////////////////////////////////////// |
---|
321 | |
---|
322 | static unsigned int _check_name_length( char* name, |
---|
323 | unsigned int* length, |
---|
324 | unsigned int* nb_lfn ); |
---|
325 | |
---|
326 | ////////////////////////////////////////////////////////////////////////////////// |
---|
327 | // For a node identified by the "inode" argument, this function updates the |
---|
328 | // "size" and "cluster" values in the entry of the parent directory File-Cache. |
---|
329 | // It set the dirty bit in the modified buffer of the parent directory File-Cache. |
---|
330 | ////////////////////////////////////////////////////////////////////////////////// |
---|
331 | |
---|
332 | static unsigned int _update_dir_entry( fat_inode_t* inode ); |
---|
333 | |
---|
334 | ////////////////////////////////////////////////////////////////////////////////// |
---|
335 | // The following function add new "child" in Cache-File of "parent" directory. |
---|
336 | // It access the File_Cache associated to the parent directory, and scan the |
---|
337 | // clusters allocated to this directory to find the NO_MORE entry. |
---|
338 | // This entry will be the first modified entry in the directory. |
---|
339 | // Regarding the name storage, it uses LFN entries for all names. |
---|
340 | // Therefore, it writes 1, 2, or 3 LFN entries (depending on the child name |
---|
341 | // actual length, it writes one NORMAL entry, and writes the new NOMORE entry. |
---|
342 | // It updates the dentry field in the child inode. |
---|
343 | // It set the dirty bit for all modified File-Cache buffers. |
---|
344 | // The block device is not modified by this function. |
---|
345 | ////////////////////////////////////////////////////////////////////////////////// |
---|
346 | |
---|
347 | static unsigned int _add_dir_entry( fat_inode_t* child, |
---|
348 | fat_inode_t* parent ); |
---|
349 | |
---|
350 | ////////////////////////////////////////////////////////////////////////////////// |
---|
351 | // The following function invalidates all dir_entries associated to the "inode" |
---|
352 | // argument from its parent directory. |
---|
353 | // It set the dirty bit for all modified buffers in parent directory Cache-File. |
---|
354 | // The inode itself is not modified by this function. |
---|
355 | // The block device is not modified by this function. |
---|
356 | ////////////////////////////////////////////////////////////////////////////////// |
---|
357 | |
---|
358 | static unsigned int _remove_dir_entry( fat_inode_t* inode ); |
---|
359 | |
---|
360 | ////////////////////////////////////////////////////////////////////////////////// |
---|
361 | // The following function add the special entries "." and ".." in the File-Cache |
---|
362 | // of the directory identified by the "child" argument. |
---|
363 | // The parent directory is defined by the "parent" argument. |
---|
364 | // The child directory File-Cache is supposed to be empty. |
---|
365 | // We use two NORMAL entries for these "." and ".." entries. |
---|
366 | // The block device is not modified by this function. |
---|
367 | ////////////////////////////////////////////////////////////////////////////////// |
---|
368 | |
---|
369 | static void _add_special_directories( fat_inode_t* child, |
---|
370 | fat_inode_t* parent ); |
---|
371 | |
---|
372 | ////////////////////////////////////////////////////////////////////////////////// |
---|
373 | // The following function releases all clusters allocated to a file or directory, |
---|
374 | // from the cluster index defined by the "cluster" argument, until the end |
---|
375 | // of the FAT linked list. |
---|
376 | // It calls _get_fat_entry() and _set_fat_entry() functions to scan the FAT, |
---|
377 | // and to update the clusters chaining. |
---|
378 | // The FAT region on block device is updated. |
---|
379 | // It returns 0 if success. |
---|
380 | // It returns 1 if error. |
---|
381 | ////////////////////////////////////////////////////////////////////////////////// |
---|
382 | |
---|
383 | static unsigned int _clusters_release( unsigned int cluster ); |
---|
384 | |
---|
385 | ////////////////////////////////////////////////////////////////////////////////// |
---|
386 | // This function allocate "nb_clusters_more" new clusters to a file (or directory) |
---|
387 | // identified by the "inode" pointer. It allocates also the associated buffers |
---|
388 | // and buffer descriptors in the Cache-File. |
---|
389 | // It calls _get_fat_entry() and _set_fat_entry() functions to update the |
---|
390 | // clusters chaining in the Cache-Fat. The FAT region on block device is updated. |
---|
391 | // It returns 0 if success. |
---|
392 | // It returns 1 if error. |
---|
393 | ////////////////////////////////////////////////////////////////////////////////// |
---|
394 | |
---|
395 | static unsigned int _clusters_allocate( fat_inode_t* inode, |
---|
396 | unsigned int nb_clusters_current, |
---|
397 | unsigned int nb_clusters_more ); |
---|
398 | |
---|
399 | ////////////////////////////////////////////////////////////////////////////////// |
---|
400 | // This recursive function scan one File-Cache (or Fat-Cache) from root to leaves. |
---|
401 | // The cache 64-Tree infrastructure is kept, but all memory allocated for 4 Kbytes |
---|
402 | // buffers, and for buffer descriptors (in leaves) is released. |
---|
403 | // The cache is identified by the "root" an "levels" arguments. |
---|
404 | // It should not contain any dirty clusters. |
---|
405 | // It returns 0 if success. |
---|
406 | // It returns 1 if error. |
---|
407 | ////////////////////////////////////////////////////////////////////////////////// |
---|
408 | |
---|
409 | static unsigned int _release_cache_memory( fat_cache_node_t* root, |
---|
410 | unsigned int levels ); |
---|
411 | |
---|
412 | ////////////////////////////////////////////////////////////////////////////////// |
---|
413 | // The following function allocates and initializes a new inode, |
---|
414 | // using the values defined by the arguments. |
---|
415 | // If the "cache_allocate" argument is true ans empty cache is allocated. |
---|
416 | // The Fat-Cache is initialised as empty: all children set to NULL. |
---|
417 | // It returns a pointer on the new inode. |
---|
418 | ////////////////////////////////////////////////////////////////////////////////// |
---|
419 | |
---|
420 | static fat_inode_t* _allocate_one_inode( char* name, |
---|
421 | unsigned int is_dir, |
---|
422 | unsigned int cluster, |
---|
423 | unsigned int size, |
---|
424 | unsigned int count, |
---|
425 | unsigned int dentry, |
---|
426 | unsigned int cache_allocate ); |
---|
427 | |
---|
428 | ////////////////////////////////////////////////////////////////////////////////// |
---|
429 | // The following function allocates one 4 Kbytes buffer and associated cluster |
---|
430 | // descriptor for the file (or directory) identified by the "inode" argument, |
---|
431 | // and updates the Cache_File slot identified by the "cluster_id" argument. |
---|
432 | // The File-Cache slot must be empty. |
---|
433 | // It updates the cluster descriptor, using the "cluster" argument, that is |
---|
434 | // the cluster index in FAT. The cluster descriptor dirty field is set. |
---|
435 | // It traverse the 64-tree Cache-file from top to bottom to find the last level. |
---|
436 | ////////////////////////////////////////////////////////////////////////////////// |
---|
437 | |
---|
438 | static void _allocate_one_buffer( fat_inode_t* inode, |
---|
439 | unsigned int cluster_id, |
---|
440 | unsigned int cluster ); |
---|
441 | |
---|
442 | ////////////////////////////////////////////////////////////////////////////////// |
---|
443 | // The following function allocates one free cluster from the FAT "heap" of free |
---|
444 | // clusters, and returns the cluster index in the "cluster" argument. |
---|
445 | // It updates the FAT slot, and the two FAT global variables: first_free_cluster, |
---|
446 | // and free_clusters_number. |
---|
447 | // It returns O if success. |
---|
448 | // It returns 1 if error. |
---|
449 | ////////////////////////////////////////////////////////////////////////////////// |
---|
450 | |
---|
451 | static unsigned int _allocate_one_cluster( unsigned int* cluster ); |
---|
452 | |
---|
453 | ///////////////////////////////////////////////////////////////////////////// |
---|
454 | // This function remove from the file system a file or a directory |
---|
455 | // identified by the "inode" argument. |
---|
456 | // The remove condition must be checked by the caller. |
---|
457 | // The relevant lock(s) must have been taken by te caller. |
---|
458 | // It returns O if success. |
---|
459 | // It returns 1 if error. |
---|
460 | ///////////////////////////////////////////////////////////////////////////// |
---|
461 | |
---|
462 | static unsigned int _remove_node_from_fs( fat_inode_t* inode ); |
---|
463 | |
---|
464 | ///////////////////////////////////////////////////////////////////////////// |
---|
465 | // This function return the cluster index and the size for a file |
---|
466 | // identified by the "pathname" argument, scanning directly the block |
---|
467 | // device DATA region. |
---|
468 | // It is intended to be called only by the _fat_load_no_cache() function, |
---|
469 | // it does not use the dynamically allocated File Caches, but uses only |
---|
470 | // the 4 Kbytes _fat_buffer_data. |
---|
471 | // It returns 0 if success. |
---|
472 | // It returns 1 if error. |
---|
473 | ///////////////////////////////////////////////////////////////////////////// |
---|
474 | |
---|
475 | static unsigned int _file_info_no_cache( char* pathname, |
---|
476 | unsigned int* file_cluster, |
---|
477 | unsigned int* file_size ); |
---|
478 | |
---|
479 | ///////////////////////////////////////////////////////////////////////////// |
---|
480 | // This function scan directly the FAT region on the block device, |
---|
481 | // and returns in the "next" argument the value stored in the fat slot |
---|
482 | // identified by the "cluster" argument. |
---|
483 | // It is intended to be called only by the _fat_load_no_cache() function, |
---|
484 | // as it does not use the dynamically allocated Fat-Cache, but uses only |
---|
485 | // the 4 Kbytes _fat_buffer_fat. |
---|
486 | // It returns 0 if success. |
---|
487 | // It returns 1 if error. |
---|
488 | ///////////////////////////////////////////////////////////////////////////// |
---|
489 | |
---|
490 | static unsigned int _next_cluster_no_cache( unsigned int cluster, |
---|
491 | unsigned int* next ); |
---|
492 | |
---|
493 | |
---|
494 | ////////////////////////////////////////////////////////////////////////////////// |
---|
495 | // The following functions return the length or the size of a FAT field, |
---|
496 | // identified by an (offset,length) mnemonic defined in the fat32.h file. |
---|
497 | ////////////////////////////////////////////////////////////////////////////////// |
---|
498 | |
---|
499 | static inline int get_length( int offset , int length ) { return length; } |
---|
500 | |
---|
501 | static inline int get_offset( int offset , int length ) { return offset; } |
---|
502 | |
---|
503 | |
---|
504 | |
---|
505 | |
---|
506 | |
---|
507 | ////////////////////////////////////////////////////////////////////////////////// |
---|
508 | ////////////////////////////////////////////////////////////////////////////////// |
---|
509 | // Static functions definition |
---|
510 | ////////////////////////////////////////////////////////////////////////////////// |
---|
511 | ////////////////////////////////////////////////////////////////////////////////// |
---|
512 | |
---|
513 | #if GIET_DEBUG_FAT |
---|
514 | /////////////////////////////////////////////////// |
---|
515 | static void _display_one_block( unsigned char* buf, |
---|
516 | char* string, |
---|
517 | unsigned int block_id ) |
---|
518 | { |
---|
519 | unsigned int line; |
---|
520 | unsigned int word; |
---|
521 | |
---|
522 | _printf("\n*** <%s> block %x ***********************************\n", |
---|
523 | string , block_id ); |
---|
524 | |
---|
525 | for ( line = 0 ; line < 16 ; line++ ) |
---|
526 | { |
---|
527 | // display line index |
---|
528 | _printf("%x : ", line ); |
---|
529 | |
---|
530 | // display 8*4 bytes hexa |
---|
531 | for ( word=0 ; word<8 ; word++ ) |
---|
532 | { |
---|
533 | unsigned int byte = (line<<5) + (word<<2); |
---|
534 | unsigned int hexa = (buf[byte ]<<24) | |
---|
535 | (buf[byte+1]<<16) | |
---|
536 | (buf[byte+2]<< 8) | |
---|
537 | (buf[byte+3] ); |
---|
538 | _printf(" %X |", hexa ); |
---|
539 | } |
---|
540 | _printf("\n"); |
---|
541 | } |
---|
542 | _printf("*******************************************************************\n"); |
---|
543 | } // end _display_one_block() |
---|
544 | #endif |
---|
545 | |
---|
546 | |
---|
547 | |
---|
548 | #if GIET_DEBUG_FAT |
---|
549 | ///////////////////////////////////// |
---|
550 | static void _display_fat_descriptor() |
---|
551 | { |
---|
552 | _printf("\n############### FAT DESCRIPTOR ################################" |
---|
553 | "\nFAT initialised %x" |
---|
554 | "\nBlock Size (bytes) %x" |
---|
555 | "\nCluster Size (bytes) %x" |
---|
556 | "\nFAT region first lba %x" |
---|
557 | "\nFAT region size (blocks) %x" |
---|
558 | "\nDATA region first lba %x" |
---|
559 | "\nDATA region size (blocks) %x" |
---|
560 | "\nNumber of free clusters %x" |
---|
561 | "\nFirst free cluster index %x" |
---|
562 | "\nFat_cache_levels %d" |
---|
563 | "\n#################################################################\n", |
---|
564 | _fat.initialised, |
---|
565 | _fat.sector_size, |
---|
566 | _fat.cluster_size, |
---|
567 | _fat.fat_lba, |
---|
568 | _fat.fat_sectors, |
---|
569 | _fat.data_lba, |
---|
570 | _fat.data_sectors, |
---|
571 | _fat.free_clusters_number, |
---|
572 | _fat.first_free_cluster, |
---|
573 | _fat.fat_cache_levels ); |
---|
574 | |
---|
575 | } // end _display_fat_descriptor() |
---|
576 | #endif |
---|
577 | |
---|
578 | |
---|
579 | |
---|
580 | #if GIET_DEBUG_FAT |
---|
581 | //////////////////////////////////////////////////////// |
---|
582 | static void _display_clusters_list( fat_inode_t* inode ) |
---|
583 | { |
---|
584 | _printf("\n**************** clusters for <%s> ***********************\n", inode->name ); |
---|
585 | unsigned int next; |
---|
586 | unsigned int n = 0; |
---|
587 | unsigned int current = inode->cluster; |
---|
588 | while( (current < END_OF_CHAIN_CLUSTER_MIN) && (n < 1024) ) |
---|
589 | { |
---|
590 | _get_fat_entry( current , &next ); |
---|
591 | _printf(" > %X", current ); |
---|
592 | n++; |
---|
593 | if ( (n & 0x7) == 0 ) _printf("\n"); |
---|
594 | current = next; |
---|
595 | } |
---|
596 | _printf("\n"); |
---|
597 | } // end _display_clusters_list() |
---|
598 | #endif |
---|
599 | |
---|
600 | |
---|
601 | |
---|
602 | ///////////////////////////////////////////////////////////////////////////////// |
---|
603 | static int _fat_ioc_access( unsigned int use_irq, // descheduling if non zero |
---|
604 | unsigned int to_mem, // read / write |
---|
605 | unsigned int lba, // first sector on device |
---|
606 | unsigned int buf_vaddr, // memory buffer vaddr |
---|
607 | unsigned int count ) // number of sectors |
---|
608 | { |
---|
609 | // compute memory buffer physical address |
---|
610 | unsigned int flags; // for _v2p_translate |
---|
611 | unsigned long long buf_paddr; // buffer physical address |
---|
612 | |
---|
613 | if ( ((_get_mmu_mode() & 0x4) == 0 ) || USE_IOC_RDK ) // identity |
---|
614 | { |
---|
615 | buf_paddr = (unsigned long long)buf_vaddr; |
---|
616 | } |
---|
617 | else // V2P translation required |
---|
618 | { |
---|
619 | buf_paddr = _v2p_translate( buf_vaddr , &flags ); |
---|
620 | } |
---|
621 | |
---|
622 | #if (GIET_DEBUG_FAT & 1) |
---|
623 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
624 | _printf("\n[DEBUG FAT] _fat_ioc_access() : enters at cycle %d\n" |
---|
625 | " to_mem = %d / vaddr = %x / paddr = %l / sectors = %d / lba = %x\n", |
---|
626 | _get_proctime(), to_mem, buf_vaddr, buf_paddr, count, lba ); |
---|
627 | #endif |
---|
628 | |
---|
629 | |
---|
630 | #if GIET_NO_HARD_CC // L1 cache inval (virtual addresses) |
---|
631 | if ( to_mem ) _dcache_buf_invalidate( buf_vaddr, count<<9 ); |
---|
632 | #endif |
---|
633 | |
---|
634 | |
---|
635 | #if ( USE_IOC_BDV ) // call the proper driver |
---|
636 | return( _bdv_access( use_irq , to_mem , lba , buf_paddr , count ) ); |
---|
637 | #elif ( USE_IOC_HBA ) |
---|
638 | return( _hba_access( use_irq , to_mem , lba , buf_paddr , count ) ); |
---|
639 | #elif ( USE_IOC_SDC ) |
---|
640 | return( _sdc_access( use_irq , to_mem , lba , buf_paddr , count ) ); |
---|
641 | #elif ( USE_IOC_SPI ) |
---|
642 | return( _spi_access( use_irq , to_mem , lba , buf_paddr , count ) ); |
---|
643 | #elif ( USE_IOC_RDK ) |
---|
644 | return( _rdk_access( use_irq , to_mem , lba , buf_paddr , count ) ); |
---|
645 | #else |
---|
646 | _printf("\n[FAT ERROR] in _fat_ioc_access() : no IOC driver\n"); |
---|
647 | _exit(); |
---|
648 | #endif |
---|
649 | |
---|
650 | } // end _fat_ioc_access() |
---|
651 | |
---|
652 | |
---|
653 | |
---|
654 | |
---|
655 | ///////////////////////////////////////////////////////////////////// |
---|
656 | static inline unsigned int _get_levels_from_size( unsigned int size ) |
---|
657 | { |
---|
658 | if ( size <= (1<<18) ) return 1; // 64 clusters == 256 Kbytes |
---|
659 | else if ( size <= (1<<24) ) return 2; // 64 * 64 clusters => 16 Mbytes |
---|
660 | else if ( size <= (1<<30) ) return 3; // 64 * 64 * 64 cluster => 1 Gbytes |
---|
661 | else return 4; // 64 * 64 * 64 * 64 clusters |
---|
662 | } |
---|
663 | |
---|
664 | |
---|
665 | |
---|
666 | //////////////////////////////////////////////////////// |
---|
667 | static unsigned int _read_entry( unsigned int offset, |
---|
668 | unsigned int size, |
---|
669 | unsigned char* buffer, |
---|
670 | unsigned int little_endian ) |
---|
671 | { |
---|
672 | unsigned int n; |
---|
673 | unsigned int res = 0; |
---|
674 | |
---|
675 | if ( little_endian) |
---|
676 | { |
---|
677 | for( n = size ; n > 0 ; n-- ) res = (res<<8) | buffer[offset+n-1]; |
---|
678 | } |
---|
679 | else |
---|
680 | { |
---|
681 | for( n = 0 ; n < size ; n++ ) res = (res<<8) | buffer[offset+n]; |
---|
682 | } |
---|
683 | return res; |
---|
684 | |
---|
685 | } // end _read_entry |
---|
686 | |
---|
687 | |
---|
688 | |
---|
689 | ////////////////////////////////////////////////////////////////// |
---|
690 | static inline unsigned int _cluster_to_lba( unsigned int cluster ) |
---|
691 | { |
---|
692 | if ( cluster < 2 ) |
---|
693 | { |
---|
694 | _printf("\n[FAT ERROR] in _cluster_to_lba() cluster smaller than 2\n"); |
---|
695 | _exit(); |
---|
696 | } |
---|
697 | |
---|
698 | return ((cluster - 2) << 3) + _fat.data_lba; |
---|
699 | } |
---|
700 | |
---|
701 | |
---|
702 | ////////////////////////////////////////////////////// |
---|
703 | static inline unsigned char _to_lower(unsigned char c) |
---|
704 | { |
---|
705 | if (c >= 'A' && c <= 'Z') return (c | 0x20); |
---|
706 | else return c; |
---|
707 | } |
---|
708 | |
---|
709 | |
---|
710 | ////////////////////////////////////////////////////// |
---|
711 | static inline unsigned char _to_upper(unsigned char c) |
---|
712 | { |
---|
713 | if (c >= 'a' && c <= 'z') return (c & ~(0x20)); |
---|
714 | else return c; |
---|
715 | } |
---|
716 | |
---|
717 | |
---|
718 | |
---|
719 | /////////////////////////////////////////////////////////////////////////// |
---|
720 | static unsigned int _get_name_from_path( char* pathname, // input |
---|
721 | char* name, // output |
---|
722 | unsigned int* nb_read ) // input & output |
---|
723 | { |
---|
724 | // skip leading "/" character |
---|
725 | if ( pathname[*nb_read] == '/' ) *nb_read = *nb_read + 1; |
---|
726 | |
---|
727 | // initialises current indexes |
---|
728 | unsigned int i = *nb_read; |
---|
729 | unsigned int j = 0; |
---|
730 | |
---|
731 | while ( (pathname[i] != '/') && (pathname[i] != 0) ) |
---|
732 | { |
---|
733 | name[j++] = pathname[i++]; |
---|
734 | if ( j > NAME_MAX_SIZE ) return 1; |
---|
735 | } |
---|
736 | |
---|
737 | // set end of string |
---|
738 | name[j] = 0; |
---|
739 | |
---|
740 | // skip trailing "/" character |
---|
741 | if ( pathname[i] == '/' ) *nb_read += j+1; |
---|
742 | else *nb_read += j; |
---|
743 | |
---|
744 | return 0; |
---|
745 | } |
---|
746 | |
---|
747 | |
---|
748 | |
---|
749 | //////////////////////////////////////////////////////////////////// |
---|
750 | static unsigned int _get_last_name( char* pathname, // input |
---|
751 | char* name ) // output |
---|
752 | { |
---|
753 | unsigned int nb_read = 0; |
---|
754 | while ( pathname[nb_read] != 0 ) |
---|
755 | { |
---|
756 | if ( _get_name_from_path( pathname, name, &nb_read ) ) return 1; |
---|
757 | } |
---|
758 | |
---|
759 | return 0; |
---|
760 | } // end _get_last_name() |
---|
761 | |
---|
762 | |
---|
763 | |
---|
764 | //////////////////////////////////////////////////////////////////////////////// |
---|
765 | static void _get_name_from_short( unsigned char* buffer, // input: SFN dir_entry |
---|
766 | char* name ) // output: name |
---|
767 | { |
---|
768 | unsigned int i; |
---|
769 | unsigned int j = 0; |
---|
770 | |
---|
771 | // get name |
---|
772 | for ( i = 0; i < 8 && buffer[i] != ' '; i++ ) |
---|
773 | { |
---|
774 | name[j] = _to_lower( buffer[i] ); |
---|
775 | j++; |
---|
776 | } |
---|
777 | |
---|
778 | // get extension |
---|
779 | for ( i = 8; i < 8 + 3 && buffer[i] != ' '; i++ ) |
---|
780 | { |
---|
781 | // we entered the loop so there is an extension. add the dot |
---|
782 | if ( i == 8 ) |
---|
783 | { |
---|
784 | name[j] = '.'; |
---|
785 | j++; |
---|
786 | } |
---|
787 | |
---|
788 | name[j] = _to_lower( buffer[i] ); |
---|
789 | j++; |
---|
790 | } |
---|
791 | |
---|
792 | name[j] = '\0'; |
---|
793 | } |
---|
794 | |
---|
795 | /////////////////////////////////////////////////////////////////////////////// |
---|
796 | static void _get_name_from_long( unsigned char* buffer, // input : LFN dir_entry |
---|
797 | char* name ) // output : name |
---|
798 | { |
---|
799 | unsigned int name_offset = 0; |
---|
800 | unsigned int buffer_offset = get_length(LDIR_ORD); |
---|
801 | unsigned int l_name_1 = get_length(LDIR_NAME_1); |
---|
802 | unsigned int l_name_2 = get_length(LDIR_NAME_2); |
---|
803 | unsigned int l_name_3 = get_length(LDIR_NAME_3); |
---|
804 | unsigned int l_attr = get_length(LDIR_ATTR); |
---|
805 | unsigned int l_type = get_length(LDIR_TYPE); |
---|
806 | unsigned int l_chksum = get_length(LDIR_CHKSUM); |
---|
807 | unsigned int l_rsvd = get_length(LDIR_RSVD); |
---|
808 | |
---|
809 | unsigned int j = 0; |
---|
810 | unsigned int eof = 0; |
---|
811 | |
---|
812 | while ( (buffer_offset != DIR_ENTRY_SIZE) && (!eof) ) |
---|
813 | { |
---|
814 | while (j != l_name_1 && !eof ) |
---|
815 | { |
---|
816 | if ( (buffer[buffer_offset] == 0x00) || |
---|
817 | (buffer[buffer_offset] == 0xFF) ) |
---|
818 | { |
---|
819 | eof = 1; |
---|
820 | continue; |
---|
821 | } |
---|
822 | name[name_offset] = buffer[buffer_offset]; |
---|
823 | buffer_offset += 2; |
---|
824 | j += 2; |
---|
825 | name_offset++; |
---|
826 | } |
---|
827 | |
---|
828 | buffer_offset += (l_attr + l_type + l_chksum); |
---|
829 | j = 0; |
---|
830 | |
---|
831 | while (j != l_name_2 && !eof ) |
---|
832 | { |
---|
833 | if ( (buffer[buffer_offset] == 0x00) || |
---|
834 | (buffer[buffer_offset] == 0xFF) ) |
---|
835 | { |
---|
836 | eof = 1; |
---|
837 | continue; |
---|
838 | } |
---|
839 | name[name_offset] = buffer[buffer_offset]; |
---|
840 | buffer_offset += 2; |
---|
841 | j += 2; |
---|
842 | name_offset++; |
---|
843 | } |
---|
844 | |
---|
845 | buffer_offset += l_rsvd; |
---|
846 | j = 0; |
---|
847 | |
---|
848 | while (j != l_name_3 && !eof ) |
---|
849 | { |
---|
850 | if ( (buffer[buffer_offset] == 0x00) || |
---|
851 | (buffer[buffer_offset] == 0xFF) ) |
---|
852 | { |
---|
853 | eof = 1; |
---|
854 | continue; |
---|
855 | } |
---|
856 | name[name_offset] = buffer[buffer_offset]; |
---|
857 | buffer_offset += 2; |
---|
858 | j += 2; |
---|
859 | name_offset++; |
---|
860 | } |
---|
861 | } |
---|
862 | name[name_offset] = 0; |
---|
863 | } // end get_name_from_long() |
---|
864 | |
---|
865 | |
---|
866 | |
---|
867 | |
---|
868 | //////////////////////////////////////////////////////////// |
---|
869 | static fat_inode_t* _allocate_one_inode( char* name, |
---|
870 | unsigned int is_dir, |
---|
871 | unsigned int cluster, |
---|
872 | unsigned int size, |
---|
873 | unsigned int count, |
---|
874 | unsigned int dentry, |
---|
875 | unsigned int cache_allocate ) |
---|
876 | { |
---|
877 | fat_inode_t* new_inode = _malloc( sizeof(fat_inode_t) ); |
---|
878 | |
---|
879 | new_inode->parent = NULL; // set by _add_inode_in_tree() |
---|
880 | new_inode->next = NULL; // set by _add_inode_in_tree() |
---|
881 | new_inode->child = NULL; // set by _add_inode_in_tree() |
---|
882 | new_inode->cluster = cluster; |
---|
883 | new_inode->size = size; |
---|
884 | new_inode->cache = NULL; |
---|
885 | new_inode->levels = 0; |
---|
886 | new_inode->count = count; |
---|
887 | new_inode->is_dir = (is_dir != 0); |
---|
888 | new_inode->dentry = dentry; |
---|
889 | |
---|
890 | _strcpy( new_inode->name , name ); |
---|
891 | |
---|
892 | if ( cache_allocate ) |
---|
893 | { |
---|
894 | fat_cache_node_t* new_cache = _malloc( sizeof(fat_cache_node_t) ); |
---|
895 | |
---|
896 | new_inode->cache = new_cache; |
---|
897 | new_inode->levels = _get_levels_from_size( size ); |
---|
898 | |
---|
899 | unsigned int index; |
---|
900 | for ( index = 0 ; index < 64 ; index ++ ) new_cache->children[index] = NULL; |
---|
901 | } |
---|
902 | |
---|
903 | return new_inode; |
---|
904 | } // end _allocate_one_inode() |
---|
905 | |
---|
906 | |
---|
907 | |
---|
908 | |
---|
909 | //////////////////////////////////////////////////// |
---|
910 | static void _add_inode_in_tree( fat_inode_t* child, |
---|
911 | fat_inode_t* parent ) |
---|
912 | { |
---|
913 | child->parent = parent; |
---|
914 | child->next = parent->child; |
---|
915 | parent->child = child; |
---|
916 | } // end _add_inode-in_tree() |
---|
917 | |
---|
918 | |
---|
919 | |
---|
920 | |
---|
921 | ////////////////////////////////////////////////////////// |
---|
922 | static void _remove_inode_from_tree( fat_inode_t* inode ) |
---|
923 | { |
---|
924 | fat_inode_t* current; |
---|
925 | fat_inode_t* prev = inode->parent->child; |
---|
926 | |
---|
927 | if ( inode == prev ) // removed inode is first in its linked list |
---|
928 | { |
---|
929 | inode->parent->child = inode->next; |
---|
930 | } |
---|
931 | else // removed inode is not the first |
---|
932 | { |
---|
933 | for( current = prev->next ; current ; current = current->next ) |
---|
934 | { |
---|
935 | if ( current == inode ) |
---|
936 | { |
---|
937 | prev->next = current->next; |
---|
938 | } |
---|
939 | prev = current; |
---|
940 | } |
---|
941 | } |
---|
942 | } // end _delete_one_inode() |
---|
943 | |
---|
944 | |
---|
945 | |
---|
946 | |
---|
947 | ////////////////////////////////////////////////////////////////////// |
---|
948 | static unsigned int _get_buffer_from_cache( fat_inode_t* inode, |
---|
949 | unsigned int cluster_id, |
---|
950 | fat_cache_desc_t** desc ) |
---|
951 | { |
---|
952 | // get cache pointer and levels |
---|
953 | fat_cache_node_t* node; // pointer on a 64-tree node |
---|
954 | unsigned int level; // cache level |
---|
955 | |
---|
956 | if ( inode == NULL ) // searched cache is the Fat-Cache |
---|
957 | { |
---|
958 | node = _fat.fat_cache_root; |
---|
959 | level = _fat.fat_cache_levels; |
---|
960 | |
---|
961 | #if (GIET_DEBUG_FAT & 1) |
---|
962 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
963 | _printf("\n[DEBUG FAT] _get_buffer_from_cache() : enters in FAT-Cache" |
---|
964 | " for cluster_id = %d\n", cluster_id ); |
---|
965 | #endif |
---|
966 | |
---|
967 | } |
---|
968 | else // searched cache is a File-Cache |
---|
969 | { |
---|
970 | node = inode->cache; |
---|
971 | level = inode->levels; |
---|
972 | |
---|
973 | #if (GIET_DEBUG_FAT & 1) |
---|
974 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
975 | _printf("\n[DEBUG FAT] _get_buffer_from_cache() : enters in File-Cache <%s>" |
---|
976 | " for cluster_id = %d\n", inode->name , cluster_id ); |
---|
977 | #endif |
---|
978 | |
---|
979 | } |
---|
980 | |
---|
981 | // search the 64-tree cache from top to bottom |
---|
982 | while ( level ) |
---|
983 | { |
---|
984 | // compute child index at each level |
---|
985 | unsigned int index = (cluster_id >> (6*(level-1))) & 0x3F; |
---|
986 | |
---|
987 | if ( level == 1 ) // last level => children are cluster descriptors |
---|
988 | { |
---|
989 | fat_cache_desc_t* pdesc = (fat_cache_desc_t*)node->children[index]; |
---|
990 | |
---|
991 | if ( pdesc == NULL ) // miss |
---|
992 | { |
---|
993 | // allocate one cluster descriptor and one 4K buffer |
---|
994 | unsigned char* buf = _malloc( 4096 ); |
---|
995 | pdesc = _malloc( sizeof(fat_cache_desc_t) ); |
---|
996 | |
---|
997 | // get missing cluster index lba |
---|
998 | unsigned int lba; |
---|
999 | unsigned int next; |
---|
1000 | unsigned int current = inode->cluster; |
---|
1001 | unsigned int count = cluster_id; |
---|
1002 | |
---|
1003 | if ( inode == NULL ) // searched cache is the Fat-Cache |
---|
1004 | { |
---|
1005 | |
---|
1006 | #if (GIET_DEBUG_FAT & 1) |
---|
1007 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
1008 | _printf("\n[DEBUG FAT] _get_buffer_from_cache() : miss in FAT-Cache for cluster_id %d\n", |
---|
1009 | cluster_id ); |
---|
1010 | #endif |
---|
1011 | lba = _fat.fat_lba + (cluster_id << 3); |
---|
1012 | } |
---|
1013 | else // searched cache is a File-Cache |
---|
1014 | { |
---|
1015 | |
---|
1016 | #if (GIET_DEBUG_FAT & 1) |
---|
1017 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
1018 | _printf("\n[DEBUG FAT] _get_buffer_from_cache() : miss in File-Cache <%s> " |
---|
1019 | "for cluster_id %d\n", inode->name, cluster_id ); |
---|
1020 | #endif |
---|
1021 | while ( count ) |
---|
1022 | { |
---|
1023 | if ( _get_fat_entry( current , &next ) ) return 1; |
---|
1024 | current = next; |
---|
1025 | count--; |
---|
1026 | } |
---|
1027 | lba = _cluster_to_lba( current ); |
---|
1028 | } |
---|
1029 | |
---|
1030 | // load one cluster (8 blocks) from block device |
---|
1031 | if ( _fat_ioc_access( 1, // descheduling |
---|
1032 | 1, // to memory |
---|
1033 | lba, |
---|
1034 | (unsigned int)buf, |
---|
1035 | 8 ) ) |
---|
1036 | { |
---|
1037 | _printf("\n[FAT ERROR] in _get_buffer_from_cache()" |
---|
1038 | " : cannot access block device for lba = %x\n", lba ); |
---|
1039 | return 1; |
---|
1040 | } |
---|
1041 | |
---|
1042 | // update cache and buffer descriptor |
---|
1043 | node->children[index] = pdesc; |
---|
1044 | pdesc->lba = lba; |
---|
1045 | pdesc->buffer = buf; |
---|
1046 | pdesc->dirty = 0; |
---|
1047 | |
---|
1048 | #if (GIET_DEBUG_FAT & 1) |
---|
1049 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
1050 | _printf("\n[DEBUG FAT] _get_buffer_from_cache() : buffer loaded from device" |
---|
1051 | " at vaddr = %x\n", (unsigned int)buf ); |
---|
1052 | #endif |
---|
1053 | } |
---|
1054 | |
---|
1055 | // return pdesc pointer |
---|
1056 | *desc = pdesc; |
---|
1057 | |
---|
1058 | // prepare next iteration |
---|
1059 | level--; |
---|
1060 | } |
---|
1061 | else // not last level => children are 64-tree nodes |
---|
1062 | { |
---|
1063 | fat_cache_node_t* child = (fat_cache_node_t*)node->children[index]; |
---|
1064 | if ( child == NULL ) // miss |
---|
1065 | { |
---|
1066 | // allocate a cache node if miss |
---|
1067 | child = (fat_cache_node_t*)_malloc( sizeof(fat_cache_node_t) ); |
---|
1068 | node->children[index] = child; |
---|
1069 | } |
---|
1070 | |
---|
1071 | // prepare next iteration |
---|
1072 | node = child; |
---|
1073 | level--; |
---|
1074 | } |
---|
1075 | } // end while |
---|
1076 | |
---|
1077 | return 0; |
---|
1078 | } // end _get_buffer_from_cache() |
---|
1079 | |
---|
1080 | |
---|
1081 | |
---|
1082 | |
---|
1083 | ///////////////////////////////////// |
---|
1084 | static unsigned int _update_fs_info() |
---|
1085 | { |
---|
1086 | // update buffer if miss |
---|
1087 | if ( _fat.fs_info_lba != _fat.block_buffer_lba ) |
---|
1088 | { |
---|
1089 | if ( _fat_ioc_access( 1, // descheduling |
---|
1090 | 1, // read |
---|
1091 | _fat.fs_info_lba, |
---|
1092 | (unsigned int)_fat.block_buffer, |
---|
1093 | 1 ) ) // one block |
---|
1094 | { |
---|
1095 | _printf("\n[FAT_ERROR] in _update_fs_info() cannot read block\n"); |
---|
1096 | return 1; |
---|
1097 | } |
---|
1098 | _fat.block_buffer_lba = _fat.fs_info_lba; |
---|
1099 | } |
---|
1100 | |
---|
1101 | // update FAT buffer |
---|
1102 | unsigned int* ptr; |
---|
1103 | ptr = (unsigned int*)(_fat.block_buffer + get_offset(FS_FREE_CLUSTERS) ); |
---|
1104 | *ptr = _fat.free_clusters_number; |
---|
1105 | |
---|
1106 | ptr = (unsigned int*)(_fat.block_buffer + get_offset(FS_FREE_CLUSTER_HINT) ); |
---|
1107 | *ptr = _fat.first_free_cluster; |
---|
1108 | |
---|
1109 | // write bloc to FAT |
---|
1110 | if ( _fat_ioc_access( 1, // descheduling |
---|
1111 | 0, // write |
---|
1112 | _fat.fs_info_lba, |
---|
1113 | (unsigned int)_fat.block_buffer, |
---|
1114 | 1 ) ) // one block |
---|
1115 | { |
---|
1116 | _printf("\n[FAT_ERROR] in _update_fs_info() cannot write block\n"); |
---|
1117 | return 1; |
---|
1118 | } |
---|
1119 | |
---|
1120 | #if (GIET_DEBUG_FAT & 1) |
---|
1121 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
1122 | _printf("\n[DEBUG FAT] _update_fs_info() : nb_free = %x / first_free = %x\n", |
---|
1123 | _fat.free_clusters_number , _fat.first_free_cluster ); |
---|
1124 | #endif |
---|
1125 | |
---|
1126 | return 0; |
---|
1127 | } // end _update_fs_info() |
---|
1128 | |
---|
1129 | |
---|
1130 | |
---|
1131 | ///////////////////////////////////////////////////////////////// |
---|
1132 | static inline unsigned int _get_fat_entry( unsigned int cluster, |
---|
1133 | unsigned int* value ) |
---|
1134 | { |
---|
1135 | // compute cluster_id & entry_id in FAT |
---|
1136 | // a FAT cluster is an array of 1024 unsigned int entries |
---|
1137 | unsigned int cluster_id = cluster >> 10; |
---|
1138 | unsigned int entry_id = cluster & 0x3FF; |
---|
1139 | |
---|
1140 | // get pointer on the relevant cluster descriptor in FAT cache |
---|
1141 | fat_cache_desc_t* pdesc; |
---|
1142 | unsigned int* buffer; |
---|
1143 | if ( _get_buffer_from_cache( NULL, // Fat-Cache |
---|
1144 | cluster_id, |
---|
1145 | &pdesc ) ) return 1; |
---|
1146 | |
---|
1147 | // get value from FAT slot |
---|
1148 | buffer = (unsigned int*)pdesc->buffer; |
---|
1149 | *value = buffer[entry_id]; |
---|
1150 | |
---|
1151 | return 0; |
---|
1152 | } // end _get_fat_entry() |
---|
1153 | |
---|
1154 | |
---|
1155 | |
---|
1156 | //////////////////////////////////////////////////////////////// |
---|
1157 | static inline unsigned int _set_fat_entry( unsigned int cluster, |
---|
1158 | unsigned int value ) |
---|
1159 | { |
---|
1160 | // compute cluster_id & entry_id in FAT |
---|
1161 | // a FAT cluster is an array of 1024 unsigned int entries |
---|
1162 | unsigned int cluster_id = cluster >> 10; |
---|
1163 | unsigned int entry_id = cluster & 0x3FF; |
---|
1164 | |
---|
1165 | // get pointer on the relevant cluster descriptor in FAT cache |
---|
1166 | fat_cache_desc_t* pdesc; |
---|
1167 | unsigned int* buffer; |
---|
1168 | if ( _get_buffer_from_cache( NULL, // Fat-Cache |
---|
1169 | cluster_id, |
---|
1170 | &pdesc ) ) return 1; |
---|
1171 | |
---|
1172 | // set value into FAT slot |
---|
1173 | buffer = (unsigned int*)pdesc->buffer; |
---|
1174 | buffer[entry_id] = value; |
---|
1175 | pdesc->dirty = 1; |
---|
1176 | |
---|
1177 | return 0; |
---|
1178 | } // end _set_fat_entry() |
---|
1179 | |
---|
1180 | |
---|
1181 | |
---|
1182 | ////////////////////////////////////////////////////// |
---|
1183 | static void _allocate_one_buffer( fat_inode_t* inode, |
---|
1184 | unsigned int cluster_id, |
---|
1185 | unsigned int cluster ) |
---|
1186 | { |
---|
1187 | // search the 64-tree cache from top to bottom |
---|
1188 | fat_cache_node_t* node = inode->cache; |
---|
1189 | unsigned int level = inode->levels; |
---|
1190 | |
---|
1191 | while ( level ) |
---|
1192 | { |
---|
1193 | // compute child index |
---|
1194 | unsigned int index = (cluster_id >> (6*(level-1))) & 0x3F; |
---|
1195 | |
---|
1196 | if ( level == 1 ) // last level => children are cluster descriptors |
---|
1197 | { |
---|
1198 | fat_cache_desc_t* pdesc = (fat_cache_desc_t*)node->children[index]; |
---|
1199 | if ( pdesc != NULL ) // slot not empty!!! |
---|
1200 | { |
---|
1201 | _printf("\n[FAT ERROR] in allocate_one buffer() : slot not empty " |
---|
1202 | "in File-Cache <%s> / cluster_id = %d\n", inode->name , cluster_id ); |
---|
1203 | _exit(); |
---|
1204 | } |
---|
1205 | |
---|
1206 | // allocate a cluster descriptor and a 4K buffer |
---|
1207 | pdesc = _malloc( sizeof(fat_cache_desc_t) ); |
---|
1208 | unsigned char* buffer = _malloc( 4096 ); |
---|
1209 | |
---|
1210 | #if (GIET_DEBUG_FAT & 1) |
---|
1211 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
1212 | _printf("\n[DEBUG FAT] _allocate_one_buffer() : buffer allocated to <%s> for cluster_id %d\n", |
---|
1213 | inode->name, cluster_id ); |
---|
1214 | #endif |
---|
1215 | |
---|
1216 | // update cache and pdesc |
---|
1217 | node->children[index] = pdesc; |
---|
1218 | pdesc->lba = _cluster_to_lba( cluster ); |
---|
1219 | pdesc->buffer = buffer; |
---|
1220 | pdesc->dirty = 1; |
---|
1221 | |
---|
1222 | // prepare next iteration |
---|
1223 | level--; |
---|
1224 | } |
---|
1225 | else // not last level => children are 64-tree nodes |
---|
1226 | { |
---|
1227 | fat_cache_node_t* child = (fat_cache_node_t*)node->children[index]; |
---|
1228 | if ( child == NULL ) // miss |
---|
1229 | { |
---|
1230 | // allocate a cache node if miss |
---|
1231 | child = (fat_cache_node_t*)_malloc( sizeof(fat_cache_node_t) ); |
---|
1232 | node->children[index] = child; |
---|
1233 | } |
---|
1234 | |
---|
1235 | // prepare next iteration |
---|
1236 | node = child; |
---|
1237 | level--; |
---|
1238 | } |
---|
1239 | } // end while |
---|
1240 | } // end _allocate_one_buffer |
---|
1241 | |
---|
1242 | |
---|
1243 | |
---|
1244 | |
---|
1245 | /////////////////////////////////////////////////////////////////// |
---|
1246 | static unsigned int _allocate_one_cluster( unsigned int* cluster ) |
---|
1247 | { |
---|
1248 | unsigned int nb_free = _fat.free_clusters_number; |
---|
1249 | unsigned int free = _fat.first_free_cluster; |
---|
1250 | |
---|
1251 | // scan FAT to get next free cluster index |
---|
1252 | unsigned int current = free; |
---|
1253 | unsigned int found = 0; |
---|
1254 | unsigned int max = (_fat.data_sectors >> 3); |
---|
1255 | unsigned int value; |
---|
1256 | do |
---|
1257 | { |
---|
1258 | // increment current |
---|
1259 | current++; |
---|
1260 | |
---|
1261 | // get FAT entry indexed by current |
---|
1262 | if ( _get_fat_entry( current , &value ) ) return 1; |
---|
1263 | // test if free |
---|
1264 | if ( value == FREE_CLUSTER ) found = 1; |
---|
1265 | } |
---|
1266 | while ( (current < max) && (found == 0) ); |
---|
1267 | |
---|
1268 | // check found |
---|
1269 | if ( found == 0 ) |
---|
1270 | { |
---|
1271 | _printf("\n[FAT_ERROR] in _allocate_one_cluster() : unconsistent FAT state"); |
---|
1272 | return 1; |
---|
1273 | } |
---|
1274 | |
---|
1275 | // update allocated FAT slot |
---|
1276 | if ( _set_fat_entry( free , END_OF_CHAIN_CLUSTER_MAX ) ) return 1; |
---|
1277 | |
---|
1278 | // update FAT descriptor global variables |
---|
1279 | _fat.free_clusters_number = nb_free - 1; |
---|
1280 | _fat.first_free_cluster = current; |
---|
1281 | |
---|
1282 | #if (GIET_DEBUG_FAT & 1) |
---|
1283 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
1284 | _printf("\n[DEBUG FAT] _allocate_one_cluster() : cluster = %x / first_free = %x\n", |
---|
1285 | free , current ); |
---|
1286 | #endif |
---|
1287 | |
---|
1288 | // returns free cluster index |
---|
1289 | *cluster = free; |
---|
1290 | return 0; |
---|
1291 | |
---|
1292 | } // end _allocate_one_cluster() |
---|
1293 | |
---|
1294 | |
---|
1295 | |
---|
1296 | |
---|
1297 | ////////////////////////////////////////////////////////////////////////// |
---|
1298 | static unsigned int _update_device_from_cache( unsigned int levels, |
---|
1299 | fat_cache_node_t* root, |
---|
1300 | char* string ) |
---|
1301 | { |
---|
1302 | unsigned int index; |
---|
1303 | unsigned int ret = 0; |
---|
1304 | |
---|
1305 | if ( levels == 1 ) // last level => children are cluster descriptors |
---|
1306 | { |
---|
1307 | for( index = 0 ; index < 64 ; index++ ) |
---|
1308 | { |
---|
1309 | fat_cache_desc_t* pdesc = root->children[index]; |
---|
1310 | if ( pdesc != NULL ) |
---|
1311 | { |
---|
1312 | // update cluster on device if dirty |
---|
1313 | if ( pdesc->dirty ) |
---|
1314 | { |
---|
1315 | if ( _fat_ioc_access( 1, // descheduling |
---|
1316 | 0, // to block device |
---|
1317 | pdesc->lba, |
---|
1318 | (unsigned int)pdesc->buffer, |
---|
1319 | 8 ) ) |
---|
1320 | { |
---|
1321 | _printf("\n[FAT_ERROR] in _update_device from_cache() : " |
---|
1322 | " cannot access lba = %x\n", pdesc->lba ); |
---|
1323 | ret = 1; |
---|
1324 | } |
---|
1325 | else |
---|
1326 | { |
---|
1327 | pdesc->dirty = 0; |
---|
1328 | |
---|
1329 | #if (GIET_DEBUG_FAT & 1) |
---|
1330 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
1331 | _printf("\n[DEBUG FAT] _update_device_from_cache() : cluster_id = %d for <%s>\n", |
---|
1332 | index , string ); |
---|
1333 | #endif |
---|
1334 | |
---|
1335 | } |
---|
1336 | } |
---|
1337 | } |
---|
1338 | } |
---|
1339 | } |
---|
1340 | else // not the last level = recursive call on each children |
---|
1341 | { |
---|
1342 | for( index = 0 ; index < 64 ; index++ ) |
---|
1343 | { |
---|
1344 | fat_cache_node_t* pnode = root->children[index]; |
---|
1345 | if ( pnode != NULL ) |
---|
1346 | { |
---|
1347 | if ( _update_device_from_cache( levels - 1, |
---|
1348 | root->children[index], |
---|
1349 | string ) ) ret = 1; |
---|
1350 | } |
---|
1351 | } |
---|
1352 | } |
---|
1353 | return ret; |
---|
1354 | } // end _update_device_from_cache() |
---|
1355 | |
---|
1356 | |
---|
1357 | |
---|
1358 | /////////////////////////////////////////////////////////////////// |
---|
1359 | static unsigned int _release_cache_memory( fat_cache_node_t* root, |
---|
1360 | unsigned int levels ) |
---|
1361 | { |
---|
1362 | unsigned int index; |
---|
1363 | unsigned int ret = 0; |
---|
1364 | |
---|
1365 | if ( levels == 1 ) // last level => children are cluster descriptors |
---|
1366 | { |
---|
1367 | for( index = 0 ; index < 64 ; index++ ) |
---|
1368 | { |
---|
1369 | fat_cache_desc_t* pdesc = root->children[index]; |
---|
1370 | |
---|
1371 | if ( pdesc != NULL ) |
---|
1372 | { |
---|
1373 | // check dirty |
---|
1374 | if ( pdesc->dirty ) |
---|
1375 | { |
---|
1376 | _printf("\n[FAT_ERROR] in _release_cache_memory() : dirty cluster\n"); |
---|
1377 | ret = 1; |
---|
1378 | } |
---|
1379 | else |
---|
1380 | { |
---|
1381 | _free( pdesc->buffer ); |
---|
1382 | _free( pdesc ); |
---|
1383 | root->children[index] = NULL; |
---|
1384 | } |
---|
1385 | } |
---|
1386 | } |
---|
1387 | } |
---|
1388 | else // not the last level = recursive call on each children |
---|
1389 | { |
---|
1390 | for( index = 0 ; index < 64 ; index++ ) |
---|
1391 | { |
---|
1392 | fat_cache_node_t* cnode = root->children[index]; |
---|
1393 | |
---|
1394 | if ( cnode != NULL ) ret = _release_cache_memory( root->children[index], |
---|
1395 | levels - 1 ); |
---|
1396 | } |
---|
1397 | } |
---|
1398 | return ret; |
---|
1399 | } // end _release_cache_memory() |
---|
1400 | |
---|
1401 | |
---|
1402 | |
---|
1403 | |
---|
1404 | |
---|
1405 | ///////////////////////////////////////////////////////////// |
---|
1406 | static unsigned int _clusters_allocate( fat_inode_t* inode, |
---|
1407 | unsigned int nb_current_clusters, |
---|
1408 | unsigned int nb_required_clusters ) |
---|
1409 | { |
---|
1410 | // Check if FAT contains enough free clusters |
---|
1411 | if ( nb_required_clusters > _fat.free_clusters_number ) |
---|
1412 | { |
---|
1413 | _printf("\n[FAT ERROR] in _clusters_allocate() : required_clusters = %d" |
---|
1414 | " / free_clusters = %d\n", nb_required_clusters , _fat.free_clusters_number ); |
---|
1415 | return 1; |
---|
1416 | } |
---|
1417 | |
---|
1418 | #if (GIET_DEBUG_FAT & 1) |
---|
1419 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
1420 | _printf("\n[DEBUG FAT] _clusters_allocate() : enters for <%s> / nb_current_clusters = %d " |
---|
1421 | "/ nb_required_clusters = %d\n", |
---|
1422 | inode->name , nb_current_clusters , nb_required_clusters ); |
---|
1423 | #endif |
---|
1424 | |
---|
1425 | // compute last allocated cluster index when (nb_current_clusters > 0) |
---|
1426 | unsigned int current = inode->cluster; |
---|
1427 | unsigned int next; |
---|
1428 | unsigned int last; |
---|
1429 | if ( nb_current_clusters ) // clusters allocated => search last |
---|
1430 | { |
---|
1431 | while ( current < END_OF_CHAIN_CLUSTER_MIN ) |
---|
1432 | { |
---|
1433 | // get next cluster |
---|
1434 | if ( _get_fat_entry( current , &next ) ) return 1; |
---|
1435 | last = current; |
---|
1436 | current = next; |
---|
1437 | } |
---|
1438 | } |
---|
1439 | |
---|
1440 | // Loop on the new clusters to be allocated |
---|
1441 | // if (nb_current_clusters == 0) the first new cluster index must |
---|
1442 | // be written in inode->cluster field |
---|
1443 | // if (nb_current_clusters > 0) the first new cluster index must |
---|
1444 | // be written in FAT[last] |
---|
1445 | unsigned int cluster_id; |
---|
1446 | unsigned int new; |
---|
1447 | for ( cluster_id = nb_current_clusters ; |
---|
1448 | cluster_id < (nb_current_clusters + nb_required_clusters) ; |
---|
1449 | cluster_id ++ ) |
---|
1450 | { |
---|
1451 | // allocate one cluster on block device |
---|
1452 | if ( _allocate_one_cluster( &new ) ) return 1; |
---|
1453 | |
---|
1454 | // allocate one 4K buffer to File-Cache |
---|
1455 | _allocate_one_buffer( inode, |
---|
1456 | cluster_id, |
---|
1457 | new ); |
---|
1458 | |
---|
1459 | if ( cluster_id == 0 ) // update inode |
---|
1460 | { |
---|
1461 | inode->cluster = new; |
---|
1462 | } |
---|
1463 | else // update FAT |
---|
1464 | { |
---|
1465 | if ( _set_fat_entry( last , new ) ) return 1; |
---|
1466 | } |
---|
1467 | |
---|
1468 | #if (GIET_DEBUG_FAT & 1) |
---|
1469 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
1470 | _printf("\n[DEBUG FAT] _clusters_allocate() : done for cluster_id = %d / cluster = %x\n", |
---|
1471 | cluster_id , new ); |
---|
1472 | #endif |
---|
1473 | |
---|
1474 | // update loop variables |
---|
1475 | last = new; |
---|
1476 | |
---|
1477 | } // end for loop |
---|
1478 | |
---|
1479 | // update FAT : last slot should contain END_OF_CHAIN_CLUSTER_MAX |
---|
1480 | if ( _set_fat_entry( last , END_OF_CHAIN_CLUSTER_MAX ) ) return 1; |
---|
1481 | |
---|
1482 | // update the FAT on block device |
---|
1483 | if ( _update_device_from_cache( _fat.fat_cache_levels, |
---|
1484 | _fat.fat_cache_root, |
---|
1485 | "FAT" ) ) return 1; |
---|
1486 | return 0; |
---|
1487 | } // end _clusters_allocate() |
---|
1488 | |
---|
1489 | |
---|
1490 | |
---|
1491 | ////////////////////////////////////////////////////////////// |
---|
1492 | static unsigned int _clusters_release( unsigned int cluster ) |
---|
1493 | { |
---|
1494 | // scan the FAT |
---|
1495 | unsigned int current = cluster; |
---|
1496 | unsigned int next; |
---|
1497 | do |
---|
1498 | { |
---|
1499 | // get next_cluster index |
---|
1500 | if ( _get_fat_entry( current , &next ) ) return 1; |
---|
1501 | |
---|
1502 | // release current_cluster |
---|
1503 | if ( _set_fat_entry( current , FREE_CLUSTER ) ) return 1; |
---|
1504 | |
---|
1505 | // update first_free_cluster and free_clusters_number in FAT descriptor |
---|
1506 | _fat.free_clusters_number = _fat.free_clusters_number + 1; |
---|
1507 | if ( _fat.first_free_cluster > current ) _fat.first_free_cluster = current; |
---|
1508 | |
---|
1509 | // update loop variable |
---|
1510 | current = next; |
---|
1511 | } |
---|
1512 | while ( next < END_OF_CHAIN_CLUSTER_MIN ); |
---|
1513 | |
---|
1514 | // update the FAT on block device |
---|
1515 | if ( _update_device_from_cache( _fat.fat_cache_levels, |
---|
1516 | _fat.fat_cache_root, |
---|
1517 | "FAT" ) ) return 1; |
---|
1518 | return 0; |
---|
1519 | } // end _clusters_release() |
---|
1520 | |
---|
1521 | |
---|
1522 | |
---|
1523 | /////////////////////////////////////////////////////////// |
---|
1524 | static void _add_special_directories( fat_inode_t* child, |
---|
1525 | fat_inode_t* parent ) |
---|
1526 | { |
---|
1527 | // get first File-Cache buffer for child |
---|
1528 | fat_cache_desc_t* pdesc = (fat_cache_desc_t*)child->cache->children[0]; |
---|
1529 | unsigned char* entry; |
---|
1530 | |
---|
1531 | unsigned int i; |
---|
1532 | unsigned int cluster; |
---|
1533 | unsigned int size; |
---|
1534 | |
---|
1535 | // set "." entry (32 bytes) |
---|
1536 | cluster = child->cluster; |
---|
1537 | size = child->size; |
---|
1538 | entry = pdesc->buffer; |
---|
1539 | |
---|
1540 | for ( i = 0 ; i < 32 ; i++ ) |
---|
1541 | { |
---|
1542 | if (i == 0 ) entry[i] = 0x2E; // SFN |
---|
1543 | else if (i < 11) entry[i] = 0x20; // SFN |
---|
1544 | else if (i == 11) entry[i] = 0x10; // ATTR == dir |
---|
1545 | else if (i == 20) entry[i] = cluster>>16; // cluster.B2 |
---|
1546 | else if (i == 21) entry[i] = cluster>>24; // cluster.B3 |
---|
1547 | else if (i == 26) entry[i] = cluster>>0; // cluster.B0 |
---|
1548 | else if (i == 27) entry[i] = cluster>>8; // cluster.B1 |
---|
1549 | else if (i == 28) entry[i] = size>>0; // size.B0 |
---|
1550 | else if (i == 29) entry[i] = size>>8; // size.B1 |
---|
1551 | else if (i == 30) entry[i] = size>>16; // size.B2 |
---|
1552 | else if (i == 31) entry[i] = size>>24; // size.B3 |
---|
1553 | else entry[i] = 0x00; |
---|
1554 | } |
---|
1555 | |
---|
1556 | // set ".." entry (32 bytes) |
---|
1557 | cluster = parent->cluster; |
---|
1558 | size = parent->size; |
---|
1559 | entry = pdesc->buffer + 32; |
---|
1560 | |
---|
1561 | for ( i = 0 ; i < 32 ; i++ ) |
---|
1562 | { |
---|
1563 | if (i < 2 ) entry[i] = 0x2E; // SFN |
---|
1564 | else if (i < 11) entry[i] = 0x20; // SFN |
---|
1565 | else if (i == 11) entry[i] = 0x10; // ATTR == dir |
---|
1566 | else if (i == 20) entry[i] = cluster>>16; // cluster.B2 |
---|
1567 | else if (i == 21) entry[i] = cluster>>24; // cluster.B3 |
---|
1568 | else if (i == 26) entry[i] = cluster>>0; // cluster.B0 |
---|
1569 | else if (i == 27) entry[i] = cluster>>8; // cluster.B1 |
---|
1570 | else if (i == 28) entry[i] = size>>0; // size.B0 |
---|
1571 | else if (i == 29) entry[i] = size>>8; // size.B1 |
---|
1572 | else if (i == 30) entry[i] = size>>16; // size.B2 |
---|
1573 | else if (i == 31) entry[i] = size>>24; // size.B3 |
---|
1574 | else entry[i] = 0x00; |
---|
1575 | } |
---|
1576 | } // end _add_special_directories |
---|
1577 | |
---|
1578 | |
---|
1579 | |
---|
1580 | //////////////////////////////////////////////////////////// |
---|
1581 | static unsigned int _check_name_length( char* name, |
---|
1582 | unsigned int* length, |
---|
1583 | unsigned int* nb_lfn ) |
---|
1584 | { |
---|
1585 | unsigned int len = _strlen( name ); |
---|
1586 | if ( len <= 13 ) |
---|
1587 | { |
---|
1588 | *length = len; |
---|
1589 | *nb_lfn = 1; |
---|
1590 | return 0; |
---|
1591 | } |
---|
1592 | else if ( len <= 26 ) |
---|
1593 | { |
---|
1594 | *length = len; |
---|
1595 | *nb_lfn = 2; |
---|
1596 | return 0; |
---|
1597 | } |
---|
1598 | else if ( len <= 31 ) |
---|
1599 | { |
---|
1600 | *length = len; |
---|
1601 | *nb_lfn = 3; |
---|
1602 | return 0; |
---|
1603 | } |
---|
1604 | else |
---|
1605 | { |
---|
1606 | return 1; |
---|
1607 | } |
---|
1608 | } // _check_name_length() |
---|
1609 | |
---|
1610 | |
---|
1611 | |
---|
1612 | |
---|
1613 | /////////////////////////////////////////////////////////// |
---|
1614 | static unsigned int _get_nb_entries( fat_inode_t* inode, |
---|
1615 | unsigned int* nb_entries ) |
---|
1616 | { |
---|
1617 | // scan directory until "end of directory" with two embedded loops: |
---|
1618 | // - scan the clusters allocated to this directory |
---|
1619 | // - scan the entries to find NO_MORE_ENTRY |
---|
1620 | fat_cache_desc_t* pdesc; // pointer on buffer descriptor |
---|
1621 | unsigned char* buffer; // 4 Kbytes buffer (one cluster) |
---|
1622 | unsigned int ord; // ORD field in directory entry |
---|
1623 | unsigned int attr; // ATTR field in directory entry |
---|
1624 | unsigned int cluster_id = 0; // cluster index in directory |
---|
1625 | unsigned int offset = 0; // position in scanned buffer |
---|
1626 | unsigned int found = 0; // NO_MORE_ENTRY found |
---|
1627 | unsigned int count = 0; // number of valid NORMAL entries |
---|
1628 | |
---|
1629 | // loop on clusters allocated to directory |
---|
1630 | while ( found == 0 ) |
---|
1631 | { |
---|
1632 | // get one 4 Kytes buffer from File_Cache |
---|
1633 | if ( _get_buffer_from_cache( inode, |
---|
1634 | cluster_id, |
---|
1635 | &pdesc ) ) return 1; |
---|
1636 | buffer = pdesc->buffer; |
---|
1637 | |
---|
1638 | // loop on directory entries in buffer |
---|
1639 | while ( (offset < 4096) && (found == 0) ) |
---|
1640 | { |
---|
1641 | attr = _read_entry( DIR_ATTR , buffer + offset , 0 ); |
---|
1642 | ord = _read_entry( LDIR_ORD , buffer + offset , 0 ); |
---|
1643 | |
---|
1644 | if ( ord == NO_MORE_ENTRY ) |
---|
1645 | { |
---|
1646 | found = 1; |
---|
1647 | } |
---|
1648 | else if ( ord == FREE_ENTRY ) // free entry => skip |
---|
1649 | { |
---|
1650 | offset = offset + 32; |
---|
1651 | } |
---|
1652 | else if ( attr == ATTR_LONG_NAME_MASK ) // LFN entry => skip |
---|
1653 | { |
---|
1654 | offset = offset + 32; |
---|
1655 | } |
---|
1656 | else // NORMAL entry |
---|
1657 | { |
---|
1658 | offset = offset + 32; |
---|
1659 | count++; |
---|
1660 | } |
---|
1661 | } // end loop on directory entries |
---|
1662 | |
---|
1663 | cluster_id++; |
---|
1664 | offset = 0; |
---|
1665 | |
---|
1666 | } // end loop on clusters |
---|
1667 | |
---|
1668 | // return nb_entries |
---|
1669 | *nb_entries = count; |
---|
1670 | |
---|
1671 | return 0; |
---|
1672 | } // end dir_not_empty() |
---|
1673 | |
---|
1674 | |
---|
1675 | |
---|
1676 | /////////////////////////////////////////////////////////// |
---|
1677 | static unsigned int _add_dir_entry( fat_inode_t* child, |
---|
1678 | fat_inode_t* parent ) |
---|
1679 | { |
---|
1680 | // get child attributes |
---|
1681 | unsigned int is_dir = child->is_dir; |
---|
1682 | unsigned int size = child->size; |
---|
1683 | unsigned int cluster = child->cluster; |
---|
1684 | |
---|
1685 | // compute number of required entries to store child->name |
---|
1686 | // - Short name (less than 13 characters) require 3 entries: |
---|
1687 | // one LFN entry / one NORMAL entry / one NO_MORE_ENTRY entry. |
---|
1688 | // - Longer names (up to 31 characters) can require 4 or 5 entries: |
---|
1689 | // 2 or 3 LFN entries / one NORMAL entry / one NO_MORE entry. |
---|
1690 | unsigned int length; |
---|
1691 | unsigned int nb_lfn; |
---|
1692 | if ( _check_name_length( child->name, |
---|
1693 | &length, |
---|
1694 | &nb_lfn ) ) return 1; |
---|
1695 | |
---|
1696 | #if (GIET_DEBUG_FAT & 1) |
---|
1697 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
1698 | _printf("\n[DEBUG FAT] _add_dir_entry() : try to add <%s> in <%s> / nb_lfn = %d\n", |
---|
1699 | child->name , parent->name, nb_lfn ); |
---|
1700 | #endif |
---|
1701 | |
---|
1702 | // Find end of directory : two embedded loops: |
---|
1703 | // - scan the clusters allocated to this directory |
---|
1704 | // - scan the entries to find NO_MORE_ENTRY |
---|
1705 | fat_cache_desc_t* pdesc; // pointer on buffer descriptor |
---|
1706 | unsigned char* buffer; // 4 Kbytes buffer (one cluster) |
---|
1707 | unsigned int cluster_id = 0; // cluster index in directory |
---|
1708 | unsigned int offset = 0; // position in scanned buffer |
---|
1709 | unsigned int found = 0; // NO_MORE_ENTRY found |
---|
1710 | |
---|
1711 | // loop on clusters allocated to directory |
---|
1712 | while ( found == 0 ) |
---|
1713 | { |
---|
1714 | // get one 4 Kytes buffer from File_Cache |
---|
1715 | if ( _get_buffer_from_cache( parent, |
---|
1716 | cluster_id, |
---|
1717 | &pdesc ) ) return 1; |
---|
1718 | |
---|
1719 | buffer = pdesc->buffer; |
---|
1720 | |
---|
1721 | // loop on directory entries in buffer |
---|
1722 | while ( (offset < 4096) && (found == 0) ) |
---|
1723 | { |
---|
1724 | if ( _read_entry( LDIR_ORD , buffer + offset , 0 ) == NO_MORE_ENTRY ) |
---|
1725 | { |
---|
1726 | found = 1; |
---|
1727 | pdesc->dirty = 1; |
---|
1728 | } |
---|
1729 | else |
---|
1730 | { |
---|
1731 | offset = offset + 32; |
---|
1732 | } |
---|
1733 | } // end loop on entries |
---|
1734 | if ( found == 0 ) |
---|
1735 | { |
---|
1736 | cluster_id++; |
---|
1737 | offset = 0; |
---|
1738 | } |
---|
1739 | } // end loop on clusters |
---|
1740 | |
---|
1741 | #if (GIET_DEBUG_FAT & 1) |
---|
1742 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
1743 | _printf("\n[DEBUG FAT] _add_dir_entry() : get NO_MORE directory entry : " |
---|
1744 | " buffer = %x / offset = %x / cluster_id = %d\n", |
---|
1745 | (unsigned int)buffer , offset , cluster_id ); |
---|
1746 | #endif |
---|
1747 | |
---|
1748 | // enter FSM : |
---|
1749 | // The new child requires to write 3, 4, or 5 directory entries. |
---|
1750 | // To actually register the new child, we use a 5 steps FSM |
---|
1751 | // (one state per entry to be written), that is traversed as: |
---|
1752 | // LFN3 -> LFN2 -> LFN1 -> NORMAL -> NOMORE |
---|
1753 | // The buffer and first directory entry to be written are identified |
---|
1754 | // by the variables : buffer / cluster_id / offset |
---|
1755 | |
---|
1756 | unsigned char* name = (unsigned char*)child->name; |
---|
1757 | |
---|
1758 | unsigned int step; // FSM state |
---|
1759 | |
---|
1760 | if ( nb_lfn == 1 ) step = 3; |
---|
1761 | else if ( nb_lfn == 2 ) step = 4; |
---|
1762 | else if ( nb_lfn == 3 ) step = 5; |
---|
1763 | |
---|
1764 | unsigned int i; // byte index in 32 bytes directory |
---|
1765 | unsigned int c; // character index in name |
---|
1766 | unsigned char* entry; // buffer + offset; |
---|
1767 | |
---|
1768 | while ( step ) |
---|
1769 | { |
---|
1770 | // get another buffer if required |
---|
1771 | if ( offset >= 4096 ) // new buffer required |
---|
1772 | { |
---|
1773 | if ( cluster_id == 63 ) // we need to increase depth of File-Cache |
---|
1774 | { |
---|
1775 | _printf("\n[FAT ERROR] in add_dir_entry() File Cache depth extension " |
---|
1776 | " not implemented\n" ); |
---|
1777 | _exit(); // TODO |
---|
1778 | } |
---|
1779 | else |
---|
1780 | { |
---|
1781 | if ( _get_buffer_from_cache( parent, |
---|
1782 | cluster_id + 1, |
---|
1783 | &pdesc ) ) return 1; |
---|
1784 | buffer = pdesc->buffer; |
---|
1785 | pdesc->dirty = 1; |
---|
1786 | offset = 0; |
---|
1787 | } |
---|
1788 | } |
---|
1789 | |
---|
1790 | // compute directory entry address |
---|
1791 | entry = buffer + offset; |
---|
1792 | |
---|
1793 | #if (GIET_DEBUG_FAT & 1) |
---|
1794 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
1795 | _printf("\n[DEBUG FAT] _add_dir_entry() : FSM step = %d /" |
---|
1796 | " offset = %x / nb_lfn = %d\n", step, offset, nb_lfn ); |
---|
1797 | #endif |
---|
1798 | |
---|
1799 | // write one 32 bytes directory entry per iteration |
---|
1800 | switch ( step ) |
---|
1801 | { |
---|
1802 | case 5: // write LFN3 entry |
---|
1803 | { |
---|
1804 | c = 26; |
---|
1805 | // scan the 32 bytes in dir_entry |
---|
1806 | for ( i = 0 ; i < 32 ; i++ ) |
---|
1807 | { |
---|
1808 | if (i == 0) |
---|
1809 | { |
---|
1810 | if ( nb_lfn == 3) entry[i] = 0x43; |
---|
1811 | else entry[i] = 0x03; |
---|
1812 | } |
---|
1813 | else if ( ( ((i >= 1 ) && (i<=10) && ((i&1)==1)) || |
---|
1814 | ((i >= 14) && (i<=25) && ((i&1)==0)) || |
---|
1815 | ((i >= 28) && (i<=31) && ((i&1)==0)) ) && |
---|
1816 | ( c < length ) ) |
---|
1817 | { |
---|
1818 | entry[i] = name[c]; |
---|
1819 | c++; |
---|
1820 | } |
---|
1821 | else if (i == 11) entry[i] = 0x0F; |
---|
1822 | else if (i == 12) entry[i] = 0xCA; |
---|
1823 | else entry[i] = 0x00; |
---|
1824 | } |
---|
1825 | step--; |
---|
1826 | break; |
---|
1827 | } |
---|
1828 | case 4: // write LFN2 entry |
---|
1829 | { |
---|
1830 | c = 13; |
---|
1831 | // scan the 32 bytes in dir_entry |
---|
1832 | for ( i = 0 ; i < 32 ; i++ ) |
---|
1833 | { |
---|
1834 | if (i == 0) |
---|
1835 | { |
---|
1836 | if ( nb_lfn == 2) entry[i] = 0x42; |
---|
1837 | else entry[i] = 0x02; |
---|
1838 | } |
---|
1839 | else if ( ( ((i >= 1 ) && (i<=10) && ((i&1)==1)) || |
---|
1840 | ((i >= 14) && (i<=25) && ((i&1)==0)) || |
---|
1841 | ((i >= 28) && (i<=31) && ((i&1)==0)) ) && |
---|
1842 | ( c < length ) ) |
---|
1843 | { |
---|
1844 | entry[i] = name[c]; |
---|
1845 | c++; |
---|
1846 | } |
---|
1847 | else if (i == 11) entry[i] = 0x0F; |
---|
1848 | else if (i == 12) entry[i] = 0xCA; |
---|
1849 | else entry[i] = 0x00; |
---|
1850 | } |
---|
1851 | step--; |
---|
1852 | break; |
---|
1853 | } |
---|
1854 | case 3: // Write LFN1 entry |
---|
1855 | { |
---|
1856 | c = 0; |
---|
1857 | // scan the 32 bytes in dir_entry |
---|
1858 | for ( i = 0 ; i < 32 ; i++ ) |
---|
1859 | { |
---|
1860 | if (i == 0) |
---|
1861 | { |
---|
1862 | if ( nb_lfn == 1) entry[i] = 0x41; |
---|
1863 | else entry[i] = 0x01; |
---|
1864 | } |
---|
1865 | else if ( ( ((i >= 1 ) && (i<=10) && ((i&1)==1)) || |
---|
1866 | ((i >= 14) && (i<=25) && ((i&1)==0)) || |
---|
1867 | ((i >= 28) && (i<=31) && ((i&1)==0)) ) && |
---|
1868 | ( c < length ) ) |
---|
1869 | { |
---|
1870 | entry[i] = name[c]; |
---|
1871 | c++; |
---|
1872 | } |
---|
1873 | else if (i == 11) entry[i] = 0x0F; |
---|
1874 | else if (i == 12) entry[i] = 0xCA; |
---|
1875 | else entry[i] = 0x00; |
---|
1876 | } |
---|
1877 | step--; |
---|
1878 | break; |
---|
1879 | } |
---|
1880 | case 2: // write NORMAL entry |
---|
1881 | { |
---|
1882 | c = 0; |
---|
1883 | // scan the 32 bytes in dir_entry |
---|
1884 | for ( i = 0 ; i < 32 ; i++ ) |
---|
1885 | { |
---|
1886 | if ( (i < 8) && (c < length) ) // SFN |
---|
1887 | { |
---|
1888 | entry[i] = _to_upper( name[c] ); |
---|
1889 | c++; |
---|
1890 | } |
---|
1891 | else if (i < 11) entry[i] = 0x20; // EXT |
---|
1892 | else if (i == 11) // ATTR |
---|
1893 | { |
---|
1894 | if (is_dir) entry[i] = 0x10; |
---|
1895 | else entry[i] = 0x20; |
---|
1896 | } |
---|
1897 | else if (i == 20) entry[i] = cluster>>16; // cluster.B2 |
---|
1898 | else if (i == 21) entry[i] = cluster>>24; // cluster.B3 |
---|
1899 | else if (i == 26) entry[i] = cluster>>0; // cluster.B0 |
---|
1900 | else if (i == 27) entry[i] = cluster>>8; // cluster.B1 |
---|
1901 | else if (i == 28) entry[i] = size>>0; // size.B0 |
---|
1902 | else if (i == 29) entry[i] = size>>8; // size.B1 |
---|
1903 | else if (i == 30) entry[i] = size>>16; // size.B2 |
---|
1904 | else if (i == 31) entry[i] = size>>24; // size.B3 |
---|
1905 | else entry[i] = 0x00; |
---|
1906 | } |
---|
1907 | |
---|
1908 | // update the dentry field in child inode |
---|
1909 | child->dentry = ((cluster_id<<12) + offset)>>5; |
---|
1910 | |
---|
1911 | step--; |
---|
1912 | break; |
---|
1913 | } |
---|
1914 | case 1: // write NOMORE entry |
---|
1915 | { |
---|
1916 | step--; |
---|
1917 | entry [0] = 0x00; |
---|
1918 | break; |
---|
1919 | } |
---|
1920 | } // end switch step |
---|
1921 | offset += 32; |
---|
1922 | } // exit while => exit FSM |
---|
1923 | |
---|
1924 | #if (GIET_DEBUG_FAT & 1) |
---|
1925 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
1926 | { |
---|
1927 | _printf("\n[DEBUG FAT] _add_dir_entry() : <%s> successfully added in <%s>\n", |
---|
1928 | child->name , parent->name ); |
---|
1929 | } |
---|
1930 | #endif |
---|
1931 | |
---|
1932 | return 0; |
---|
1933 | } // end _add_dir_entry |
---|
1934 | |
---|
1935 | |
---|
1936 | |
---|
1937 | //////////////////////////////////////////////////////////// |
---|
1938 | static unsigned int _remove_dir_entry( fat_inode_t* inode ) |
---|
1939 | { |
---|
1940 | // compute number of LFN entries |
---|
1941 | unsigned int length; |
---|
1942 | unsigned int nb_lfn; |
---|
1943 | if ( _check_name_length( inode->name, |
---|
1944 | &length, |
---|
1945 | &nb_lfn ) ) return 1; |
---|
1946 | |
---|
1947 | // get cluster_id and offset in parent directory cache |
---|
1948 | unsigned int dentry = inode->dentry; |
---|
1949 | unsigned int cluster_id = dentry >> 7; |
---|
1950 | unsigned int offset = (dentry & 0x7F)<<5; |
---|
1951 | |
---|
1952 | // get buffer from parent directory cache |
---|
1953 | unsigned char* buffer; |
---|
1954 | fat_cache_desc_t* pdesc; |
---|
1955 | |
---|
1956 | if ( _get_buffer_from_cache( inode->parent, |
---|
1957 | cluster_id, |
---|
1958 | &pdesc ) ) return 1; |
---|
1959 | buffer = pdesc->buffer; |
---|
1960 | pdesc->dirty = 1; |
---|
1961 | |
---|
1962 | // invalidate NORMAL entry in directory cache |
---|
1963 | buffer[offset] = 0xE5; |
---|
1964 | |
---|
1965 | // invalidate LFN entries |
---|
1966 | while ( nb_lfn ) |
---|
1967 | { |
---|
1968 | if (offset == 0) // we must load buffer for (cluster_id - 1) |
---|
1969 | { |
---|
1970 | if ( _get_buffer_from_cache( inode->parent, |
---|
1971 | cluster_id - 1, |
---|
1972 | &pdesc ) ) return 1; |
---|
1973 | buffer = pdesc->buffer; |
---|
1974 | pdesc->dirty = 1; |
---|
1975 | offset = 4096; |
---|
1976 | } |
---|
1977 | |
---|
1978 | offset = offset - 32; |
---|
1979 | |
---|
1980 | // check for LFN entry |
---|
1981 | if ( _read_entry( DIR_ATTR , buffer + offset , 0 ) != ATTR_LONG_NAME_MASK ) |
---|
1982 | break; |
---|
1983 | |
---|
1984 | // invalidate LFN entry |
---|
1985 | buffer[offset] = 0xE5; |
---|
1986 | |
---|
1987 | nb_lfn--; |
---|
1988 | } |
---|
1989 | |
---|
1990 | return 0; |
---|
1991 | } // end _remove_dir_entry |
---|
1992 | |
---|
1993 | |
---|
1994 | |
---|
1995 | |
---|
1996 | //////////////////////////////////////////////////////////// |
---|
1997 | static unsigned int _update_dir_entry( fat_inode_t* inode ) |
---|
1998 | { |
---|
1999 | // get Cache-File buffer containing the parent directory entry |
---|
2000 | // 128 directories entries in one 4 Kbytes buffer |
---|
2001 | fat_cache_desc_t* pdesc; |
---|
2002 | unsigned char* buffer; |
---|
2003 | unsigned int cluster_id = inode->dentry>>7; |
---|
2004 | unsigned int offset = (inode->dentry & 0x3F)<<5; |
---|
2005 | |
---|
2006 | if ( _get_buffer_from_cache( inode->parent, |
---|
2007 | cluster_id, |
---|
2008 | &pdesc ) ) return 1; |
---|
2009 | buffer = pdesc->buffer; |
---|
2010 | pdesc->dirty = 1; |
---|
2011 | |
---|
2012 | // update size field |
---|
2013 | buffer[offset + 28] = inode->size>>0; // size.B0 |
---|
2014 | buffer[offset + 29] = inode->size>>8; // size.B1 |
---|
2015 | buffer[offset + 30] = inode->size>>16; // size.B2 |
---|
2016 | buffer[offset + 31] = inode->size>>24; // size.B3 |
---|
2017 | |
---|
2018 | // update cluster field |
---|
2019 | buffer[offset + 26] = inode->cluster>>0; // cluster.B0 |
---|
2020 | buffer[offset + 27] = inode->cluster>>8; // cluster.B1 |
---|
2021 | buffer[offset + 20] = inode->cluster>>16; // cluster.B2 |
---|
2022 | buffer[offset + 21] = inode->cluster>>24; // cluster.B3 |
---|
2023 | |
---|
2024 | return 0; |
---|
2025 | } // end _update_dir_entry() |
---|
2026 | |
---|
2027 | |
---|
2028 | |
---|
2029 | |
---|
2030 | ////////////////////////////////////////////////////////////////// |
---|
2031 | static unsigned int _get_child_from_parent( fat_inode_t* parent, |
---|
2032 | char* name, |
---|
2033 | fat_inode_t** inode ) |
---|
2034 | { |
---|
2035 | fat_inode_t* current; |
---|
2036 | |
---|
2037 | #if (GIET_DEBUG_FAT & 1) |
---|
2038 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
2039 | _printf("\n[DEBUG FAT] _get_child_from_parent() : search <%s> in directory <%s>\n", |
---|
2040 | name , parent->name ); |
---|
2041 | #endif |
---|
2042 | |
---|
2043 | // scan inodes in the parent directory |
---|
2044 | for ( current = parent->child ; current ; current = current->next ) |
---|
2045 | { |
---|
2046 | if ( _strcmp( name , current->name ) == 0 ) |
---|
2047 | { |
---|
2048 | |
---|
2049 | #if (GIET_DEBUG_FAT & 1) |
---|
2050 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
2051 | _printf("\n[DEBUG FAT] _get_child_from_parent() : found inode <%s> in directory <%s>\n", |
---|
2052 | name , parent->name ); |
---|
2053 | #endif |
---|
2054 | *inode = current; |
---|
2055 | return 0; // name found |
---|
2056 | } |
---|
2057 | } |
---|
2058 | |
---|
2059 | // not found in Inode-Tree => access the parent directory |
---|
2060 | // file_cache. Two embedded loops: |
---|
2061 | // - scan the clusters allocated to this directory |
---|
2062 | // - scan the directory entries in each 4 Kbytes buffer |
---|
2063 | |
---|
2064 | unsigned char* buffer; // pointer on one cache buffer |
---|
2065 | char cname[32]; // buffer for one full entry name |
---|
2066 | char lfn1[16]; // buffer for one partial name |
---|
2067 | char lfn2[16]; // buffer for one partial name |
---|
2068 | char lfn3[16]; // buffer for one partial name |
---|
2069 | unsigned int size; // searched file/dir size (bytes) |
---|
2070 | unsigned int cluster; // searched file/dir cluster index |
---|
2071 | unsigned int is_dir; // searched file/dir type |
---|
2072 | unsigned int attr; // directory entry ATTR field |
---|
2073 | unsigned int ord; // directory entry ORD field |
---|
2074 | unsigned int lfn = 0; // LFN entries number |
---|
2075 | unsigned int dentry; // directory entry index |
---|
2076 | unsigned int offset = 0; // byte offset in buffer |
---|
2077 | unsigned int cluster_id = 0; // cluster index in directory |
---|
2078 | int found = 0; // not found (0) / name found (1) / end of dir (-1) |
---|
2079 | |
---|
2080 | #if (GIET_DEBUG_FAT & 1) |
---|
2081 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
2082 | _printf("\n[DEBUG FAT] _get_child_from_parent() : does not found inode <%s>" |
---|
2083 | " in directory <%s> => search in cache\n", name , parent->name ); |
---|
2084 | #endif |
---|
2085 | |
---|
2086 | // scan the clusters allocated to parent directory |
---|
2087 | while ( found == 0 ) |
---|
2088 | { |
---|
2089 | // get one 4 Kytes buffer from parent File_Cache |
---|
2090 | fat_cache_desc_t* pdesc; |
---|
2091 | if ( _get_buffer_from_cache( parent, |
---|
2092 | cluster_id, |
---|
2093 | &pdesc ) ) return 2; |
---|
2094 | buffer = pdesc->buffer; |
---|
2095 | |
---|
2096 | // scan this buffer until end of directory, end of buffer, or name found |
---|
2097 | while( (offset < 4096) && (found == 0) ) |
---|
2098 | { |
---|
2099 | |
---|
2100 | #if (GIET_DEBUG_FAT & 1) |
---|
2101 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
2102 | _printf("\n[DEBUG FAT] _get_child_from_parent() : scan buffer %d for <%s>\n", |
---|
2103 | cluster_id , name ); |
---|
2104 | #endif |
---|
2105 | attr = _read_entry( DIR_ATTR , buffer + offset , 0 ); |
---|
2106 | ord = _read_entry( LDIR_ORD , buffer + offset , 0 ); |
---|
2107 | |
---|
2108 | if (ord == NO_MORE_ENTRY) // no more entry in directory => break |
---|
2109 | { |
---|
2110 | found = -1; |
---|
2111 | } |
---|
2112 | else if ( ord == FREE_ENTRY ) // free entry => skip |
---|
2113 | { |
---|
2114 | offset = offset + 32; |
---|
2115 | } |
---|
2116 | else if ( attr == ATTR_LONG_NAME_MASK ) // LFN entry => get partial name |
---|
2117 | { |
---|
2118 | unsigned int seq = ord & 0x3; |
---|
2119 | lfn = (seq > lfn) ? seq : lfn; |
---|
2120 | if ( seq == 1 ) _get_name_from_long( buffer + offset, lfn1 ); |
---|
2121 | else if ( seq == 2 ) _get_name_from_long( buffer + offset, lfn2 ); |
---|
2122 | else if ( seq == 3 ) _get_name_from_long( buffer + offset, lfn3 ); |
---|
2123 | offset = offset + 32; |
---|
2124 | } |
---|
2125 | else // NORMAL entry |
---|
2126 | { |
---|
2127 | // build the extracted name |
---|
2128 | if ( lfn == 0 ) |
---|
2129 | { |
---|
2130 | _get_name_from_short( buffer + offset , cname ); |
---|
2131 | } |
---|
2132 | else if ( lfn == 1 ) |
---|
2133 | { |
---|
2134 | _strcpy( cname , lfn1 ); |
---|
2135 | } |
---|
2136 | else if ( lfn == 2 ) |
---|
2137 | { |
---|
2138 | _strcpy( cname , lfn1 ); |
---|
2139 | _strcpy( cname + 13 , lfn2 ); |
---|
2140 | } |
---|
2141 | else if ( lfn == 3 ) |
---|
2142 | { |
---|
2143 | _strcpy( cname , lfn1 ); |
---|
2144 | _strcpy( cname + 13 , lfn2 ); |
---|
2145 | _strcpy( cname + 26 , lfn3 ); |
---|
2146 | } |
---|
2147 | |
---|
2148 | // test if extracted name == searched name |
---|
2149 | if ( _strcmp( name , cname ) == 0 ) |
---|
2150 | { |
---|
2151 | cluster = (_read_entry( DIR_FST_CLUS_HI , buffer + offset , 1 ) << 16) | |
---|
2152 | (_read_entry( DIR_FST_CLUS_LO , buffer + offset , 1 ) ) ; |
---|
2153 | dentry = ((cluster_id<<12) + offset)>>5; |
---|
2154 | is_dir = ((attr & ATTR_DIRECTORY) == ATTR_DIRECTORY); |
---|
2155 | size = _read_entry( DIR_FILE_SIZE , buffer + offset , 1 ); |
---|
2156 | found = 1; |
---|
2157 | } |
---|
2158 | offset = offset + 32; |
---|
2159 | lfn = 0; |
---|
2160 | } |
---|
2161 | } // end loop on directory entries |
---|
2162 | cluster_id++; |
---|
2163 | offset = 0; |
---|
2164 | } // end loop on buffers |
---|
2165 | |
---|
2166 | if ( found == -1 ) // found end of directory in parent directory |
---|
2167 | { |
---|
2168 | |
---|
2169 | #if (GIET_DEBUG_FAT & 1) |
---|
2170 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
2171 | _printf("\n[DEBUG FAT] _get_child_from_parent() : found end of directory in <%s>\n", |
---|
2172 | parent->name ); |
---|
2173 | #endif |
---|
2174 | *inode = NULL; |
---|
2175 | return 1; |
---|
2176 | } |
---|
2177 | else // found searched name in parent directory |
---|
2178 | { |
---|
2179 | // allocate a new inode and an empty Cache-File |
---|
2180 | *inode = _allocate_one_inode( name, |
---|
2181 | is_dir, |
---|
2182 | cluster, |
---|
2183 | size, |
---|
2184 | 0, // count |
---|
2185 | dentry, |
---|
2186 | 1 ); // cache_allocate |
---|
2187 | |
---|
2188 | // introduce it in Inode-Tree |
---|
2189 | _add_inode_in_tree( *inode , parent ); |
---|
2190 | |
---|
2191 | #if (GIET_DEBUG_FAT & 1) |
---|
2192 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
2193 | _printf("\n[DEBUG FAT] _get_child_from_parent() : found <%s> on device\n", name ); |
---|
2194 | #endif |
---|
2195 | return 0; |
---|
2196 | } |
---|
2197 | } // end _get_child_from_parent() |
---|
2198 | |
---|
2199 | |
---|
2200 | |
---|
2201 | |
---|
2202 | ////////////////////////////////////////////////////////////////// |
---|
2203 | static unsigned int _get_inode_from_path( char* pathname, |
---|
2204 | fat_inode_t** inode ) |
---|
2205 | { |
---|
2206 | char name[32]; // buffer for one name in pathname |
---|
2207 | unsigned int nb_read; // number of characters written in name[] |
---|
2208 | fat_inode_t* parent; // parent inode |
---|
2209 | fat_inode_t* child; // child inode |
---|
2210 | unsigned int last; // while exit condition |
---|
2211 | unsigned int code; // return value |
---|
2212 | |
---|
2213 | #if (GIET_DEBUG_FAT & 1) |
---|
2214 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
2215 | _printf("\n[DEBUG FAT] _get_inode_from_path() : enters for path <%s>\n", pathname ); |
---|
2216 | #endif |
---|
2217 | |
---|
2218 | // handle root directory case |
---|
2219 | if ( _strcmp( pathname , "/" ) == 0 ) |
---|
2220 | { |
---|
2221 | |
---|
2222 | #if (GIET_DEBUG_FAT & 1) |
---|
2223 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
2224 | _printf("\n[DEBUG FAT] _get_inode_from_path() : found root inode for <%s>\n", |
---|
2225 | pathname ); |
---|
2226 | #endif |
---|
2227 | *inode = _fat.inode_tree_root; |
---|
2228 | return 0; |
---|
2229 | } |
---|
2230 | |
---|
2231 | // If the pathname is not "/", we traverse the inode tree from the root. |
---|
2232 | // We use _get_name_from_path() to scan pathname and extract inode names. |
---|
2233 | // We use _get_child_from_parent() to scan each directory in the path. |
---|
2234 | |
---|
2235 | last = 0; |
---|
2236 | nb_read = 0; // number of characters analysed in path |
---|
2237 | parent = _fat.inode_tree_root; // Inode-Tree root |
---|
2238 | |
---|
2239 | while ( !last ) |
---|
2240 | { |
---|
2241 | // get searched file/dir name |
---|
2242 | if ( _get_name_from_path( pathname, name, &nb_read ) ) |
---|
2243 | { |
---|
2244 | return 3; // error : name too long |
---|
2245 | } |
---|
2246 | |
---|
2247 | // compute last iteration condition |
---|
2248 | last = (pathname[nb_read] == 0); |
---|
2249 | |
---|
2250 | #if (GIET_DEBUG_FAT & 1) |
---|
2251 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
2252 | _printf("\n[DEBUG FAT] _get_inode_from_path() : got name <%s>\n", name ); |
---|
2253 | #endif |
---|
2254 | |
---|
2255 | if ( _strcmp( name, ".." ) == 0) |
---|
2256 | { |
---|
2257 | // found special name "..", try to go up |
---|
2258 | code = 0; |
---|
2259 | if ( parent->parent ) |
---|
2260 | child = parent->parent; |
---|
2261 | else |
---|
2262 | child = parent; |
---|
2263 | } |
---|
2264 | else if ( _strcmp( name, "." ) == 0 ) |
---|
2265 | { |
---|
2266 | // found special name ".", stay on the same level |
---|
2267 | code = 0; |
---|
2268 | child = parent; |
---|
2269 | } |
---|
2270 | else |
---|
2271 | { |
---|
2272 | // get child inode from parent directory |
---|
2273 | code = _get_child_from_parent( parent, |
---|
2274 | name, |
---|
2275 | &child ); |
---|
2276 | |
---|
2277 | // we need to find the child inode for all non terminal names |
---|
2278 | if ( (code == 2) || ((code == 1 ) && !last) ) |
---|
2279 | { |
---|
2280 | |
---|
2281 | #if (GIET_DEBUG_FAT & 1) |
---|
2282 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
2283 | _printf("\n[DEBUG FAT] _get_inode_from_path() : neither parent, nor child found for <%s>\n", |
---|
2284 | pathname ); |
---|
2285 | #endif |
---|
2286 | return 2; // error : parent inode not found |
---|
2287 | } |
---|
2288 | } |
---|
2289 | |
---|
2290 | // update parent if not the last iteration |
---|
2291 | if ( !last ) |
---|
2292 | parent = child; |
---|
2293 | } // end while |
---|
2294 | |
---|
2295 | // returns inode pointer |
---|
2296 | if (code == 0 ) |
---|
2297 | { |
---|
2298 | |
---|
2299 | #if (GIET_DEBUG_FAT & 1) |
---|
2300 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
2301 | _printf("\n[DEBUG FAT] _get_inode_from_path() : found inode for <%s>\n", |
---|
2302 | pathname ); |
---|
2303 | #endif |
---|
2304 | *inode = child; |
---|
2305 | } |
---|
2306 | else |
---|
2307 | { |
---|
2308 | |
---|
2309 | #if (GIET_DEBUG_FAT & 1) |
---|
2310 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
2311 | _printf("\n[DEBUG FAT] _get_inode_from_path() : found only parent inode for <%s>\n", |
---|
2312 | pathname ); |
---|
2313 | #endif |
---|
2314 | *inode = parent; |
---|
2315 | } |
---|
2316 | |
---|
2317 | return code; // can be 0 (found) or 1 (not found) |
---|
2318 | |
---|
2319 | } // end _get_inode_from_path() |
---|
2320 | |
---|
2321 | |
---|
2322 | |
---|
2323 | |
---|
2324 | ////////////////////////////////////////////////////////////// |
---|
2325 | static unsigned int _remove_node_from_fs( fat_inode_t* inode ) |
---|
2326 | { |
---|
2327 | // remove entry in parent directory |
---|
2328 | if ( _remove_dir_entry( inode ) ) return 1; |
---|
2329 | |
---|
2330 | // update parent directory on device |
---|
2331 | if ( _update_device_from_cache( inode->parent->levels, |
---|
2332 | inode->parent->cache, |
---|
2333 | inode->parent->name ) ) return 1; |
---|
2334 | |
---|
2335 | // release clusters allocated to file/dir in DATA region |
---|
2336 | if ( _clusters_release( inode->cluster ) ) return 1; |
---|
2337 | |
---|
2338 | // release File-Cache |
---|
2339 | if ( _release_cache_memory( inode->cache, |
---|
2340 | inode->levels ) ) return 1; |
---|
2341 | |
---|
2342 | // remove inode from Inode-Tree |
---|
2343 | _remove_inode_from_tree( inode ); |
---|
2344 | |
---|
2345 | // release inode |
---|
2346 | _free ( inode ); |
---|
2347 | |
---|
2348 | return 0; |
---|
2349 | } // end _remove_node_from_fs() |
---|
2350 | |
---|
2351 | |
---|
2352 | ////////////////////////////////////////////////////////////////// |
---|
2353 | static unsigned int _next_cluster_no_cache( unsigned int cluster, |
---|
2354 | unsigned int* next ) |
---|
2355 | { |
---|
2356 | // compute cluster_id and slot_id |
---|
2357 | // each cluster contains 1024 slots (4 bytes per slot) |
---|
2358 | unsigned int cluster_id = cluster >> 10; |
---|
2359 | unsigned int slot_id = cluster & 0x3FF; |
---|
2360 | |
---|
2361 | // compute lba of cluster identified by cluster_id |
---|
2362 | unsigned int lba = _fat.fat_lba + (cluster_id << 3); |
---|
2363 | |
---|
2364 | // get cluster containing the adressed FAT slot in FAT buffer |
---|
2365 | if ( _fat_buffer_fat_lba != lba ) |
---|
2366 | { |
---|
2367 | if ( _fat_ioc_access( 0, // no descheduling |
---|
2368 | 1, // read |
---|
2369 | lba, |
---|
2370 | (unsigned int)_fat_buffer_fat, |
---|
2371 | 8 ) ) |
---|
2372 | { |
---|
2373 | _printf("\n[FAT ERROR] in _next_cluster_no_cache() : " |
---|
2374 | "cannot load lba = %x into fat_buffer\n", lba ); |
---|
2375 | return 1; |
---|
2376 | } |
---|
2377 | |
---|
2378 | _fat_buffer_fat_lba = lba; |
---|
2379 | } |
---|
2380 | |
---|
2381 | // return next cluster index |
---|
2382 | unsigned int* buf = (unsigned int*)_fat_buffer_fat; |
---|
2383 | *next = buf[slot_id]; |
---|
2384 | return 0; |
---|
2385 | |
---|
2386 | } // end _next_cluster_no_cache() |
---|
2387 | |
---|
2388 | |
---|
2389 | |
---|
2390 | |
---|
2391 | ///////////////////////////////////////////////////////////////// |
---|
2392 | static unsigned int _file_info_no_cache( char* pathname, |
---|
2393 | unsigned int* file_cluster, |
---|
2394 | unsigned int* file_size ) |
---|
2395 | { |
---|
2396 | |
---|
2397 | #if (GIET_DEBUG_FAT & 1) |
---|
2398 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
2399 | _printf("\n[DEBUG FAT] _file_info_no_cache() : enters for path <%s>\n", pathname ); |
---|
2400 | #endif |
---|
2401 | |
---|
2402 | char name[32]; // buffer for one name in the analysed pathname |
---|
2403 | char lfn1[16]; // buffer for a partial name in LFN entry |
---|
2404 | char lfn2[16]; // buffer for a partial name in LFN entry |
---|
2405 | char lfn3[16]; // buffer for a partial name in LFN entry |
---|
2406 | char cname[32]; // buffer for a full name in a directory entry |
---|
2407 | unsigned int nb_read; // number of characters analysed in path |
---|
2408 | unsigned int parent_cluster; // cluster index for the parent directory |
---|
2409 | unsigned int child_cluster = 0; // cluster index for the searched file/dir |
---|
2410 | unsigned int child_size = 0; // size of the searched file/dir |
---|
2411 | unsigned int child_is_dir; // type of the searched file/dir |
---|
2412 | unsigned int offset; // offset in a 4 Kbytes buffer |
---|
2413 | unsigned int ord; // ORD field in a directory entry |
---|
2414 | unsigned int attr; // ATTR field in a directory entry |
---|
2415 | unsigned int lfn = 0; // number of lfn entries |
---|
2416 | unsigned char* buf; // pointer on a 4 Kbytes buffer |
---|
2417 | unsigned int found; // name found in current directory entry |
---|
2418 | |
---|
2419 | // Three embedded loops: |
---|
2420 | // - scan pathname to extract file/dir names, |
---|
2421 | // - for each name, scan the clusters of the parent directory |
---|
2422 | // - for each cluster, scan the 4 Kbytes buffer to find the file/dir name |
---|
2423 | // The starting point is the root directory (cluster 2) |
---|
2424 | |
---|
2425 | nb_read = 0; |
---|
2426 | parent_cluster = 2; |
---|
2427 | |
---|
2428 | // scan pathname |
---|
2429 | while ( pathname[nb_read] != 0 ) |
---|
2430 | { |
---|
2431 | // get searched file/dir name |
---|
2432 | if ( _get_name_from_path( pathname, name, &nb_read ) ) return 1; |
---|
2433 | |
---|
2434 | #if (GIET_DEBUG_FAT & 1) |
---|
2435 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
2436 | _printf("\n[DEBUG FAT] _file_info_no_cache() : search name <%s>" |
---|
2437 | " in cluster %x\n", name , parent_cluster ); |
---|
2438 | #endif |
---|
2439 | found = 0; |
---|
2440 | |
---|
2441 | // scan clusters containing the parent directory |
---|
2442 | while ( found == 0 ) |
---|
2443 | { |
---|
2444 | // compute lba |
---|
2445 | unsigned int lba = _cluster_to_lba( parent_cluster ); |
---|
2446 | |
---|
2447 | // load one cluster of the parent directory into data_buffer |
---|
2448 | if ( _fat_buffer_data_lba != lba ) |
---|
2449 | { |
---|
2450 | if ( _fat_ioc_access( 0, // no descheduling |
---|
2451 | 1, // read |
---|
2452 | lba, |
---|
2453 | (unsigned int)_fat_buffer_data, |
---|
2454 | 8 ) ) |
---|
2455 | { |
---|
2456 | _printf("\n[FAT ERROR] in _file_info_no_cache() : " |
---|
2457 | "cannot load lba = %x into data_buffer\n", lba ); |
---|
2458 | return 1; |
---|
2459 | } |
---|
2460 | |
---|
2461 | _fat_buffer_data_lba = lba; |
---|
2462 | } |
---|
2463 | |
---|
2464 | offset = 0; |
---|
2465 | |
---|
2466 | // scan this 4 Kbytes buffer |
---|
2467 | while ( (offset < 4096) && (found == 0) ) |
---|
2468 | { |
---|
2469 | buf = _fat_buffer_data + offset; |
---|
2470 | attr = _read_entry( DIR_ATTR , buf , 0 ); |
---|
2471 | ord = _read_entry( LDIR_ORD , buf , 0 ); |
---|
2472 | |
---|
2473 | if (ord == NO_MORE_ENTRY) // no more entry => break |
---|
2474 | { |
---|
2475 | found = 2; |
---|
2476 | } |
---|
2477 | else if ( ord == FREE_ENTRY ) // free entry => skip |
---|
2478 | { |
---|
2479 | offset = offset + 32; |
---|
2480 | } |
---|
2481 | else if ( attr == ATTR_LONG_NAME_MASK ) // LFN entry => get partial name |
---|
2482 | { |
---|
2483 | unsigned int seq = ord & 0x3; |
---|
2484 | lfn = (seq > lfn) ? seq : lfn; |
---|
2485 | if ( seq == 1 ) _get_name_from_long( buf, lfn1 ); |
---|
2486 | else if ( seq == 2 ) _get_name_from_long( buf, lfn2 ); |
---|
2487 | else if ( seq == 3 ) _get_name_from_long( buf, lfn3 ); |
---|
2488 | offset = offset + 32; |
---|
2489 | } |
---|
2490 | else // NORMAL entry |
---|
2491 | { |
---|
2492 | // build the full mame for current directory entry |
---|
2493 | if ( lfn == 0 ) |
---|
2494 | { |
---|
2495 | _get_name_from_short( buf , cname ); |
---|
2496 | } |
---|
2497 | else if ( lfn == 1 ) |
---|
2498 | { |
---|
2499 | _strcpy( cname , lfn1 ); |
---|
2500 | } |
---|
2501 | else if ( lfn == 2 ) |
---|
2502 | { |
---|
2503 | _strcpy( cname , lfn1 ); |
---|
2504 | _strcpy( cname + 13 , lfn2 ); |
---|
2505 | } |
---|
2506 | else if ( lfn == 3 ) |
---|
2507 | { |
---|
2508 | _strcpy( cname , lfn1 ); |
---|
2509 | _strcpy( cname + 13 , lfn2 ); |
---|
2510 | _strcpy( cname + 26 , lfn3 ); |
---|
2511 | } |
---|
2512 | |
---|
2513 | // test if extracted name == searched name |
---|
2514 | if ( _strcmp( name , cname ) == 0 ) |
---|
2515 | { |
---|
2516 | child_cluster = (_read_entry( DIR_FST_CLUS_HI , buf , 1 ) << 16) | |
---|
2517 | (_read_entry( DIR_FST_CLUS_LO , buf , 1 ) ) ; |
---|
2518 | child_is_dir = ((attr & ATTR_DIRECTORY) == ATTR_DIRECTORY); |
---|
2519 | child_size = _read_entry( DIR_FILE_SIZE , buf , 1 ); |
---|
2520 | found = 1; |
---|
2521 | } |
---|
2522 | offset = offset + 32; |
---|
2523 | lfn = 0; |
---|
2524 | } |
---|
2525 | } // en loop on directory entries |
---|
2526 | |
---|
2527 | // compute next cluster index |
---|
2528 | unsigned int next; |
---|
2529 | if ( _next_cluster_no_cache ( parent_cluster , &next ) ) return 1; |
---|
2530 | parent_cluster = next; |
---|
2531 | } // end loop on clusters |
---|
2532 | |
---|
2533 | if ( found == 2 ) // found end of directory => error |
---|
2534 | { |
---|
2535 | _printf("\n[FAT ERROR] in _file_info_no_cache() : <%s> not found\n", |
---|
2536 | name ); |
---|
2537 | return 1; |
---|
2538 | } |
---|
2539 | |
---|
2540 | // check type |
---|
2541 | if ( ((pathname[nb_read] == 0) && (child_is_dir != 0)) || |
---|
2542 | ((pathname[nb_read] != 0) && (child_is_dir == 0)) ) |
---|
2543 | { |
---|
2544 | _printf("\n[FAT ERROR] in _file_info_no_cache() : illegal type for <%s>\n", name ); |
---|
2545 | return 1; |
---|
2546 | } |
---|
2547 | |
---|
2548 | // update parent_cluster for next name |
---|
2549 | parent_cluster = child_cluster; |
---|
2550 | |
---|
2551 | } // end loop on names |
---|
2552 | |
---|
2553 | #if (GIET_DEBUG_FAT & 1) |
---|
2554 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
2555 | _printf("\n[DEBUG FAT] _file_info_no_cache() : success for <%s> / " |
---|
2556 | "file_size = %x / file_cluster = %x\n", pathname, child_size, child_cluster ); |
---|
2557 | #endif |
---|
2558 | |
---|
2559 | // return file cluster and size |
---|
2560 | *file_size = child_size; |
---|
2561 | *file_cluster = child_cluster; |
---|
2562 | return 0; |
---|
2563 | |
---|
2564 | } // end _file_info_no_cache() |
---|
2565 | |
---|
2566 | |
---|
2567 | |
---|
2568 | ///////////////////////////////////////////////////////////////////////////// |
---|
2569 | ///////////////////////////////////////////////////////////////////////////// |
---|
2570 | // Extern functions |
---|
2571 | ///////////////////////////////////////////////////////////////////////////// |
---|
2572 | ///////////////////////////////////////////////////////////////////////////// |
---|
2573 | |
---|
2574 | |
---|
2575 | ///////////////////////////////////////////////////////////////////////////// |
---|
2576 | // This function initializes the FAT structures. |
---|
2577 | // - The Fat-Descriptor is always initialised. |
---|
2578 | // - The dynamically allocated structures (the Inode-Tre, the Fat_Cache, |
---|
2579 | // and the File-Cache for the root directory) are only allocated |
---|
2580 | // and initialised if the "kernel_mode" argument is set. |
---|
2581 | ///////////////////////////////////////////////////////////////////////////// |
---|
2582 | // Implementation note: |
---|
2583 | // This function is called twice, by the boot-loader, and by the kernel_init. |
---|
2584 | // It does not use dynamic memory allocation from the distributed heap. |
---|
2585 | // It use informations found in the boot sector and FS-INFO sector. |
---|
2586 | // that are loaded in the Fat-Descriptor temporary block_buffer. |
---|
2587 | ///////////////////////////////////////////////////////////////////////////// |
---|
2588 | // Returns 0 if success. |
---|
2589 | // Returns -1 if error. |
---|
2590 | ///////////////////////////////////////////////////////////////////////////// |
---|
2591 | int _fat_init( unsigned int kernel_mode ) |
---|
2592 | { |
---|
2593 | |
---|
2594 | #if GIET_DEBUG_FAT |
---|
2595 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
2596 | _printf("\n[DEBUG FAT] _fat_init() : enters at cycle %d\n", _get_proctime() ); |
---|
2597 | #endif |
---|
2598 | |
---|
2599 | // FAT initialisation should be done only once |
---|
2600 | if ( _fat.initialised == FAT_INITIALISED ) |
---|
2601 | { |
---|
2602 | _printf("\n[FAT ERROR] in _fat_init() : FAT already initialised\n"); |
---|
2603 | return -1; |
---|
2604 | } |
---|
2605 | |
---|
2606 | // load Boot sector (VBR) into FAT buffer |
---|
2607 | if ( _fat_ioc_access( 0, // no descheduling |
---|
2608 | 1, // read |
---|
2609 | 0, // block index |
---|
2610 | (unsigned int)_fat.block_buffer, |
---|
2611 | 1 ) ) // one block |
---|
2612 | { |
---|
2613 | _printf("\n[FAT ERROR] in _fat_init() cannot load VBR\n"); |
---|
2614 | return -1; |
---|
2615 | } |
---|
2616 | |
---|
2617 | _fat.block_buffer_lba = 0; |
---|
2618 | |
---|
2619 | #if GIET_DEBUG_FAT |
---|
2620 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
2621 | { |
---|
2622 | _printf("\n[DEBUG FAT] _fat_init() : Boot sector loaded\n"); |
---|
2623 | _display_one_block( _fat.block_buffer, "block device", _fat.block_buffer_lba ); |
---|
2624 | } |
---|
2625 | #endif |
---|
2626 | |
---|
2627 | // checking various FAT32 assuptions from boot sector |
---|
2628 | if( _read_entry( BPB_BYTSPERSEC, _fat.block_buffer, 1 ) != 512 ) |
---|
2629 | { |
---|
2630 | _printf("\n[FAT ERROR] The sector size must be 512 bytes\n"); |
---|
2631 | return -1; |
---|
2632 | } |
---|
2633 | if( _read_entry( BPB_SECPERCLUS, _fat.block_buffer, 1 ) != 8 ) |
---|
2634 | { |
---|
2635 | _printf("\n[FAT ERROR] The cluster size must be 8 blocks\n"); |
---|
2636 | return -1; |
---|
2637 | } |
---|
2638 | if( _read_entry( BPB_NUMFATS, _fat.block_buffer, 1 ) != 1 ) |
---|
2639 | { |
---|
2640 | _printf("\n[FAT ERROR] The number of FAT copies in FAT region must be 1\n"); |
---|
2641 | return -1; |
---|
2642 | } |
---|
2643 | if( (_read_entry( BPB_FAT32_FATSZ32, _fat.block_buffer, 1 ) & 0xF) != 0 ) |
---|
2644 | { |
---|
2645 | _printf("\n[FAT ERROR] The FAT region must be multiple of 16 sectors\n"); |
---|
2646 | return -1; |
---|
2647 | } |
---|
2648 | if( _read_entry( BPB_FAT32_ROOTCLUS, _fat.block_buffer, 1 ) != 2 ) |
---|
2649 | { |
---|
2650 | _printf("\n[FAT ERROR] The root directory must be at cluster 2\n"); |
---|
2651 | return -1; |
---|
2652 | } |
---|
2653 | |
---|
2654 | // initialise Fat-Descriptor from VBR |
---|
2655 | _fat.sector_size = 512; |
---|
2656 | _fat.cluster_size = 4096; |
---|
2657 | _fat.fat_sectors = _read_entry( BPB_FAT32_FATSZ32 , _fat.block_buffer , 1 ); |
---|
2658 | _fat.fat_lba = _read_entry( BPB_RSVDSECCNT , _fat.block_buffer , 1 ); |
---|
2659 | _fat.data_sectors = _fat.fat_sectors << 10; |
---|
2660 | _fat.data_lba = _fat.fat_lba + _fat.fat_sectors; |
---|
2661 | _fat.fs_info_lba = _read_entry( BPB_FAT32_FSINFO , _fat.block_buffer , 1 ); |
---|
2662 | _fat_buffer_fat_lba = 0xFFFFFFFF; |
---|
2663 | _fat_buffer_data_lba = 0xFFFFFFFF; |
---|
2664 | _fat.initialised = FAT_INITIALISED; |
---|
2665 | |
---|
2666 | // load FS_INFO sector into FAT buffer |
---|
2667 | if ( _fat_ioc_access( 0, // no descheduling |
---|
2668 | 1, // read |
---|
2669 | _fat.fs_info_lba, // lba |
---|
2670 | (unsigned int)_fat.block_buffer, |
---|
2671 | 1 ) ) // one block |
---|
2672 | { |
---|
2673 | _printf("\n[FAT ERROR] in _fat_init() cannot load FS_INFO Sector\n"); |
---|
2674 | return -1; |
---|
2675 | } |
---|
2676 | |
---|
2677 | _fat.block_buffer_lba = _fat.fs_info_lba; |
---|
2678 | |
---|
2679 | #if GIET_DEBUG_FAT |
---|
2680 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
2681 | { |
---|
2682 | _printf("\n[DEBUG FAT] _fat_init() : FS-INFO sector loaded\n"); |
---|
2683 | _display_one_block( _fat.block_buffer, "block device", _fat.block_buffer_lba ); |
---|
2684 | } |
---|
2685 | #endif |
---|
2686 | |
---|
2687 | // initialise Fat-Descriptor from FS_INFO |
---|
2688 | _fat.free_clusters_number = _read_entry( FS_FREE_CLUSTERS , _fat.block_buffer, 1); |
---|
2689 | _fat.first_free_cluster = _read_entry( FS_FREE_CLUSTER_HINT, _fat.block_buffer, 1); |
---|
2690 | |
---|
2691 | // This is done only when the _fat_init() is called in kernel mode |
---|
2692 | |
---|
2693 | if ( kernel_mode ) |
---|
2694 | { |
---|
2695 | unsigned int n; |
---|
2696 | |
---|
2697 | // allocate and initialise the Inode-Tree root |
---|
2698 | fat_inode_t* inode = _malloc( sizeof(fat_inode_t) ); |
---|
2699 | fat_cache_node_t* cache = _malloc( sizeof(fat_cache_node_t) ); |
---|
2700 | |
---|
2701 | inode->parent = NULL; // no parent |
---|
2702 | inode->next = NULL; // no brother |
---|
2703 | inode->child = NULL; // children come later |
---|
2704 | inode->cache = cache; // empty cache |
---|
2705 | inode->cluster = 2; // forced value |
---|
2706 | inode->size = 0; // no size for a directory |
---|
2707 | inode->count = 0; // children come later |
---|
2708 | inode->dentry = 0; // no parent => no dentry |
---|
2709 | inode->levels = 1; // one level cache |
---|
2710 | inode->is_dir = 1; // it's a directory |
---|
2711 | _strcpy( inode->name , "/" ); |
---|
2712 | |
---|
2713 | for ( n = 0 ; n < 64 ; n ++ ) cache->children[n] = NULL; |
---|
2714 | |
---|
2715 | _fat.inode_tree_root = inode; |
---|
2716 | |
---|
2717 | // initialise the lock |
---|
2718 | _spin_lock_init( &_fat.fat_lock ); |
---|
2719 | |
---|
2720 | // initialise File Descriptor Array |
---|
2721 | for( n = 0 ; n < GIET_OPEN_FILES_MAX ; n++ ) _fat.fd[n].allocated = 0; |
---|
2722 | |
---|
2723 | // initialise fat_cache root |
---|
2724 | fat_cache_node_t* fat_cache_root = _malloc( sizeof(fat_cache_node_t) ); |
---|
2725 | _fat.fat_cache_root = fat_cache_root; |
---|
2726 | _fat.fat_cache_levels = _get_levels_from_size( _fat.fat_sectors << 9 ); |
---|
2727 | for ( n = 0 ; n < 64 ; n++ ) _fat.fat_cache_root->children[n] = NULL; |
---|
2728 | |
---|
2729 | } // end if kernel_mode |
---|
2730 | |
---|
2731 | #if GIET_DEBUG_FAT |
---|
2732 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
2733 | _display_fat_descriptor(); |
---|
2734 | #endif |
---|
2735 | |
---|
2736 | return 0; |
---|
2737 | } // end _fat_init() |
---|
2738 | |
---|
2739 | |
---|
2740 | |
---|
2741 | |
---|
2742 | /////////////////////////////////////////////////////////////////////////////// |
---|
2743 | // This function implements the giet_fat_open() system call. |
---|
2744 | // The semantic is similar to the UNIX open() function, but only the O_CREATE |
---|
2745 | // and O_RDONLY flags are supported. The UNIX access rights are not supported. |
---|
2746 | // If the file does not exist in the specified directory, it is created. |
---|
2747 | // If the specified directory does not exist, an error is returned. |
---|
2748 | // It allocates a file descriptor to the calling task, for the file identified |
---|
2749 | // by "pathname". If several tasks try to open the same file, each task |
---|
2750 | // obtains a private file descriptor. |
---|
2751 | // A node name (file or directory) cannot be larger than 31 characters. |
---|
2752 | /////////////////////////////////////////////////////////////////////////////// |
---|
2753 | // Returns file descriptor index if success |
---|
2754 | // Returns a negative value if error: |
---|
2755 | // -1 : "fat not initialised" |
---|
2756 | // -2 : "path to parent not found" |
---|
2757 | // -3 : "one name in path too long" |
---|
2758 | // -4 : "file not found" |
---|
2759 | // -5 : "Cannot update parent directory" |
---|
2760 | // -6 : "Cannot update DATA region" |
---|
2761 | // -7 : "Cannot update FAT region" |
---|
2762 | // -8 : "Cannot update FS_INFO sector" |
---|
2763 | // -9 : "file descriptor array full" |
---|
2764 | /////////////////////////////////////////////////////////////////////////////// |
---|
2765 | int _fat_open( char* pathname, // absolute path from root |
---|
2766 | unsigned int flags ) // O_CREATE and O_RDONLY |
---|
2767 | { |
---|
2768 | unsigned int fd_id; // index in File-Descriptor-Array |
---|
2769 | unsigned int code; // error code |
---|
2770 | fat_inode_t* inode; // anonymous inode pointer |
---|
2771 | fat_inode_t* child; // pointer on searched file inode |
---|
2772 | fat_inode_t* parent; // pointer on parent directory inode |
---|
2773 | |
---|
2774 | // get flags |
---|
2775 | unsigned int create = ((flags & O_CREATE) != 0); |
---|
2776 | unsigned int read_only = ((flags & O_RDONLY) != 0); |
---|
2777 | |
---|
2778 | #if GIET_DEBUG_FAT |
---|
2779 | unsigned int procid = _get_procid(); |
---|
2780 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
2781 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
---|
2782 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
2783 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
2784 | _printf("\n[DEBUG FAT] _fat_open() : P[%d,%d,%d] enters for path <%s> / " |
---|
2785 | " create = %d / read_only = %d\n", |
---|
2786 | x, y, p, pathname , create , read_only ); |
---|
2787 | #endif |
---|
2788 | |
---|
2789 | // checking FAT initialised |
---|
2790 | if( _fat.initialised != FAT_INITIALISED ) |
---|
2791 | { |
---|
2792 | _printf("\n[FAT ERROR] in _fat_open() : FAT not initialised\n"); |
---|
2793 | return -1; |
---|
2794 | } |
---|
2795 | |
---|
2796 | // takes the lock |
---|
2797 | _spin_lock_acquire( &_fat.fat_lock ); |
---|
2798 | |
---|
2799 | // get inode pointer |
---|
2800 | code = _get_inode_from_path( pathname , &inode ); |
---|
2801 | |
---|
2802 | if ( code == 2 ) |
---|
2803 | { |
---|
2804 | _spin_lock_release( &_fat.fat_lock ); |
---|
2805 | _printf("\n[FAT ERROR] in _fat_open() : path to parent not found" |
---|
2806 | " for file <%s>\n", pathname ); |
---|
2807 | return -2; |
---|
2808 | } |
---|
2809 | else if ( code == 3 ) |
---|
2810 | { |
---|
2811 | _spin_lock_release( &_fat.fat_lock ); |
---|
2812 | _printf("\n[FAT ERROR] in _fat_open() : one name in path too long" |
---|
2813 | " for file <%s>\n", pathname ); |
---|
2814 | return -3; |
---|
2815 | } |
---|
2816 | else if ( (code == 1) && (create == 0) ) |
---|
2817 | { |
---|
2818 | _spin_lock_release( &_fat.fat_lock ); |
---|
2819 | _printf("\n[FAT ERROR] in _fat_open() : file not found" |
---|
2820 | " for file <%s>\n", pathname ); |
---|
2821 | return -4; |
---|
2822 | } |
---|
2823 | else if ( (code == 1) && (create != 0) ) // file name not found => create |
---|
2824 | { |
---|
2825 | // set parent inode pointer |
---|
2826 | parent = inode; |
---|
2827 | |
---|
2828 | #if GIET_DEBUG_FAT |
---|
2829 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
2830 | _printf("\n[DEBUG FAT] _fat_open() : P[%d,%d,%d] create a new file <%s>\n", |
---|
2831 | x , y , p , pathname ); |
---|
2832 | #endif |
---|
2833 | |
---|
2834 | // get new file name / error check already done by _get_inode_from_path() |
---|
2835 | char name[32]; |
---|
2836 | _get_last_name( pathname , name ); |
---|
2837 | |
---|
2838 | // allocate a new inode and an empty Cache-File |
---|
2839 | child = _allocate_one_inode( name, |
---|
2840 | 0, // not a directory |
---|
2841 | END_OF_CHAIN_CLUSTER_MAX, // no cluster allocated |
---|
2842 | 0, // size : new file is empty |
---|
2843 | 0, // count incremented later |
---|
2844 | 0, // dentry set by add_dir_entry |
---|
2845 | 1 ); // cache_allocate |
---|
2846 | |
---|
2847 | // introduce inode into Inode-Tree |
---|
2848 | _add_inode_in_tree( child , parent ); |
---|
2849 | |
---|
2850 | // add an entry in the parent directory Cache_file |
---|
2851 | if ( _add_dir_entry( child , parent ) ) |
---|
2852 | { |
---|
2853 | _spin_lock_release( &_fat.fat_lock ); |
---|
2854 | _printf("\n[FAT ERROR] in _fat_open() : cannot update parent directory" |
---|
2855 | " for file <%s>\n" , pathname ); |
---|
2856 | return -5; |
---|
2857 | } |
---|
2858 | |
---|
2859 | // update DATA region on block device for parent directory |
---|
2860 | if ( _update_device_from_cache( parent->levels, |
---|
2861 | parent->cache, |
---|
2862 | parent->name ) ) |
---|
2863 | { |
---|
2864 | _spin_lock_release( &_fat.fat_lock ); |
---|
2865 | _printf("\n[FAT ERROR] in _fat_open() : cannot update DATA region " |
---|
2866 | " for parent of file <%s>\n", pathname ); |
---|
2867 | return -6; |
---|
2868 | } |
---|
2869 | |
---|
2870 | // update FAT region on block device |
---|
2871 | if ( _update_device_from_cache( _fat.fat_cache_levels, |
---|
2872 | _fat.fat_cache_root, |
---|
2873 | "FAT" ) ) |
---|
2874 | { |
---|
2875 | _spin_lock_release( &_fat.fat_lock ); |
---|
2876 | _printf("\n[FAT ERROR] in _fat_open() : cannot update FAT region" |
---|
2877 | " for file <%s>\n", pathname ); |
---|
2878 | return -7; |
---|
2879 | } |
---|
2880 | |
---|
2881 | // update FS_INFO sector |
---|
2882 | if ( _update_fs_info() ) |
---|
2883 | { |
---|
2884 | _spin_lock_release( &_fat.fat_lock ); |
---|
2885 | _printf("\n[FAT ERROR] in _fat_open() : cannot update FS-INFO" |
---|
2886 | " for file <%s>\n", pathname ); |
---|
2887 | return -8; |
---|
2888 | } |
---|
2889 | } |
---|
2890 | else // code == 0 |
---|
2891 | { |
---|
2892 | // set searched file inode pointer |
---|
2893 | child = inode; |
---|
2894 | |
---|
2895 | #if GIET_DEBUG_FAT |
---|
2896 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
2897 | _printf("\n[DEBUG FAT] _fat_open() : P[%d,%d,%d] found file <%s> on device : inode = %x\n", |
---|
2898 | x , y , p , pathname , child ); |
---|
2899 | #endif |
---|
2900 | |
---|
2901 | } |
---|
2902 | |
---|
2903 | // Search an empty slot in file descriptors array |
---|
2904 | fd_id = 0; |
---|
2905 | while ( (_fat.fd[fd_id].allocated) != 0 && (fd_id < GIET_OPEN_FILES_MAX) ) |
---|
2906 | { |
---|
2907 | fd_id++; |
---|
2908 | } |
---|
2909 | |
---|
2910 | // set file descriptor if an empty slot has been found |
---|
2911 | if ( fd_id < GIET_OPEN_FILES_MAX ) |
---|
2912 | { |
---|
2913 | // update file descriptor |
---|
2914 | _fat.fd[fd_id].allocated = 1; |
---|
2915 | _fat.fd[fd_id].seek = 0; |
---|
2916 | _fat.fd[fd_id].read_only = read_only; |
---|
2917 | _fat.fd[fd_id].inode = child; |
---|
2918 | |
---|
2919 | // increment the refcount |
---|
2920 | child->count = child->count + 1; |
---|
2921 | |
---|
2922 | // releases the lock |
---|
2923 | _spin_lock_release( &_fat.fat_lock ); |
---|
2924 | |
---|
2925 | #if GIET_DEBUG_FAT |
---|
2926 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
2927 | _printf("\n[DEBUG FAT] _fat_open() : P[%d,%d,%d] got fd = %d for <%s> / " |
---|
2928 | "read_only = %d\n", |
---|
2929 | x , y , p , fd_id , pathname , read_only ); |
---|
2930 | #endif |
---|
2931 | return fd_id; |
---|
2932 | } |
---|
2933 | else |
---|
2934 | { |
---|
2935 | _spin_lock_release( &_fat.fat_lock ); |
---|
2936 | _printf("\n[FAT ERROR] in _fat_open() : File-Descriptors-Array full\n"); |
---|
2937 | return -9; |
---|
2938 | } |
---|
2939 | } // end _fat_open() |
---|
2940 | |
---|
2941 | |
---|
2942 | |
---|
2943 | |
---|
2944 | ///////////////////////////////////////////////////////////////////////////////// |
---|
2945 | // This function implements the "giet_fat_close()" system call. |
---|
2946 | // It decrements the inode reference count, and release the fd_id entry |
---|
2947 | // in the file descriptors array. |
---|
2948 | // If the reference count is zero, it writes all dirty clusters on block device, |
---|
2949 | // and releases the memory allocated to the file_cache: The cache 64-Tree |
---|
2950 | // infrastructure (depending on file size) is kept, but all buffers and all |
---|
2951 | // buffer descriptors are released. |
---|
2952 | ///////////////////////////////////////////////////////////////////////////////// |
---|
2953 | // Returns 0 on success. |
---|
2954 | // Returns negative value if error: |
---|
2955 | // -1 : "FAT not initialised" |
---|
2956 | // -2 : "Illegal file descriptor" |
---|
2957 | // -3 : "Cannot update DATA region for closed file" |
---|
2958 | // -4 : "Cannot release memory" |
---|
2959 | ///////////////////////////////////////////////////////////////////////////////// |
---|
2960 | int _fat_close( unsigned int fd_id ) |
---|
2961 | { |
---|
2962 | // checking FAT initialised |
---|
2963 | if( _fat.initialised != FAT_INITIALISED ) |
---|
2964 | { |
---|
2965 | _printf("\n[FAT ERROR] in _fat_close() : FAT not initialised\n"); |
---|
2966 | return -1; |
---|
2967 | } |
---|
2968 | |
---|
2969 | if( (fd_id >= GIET_OPEN_FILES_MAX) ) |
---|
2970 | { |
---|
2971 | _printf("\n[FAT ERROR] in _fat_close() : illegal file descriptor index\n"); |
---|
2972 | return -2; |
---|
2973 | } |
---|
2974 | |
---|
2975 | // takes lock |
---|
2976 | _spin_lock_acquire( &_fat.fat_lock ); |
---|
2977 | |
---|
2978 | // get the inode pointer |
---|
2979 | fat_inode_t* inode = _fat.fd[fd_id].inode; |
---|
2980 | |
---|
2981 | // decrement reference count |
---|
2982 | inode->count = inode->count - 1; |
---|
2983 | |
---|
2984 | #if GIET_DEBUG_FAT |
---|
2985 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
2986 | _printf("\n[FAT DEBUG] _fat_close() for file <%s> : refcount = %d\n", |
---|
2987 | inode->name , inode->count ); |
---|
2988 | #endif |
---|
2989 | |
---|
2990 | // update block device and release File-Cache if no more references |
---|
2991 | if ( inode->count == 0 ) |
---|
2992 | { |
---|
2993 | // update all dirty clusters for closed file |
---|
2994 | if ( _update_device_from_cache( inode->levels, |
---|
2995 | inode->cache, |
---|
2996 | inode->name ) ) |
---|
2997 | { |
---|
2998 | _spin_lock_release( &_fat.fat_lock ); |
---|
2999 | _printf("\n[FAT ERROR] in _fat_close() : cannot write dirty clusters " |
---|
3000 | "for file <%s>\n", inode->name ); |
---|
3001 | return -3; |
---|
3002 | } |
---|
3003 | |
---|
3004 | #if GIET_DEBUG_FAT |
---|
3005 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
3006 | _printf("\n[FAT DEBUG] _fat_close() update device for file <%s>\n", inode->name ); |
---|
3007 | #endif |
---|
3008 | |
---|
3009 | // update directory dirty clusters for parent directory |
---|
3010 | if ( _update_device_from_cache( inode->parent->levels, |
---|
3011 | inode->parent->cache, |
---|
3012 | inode->parent->name ) ) |
---|
3013 | { |
---|
3014 | _spin_lock_release( &_fat.fat_lock ); |
---|
3015 | _printf("\n[FAT ERROR] in _fat_close() : cannot write dirty clusters " |
---|
3016 | "for directory <%s>\n", inode->parent->name ); |
---|
3017 | return -4; |
---|
3018 | } |
---|
3019 | |
---|
3020 | #if GIET_DEBUG_FAT |
---|
3021 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
3022 | _printf("\n[FAT DEBUG] _fat_close() update device for parent directory <%s>\n", |
---|
3023 | inode->parent->name ); |
---|
3024 | #endif |
---|
3025 | |
---|
3026 | // release memory allocated to File-Cache |
---|
3027 | if ( _release_cache_memory( inode->cache, |
---|
3028 | inode-> levels ) ) |
---|
3029 | { |
---|
3030 | _spin_lock_release( &_fat.fat_lock ); |
---|
3031 | _printf("\n[FAT ERROR] in _fat_close() : cannot release cache memory " |
---|
3032 | "for file <%s>\n", _fat.fd[fd_id].inode->name ); |
---|
3033 | return -5; |
---|
3034 | } |
---|
3035 | |
---|
3036 | #if GIET_DEBUG_FAT |
---|
3037 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
3038 | _printf("\n[FAT DEBUG] _fat_close() release memory for File-Cache <%s>\n", |
---|
3039 | inode->name ); |
---|
3040 | #endif |
---|
3041 | |
---|
3042 | } |
---|
3043 | |
---|
3044 | // release fd_id entry in file descriptor array |
---|
3045 | _fat.fd[fd_id].allocated = 0; |
---|
3046 | |
---|
3047 | // release lock |
---|
3048 | _spin_lock_release( &_fat.fat_lock ); |
---|
3049 | |
---|
3050 | return 0; |
---|
3051 | } // end fat_close() |
---|
3052 | |
---|
3053 | |
---|
3054 | |
---|
3055 | |
---|
3056 | ///////////////////////////////////////////////////////////////////////////////// |
---|
3057 | // This function implements the giet_fat_file_info() system call. |
---|
3058 | // It returns the size, the current offset and the directory info for a file |
---|
3059 | // identified by the "fd_id" argument. |
---|
3060 | ///////////////////////////////////////////////////////////////////////////////// |
---|
3061 | // Returns 0 if success. |
---|
3062 | // Returns negative value if error. |
---|
3063 | // -1 : "FAT not initialised" |
---|
3064 | // -2 : "Illegal file descriptor" |
---|
3065 | // -3 : "File not open" |
---|
3066 | ///////////////////////////////////////////////////////////////////////////////// |
---|
3067 | int _fat_file_info( unsigned int fd_id, |
---|
3068 | struct fat_file_info_s* info ) |
---|
3069 | { |
---|
3070 | if ( _fat.initialised != FAT_INITIALISED ) |
---|
3071 | { |
---|
3072 | _printf("\n[FAT ERROR] in _fat_file_info() : FAT not initialised\n"); |
---|
3073 | return -1; |
---|
3074 | } |
---|
3075 | |
---|
3076 | if ( fd_id >= GIET_OPEN_FILES_MAX ) |
---|
3077 | { |
---|
3078 | _printf("\n[FAT ERROR] in _fat_file_info() : illegal file descriptor index\n"); |
---|
3079 | return -2; |
---|
3080 | } |
---|
3081 | |
---|
3082 | if ( _fat.fd[fd_id].allocated == 0 ) |
---|
3083 | { |
---|
3084 | _printf("\n[FAT ERROR] in _fat_file_info() : file <%s> not open\n", |
---|
3085 | _fat.fd[fd_id].inode->name ); |
---|
3086 | return -3; |
---|
3087 | } |
---|
3088 | |
---|
3089 | info->size = _fat.fd[fd_id].inode->size; |
---|
3090 | info->offset = _fat.fd[fd_id].seek; |
---|
3091 | info->is_dir = _fat.fd[fd_id].inode->is_dir; |
---|
3092 | |
---|
3093 | return 0; |
---|
3094 | } // end _fat_file_info() |
---|
3095 | |
---|
3096 | |
---|
3097 | |
---|
3098 | |
---|
3099 | ///////////////////////////////////////////////////////////////////////////////// |
---|
3100 | // The following function implements the "giet_fat_read()" system call. |
---|
3101 | // It transfers "count" bytes from the File_Cache associated to the file |
---|
3102 | // identified by "fd_id", to the user "buffer", from the current file offset. |
---|
3103 | // It has the same semantic as the UNIX "read()" function. |
---|
3104 | // In case of miss in the File_Cache, it loads all involved clusters into cache. |
---|
3105 | ///////////////////////////////////////////////////////////////////////////////// |
---|
3106 | // Returns number of bytes actually transfered if success . |
---|
3107 | // Returns 0 if EOF encountered (offset + count > file_size). |
---|
3108 | // Returns a negative value if error: |
---|
3109 | // -1 : "fat not initialised" |
---|
3110 | // -2 : "file not open" |
---|
3111 | // -3 : "cannot load file from device" |
---|
3112 | ///////////////////////////////////////////////////////////////////////////////// |
---|
3113 | int _fat_read( unsigned int fd_id, // file descriptor index |
---|
3114 | void* buffer, // destination buffer |
---|
3115 | unsigned int count ) // number of bytes to read |
---|
3116 | { |
---|
3117 | // checking FAT initialised |
---|
3118 | if( _fat.initialised != FAT_INITIALISED ) |
---|
3119 | { |
---|
3120 | _printf("\n[FAT ERROR] in _fat_write() : FAT not initialised\n"); |
---|
3121 | return -1; |
---|
3122 | } |
---|
3123 | |
---|
3124 | // check fd_id overflow |
---|
3125 | if ( fd_id >= GIET_OPEN_FILES_MAX ) |
---|
3126 | { |
---|
3127 | _printf("\n[FAT ERROR] in _fat_read() : illegal file descriptor\n"); |
---|
3128 | return -1; |
---|
3129 | } |
---|
3130 | |
---|
3131 | // check file is open |
---|
3132 | if ( _fat.fd[fd_id].allocated == 0 ) |
---|
3133 | { |
---|
3134 | _printf("\n[FAT ERROR] in _fat_read() : file not open\n"); |
---|
3135 | return -2; |
---|
3136 | } |
---|
3137 | |
---|
3138 | // takes lock |
---|
3139 | _spin_lock_acquire( &_fat.fat_lock ); |
---|
3140 | |
---|
3141 | // get file inode pointer and offset |
---|
3142 | fat_inode_t* inode = _fat.fd[fd_id].inode; |
---|
3143 | unsigned int seek = _fat.fd[fd_id].seek; |
---|
3144 | |
---|
3145 | // check count & seek versus file size |
---|
3146 | if ( count + seek > inode->size ) |
---|
3147 | { |
---|
3148 | _spin_lock_release( &_fat.fat_lock ); |
---|
3149 | _printf("\n[FAT ERROR] in _fat_read() : file too small" |
---|
3150 | " / seek = %x / count = %x / file_size = %x\n", |
---|
3151 | seek , count , inode->size ); |
---|
3152 | return 0; |
---|
3153 | } |
---|
3154 | |
---|
3155 | // compute first_cluster_id and first_byte_to_move |
---|
3156 | unsigned int first_cluster_id = seek >> 12; |
---|
3157 | unsigned int first_byte_to_move = seek & 0xFFF; |
---|
3158 | |
---|
3159 | // compute last_cluster and last_byte_to_move |
---|
3160 | unsigned int last_cluster_id = (seek + count - 1) >> 12; |
---|
3161 | unsigned int last_byte_to_move = (seek + count - 1) & 0xFFF; |
---|
3162 | |
---|
3163 | #if GIET_DEBUG_FAT |
---|
3164 | unsigned int procid = _get_procid(); |
---|
3165 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
3166 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
---|
3167 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
3168 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
3169 | _printf("\n[DEBUG FAT] _fat_read() : P[%d,%d,%d] enters for file <%s> " |
---|
3170 | " / bytes = %x / offset = %x\n" |
---|
3171 | "first_cluster_id = %x / first_byte_to_move = %x" |
---|
3172 | " / last_cluster_id = %x / last_byte_to_move = %x\n", |
---|
3173 | x , y , p , inode->name , count , seek , |
---|
3174 | first_cluster_id , first_byte_to_move , last_cluster_id , last_byte_to_move ); |
---|
3175 | #endif |
---|
3176 | |
---|
3177 | // loop on all cluster covering the requested transfer |
---|
3178 | unsigned int cluster_id; |
---|
3179 | unsigned int done = 0; |
---|
3180 | for ( cluster_id = first_cluster_id ; cluster_id <= last_cluster_id ; cluster_id++ ) |
---|
3181 | { |
---|
3182 | // get pointer on the cluster_id buffer in cache |
---|
3183 | unsigned char* cbuf; |
---|
3184 | fat_cache_desc_t* pdesc; |
---|
3185 | if ( _get_buffer_from_cache( inode, |
---|
3186 | cluster_id, |
---|
3187 | &pdesc ) ) |
---|
3188 | { |
---|
3189 | _spin_lock_release( &_fat.fat_lock ); |
---|
3190 | _printf("\n[FAT ERROR] in _fat_read() : cannot load file <%s>\n", |
---|
3191 | inode->name ); |
---|
3192 | return -3; |
---|
3193 | } |
---|
3194 | cbuf = pdesc->buffer; |
---|
3195 | |
---|
3196 | #if GIET_DEBUG_FAT |
---|
3197 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
3198 | _printf("\n[DEBUG FAT] _fat_read() : P[%d,%d,%d] moves cluster_id %d from Cache-File <%s>\n", |
---|
3199 | x , y , p , cluster_id, inode->name ); |
---|
3200 | #endif |
---|
3201 | |
---|
3202 | // compute memcpy arguments |
---|
3203 | unsigned char* source; |
---|
3204 | unsigned int nbytes; |
---|
3205 | unsigned char* dest = (unsigned char*)buffer + done; |
---|
3206 | if ( (cluster_id == first_cluster_id) && (cluster_id == last_cluster_id) ) |
---|
3207 | { |
---|
3208 | source = cbuf + first_byte_to_move; |
---|
3209 | nbytes = last_byte_to_move - first_byte_to_move + 1; |
---|
3210 | } |
---|
3211 | else if ( cluster_id == first_cluster_id ) |
---|
3212 | { |
---|
3213 | source = cbuf + first_byte_to_move; |
---|
3214 | nbytes = 4096 - first_byte_to_move; |
---|
3215 | } |
---|
3216 | else if ( cluster_id == last_cluster_id ) |
---|
3217 | { |
---|
3218 | source = cbuf; |
---|
3219 | nbytes = last_byte_to_move + 1; |
---|
3220 | } |
---|
3221 | else // not first / not last |
---|
3222 | { |
---|
3223 | source = cbuf; |
---|
3224 | nbytes = 4096; |
---|
3225 | } |
---|
3226 | |
---|
3227 | // move data |
---|
3228 | memcpy( dest , source , nbytes ); |
---|
3229 | done = done + nbytes; |
---|
3230 | } |
---|
3231 | |
---|
3232 | #if GIET_DEBUG_FAT |
---|
3233 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
3234 | _printf("\n[DEBUG FAT] _fat_read() : P[%d,%d,%d] loaded file <%s> from Cache-File\n", |
---|
3235 | x , y , p , inode->name ); |
---|
3236 | #endif |
---|
3237 | |
---|
3238 | // update seek |
---|
3239 | _fat.fd[fd_id].seek += done; |
---|
3240 | |
---|
3241 | // release lock |
---|
3242 | _spin_lock_release( &_fat.fat_lock ); |
---|
3243 | |
---|
3244 | return done; |
---|
3245 | } // end _fat_read() |
---|
3246 | |
---|
3247 | |
---|
3248 | |
---|
3249 | |
---|
3250 | ///////////////////////////////////////////////////////////////////////////////// |
---|
3251 | // The following function implements the "giet_fat_write()" system call. |
---|
3252 | // It transfers "count" bytes to the fat_cache associated to the file |
---|
3253 | // identified by "fd_id", from the user "buffer", using the current file offset. |
---|
3254 | // It has the same semantic as the UNIX "write()" function. |
---|
3255 | // It increases the file size and allocate new clusters if (count + offset) |
---|
3256 | // is larger than the current file size. Then it loads and updates all |
---|
3257 | // involved clusters in the cache. |
---|
3258 | ///////////////////////////////////////////////////////////////////////////////// |
---|
3259 | // Returns number of bytes actually written if success. |
---|
3260 | // Returns a negative value if error: |
---|
3261 | // -1 : "FAT not initialised" |
---|
3262 | // -2 : "Illegal file descriptor" |
---|
3263 | // -3 : "File not open" |
---|
3264 | // -4 : "File not writable" |
---|
3265 | // -5 : "No free clusters" |
---|
3266 | // -6 : "Cannot update parent directory entry" |
---|
3267 | // -7 : "Cannot access File-Cache" |
---|
3268 | ///////////////////////////////////////////////////////////////////////////////// |
---|
3269 | int _fat_write( unsigned int fd_id, // file descriptor index |
---|
3270 | void* buffer, // source buffer |
---|
3271 | unsigned int count ) // number of bytes to write |
---|
3272 | { |
---|
3273 | // checking FAT initialised |
---|
3274 | if( _fat.initialised != FAT_INITIALISED ) |
---|
3275 | { |
---|
3276 | _printf("\n[FAT ERROR] in _fat_write() : FAT not initialised\n"); |
---|
3277 | return -1; |
---|
3278 | } |
---|
3279 | |
---|
3280 | // takes lock |
---|
3281 | _spin_lock_acquire( &_fat.fat_lock ); |
---|
3282 | |
---|
3283 | // check fd_id overflow |
---|
3284 | if ( fd_id >= GIET_OPEN_FILES_MAX ) |
---|
3285 | { |
---|
3286 | _spin_lock_release( &_fat.fat_lock ); |
---|
3287 | _printf("\n[FAT ERROR] in _fat_write() : illegal file descriptor\n"); |
---|
3288 | return -2; |
---|
3289 | } |
---|
3290 | |
---|
3291 | // check file open |
---|
3292 | if ( _fat.fd[fd_id].allocated == 0 ) |
---|
3293 | { |
---|
3294 | _spin_lock_release( &_fat.fat_lock ); |
---|
3295 | _printf("\n[FAT ERROR] in _fat_write() : file not open\n" ); |
---|
3296 | return -3; |
---|
3297 | } |
---|
3298 | |
---|
3299 | // check file writable |
---|
3300 | if ( _fat.fd[fd_id].read_only ) |
---|
3301 | { |
---|
3302 | _spin_lock_release( &_fat.fat_lock ); |
---|
3303 | _printf("\n[FAT ERROR] in _fat_write() : file <%s> is read-only\n", |
---|
3304 | _fat.fd[fd_id].inode->name ); |
---|
3305 | return -4; |
---|
3306 | } |
---|
3307 | |
---|
3308 | // get file inode pointer and seek |
---|
3309 | fat_inode_t* inode = _fat.fd[fd_id].inode; |
---|
3310 | unsigned int seek = _fat.fd[fd_id].seek; |
---|
3311 | |
---|
3312 | #if GIET_DEBUG_FAT |
---|
3313 | unsigned int procid = _get_procid(); |
---|
3314 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
3315 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
---|
3316 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
3317 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
3318 | _printf("\n[DEBUG FAT] _fat_write() : P[%d,%d,%d] enters for file <%s> " |
---|
3319 | " / bytes = %x / seek = %x\n", |
---|
3320 | x , y , p , inode->name , count , seek ); |
---|
3321 | #endif |
---|
3322 | |
---|
3323 | // chek if file size must be incremented |
---|
3324 | // and allocate new clusters from FAT if required |
---|
3325 | unsigned int old_size = inode->size; |
---|
3326 | unsigned int new_size = seek + count; |
---|
3327 | if ( new_size > old_size ) |
---|
3328 | { |
---|
3329 | // update size in inode |
---|
3330 | inode->size = new_size; |
---|
3331 | |
---|
3332 | // compute current and required numbers of clusters |
---|
3333 | unsigned old_clusters = old_size >> 12; |
---|
3334 | if ( old_size & 0xFFF ) old_clusters++; |
---|
3335 | |
---|
3336 | unsigned new_clusters = new_size >> 12; |
---|
3337 | if ( new_size & 0xFFF ) new_clusters++; |
---|
3338 | |
---|
3339 | // allocate new clusters from FAT if required |
---|
3340 | if ( new_clusters > old_clusters ) |
---|
3341 | { |
---|
3342 | |
---|
3343 | #if GIET_DEBUG_FAT |
---|
3344 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
3345 | _printf("\n[DEBUG FAT] _fat_write() : P[%d,%d,%d] allocates new clusters for file <%s>" |
---|
3346 | " / current = %d / required = %d\n", |
---|
3347 | x , y , p , inode->name , old_clusters , new_clusters ); |
---|
3348 | #endif |
---|
3349 | // allocate missing clusters |
---|
3350 | if ( _clusters_allocate( inode, |
---|
3351 | old_clusters, |
---|
3352 | new_clusters - old_clusters ) ) |
---|
3353 | { |
---|
3354 | _spin_lock_release( &_fat.fat_lock ); |
---|
3355 | _printf("\n[FAT ERROR] in _fat_write() : no free clusters" |
---|
3356 | " for file <%s>\n", _fat.fd[fd_id].inode->name ); |
---|
3357 | return -5; |
---|
3358 | } |
---|
3359 | } |
---|
3360 | |
---|
3361 | // update parent directory entry (size an cluster index) |
---|
3362 | if ( _update_dir_entry( inode ) ) |
---|
3363 | { |
---|
3364 | _spin_lock_release( &_fat.fat_lock ); |
---|
3365 | _printf("\n[FAT ERROR] in _fat_write() : cannot update parent directory entry" |
---|
3366 | " for file <%s>\n", _fat.fd[fd_id].inode->name ); |
---|
3367 | return -6; |
---|
3368 | } |
---|
3369 | |
---|
3370 | |
---|
3371 | #if GIET_DEBUG_FAT |
---|
3372 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
3373 | _printf("\n[DEBUG FAT] _fat_write() : P[%d,%d,%d] updates size for file <%s> / size = %x\n", |
---|
3374 | x , y , p , inode->name , (new_size - old_size) ); |
---|
3375 | #endif |
---|
3376 | |
---|
3377 | } |
---|
3378 | |
---|
3379 | // compute first_cluster_id and first_byte_to_move |
---|
3380 | unsigned int first_cluster_id = seek >> 12; |
---|
3381 | unsigned int first_byte_to_move = seek & 0xFFF; |
---|
3382 | |
---|
3383 | // compute last_cluster and last_byte_to_move |
---|
3384 | unsigned int last_cluster_id = (seek + count - 1) >> 12; |
---|
3385 | unsigned int last_byte_to_move = (seek + count - 1) & 0xFFF; |
---|
3386 | |
---|
3387 | #if GIET_DEBUG_FAT |
---|
3388 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
3389 | _printf("\n[DEBUG FAT] _fat_write() : P[%d,%d,%d] starts loop on clusters for file <%s>\n" |
---|
3390 | " first_cluster_id = %d / first_byte_to_move = %x" |
---|
3391 | " / last_cluster_id = %d / last_byte_to_move = %x\n", |
---|
3392 | x , y , p , inode->name , |
---|
3393 | first_cluster_id , first_byte_to_move , last_cluster_id , last_byte_to_move ); |
---|
3394 | #endif |
---|
3395 | |
---|
3396 | // loop on all clusters covering the requested transfer |
---|
3397 | unsigned int cluster_id; |
---|
3398 | unsigned int done = 0; |
---|
3399 | for ( cluster_id = first_cluster_id ; cluster_id <= last_cluster_id ; cluster_id++ ) |
---|
3400 | { |
---|
3401 | // get pointer on one 4K buffer in File-Cache |
---|
3402 | unsigned char* cbuf; |
---|
3403 | fat_cache_desc_t* pdesc; |
---|
3404 | if ( _get_buffer_from_cache( inode, |
---|
3405 | cluster_id, |
---|
3406 | &pdesc ) ) |
---|
3407 | { |
---|
3408 | _spin_lock_release( &_fat.fat_lock ); |
---|
3409 | _printf("\n[FAT ERROR] in _fat_write() : cannot load file <%s>\n", |
---|
3410 | inode->name ); |
---|
3411 | return -7; |
---|
3412 | } |
---|
3413 | |
---|
3414 | cbuf = pdesc->buffer; |
---|
3415 | pdesc->dirty = 1; |
---|
3416 | |
---|
3417 | #if GIET_DEBUG_FAT |
---|
3418 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
3419 | _printf("\n[DEBUG FAT] _fat_write() : P[%d,%d,%d] move cluster_id %d to Cache-file <%s>\n", |
---|
3420 | x , y , p , cluster_id, inode->name ); |
---|
3421 | #endif |
---|
3422 | |
---|
3423 | // compute memcpy arguments |
---|
3424 | unsigned char* source = (unsigned char*)buffer + done; |
---|
3425 | unsigned char* dest; |
---|
3426 | unsigned int nbytes; |
---|
3427 | if ( (cluster_id == first_cluster_id) && (cluster_id == last_cluster_id) ) |
---|
3428 | { |
---|
3429 | dest = cbuf + first_byte_to_move; |
---|
3430 | nbytes = last_byte_to_move - first_byte_to_move + 1; |
---|
3431 | } |
---|
3432 | else if ( cluster_id == first_cluster_id ) |
---|
3433 | { |
---|
3434 | dest = cbuf + first_byte_to_move; |
---|
3435 | nbytes = 4096 - first_byte_to_move; |
---|
3436 | } |
---|
3437 | else if ( cluster_id == last_cluster_id ) |
---|
3438 | { |
---|
3439 | dest = cbuf; |
---|
3440 | nbytes = last_byte_to_move + 1; |
---|
3441 | } |
---|
3442 | else |
---|
3443 | { |
---|
3444 | dest = cbuf; |
---|
3445 | nbytes = 4096; |
---|
3446 | } |
---|
3447 | |
---|
3448 | //move date |
---|
3449 | memcpy( dest , source , nbytes ); |
---|
3450 | done = done + nbytes; |
---|
3451 | |
---|
3452 | } // end for clusters |
---|
3453 | |
---|
3454 | // update seek |
---|
3455 | _fat.fd[fd_id].seek += done; |
---|
3456 | |
---|
3457 | #if GIET_DEBUG_FAT |
---|
3458 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
3459 | _printf("\n[DEBUG FAT] _fat_write() : P[%d,%d,%d] store file <%s> into Cache-File\n", |
---|
3460 | x , y , p , inode->name ); |
---|
3461 | #endif |
---|
3462 | |
---|
3463 | // release lock |
---|
3464 | _spin_lock_release( &_fat.fat_lock ); |
---|
3465 | |
---|
3466 | return done; |
---|
3467 | } // end _fat_write() |
---|
3468 | |
---|
3469 | |
---|
3470 | |
---|
3471 | ///////////////////////////////////////////////////////////////////////////////// |
---|
3472 | // The following function implements the "giet_fat_lseek()" system call. |
---|
3473 | // It repositions the seek in the file descriptor "fd_id", according to |
---|
3474 | // the "seek" and "whence" arguments. |
---|
3475 | // It has the same semantic as the UNIX lseek() function. |
---|
3476 | // Accepted values for whence are SEEK_SET and SEEK_CUR. |
---|
3477 | ///////////////////////////////////////////////////////////////////////////////// |
---|
3478 | // Returns new seek value (bytes) if success. |
---|
3479 | // Returns negative value if error: |
---|
3480 | // -1 : "FAT not initialised" |
---|
3481 | // -2 : "Illegal file descriptor" |
---|
3482 | // -3 : "File not open" |
---|
3483 | // -4 : "Illegal whence argument" |
---|
3484 | ///////////////////////////////////////////////////////////////////////////////// |
---|
3485 | int _fat_lseek( unsigned int fd_id, |
---|
3486 | unsigned int seek, |
---|
3487 | unsigned int whence ) |
---|
3488 | { |
---|
3489 | // checking FAT initialised |
---|
3490 | if( _fat.initialised != FAT_INITIALISED ) |
---|
3491 | { |
---|
3492 | _printf("\n[FAT ERROR] in _fat_lseek() : FAT not initialised\n"); |
---|
3493 | return -1; |
---|
3494 | } |
---|
3495 | |
---|
3496 | // check fd_id overflow |
---|
3497 | if ( fd_id >= GIET_OPEN_FILES_MAX ) |
---|
3498 | { |
---|
3499 | _printf("\n[FAT ERROR] in _fat_lseek() : illegal file descriptor\n"); |
---|
3500 | return -2; |
---|
3501 | } |
---|
3502 | |
---|
3503 | // takes lock |
---|
3504 | _spin_lock_acquire( &_fat.fat_lock ); |
---|
3505 | |
---|
3506 | // check file open |
---|
3507 | if ( _fat.fd[fd_id].allocated == 0 ) |
---|
3508 | { |
---|
3509 | _spin_lock_release( &_fat.fat_lock ); |
---|
3510 | _printf("\n[FAT ERROR] in _fat_lseek() : file not open\n"); |
---|
3511 | return -3; |
---|
3512 | } |
---|
3513 | |
---|
3514 | unsigned int new_seek; |
---|
3515 | |
---|
3516 | // compute new seek |
---|
3517 | if ( whence == SEEK_CUR ) new_seek = _fat.fd[fd_id].seek + seek; |
---|
3518 | else if ( whence == SEEK_SET ) new_seek = seek; |
---|
3519 | else |
---|
3520 | { |
---|
3521 | _spin_lock_release( &_fat.fat_lock ); |
---|
3522 | _printf("\n[FAT ERROR] in _fat_user_lseek() : illegal whence valuel\n"); |
---|
3523 | return -4; |
---|
3524 | } |
---|
3525 | |
---|
3526 | // update file descriptor offset |
---|
3527 | _fat.fd[fd_id].seek = new_seek; |
---|
3528 | |
---|
3529 | #if GIET_DEBUG_FAT |
---|
3530 | unsigned int procid = _get_procid(); |
---|
3531 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
3532 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
---|
3533 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
3534 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
3535 | _printf("\n[DEBUG FAT] _fat_lseek() : P[%d,%d,%d] set seek = %x for file <%s>\n", |
---|
3536 | x , y , p , new_seek , _fat.fd[fd_id].inode->name ); |
---|
3537 | #endif |
---|
3538 | |
---|
3539 | // release lock |
---|
3540 | _spin_lock_release( &_fat.fat_lock ); |
---|
3541 | |
---|
3542 | return new_seek; |
---|
3543 | } // end _fat_lseek() |
---|
3544 | |
---|
3545 | |
---|
3546 | |
---|
3547 | ///////////////////////////////////////////////////////////////////////////////// |
---|
3548 | // The following function implements the giet_fat_remove() system call. |
---|
3549 | // It deletes the file/directory identified by the "pathname" argument from |
---|
3550 | // the file system, if the remove condition is fulfilled (directory empty, |
---|
3551 | // or file not referenced). |
---|
3552 | // All clusters allocated in the block device DATA region are released. |
---|
3553 | // The FAT region is updated on the block device. |
---|
3554 | // The Inode-Tree is updated. |
---|
3555 | // The associated File_Cache is released. |
---|
3556 | // The Fat_Cache is updated. |
---|
3557 | ///////////////////////////////////////////////////////////////////////////////// |
---|
3558 | // Returns 0 if success. |
---|
3559 | // Returns negative value if error |
---|
3560 | // -1 : "FAT not initialised" |
---|
3561 | // -2 : "File/Directory not found" |
---|
3562 | // -3 : "Name too long in path" |
---|
3563 | // -4 : "Has the wrong type" |
---|
3564 | // -5 : "File still open" |
---|
3565 | // -6 : "Cannot scan directory" |
---|
3566 | // -7 : "Directory not empty" |
---|
3567 | // -8 : "Cannot remove file/dir from FS" |
---|
3568 | ///////////////////////////////////////////////////////////////////////////////// |
---|
3569 | int _fat_remove( char* pathname, |
---|
3570 | unsigned int should_be_dir ) |
---|
3571 | { |
---|
3572 | fat_inode_t* inode; // searched file inode pointer |
---|
3573 | |
---|
3574 | #if GIET_DEBUG_FAT |
---|
3575 | unsigned int procid = _get_procid(); |
---|
3576 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
3577 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
---|
3578 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
3579 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
3580 | _printf("\n[DEBUG FAT] _fat_remove() : P[%d,%d,%d] enters for path <%s>\n", |
---|
3581 | x, y, p, pathname ); |
---|
3582 | #endif |
---|
3583 | |
---|
3584 | // checking FAT initialised |
---|
3585 | if( _fat.initialised != FAT_INITIALISED ) |
---|
3586 | { |
---|
3587 | _printf("\n[FAT ERROR] in _fat_remove() : FAT not initialised\n"); |
---|
3588 | return -1; |
---|
3589 | } |
---|
3590 | |
---|
3591 | // take the lock |
---|
3592 | _spin_lock_acquire( &_fat.fat_lock ); |
---|
3593 | |
---|
3594 | // get searched file inode |
---|
3595 | unsigned int code = _get_inode_from_path( pathname , &inode ); |
---|
3596 | |
---|
3597 | #if GIET_DEBUG_FAT |
---|
3598 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
3599 | _printf("\n[DEBUG FAT] _fat_remove() : P[%d,%d,%d] found inode %x for <%s> / code = %d\n", |
---|
3600 | x , y , p , (unsigned int)inode , pathname , code ); |
---|
3601 | #endif |
---|
3602 | |
---|
3603 | if ( (code == 1) || (code == 2) ) |
---|
3604 | { |
---|
3605 | _spin_lock_release( &_fat.fat_lock ); |
---|
3606 | _printf("\n[FAT ERROR] in _fat_remove() : file <%s> not found\n", |
---|
3607 | pathname ); |
---|
3608 | return -2; |
---|
3609 | } |
---|
3610 | else if ( code == 3 ) |
---|
3611 | { |
---|
3612 | _spin_lock_release( &_fat.fat_lock ); |
---|
3613 | _printf("\n[FAT ERROR] in _fat_remove() : name too long in <%s>\n", |
---|
3614 | pathname ); |
---|
3615 | return -3; |
---|
3616 | } |
---|
3617 | |
---|
3618 | // check inode type |
---|
3619 | if ( (inode->is_dir != 0) && (should_be_dir == 0) ) |
---|
3620 | { |
---|
3621 | _spin_lock_release( &_fat.fat_lock ); |
---|
3622 | _printf("\n[FAT ERROR] in _fat_remove() : <%s> is a directory\n", |
---|
3623 | pathname ); |
---|
3624 | return -4; |
---|
3625 | } |
---|
3626 | if ( (inode->is_dir == 0) && (should_be_dir != 0) ) |
---|
3627 | { |
---|
3628 | _spin_lock_release( &_fat.fat_lock ); |
---|
3629 | _printf("\n[FAT ERROR] in _fat_remove() : <%s> is not a directory\n", |
---|
3630 | pathname ); |
---|
3631 | return -4; |
---|
3632 | } |
---|
3633 | |
---|
3634 | #if GIET_DEBUG_FAT |
---|
3635 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
3636 | _printf("\n[DEBUG FAT] _fat_remove() : P[%d,%d,%d] checked inode type for <%s>\n", |
---|
3637 | x , y , p , pathname ); |
---|
3638 | #endif |
---|
3639 | |
---|
3640 | // check references count for a file |
---|
3641 | if ( (inode->is_dir == 0) && (inode->count != 0) ) |
---|
3642 | { |
---|
3643 | _spin_lock_release( &_fat.fat_lock ); |
---|
3644 | _printf("\n[FAT ERROR] in _fat_remove() : file <%s> still referenced\n", |
---|
3645 | pathname ); |
---|
3646 | return -5; |
---|
3647 | } |
---|
3648 | |
---|
3649 | // check empty for a directory |
---|
3650 | if ( inode->is_dir ) |
---|
3651 | { |
---|
3652 | unsigned int entries; |
---|
3653 | if ( _get_nb_entries( inode , &entries ) ) |
---|
3654 | { |
---|
3655 | _spin_lock_release( &_fat.fat_lock ); |
---|
3656 | _printf("\n[FAT ERROR] in _fat_remove() : cannot scan directory <%s>\n", |
---|
3657 | pathname ); |
---|
3658 | return -6; |
---|
3659 | } |
---|
3660 | else if ( entries > 2 ) |
---|
3661 | { |
---|
3662 | _spin_lock_release( &_fat.fat_lock ); |
---|
3663 | _printf("\n[FAT ERROR] in _fat_remove() : directory <%s> not empty\n", |
---|
3664 | pathname ); |
---|
3665 | return -7; |
---|
3666 | } |
---|
3667 | } |
---|
3668 | |
---|
3669 | #if GIET_DEBUG_FAT |
---|
3670 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
3671 | _printf("\n[DEBUG FAT] _fat_remove() : P[%d,%d,%d] checked remove condition OK for <%s>\n", |
---|
3672 | x , y , p , pathname ); |
---|
3673 | #endif |
---|
3674 | |
---|
3675 | // remove the file or directory from the file system |
---|
3676 | if ( _remove_node_from_fs( inode ) ) |
---|
3677 | { |
---|
3678 | _spin_lock_release( &_fat.fat_lock ); |
---|
3679 | _printf("\n[FAT ERROR] in _fat_remove() : cannot remove <%s> from FS\n", |
---|
3680 | pathname ); |
---|
3681 | return -8; |
---|
3682 | } |
---|
3683 | |
---|
3684 | // release lock and return success |
---|
3685 | _spin_lock_release( &_fat.fat_lock ); |
---|
3686 | |
---|
3687 | #if GIET_DEBUG_FAT |
---|
3688 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
3689 | _printf("\n[DEBUG FAT] _fat_remove() : P[%d,%d,%d] removed <%s> from FS\n", |
---|
3690 | x, y, p, pathname ); |
---|
3691 | #endif |
---|
3692 | |
---|
3693 | return 0; |
---|
3694 | |
---|
3695 | } // end _fat_remove() |
---|
3696 | |
---|
3697 | |
---|
3698 | |
---|
3699 | |
---|
3700 | |
---|
3701 | ///////////////////////////////////////////////////////////////////////////////// |
---|
3702 | // This function implements the giet_fat_rename() system call. |
---|
3703 | // It moves an existing file or directory from one node (defined by "old_path" |
---|
3704 | // argument) to another node (defined by "new_path" argument) in the FS tree. |
---|
3705 | // The type (file/directory) and content are not modified. |
---|
3706 | // If the new_path file/dir exist, it is removed from the file system, but only |
---|
3707 | // if the remove condition is respected (directory empty / file not referenced). |
---|
3708 | // The removed entry is only removed after the new entry is actually created. |
---|
3709 | ///////////////////////////////////////////////////////////////////////////////// |
---|
3710 | // Returns 0 if success. |
---|
3711 | // Returns a negative value if error: |
---|
3712 | // -1 : "FAT not initialised" |
---|
3713 | // -2 : "old_path not found" |
---|
3714 | // -3 : "new_path not found" |
---|
3715 | // -4 : "cannot scan to_remove directory" |
---|
3716 | // -5 : "to_remove directory not empty" |
---|
3717 | // -6 : "to_remove file still referenced" |
---|
3718 | // -7 : "cannot add new node to new_parent directory" |
---|
3719 | // -8 : "cannot update new_parent directory on device" |
---|
3720 | // -9 : "cannot remove old node from old_parent directory" |
---|
3721 | // -10 : "cannot update old_parent directory on device" |
---|
3722 | // -11 : "cannot remove to_remove node from FS" |
---|
3723 | ///////////////////////////////////////////////////////////////////////////////// |
---|
3724 | int _fat_rename( char* old_path, |
---|
3725 | char* new_path ) |
---|
3726 | { |
---|
3727 | fat_inode_t* inode; // anonymous inode pointer |
---|
3728 | fat_inode_t* old; // inode identified by old_path => to be deleted |
---|
3729 | fat_inode_t* new; // inode identified by new_path => to be created |
---|
3730 | fat_inode_t* old_parent; // parent inode in old_path => to be modified |
---|
3731 | fat_inode_t* new_parent; // parent inode in new_path => to be modified |
---|
3732 | fat_inode_t* to_remove; // previouly identified by new_path => to be removed |
---|
3733 | unsigned int code; |
---|
3734 | |
---|
3735 | #if GIET_DEBUG_FAT |
---|
3736 | unsigned int procid = _get_procid(); |
---|
3737 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
3738 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
---|
3739 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
3740 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
3741 | _printf("\n[DEBUG FAT] _fat_rename() : P[%d,%d,%d] enters to move <%s> to <%s>\n", |
---|
3742 | x , y , p , old_path , new_path ); |
---|
3743 | #endif |
---|
3744 | |
---|
3745 | // checking FAT initialised |
---|
3746 | if( _fat.initialised != FAT_INITIALISED ) |
---|
3747 | { |
---|
3748 | _printf("\n[FAT ERROR] in _fat_rename() : FAT not initialised\n"); |
---|
3749 | return -1; |
---|
3750 | } |
---|
3751 | |
---|
3752 | // take the lock |
---|
3753 | _spin_lock_acquire( &_fat.fat_lock ); |
---|
3754 | |
---|
3755 | // get "old" and "old_parent" inode pointers |
---|
3756 | if ( _get_inode_from_path( old_path , &inode ) ) |
---|
3757 | { |
---|
3758 | _spin_lock_release( &_fat.fat_lock ); |
---|
3759 | _printf("\n[FAT ERROR] in _fat_rename() : <%s> not found\n", old_path ); |
---|
3760 | return -2; |
---|
3761 | } |
---|
3762 | else |
---|
3763 | { |
---|
3764 | old = inode; |
---|
3765 | old_parent = inode->parent; |
---|
3766 | } |
---|
3767 | |
---|
3768 | // get "to_removed" and "new_parent" inode pointers |
---|
3769 | code = _get_inode_from_path( new_path , &inode ); |
---|
3770 | |
---|
3771 | if ( code == 0 ) // new_path inode already exist |
---|
3772 | { |
---|
3773 | if ( inode == old ) // the file will replace itself, do nothing |
---|
3774 | { |
---|
3775 | _spin_lock_release( &_fat.fat_lock ); |
---|
3776 | return 0; |
---|
3777 | } |
---|
3778 | |
---|
3779 | to_remove = inode; |
---|
3780 | new_parent = inode->parent; |
---|
3781 | } |
---|
3782 | else if ( code == 1 ) // to_remove does not exist but parent exist |
---|
3783 | { |
---|
3784 | to_remove = NULL; |
---|
3785 | new_parent = inode; |
---|
3786 | } |
---|
3787 | else // parent directory in new_path not found |
---|
3788 | { |
---|
3789 | _spin_lock_release( &_fat.fat_lock ); |
---|
3790 | _printf("\n[FAT ERROR] in _fat_rename() : <%s> not found\n", new_path ); |
---|
3791 | return -3; |
---|
3792 | } |
---|
3793 | |
---|
3794 | #if GIET_DEBUG_FAT |
---|
3795 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
3796 | { |
---|
3797 | if ( to_remove ) |
---|
3798 | _printf("\n[DEBUG FAT] _fat_rename() : old_parent = %s / old = %s / new_parent = %s " |
---|
3799 | "/ to_remove = %s\n", |
---|
3800 | old_parent->name , old->name , new_parent->name , to_remove->name ); |
---|
3801 | else |
---|
3802 | _printf("\n[DEBUG FAT] _fat_rename() : old_parent = %s / old = %s / new_parent = %s " |
---|
3803 | "/ no remove\n", |
---|
3804 | old_parent->name , old->name , new_parent->name ); |
---|
3805 | } |
---|
3806 | #endif |
---|
3807 | |
---|
3808 | // check remove condition for "to_remove" inode |
---|
3809 | if ( to_remove ) |
---|
3810 | { |
---|
3811 | if ( to_remove->is_dir ) // it's a directory |
---|
3812 | { |
---|
3813 | unsigned int entries; |
---|
3814 | if ( _get_nb_entries( to_remove , &entries ) ) |
---|
3815 | { |
---|
3816 | _spin_lock_release( &_fat.fat_lock ); |
---|
3817 | _printf("\n[FAT ERROR] in _fat_rename() : cannot scan directory <%s>\n", |
---|
3818 | to_remove->name ); |
---|
3819 | return -4; |
---|
3820 | } |
---|
3821 | else if ( entries > 2 ) |
---|
3822 | { |
---|
3823 | _spin_lock_release( &_fat.fat_lock ); |
---|
3824 | _printf("\n[FAT ERROR] in _fat_rename() : directory <%s> not empty\n", |
---|
3825 | to_remove->name ); |
---|
3826 | return -5; |
---|
3827 | } |
---|
3828 | } |
---|
3829 | else // it's a file |
---|
3830 | { |
---|
3831 | if ( to_remove->count ) |
---|
3832 | { |
---|
3833 | _spin_lock_release( &_fat.fat_lock ); |
---|
3834 | _printf("\n[FAT ERROR] in _fat_rename() : file <%s> still referenced\n", |
---|
3835 | to_remove->name ); |
---|
3836 | return -6; |
---|
3837 | } |
---|
3838 | } |
---|
3839 | } |
---|
3840 | |
---|
3841 | #if GIET_DEBUG_FAT |
---|
3842 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
3843 | _printf("\n[FAT DEBUG] _fat_rename() : P[%d,%d,%d] checked remove condition OK\n", |
---|
3844 | x , y , p ); |
---|
3845 | #endif |
---|
3846 | |
---|
3847 | // get new last name / error checking already done by _get_inode_from_path() |
---|
3848 | char new_name[32]; |
---|
3849 | _get_last_name( new_path , new_name ); |
---|
3850 | |
---|
3851 | // allocate "new" inode |
---|
3852 | new = _allocate_one_inode( new_name, |
---|
3853 | old->is_dir, |
---|
3854 | old->cluster, |
---|
3855 | old->size, |
---|
3856 | 0, // count |
---|
3857 | 0, // dentry |
---|
3858 | 0 ); // no cache_allocate |
---|
3859 | |
---|
3860 | // give the "old" File-Cache to the "new inode |
---|
3861 | new->levels = old->levels; |
---|
3862 | new->cache = old->cache; |
---|
3863 | |
---|
3864 | // add "new" to "new_parent" directory File-Cache |
---|
3865 | if ( _add_dir_entry( new , new_parent ) ) |
---|
3866 | { |
---|
3867 | _spin_lock_release( &_fat.fat_lock ); |
---|
3868 | _printf("\n[FAT ERROR] in _fat_rename() : cannot add <%s> into <%s>\n", |
---|
3869 | new->name , new_parent->name ); |
---|
3870 | return -7; |
---|
3871 | } |
---|
3872 | |
---|
3873 | // add "new" to "new_parent" directory in Inode-Tree |
---|
3874 | _add_inode_in_tree( new , new_parent ); |
---|
3875 | |
---|
3876 | // updates "new_parent" directory on device |
---|
3877 | if ( _update_device_from_cache( new_parent->levels, |
---|
3878 | new_parent->cache, |
---|
3879 | new_parent->name ) ) |
---|
3880 | { |
---|
3881 | _spin_lock_release( &_fat.fat_lock ); |
---|
3882 | _printf("\n[FAT ERROR] in _fat_rename() : cannot update <%s> on device\n", |
---|
3883 | new_parent->name ); |
---|
3884 | return -8; |
---|
3885 | } |
---|
3886 | |
---|
3887 | // remove "old" from "old_parent" File-Cache |
---|
3888 | if ( _remove_dir_entry( old ) ) |
---|
3889 | { |
---|
3890 | _spin_lock_release( &_fat.fat_lock ); |
---|
3891 | _printf("\n[FAT ERROR] in _fat_rename() : cannot remove <%s> from <%s>\n", |
---|
3892 | old->name , old_parent->name ); |
---|
3893 | return -9; |
---|
3894 | } |
---|
3895 | |
---|
3896 | // remove "old" inode from Inode-Tree |
---|
3897 | _remove_inode_from_tree( old ); |
---|
3898 | |
---|
3899 | // release "old" inode |
---|
3900 | _free( old ); |
---|
3901 | |
---|
3902 | // updates "old_parent" directory on device |
---|
3903 | if ( _update_device_from_cache( old_parent->levels, |
---|
3904 | old_parent->cache, |
---|
3905 | old_parent->name ) ) |
---|
3906 | { |
---|
3907 | _spin_lock_release( &_fat.fat_lock ); |
---|
3908 | _printf("\n[FAT ERROR] in _fat_rename() : cannot update <%s> on device\n", |
---|
3909 | old_parent->name ); |
---|
3910 | return -10; |
---|
3911 | } |
---|
3912 | |
---|
3913 | // remove "to_remove" from File System (if required) |
---|
3914 | if ( to_remove ) |
---|
3915 | { |
---|
3916 | if ( _remove_node_from_fs( to_remove ) ) |
---|
3917 | { |
---|
3918 | _spin_lock_release( &_fat.fat_lock ); |
---|
3919 | _printf("\n[FAT ERROR] in _fat_rename() : cannot remove <%s> from FS\n", |
---|
3920 | to_remove->name ); |
---|
3921 | return -11; |
---|
3922 | } |
---|
3923 | } |
---|
3924 | |
---|
3925 | // release lock |
---|
3926 | _spin_lock_release( &_fat.fat_lock ); |
---|
3927 | |
---|
3928 | return 0; |
---|
3929 | } // end _fat_rename() |
---|
3930 | |
---|
3931 | |
---|
3932 | |
---|
3933 | |
---|
3934 | ///////////////////////////////////////////////////////////////////////////////// |
---|
3935 | // The following function implements the giet_fat_mkdir() system call. |
---|
3936 | // It creates in file system the directory specified by the "pathname" argument. |
---|
3937 | // The Inode-Tree is updated. |
---|
3938 | // One cluster is allocated to the new directory. |
---|
3939 | // The associated File-Cache is created. |
---|
3940 | // The FAT region on block device is updated. |
---|
3941 | // The DATA region on block device is updated. |
---|
3942 | ///////////////////////////////////////////////////////////////////////////////// |
---|
3943 | // Returns 0 if success. |
---|
3944 | // Returns a negative value if error: |
---|
3945 | // -1 : "Fat not initialised" |
---|
3946 | // -2 : "Path to parent not found" |
---|
3947 | // -3 : "One name in path too long" |
---|
3948 | // -4 : "Directory already exist" |
---|
3949 | // -5 : "No free cluster" |
---|
3950 | // -6 : "Cannot update parent directory" |
---|
3951 | // -7 : "Cannot update parent DATA region" |
---|
3952 | // -8 : "Cannot update FAT region" |
---|
3953 | // -9 : "Cannot update FS-INFO" |
---|
3954 | // -10 : "Cannot update directory DATA region" |
---|
3955 | ///////////////////////////////////////////////////////////////////////////////// |
---|
3956 | int _fat_mkdir( char* pathname ) |
---|
3957 | { |
---|
3958 | fat_inode_t* inode; // anonymous inode pointer |
---|
3959 | fat_inode_t* child; // searched directory inode pointer |
---|
3960 | fat_inode_t* parent; // parent directory inode pointer |
---|
3961 | |
---|
3962 | #if GIET_DEBUG_FAT |
---|
3963 | unsigned int procid = _get_procid(); |
---|
3964 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
3965 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
---|
3966 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
3967 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
3968 | _printf("\n[DEBUG FAT] _fat_mkdir() : P[%d,%d,%d] enters for path <%s>\n", |
---|
3969 | x, y, p, pathname ); |
---|
3970 | #endif |
---|
3971 | |
---|
3972 | // checking FAT initialised |
---|
3973 | if( _fat.initialised != FAT_INITIALISED ) |
---|
3974 | { |
---|
3975 | _printf("\n[FAT ERROR] in _fat_mkdir() : FAT not initialised\n"); |
---|
3976 | return -1; |
---|
3977 | } |
---|
3978 | |
---|
3979 | // takes the lock |
---|
3980 | _spin_lock_acquire( &_fat.fat_lock ); |
---|
3981 | |
---|
3982 | // get inode |
---|
3983 | unsigned int code = _get_inode_from_path( pathname , &inode ); |
---|
3984 | |
---|
3985 | if ( code == 2 ) |
---|
3986 | { |
---|
3987 | _spin_lock_release( &_fat.fat_lock ); |
---|
3988 | _printf("\n[FAT ERROR] in _fat_mkdir() : path to parent not found" |
---|
3989 | " for directory <%s>\n", pathname ); |
---|
3990 | return -2; |
---|
3991 | } |
---|
3992 | else if ( code == 3 ) |
---|
3993 | { |
---|
3994 | _spin_lock_release( &_fat.fat_lock ); |
---|
3995 | _printf("\n[FAT ERROR] in _fat_mkdir() : one name in path too long" |
---|
3996 | " for directory <%s>\n", pathname ); |
---|
3997 | return -3; |
---|
3998 | } |
---|
3999 | else if ( code == 0 ) |
---|
4000 | { |
---|
4001 | _spin_lock_release( &_fat.fat_lock ); |
---|
4002 | _printf("\n[FAT ERROR] in _fat_mkdir() : directory <%s> already exist\n", |
---|
4003 | pathname ); |
---|
4004 | return -4; |
---|
4005 | } |
---|
4006 | else if ( code == 1 ) // directory not found => create |
---|
4007 | { |
---|
4008 | parent = inode; |
---|
4009 | |
---|
4010 | #if GIET_DEBUG_FAT |
---|
4011 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
4012 | _printf("\n[DEBUG FAT] _fat_mkdir() : P[%d,%d,%d] create new directory <%s>\n", |
---|
4013 | x , y , p , pathname ); |
---|
4014 | #endif |
---|
4015 | |
---|
4016 | // get directory name / error check already done by _get_inode_from_path() |
---|
4017 | char name[32]; |
---|
4018 | _get_last_name( pathname , name ); |
---|
4019 | |
---|
4020 | // allocate one cluster from FAT for the new directory |
---|
4021 | unsigned int cluster; |
---|
4022 | if ( _allocate_one_cluster( &cluster ) ) |
---|
4023 | { |
---|
4024 | _spin_lock_release( &_fat.fat_lock ); |
---|
4025 | _printf("\n[FAT ERROR] in _fat_mkdir() : no free cluster" |
---|
4026 | " for directory <%s>\n" , pathname ); |
---|
4027 | return -5; |
---|
4028 | } |
---|
4029 | |
---|
4030 | // allocate a new inode and an empty Cache-File |
---|
4031 | child = _allocate_one_inode( name, |
---|
4032 | 1, // it's a directory |
---|
4033 | cluster, |
---|
4034 | 0, // size not defined |
---|
4035 | 0, // count |
---|
4036 | 0, // dentry set by _add_dir_entry() |
---|
4037 | 1 ); // cache_allocate |
---|
4038 | |
---|
4039 | // introduce inode in Inode-Tree |
---|
4040 | _add_inode_in_tree( child , parent ); |
---|
4041 | |
---|
4042 | // allocate and initialise one 4 Kbytes buffer and associated descriptor |
---|
4043 | _allocate_one_buffer( child, |
---|
4044 | 0, // cluster_id, |
---|
4045 | cluster ); |
---|
4046 | |
---|
4047 | _add_special_directories( child, |
---|
4048 | parent ); |
---|
4049 | |
---|
4050 | // add an entry in the parent directory Cache_file |
---|
4051 | if ( _add_dir_entry( child , parent ) ) |
---|
4052 | { |
---|
4053 | _spin_lock_release( &_fat.fat_lock ); |
---|
4054 | _printf("\n[FAT ERROR] in _fat_mkdir() : cannot update parent directory" |
---|
4055 | " for directory <%s>\n" , pathname ); |
---|
4056 | return -6; |
---|
4057 | } |
---|
4058 | |
---|
4059 | // update DATA region on block device for parent directory |
---|
4060 | if ( _update_device_from_cache( parent->levels, |
---|
4061 | parent->cache, |
---|
4062 | parent->name ) ) |
---|
4063 | { |
---|
4064 | _spin_lock_release( &_fat.fat_lock ); |
---|
4065 | _printf("\n[FAT ERROR] in _fat_mkdir() : cannot update DATA region " |
---|
4066 | " for parent of directory <%s>\n", pathname ); |
---|
4067 | return -7; |
---|
4068 | } |
---|
4069 | |
---|
4070 | // update FAT region on block device |
---|
4071 | if ( _update_device_from_cache( _fat.fat_cache_levels, |
---|
4072 | _fat.fat_cache_root, |
---|
4073 | "FAT" ) ) |
---|
4074 | { |
---|
4075 | _spin_lock_release( &_fat.fat_lock ); |
---|
4076 | _printf("\n[FAT ERROR] in _fat_mkdir() : cannot update FAT region" |
---|
4077 | " for directory <%s>\n", pathname ); |
---|
4078 | return -8; |
---|
4079 | } |
---|
4080 | |
---|
4081 | // update FS_INFO sector |
---|
4082 | if ( _update_fs_info() ) |
---|
4083 | { |
---|
4084 | _spin_lock_release( &_fat.fat_lock ); |
---|
4085 | _printf("\n[FAT ERROR] in _fat_mkdir() : cannot update FS-INFO" |
---|
4086 | " for directory <%s>\n", pathname ); |
---|
4087 | return -9; |
---|
4088 | } |
---|
4089 | |
---|
4090 | // update DATA region on block device for the new directory |
---|
4091 | if ( _update_device_from_cache( child->levels, |
---|
4092 | child->cache, |
---|
4093 | child->name ) ) |
---|
4094 | { |
---|
4095 | _spin_lock_release( &_fat.fat_lock ); |
---|
4096 | _printf("\n[FAT ERROR] in _fat_mkdir() : cannot update DATA region" |
---|
4097 | " for directory <%s>\n", pathname ); |
---|
4098 | return -10; |
---|
4099 | } |
---|
4100 | } // end create directory |
---|
4101 | |
---|
4102 | // release lock |
---|
4103 | _spin_lock_release( &_fat.fat_lock ); |
---|
4104 | |
---|
4105 | return 0; |
---|
4106 | } // end _fat_mkdir() |
---|
4107 | |
---|
4108 | |
---|
4109 | |
---|
4110 | |
---|
4111 | |
---|
4112 | ///////////////////////////////////////////////////////////////////////////////// |
---|
4113 | // The following function implements the giet_fat_list() system call. |
---|
4114 | // It displays the content of a directory identified by the "pathname" argument. |
---|
4115 | // It returns an error code if the pathname is not a directory. |
---|
4116 | ///////////////////////////////////////////////////////////////////////////////// |
---|
4117 | // Returns 0 if success. |
---|
4118 | // Returns a negative value if error: |
---|
4119 | // -1 : "FAT not initialised |
---|
4120 | // -2 : "Directory not found" |
---|
4121 | // -3 : "Name in path too long |
---|
4122 | // -4 : "Not a directory" |
---|
4123 | // -5 : "Cannot access directory" |
---|
4124 | // -6 : "Name too long truncated" |
---|
4125 | ///////////////////////////////////////////////////////////////////////////////// |
---|
4126 | int _fat_list( char* pathname ) |
---|
4127 | { |
---|
4128 | fat_inode_t* inode; |
---|
4129 | |
---|
4130 | // checking FAT initialised |
---|
4131 | if( _fat.initialised != FAT_INITIALISED ) |
---|
4132 | { |
---|
4133 | _printf("\n[FAT ERROR] in _fat_list() : FAT not initialised\n"); |
---|
4134 | return -1; |
---|
4135 | } |
---|
4136 | |
---|
4137 | #if GIET_DEBUG_FAT |
---|
4138 | unsigned int procid = _get_procid(); |
---|
4139 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
4140 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
---|
4141 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
4142 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
4143 | _printf("\n[DEBUG FAT] _fat_list() : P[%d,%d,%d] enters for path <%s>\n", |
---|
4144 | x, y, p, pathname ); |
---|
4145 | #endif |
---|
4146 | |
---|
4147 | // get inode |
---|
4148 | unsigned int code = _get_inode_from_path( pathname , &inode ); |
---|
4149 | |
---|
4150 | if ( (code == 1) || (code == 2) ) |
---|
4151 | { |
---|
4152 | _printf("\n[FAT ERROR] in _fat_list() : directory <%s> not found\n", pathname ); |
---|
4153 | return -2; |
---|
4154 | } |
---|
4155 | if ( code == 3 ) |
---|
4156 | { |
---|
4157 | _printf("\n[FAT ERROR] in _fat_list() : name too long in path <%s>\n", pathname ); |
---|
4158 | return -3; |
---|
4159 | } |
---|
4160 | |
---|
4161 | // check found inode is a directory |
---|
4162 | if ( inode->is_dir == 0 ) |
---|
4163 | { |
---|
4164 | _printf("\n[FAT ERROR] in _fat_list() : <%s> is not a directory\n", pathname ); |
---|
4165 | return -4; |
---|
4166 | } |
---|
4167 | |
---|
4168 | #if GIET_DEBUG_FAT |
---|
4169 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
4170 | _printf("\n[DEBUG FAT] _fat_list() : P[%d,%d,%d] found inode for path <%s>\n", |
---|
4171 | x, y, p, pathname ); |
---|
4172 | #endif |
---|
4173 | |
---|
4174 | // scan directory up to end of directory / two embedded loops : |
---|
4175 | // - loop on the clusters allocated to the directory |
---|
4176 | // - loop on the directory entries in each 4 Kbytes buffer |
---|
4177 | unsigned char* buffer; |
---|
4178 | fat_cache_desc_t* pdesc; |
---|
4179 | unsigned int cluster_id = 0; // cluster index in directory |
---|
4180 | unsigned int offset = 0; // position in scanned buffer |
---|
4181 | unsigned int lfn = 0; // number of lfn entries |
---|
4182 | unsigned int nb_entries = 0; // number of directory entries |
---|
4183 | unsigned int done = 0; // end of directory found |
---|
4184 | unsigned int attr; // ATTR field value |
---|
4185 | unsigned int ord; // ORD field value |
---|
4186 | char lfn1[16]; // temporary buffer for string in LFN1 |
---|
4187 | char lfn2[16]; // temporary buffer for string in LFN2 |
---|
4188 | char lfn3[16]; // temporary buffer for string in LFN3 |
---|
4189 | char name[36]; // directory entry full name |
---|
4190 | unsigned int cluster; // directory entry cluster index |
---|
4191 | unsigned int size; // directory entry size |
---|
4192 | unsigned int is_dir; // directory entry is a directory |
---|
4193 | unsigned int is_vid; // directory entry is volume_id |
---|
4194 | |
---|
4195 | // TODO : define a method to transfer this information to user mode |
---|
4196 | _printf("\n<%s> cluster = %x / lba = %x\n", pathname , |
---|
4197 | inode->cluster , _cluster_to_lba( inode->cluster) ); |
---|
4198 | |
---|
4199 | while ( done == 0 ) |
---|
4200 | { |
---|
4201 | // get one 4 Kytes buffer |
---|
4202 | if ( _get_buffer_from_cache( inode, |
---|
4203 | cluster_id, |
---|
4204 | &pdesc ) ) |
---|
4205 | { |
---|
4206 | _printf("\n[FAT ERROR] in _fat_list() : cannot access <%s>\n", pathname ); |
---|
4207 | return -5; |
---|
4208 | } |
---|
4209 | buffer = pdesc->buffer; |
---|
4210 | |
---|
4211 | // scan this 4 Kbytes buffer |
---|
4212 | while ( (offset < 4096) && (done == 0) ) |
---|
4213 | { |
---|
4214 | attr = _read_entry( DIR_ATTR , buffer + offset , 0 ); |
---|
4215 | ord = _read_entry( LDIR_ORD , buffer + offset , 0 ); |
---|
4216 | |
---|
4217 | if (ord == NO_MORE_ENTRY) // no more entry in directory => break |
---|
4218 | { |
---|
4219 | done = 1; |
---|
4220 | } |
---|
4221 | else if ( ord == FREE_ENTRY ) // free entry => skip |
---|
4222 | { |
---|
4223 | offset = offset + 32; |
---|
4224 | } |
---|
4225 | else if ( attr == ATTR_LONG_NAME_MASK ) // LFN entry => get partial names |
---|
4226 | { |
---|
4227 | unsigned int seq = ord & 0x3; |
---|
4228 | lfn = (seq > lfn) ? seq : lfn; |
---|
4229 | if ( seq == 1 ) _get_name_from_long( buffer + offset, lfn1 ); |
---|
4230 | else if ( seq == 2 ) _get_name_from_long( buffer + offset, lfn2 ); |
---|
4231 | else if ( seq == 3 ) _get_name_from_long( buffer + offset, lfn3 ); |
---|
4232 | offset = offset + 32; |
---|
4233 | } |
---|
4234 | else // NORMAL entry |
---|
4235 | { |
---|
4236 | if ( lfn == 0 ) |
---|
4237 | { |
---|
4238 | _get_name_from_short( buffer + offset , name ); |
---|
4239 | } |
---|
4240 | else if ( lfn == 1 ) |
---|
4241 | { |
---|
4242 | _strcpy( name , lfn1 ); |
---|
4243 | } |
---|
4244 | else if ( lfn == 2 ) |
---|
4245 | { |
---|
4246 | _strcpy( name , lfn1 ); |
---|
4247 | _strcpy( name + 13 , lfn2 ); |
---|
4248 | } |
---|
4249 | else if ( lfn == 3 ) |
---|
4250 | { |
---|
4251 | _strcpy( name , lfn1 ); |
---|
4252 | _strcpy( name + 13 , lfn2 ); |
---|
4253 | _strcpy( name + 26 , lfn3 ); |
---|
4254 | } |
---|
4255 | |
---|
4256 | is_dir = ((attr & ATTR_DIRECTORY) == ATTR_DIRECTORY); |
---|
4257 | is_vid = ((attr & ATTR_VOLUME_ID) == ATTR_VOLUME_ID); |
---|
4258 | size = (_read_entry( DIR_FILE_SIZE , buffer + offset , 1 ) ) ; |
---|
4259 | cluster = (_read_entry( DIR_FST_CLUS_HI , buffer + offset , 1 ) << 16) | |
---|
4260 | (_read_entry( DIR_FST_CLUS_LO , buffer + offset , 1 ) ) ; |
---|
4261 | |
---|
4262 | if ( is_vid == 0 ) |
---|
4263 | { |
---|
4264 | // TODO : define a method to transfer this information to user mode |
---|
4265 | if (is_dir) _printf(" DIR | size = %X | cluster = %X | %s\n", |
---|
4266 | size , cluster, name ); |
---|
4267 | else _printf(" FILE | size = %X | cluster = %X | %s\n", |
---|
4268 | size , cluster, name ); |
---|
4269 | nb_entries++; |
---|
4270 | } |
---|
4271 | |
---|
4272 | offset = offset + 32; |
---|
4273 | lfn = 0; |
---|
4274 | } |
---|
4275 | } // end loop on directory entries |
---|
4276 | |
---|
4277 | if ( done == 0 ) |
---|
4278 | { |
---|
4279 | cluster_id++; |
---|
4280 | offset = 0; |
---|
4281 | } |
---|
4282 | } // end loop on buffers |
---|
4283 | |
---|
4284 | // TODO : define a method to transfer this information to user mode |
---|
4285 | _printf(" total = %d entries\n", nb_entries ); |
---|
4286 | |
---|
4287 | return 0; |
---|
4288 | } // end _fat_list() |
---|
4289 | |
---|
4290 | |
---|
4291 | |
---|
4292 | |
---|
4293 | |
---|
4294 | /////////////////////////////////////////////////////////////////////////////// |
---|
4295 | // This functiond load a file identified by the "pathname" argument into the |
---|
4296 | // memory buffer defined by the "buffer_vbase" and "buffer_size" arguments. |
---|
4297 | // It is intended to be called by the boot-loader, as it does not use the |
---|
4298 | // dynamically allocated FAT structures (Inode-Tree, Fat_Cache or File-Cache, |
---|
4299 | // File-Descriptor-Array). |
---|
4300 | // It uses only the 512 bytes buffer defined in the FAT descriptor. |
---|
4301 | /////////////////////////////////////////////////////////////////////////////// |
---|
4302 | // Returns 0 if success. |
---|
4303 | // Returns negative value if error: |
---|
4304 | // -1 : "FAT not initialised" |
---|
4305 | // -2 : "File not found" |
---|
4306 | // -3 : "Buffer too small" |
---|
4307 | // -4 : "Cannot access block device" |
---|
4308 | // -5 : "Cannot access FAT on block device" |
---|
4309 | /////////////////////////////////////////////////////////////////////////////// |
---|
4310 | int _fat_load_no_cache( char* pathname, |
---|
4311 | unsigned int buffer_vbase, |
---|
4312 | unsigned int buffer_size ) |
---|
4313 | { |
---|
4314 | // checking FAT initialised |
---|
4315 | if( _fat.initialised != FAT_INITIALISED ) |
---|
4316 | { |
---|
4317 | _printf("\n[FAT ERROR] in _fat_load_no_cache() : FAT not initialised\n"); |
---|
4318 | return -1; |
---|
4319 | } |
---|
4320 | |
---|
4321 | unsigned int file_size; |
---|
4322 | unsigned int cluster; |
---|
4323 | |
---|
4324 | #if GIET_DEBUG_FAT |
---|
4325 | unsigned int procid = _get_procid(); |
---|
4326 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
4327 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
---|
4328 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
4329 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
4330 | _printf("\n[DEBUG FAT] _fat_load_no_cache() : P[%d,%d,%d] enters for file <%s>\n", |
---|
4331 | x , y , p , pathname ); |
---|
4332 | #endif |
---|
4333 | |
---|
4334 | // get file size, and cluster index in FAT |
---|
4335 | if ( _file_info_no_cache( pathname, |
---|
4336 | &cluster, |
---|
4337 | &file_size ) ) |
---|
4338 | { |
---|
4339 | _printf("\n[FAT ERROR] in _fat_load_no_cache() : file <%s> not found\n", |
---|
4340 | pathname ); |
---|
4341 | return -2; |
---|
4342 | } |
---|
4343 | |
---|
4344 | // check buffer size |
---|
4345 | if ( file_size > buffer_size ) |
---|
4346 | { |
---|
4347 | _printf("\n[FAT ERROR] in _fat_load_no_cache() : buffer too small : " |
---|
4348 | "file_size = %x / buffer_size = %x", file_size , buffer_size ); |
---|
4349 | return -3; |
---|
4350 | } |
---|
4351 | |
---|
4352 | // compute total number of clusters to read |
---|
4353 | unsigned int nb_clusters = file_size >> 12; |
---|
4354 | if ( file_size & 0xFFF ) nb_clusters++; |
---|
4355 | |
---|
4356 | // initialise buffer address |
---|
4357 | unsigned int dst = buffer_vbase; |
---|
4358 | |
---|
4359 | // loop on the clusters containing the file |
---|
4360 | while ( nb_clusters > 0 ) |
---|
4361 | { |
---|
4362 | unsigned int lba = _cluster_to_lba( cluster ); |
---|
4363 | |
---|
4364 | if( _fat_ioc_access( 0, // no descheduling |
---|
4365 | 1, // read |
---|
4366 | lba, |
---|
4367 | dst, |
---|
4368 | 8 ) ) // 8 blocks |
---|
4369 | { |
---|
4370 | _printf("\n[FAT ERROR] in _fat_no _cache_read() : cannot load lba %x", lba ); |
---|
4371 | return -4; |
---|
4372 | } |
---|
4373 | |
---|
4374 | |
---|
4375 | // compute next cluster index |
---|
4376 | unsigned int next; |
---|
4377 | if ( _next_cluster_no_cache( cluster , &next ) ) |
---|
4378 | { |
---|
4379 | _printf("\n[FAT ERROR] in _fat_no _cache_read() : cannot get next cluster " |
---|
4380 | " for cluster = %x\n", cluster ); |
---|
4381 | return -5; |
---|
4382 | } |
---|
4383 | |
---|
4384 | // update variables for next iteration |
---|
4385 | nb_clusters = nb_clusters - 1; |
---|
4386 | dst = dst + 4096; |
---|
4387 | cluster = next; |
---|
4388 | } |
---|
4389 | |
---|
4390 | #if GIET_DEBUG_FAT |
---|
4391 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
4392 | _printf("\n[DEBUG FAT] _fat_load_no_cache() : P[%d,%d,%d] loaded <%s> at vaddr = %x" |
---|
4393 | " / size = %x\n", x , y , p , pathname , buffer_vbase , file_size ); |
---|
4394 | #endif |
---|
4395 | |
---|
4396 | return 0; |
---|
4397 | } // end _fat_load_no_cache() |
---|
4398 | |
---|
4399 | |
---|
4400 | |
---|
4401 | // Local Variables: |
---|
4402 | // tab-width: 4 |
---|
4403 | // c-basic-offset: 4 |
---|
4404 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
4405 | // indent-tabs-mode: nil |
---|
4406 | // End: |
---|
4407 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
4408 | |
---|