Hello,
I use the RL-FlashFS on a Dataflash and the tftp-server. When I put a new file via tftp to my dataflash which has the same filename as a already existing file, the existing file is replaced with the new one. That is correct. But the filesystem use to much memory. For example: - the old file has a size of 1kB - the new file has a size of 1kB - before puting the new file to the dataflash, there are 2kB memory free - after the filetransfer, there are only 1kB free In my opinion, the new file must replace the old one, and the free memory should be 2kB.
Must I delete the old file before putting the new one to the dataflash. Perhaps I must extend the "tftp_fopen" function in TFTP_uif.c with "fdelete(<new-file-name>)" before opening the new file for write-access?
Has someone else the same problem?
Thanks
I haven't looked into the Keil flash file system implementation, so I have to keep my answer very general.
You do realize that a flash file system does not work in the same way as a disk file system? A flash memory chip gets it's name from the fact that you can erase a full sector in constant time (flash erase) - in difference to normal EEPROM memory where you have to spend time with each individual byte.
When erasing a flash memory, you can not erase smaller blocks that one sector. If you store multiple small files into a single memory sector, you can not erase one of these files individually. You must make a copy of all files on the sector to RAM or another flash sector, then erase the sector and optionally restore the files that should survive. Obviously, that is a bit of a simplification, and if incorrectly done will risk loosing all sector data in case of an unexpected reboot. A too simple algorithm will also give uneven wear on the flash memory.
Anyway, a flash file system does not normally erase data immediately. You normally wait either until you have run out of free space, or until you have enough scattered data that it is meaningful to perform a "garbage collect" and move data from two or more sectors into a single - just erased - sector.
So how do you quick-erase a file then, if you can't erase the full sector? You have a large number of marker bits in the file system. You write a single bit in a directory entry to tell that the directory entry is unused and may be later garbage-collected. In the same way you write single bits in the data area to tell that the space consumed by the old file may later be garbage-collected. These marker bits also allows the file system to switch immediately between two states. Write all info for the new file, while the file system shows the old as existing. When the new file gets closed, the FS code can perform a single write in the data structures to "switch" from the old file to the new. A power-loss in-between will leave the file system with the old file, while the space for the new - but not accessible - file can be later reclaimed during a garbage collect.
I don't know exactly how good the Keil FS is, but whatever features it has, it is still limited by the physical characteristics of flash memory chips. A FS that immediately releases "disk space" kills your flash memory!