Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

why is it erasing blocks on every string write ? #1012

Open
vinodsdewa opened this issue Jul 30, 2024 · 3 comments
Open

why is it erasing blocks on every string write ? #1012

vinodsdewa opened this issue Jul 30, 2024 · 3 comments

Comments

@vinodsdewa
Copy link

vinodsdewa commented Jul 30, 2024

**#include "lfs.h"

lfs_t lfs;
lfs_file_t file;

#define BLOCK_COUNT 10
#define BLOCK_SIZE (1024*8)

uint8_t buff[BLOCK_SIZE * BLOCK_COUNT];

static int lfs_hal_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size)
{
// printf("read from block %d with offset %d with size %d\n", block, off, size);

memcpy(buffer, buff + (BLOCK_SIZE*block) + off, size);
return 0;

}

static int lfs_hal_prog(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size)
{
//printf("write to block %d with offset %d with size %d\n", block, off, size);

memcpy(buff + (BLOCK_SIZE*block) + off, buffer, size);
return 0;

}

static int lfs_hal_erase(const struct lfs_config *c, lfs_block_t block)
{
memset( buff + (BLOCK_SIZE * block), 0xff, BLOCK_SIZE);
printf("erasing block %d\n", block);
return 0;
}

static int lfs_hal_sync(const struct lfs_config *c)
{
printf("sync\n");
return 0;
}

// configuration of the filesystem is provided by this struct
const struct lfs_config cfg = {
// block device operations
.read = lfs_hal_read,
.prog = lfs_hal_prog,
.erase = lfs_hal_erase,
.sync = lfs_hal_sync,

// block device configuration
.read_size = 16,
.prog_size = 16,
.block_size = BLOCK_SIZE,
.block_count = BLOCK_COUNT,
.cache_size = 16,
.lookahead_size = 16,
.block_cycles = 500,

};

void main(void)
{
int err = lfs_mount(&lfs, &cfg);

memset(buff, 0xff, sizeof(buff));
// reformat if we can't mount the filesystem
// this should only happen on the first boot
if (err) {
    lfs_format(&lfs, &cfg);
    lfs_mount(&lfs, &cfg);
}

uint32_t boot_count = 0;
int val  = 0;
char url_read[100];
char url_write[100];

for(int i = 0; i < 10; i++) {

    memset(url_read, 0, sizeof(url_read));
    memset(url_write, 0, sizeof(url_write));

    if(lfs_file_open(&lfs, &file, "MQTT_URL", LFS_O_RDONLY) == 0) {// | LFS_O_CREAT);
        lfs_file_read(&lfs, &file, url_read, 99);
        printf("read data = %s\n", url_read);
        lfs_file_close(&lfs, &file);
    }


    sprintf(url_write, "http://broker.hivemq.com/%d", i);

    lfs_remove(&lfs, "MQTT_URL");
    lfs_file_open(&lfs, &file, "MQTT_URL",  LFS_O_WRONLY| LFS_O_CREAT);
    lfs_file_rewind(&lfs, &file);
    lfs_file_write(&lfs, &file, url_write, strlen(url_write));
    lfs_file_close(&lfs, &file);

}

}**

`F:\PROJECT\scratch\littlefs\lfs.c:1370:error: Corrupted dir pair at {0x0, 0x1}
erasing block 0
sync
erasing block 1
sync
sync
read data =
erasing block 8
sync
sync
read data = http://broker.hivemq.com/0
erasing block 9
sync
sync
read data = http://broker.hivemq.com/1
erasing block 2
sync
sync
read data = http://broker.hivemq.com/2
erasing block 3
sync
sync
read data = http://broker.hivemq.com/3
erasing block 4
sync
sync
read data = http://broker.hivemq.com/4
erasing block 5
sync
sync
read data = http://broker.hivemq.com/5
erasing block 6
sync
sync
read data = http://broker.hivemq.com/6
erasing block 7
sync
sync
read data = http://broker.hivemq.com/7
erasing block 8
sync
sync
read data = http://broker.hivemq.com/8
erasing block 9
sync
sync

Process returned 0 (0x0) execution time : 0.009 s
Press any key to continue.`

Trying to test it on RAM buffer, but even lot of memory is there, it is still erasing each block after writing just a small string!!
If I increase all prog size, read size, cache size etc to 128 or so, it works, but this should not be the expected behaviour and this can damage my flash memory due to this buggy erase behaviour !! Any clue ?

@pedromarinho94
Copy link

I think this is because of the APPEND flag

@vinodsdewa
Copy link
Author

I think this is because of the APPEND flag

Which is the append flag?

@bauerjj
Copy link

bauerjj commented Aug 29, 2024

Trying to test it on RAM buffer, but even lot of memory is there, it is still erasing each block after writing just a small string!!
If I increase all prog size, read size, cache size etc to 128 or so, it works, but this should not be the expected behaviour and this can damage my flash memory due to this buggy erase behaviour !! Any clue ?

I have the same "issue", however this is actually just how littlefs works. littlefs doesn't track what has been erased! See #905 (comment) .

and also #862

Let me know if there is ever a solution where littefs does NOT erase a block after any append operation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants