mirror of
https://github.com/eledio-devices/thirdparty-littlefs.git
synced 2025-11-01 16:14:13 +01:00
Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6b65737715 | ||
|
|
4ebe6030c5 | ||
|
|
7ae8d778f1 | ||
|
|
4d068a154d | ||
|
|
ba088aa213 | ||
|
|
955b296bcc | ||
|
|
241dbc6f86 | ||
|
|
8cca58f1a6 | ||
|
|
97f86af4e9 | ||
|
|
d40302c5e3 | ||
|
|
0b5a78e2cd | ||
|
|
27b6cc829b | ||
|
|
fd204ac2fb | ||
|
|
bd99402d9a | ||
|
|
bce442a86b | ||
|
|
f26e970a0e | ||
|
|
965d29b887 | ||
|
|
f7fd7d966a | ||
|
|
d5aba27d60 | ||
|
|
0c77123eee |
54
DESIGN.md
54
DESIGN.md
@@ -254,7 +254,7 @@ have weaknesses that limit their usefulness. But if we merge the two they can
|
|||||||
mutually solve each other's limitations.
|
mutually solve each other's limitations.
|
||||||
|
|
||||||
This is the idea behind littlefs. At the sub-block level, littlefs is built
|
This is the idea behind littlefs. At the sub-block level, littlefs is built
|
||||||
out of small, two blocks logs that provide atomic updates to metadata anywhere
|
out of small, two block logs that provide atomic updates to metadata anywhere
|
||||||
on the filesystem. At the super-block level, littlefs is a CObW tree of blocks
|
on the filesystem. At the super-block level, littlefs is a CObW tree of blocks
|
||||||
that can be evicted on demand.
|
that can be evicted on demand.
|
||||||
|
|
||||||
@@ -676,7 +676,7 @@ block, this cost is fairly reasonable.
|
|||||||
---
|
---
|
||||||
|
|
||||||
This is a new data structure, so we still have several questions. What is the
|
This is a new data structure, so we still have several questions. What is the
|
||||||
storage overage? Can the number of pointers exceed the size of a block? How do
|
storage overhead? Can the number of pointers exceed the size of a block? How do
|
||||||
we store a CTZ skip-list in our metadata pairs?
|
we store a CTZ skip-list in our metadata pairs?
|
||||||
|
|
||||||
To find the storage overhead, we can look at the data structure as multiple
|
To find the storage overhead, we can look at the data structure as multiple
|
||||||
@@ -742,8 +742,8 @@ where:
|
|||||||
2. popcount(![x]) = the number of bits that are 1 in ![x]
|
2. popcount(![x]) = the number of bits that are 1 in ![x]
|
||||||
|
|
||||||
Initial tests of this surprising property seem to hold. As ![n] approaches
|
Initial tests of this surprising property seem to hold. As ![n] approaches
|
||||||
infinity, we end up with an average overhead of 2 pointers, which matches what
|
infinity, we end up with an average overhead of 2 pointers, which matches our
|
||||||
our assumption from earlier. During iteration, the popcount function seems to
|
assumption from earlier. During iteration, the popcount function seems to
|
||||||
handle deviations from this average. Of course, just to make sure I wrote a
|
handle deviations from this average. Of course, just to make sure I wrote a
|
||||||
quick script that verified this property for all 32-bit integers.
|
quick script that verified this property for all 32-bit integers.
|
||||||
|
|
||||||
@@ -767,7 +767,7 @@ overflow, but we can avoid this by rearranging the equation a bit:
|
|||||||
|
|
||||||
![off = N - (B-2w/8)n - (w/8)popcount(n)][ctz-formula7]
|
![off = N - (B-2w/8)n - (w/8)popcount(n)][ctz-formula7]
|
||||||
|
|
||||||
Our solution requires quite a bit of math, but computer are very good at math.
|
Our solution requires quite a bit of math, but computers are very good at math.
|
||||||
Now we can find both our block index and offset from a size in _O(1)_, letting
|
Now we can find both our block index and offset from a size in _O(1)_, letting
|
||||||
us store CTZ skip-lists with only a pointer and size.
|
us store CTZ skip-lists with only a pointer and size.
|
||||||
|
|
||||||
@@ -850,7 +850,7 @@ nearly every write to the filesystem.
|
|||||||
|
|
||||||
Normally, block allocation involves some sort of free list or bitmap stored on
|
Normally, block allocation involves some sort of free list or bitmap stored on
|
||||||
the filesystem that is updated with free blocks. However, with power
|
the filesystem that is updated with free blocks. However, with power
|
||||||
resilience, keeping these structure consistent becomes difficult. It doesn't
|
resilience, keeping these structures consistent becomes difficult. It doesn't
|
||||||
help that any mistake in updating these structures can result in lost blocks
|
help that any mistake in updating these structures can result in lost blocks
|
||||||
that are impossible to recover.
|
that are impossible to recover.
|
||||||
|
|
||||||
@@ -894,9 +894,9 @@ high-risk error conditions.
|
|||||||
---
|
---
|
||||||
|
|
||||||
Our block allocator needs to find free blocks efficiently. You could traverse
|
Our block allocator needs to find free blocks efficiently. You could traverse
|
||||||
through every block on storage and check each one against our filesystem tree,
|
through every block on storage and check each one against our filesystem tree;
|
||||||
however the runtime would be abhorrent. We need to somehow collect multiple
|
however, the runtime would be abhorrent. We need to somehow collect multiple
|
||||||
blocks each traversal.
|
blocks per traversal.
|
||||||
|
|
||||||
Looking at existing designs, some larger filesystems that use a similar "drop
|
Looking at existing designs, some larger filesystems that use a similar "drop
|
||||||
it on the floor" strategy store a bitmap of the entire storage in [RAM]. This
|
it on the floor" strategy store a bitmap of the entire storage in [RAM]. This
|
||||||
@@ -920,8 +920,8 @@ a brute force traversal. Instead of a bitmap the size of storage, we keep track
|
|||||||
of a small, fixed-size bitmap called the lookahead buffer. During block
|
of a small, fixed-size bitmap called the lookahead buffer. During block
|
||||||
allocation, we take blocks from the lookahead buffer. If the lookahead buffer
|
allocation, we take blocks from the lookahead buffer. If the lookahead buffer
|
||||||
is empty, we scan the filesystem for more free blocks, populating our lookahead
|
is empty, we scan the filesystem for more free blocks, populating our lookahead
|
||||||
buffer. Each scan we use an increasing offset, circling the storage as blocks
|
buffer. In each scan we use an increasing offset, circling the storage as
|
||||||
are allocated.
|
blocks are allocated.
|
||||||
|
|
||||||
Here's what it might look like to allocate 4 blocks on a decently busy
|
Here's what it might look like to allocate 4 blocks on a decently busy
|
||||||
filesystem with a 32 bit lookahead and a total of 128 blocks (512 KiB
|
filesystem with a 32 bit lookahead and a total of 128 blocks (512 KiB
|
||||||
@@ -950,7 +950,7 @@ alloc = 112 lookahead: ffff8000
|
|||||||
```
|
```
|
||||||
|
|
||||||
This lookahead approach has a runtime complexity of _O(n²)_ to completely
|
This lookahead approach has a runtime complexity of _O(n²)_ to completely
|
||||||
scan storage, however, bitmaps are surprisingly compact, and in practice only
|
scan storage; however, bitmaps are surprisingly compact, and in practice only
|
||||||
one or two passes are usually needed to find free blocks. Additionally, the
|
one or two passes are usually needed to find free blocks. Additionally, the
|
||||||
performance of the allocator can be optimized by adjusting the block size or
|
performance of the allocator can be optimized by adjusting the block size or
|
||||||
size of the lookahead buffer, trading either write granularity or RAM for
|
size of the lookahead buffer, trading either write granularity or RAM for
|
||||||
@@ -1173,9 +1173,9 @@ We may find that the new block is also bad, but hopefully after repeating this
|
|||||||
cycle we'll eventually find a new block where a write succeeds. If we don't,
|
cycle we'll eventually find a new block where a write succeeds. If we don't,
|
||||||
that means that all blocks in our storage are bad, and we've reached the end of
|
that means that all blocks in our storage are bad, and we've reached the end of
|
||||||
our device's usable life. At this point, littlefs will return an "out of space"
|
our device's usable life. At this point, littlefs will return an "out of space"
|
||||||
error, which is technically true, there are no more good blocks, but as an
|
error. This is technically true, as there are no more good blocks, but as an
|
||||||
added benefit also matches the error condition expected by users of dynamically
|
added benefit it also matches the error condition expected by users of
|
||||||
sized data.
|
dynamically sized data.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -1187,7 +1187,7 @@ original data even after it has been corrupted. One such mechanism for this is
|
|||||||
ECC is an extension to the idea of a checksum. Where a checksum such as CRC can
|
ECC is an extension to the idea of a checksum. Where a checksum such as CRC can
|
||||||
detect that an error has occurred in the data, ECC can detect and actually
|
detect that an error has occurred in the data, ECC can detect and actually
|
||||||
correct some amount of errors. However, there is a limit to how many errors ECC
|
correct some amount of errors. However, there is a limit to how many errors ECC
|
||||||
can detect, call the [Hamming bound][wikipedia-hamming-bound]. As the number of
|
can detect: the [Hamming bound][wikipedia-hamming-bound]. As the number of
|
||||||
errors approaches the Hamming bound, we may still be able to detect errors, but
|
errors approaches the Hamming bound, we may still be able to detect errors, but
|
||||||
can no longer fix the data. If we've reached this point the block is
|
can no longer fix the data. If we've reached this point the block is
|
||||||
unrecoverable.
|
unrecoverable.
|
||||||
@@ -1202,7 +1202,7 @@ chip itself.
|
|||||||
In littlefs, ECC is entirely optional. Read errors can instead be prevented
|
In littlefs, ECC is entirely optional. Read errors can instead be prevented
|
||||||
proactively by wear leveling. But it's important to note that ECC can be used
|
proactively by wear leveling. But it's important to note that ECC can be used
|
||||||
at the block device level to modestly extend the life of a device. littlefs
|
at the block device level to modestly extend the life of a device. littlefs
|
||||||
respects any errors reported by the block device, allow a block device to
|
respects any errors reported by the block device, allowing a block device to
|
||||||
provide additional aggressive error detection.
|
provide additional aggressive error detection.
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -1231,7 +1231,7 @@ Generally, wear leveling algorithms fall into one of two categories:
|
|||||||
we need to consider all blocks, including blocks that already contain data.
|
we need to consider all blocks, including blocks that already contain data.
|
||||||
|
|
||||||
As a tradeoff for code size and complexity, littlefs (currently) only provides
|
As a tradeoff for code size and complexity, littlefs (currently) only provides
|
||||||
dynamic wear leveling. This is a best efforts solution. Wear is not distributed
|
dynamic wear leveling. This is a best effort solution. Wear is not distributed
|
||||||
perfectly, but it is distributed among the free blocks and greatly extends the
|
perfectly, but it is distributed among the free blocks and greatly extends the
|
||||||
life of a device.
|
life of a device.
|
||||||
|
|
||||||
@@ -1378,7 +1378,7 @@ We can make several improvements. First, instead of giving each file its own
|
|||||||
metadata pair, we can store multiple files in a single metadata pair. One way
|
metadata pair, we can store multiple files in a single metadata pair. One way
|
||||||
to do this is to directly associate a directory with a metadata pair (or a
|
to do this is to directly associate a directory with a metadata pair (or a
|
||||||
linked list of metadata pairs). This makes it easy for multiple files to share
|
linked list of metadata pairs). This makes it easy for multiple files to share
|
||||||
the directory's metadata pair for logging and reduce the collective storage
|
the directory's metadata pair for logging and reduces the collective storage
|
||||||
overhead.
|
overhead.
|
||||||
|
|
||||||
The strict binding of metadata pairs and directories also gives users
|
The strict binding of metadata pairs and directories also gives users
|
||||||
@@ -1816,12 +1816,12 @@ while manipulating the directory tree (foreshadowing!).
|
|||||||
|
|
||||||
## The move problem
|
## The move problem
|
||||||
|
|
||||||
We have one last challenge. The move problem. Phrasing the problem is simple:
|
We have one last challenge: the move problem. Phrasing the problem is simple:
|
||||||
|
|
||||||
How do you atomically move a file between two directories?
|
How do you atomically move a file between two directories?
|
||||||
|
|
||||||
In littlefs we can atomically commit to directories, but we can't create
|
In littlefs we can atomically commit to directories, but we can't create
|
||||||
an atomic commit that span multiple directories. The filesystem must go
|
an atomic commit that spans multiple directories. The filesystem must go
|
||||||
through a minimum of two distinct states to complete a move.
|
through a minimum of two distinct states to complete a move.
|
||||||
|
|
||||||
To make matters worse, file moves are a common form of synchronization for
|
To make matters worse, file moves are a common form of synchronization for
|
||||||
@@ -1831,13 +1831,13 @@ atomic moves right.
|
|||||||
So what can we do?
|
So what can we do?
|
||||||
|
|
||||||
- We definitely can't just let power-loss result in duplicated or lost files.
|
- We definitely can't just let power-loss result in duplicated or lost files.
|
||||||
This could easily break user's code and would only reveal itself in extreme
|
This could easily break users' code and would only reveal itself in extreme
|
||||||
cases. We were only able to be lazy about the threaded linked-list because
|
cases. We were only able to be lazy about the threaded linked-list because
|
||||||
it isn't user facing and we can handle the corner cases internally.
|
it isn't user facing and we can handle the corner cases internally.
|
||||||
|
|
||||||
- Some filesystems propagate COW operations up the tree until finding a common
|
- Some filesystems propagate COW operations up the tree until a common parent
|
||||||
parent. Unfortunately this interacts poorly with our threaded tree and brings
|
is found. Unfortunately this interacts poorly with our threaded tree and
|
||||||
back the issue of upward propagation of wear.
|
brings back the issue of upward propagation of wear.
|
||||||
|
|
||||||
- In a previous version of littlefs we tried to solve this problem by going
|
- In a previous version of littlefs we tried to solve this problem by going
|
||||||
back and forth between the source and destination, marking and unmarking the
|
back and forth between the source and destination, marking and unmarking the
|
||||||
@@ -1852,7 +1852,7 @@ introduction of a mechanism called "global state".
|
|||||||
---
|
---
|
||||||
|
|
||||||
Global state is a small set of state that can be updated from _any_ metadata
|
Global state is a small set of state that can be updated from _any_ metadata
|
||||||
pair. Combining global state with metadata pair's ability to update multiple
|
pair. Combining global state with metadata pairs' ability to update multiple
|
||||||
entries in one commit gives us a powerful tool for crafting complex atomic
|
entries in one commit gives us a powerful tool for crafting complex atomic
|
||||||
operations.
|
operations.
|
||||||
|
|
||||||
@@ -1910,7 +1910,7 @@ the filesystem is mounted.
|
|||||||
|
|
||||||
You may have noticed that global state is very expensive. We keep a copy in
|
You may have noticed that global state is very expensive. We keep a copy in
|
||||||
RAM and a delta in an unbounded number of metadata pairs. Even if we reset
|
RAM and a delta in an unbounded number of metadata pairs. Even if we reset
|
||||||
the global state to its initial value we can't easily clean up the deltas on
|
the global state to its initial value, we can't easily clean up the deltas on
|
||||||
disk. For this reason, it's very important that we keep the size of global
|
disk. For this reason, it's very important that we keep the size of global
|
||||||
state bounded and extremely small. But, even with a strict budget, global
|
state bounded and extremely small. But, even with a strict budget, global
|
||||||
state is incredibly valuable.
|
state is incredibly valuable.
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ const struct lfs_config cfg = {
|
|||||||
.block_count = 128,
|
.block_count = 128,
|
||||||
.cache_size = 16,
|
.cache_size = 16,
|
||||||
.lookahead_size = 16,
|
.lookahead_size = 16,
|
||||||
|
.block_cycles = 500,
|
||||||
};
|
};
|
||||||
|
|
||||||
// entry point
|
// entry point
|
||||||
|
|||||||
@@ -102,6 +102,7 @@ int lfs_emubd_create(const struct lfs_config *cfg, const char *path) {
|
|||||||
if (res < 1) {
|
if (res < 1) {
|
||||||
err = -errno;
|
err = -errno;
|
||||||
LFS_TRACE("lfs_emubd_create -> %"PRId32, err);
|
LFS_TRACE("lfs_emubd_create -> %"PRId32, err);
|
||||||
|
fclose(f);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,6 +125,7 @@ int lfs_emubd_create(const struct lfs_config *cfg, const char *path) {
|
|||||||
if (res < 1) {
|
if (res < 1) {
|
||||||
err = -errno;
|
err = -errno;
|
||||||
LFS_TRACE("lfs_emubd_create -> %"PRId32, err);
|
LFS_TRACE("lfs_emubd_create -> %"PRId32, err);
|
||||||
|
fclose(f);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -178,6 +180,7 @@ int lfs_emubd_read(const struct lfs_config *cfg, lfs_block_t block,
|
|||||||
if (err) {
|
if (err) {
|
||||||
err = -errno;
|
err = -errno;
|
||||||
LFS_TRACE("lfs_emubd_read -> %d", err);
|
LFS_TRACE("lfs_emubd_read -> %d", err);
|
||||||
|
fclose(f);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,6 +188,7 @@ int lfs_emubd_read(const struct lfs_config *cfg, lfs_block_t block,
|
|||||||
if (res < size && !feof(f)) {
|
if (res < size && !feof(f)) {
|
||||||
err = -errno;
|
err = -errno;
|
||||||
LFS_TRACE("lfs_emubd_read -> %d", err);
|
LFS_TRACE("lfs_emubd_read -> %d", err);
|
||||||
|
fclose(f);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -230,6 +234,7 @@ int lfs_emubd_prog(const struct lfs_config *cfg, lfs_block_t block,
|
|||||||
if (err) {
|
if (err) {
|
||||||
err = -errno;
|
err = -errno;
|
||||||
LFS_TRACE("lfs_emubd_prog -> %d", err);
|
LFS_TRACE("lfs_emubd_prog -> %d", err);
|
||||||
|
fclose(f);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,6 +242,7 @@ int lfs_emubd_prog(const struct lfs_config *cfg, lfs_block_t block,
|
|||||||
if (res < size) {
|
if (res < size) {
|
||||||
err = -errno;
|
err = -errno;
|
||||||
LFS_TRACE("lfs_emubd_prog -> %d", err);
|
LFS_TRACE("lfs_emubd_prog -> %d", err);
|
||||||
|
fclose(f);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -244,6 +250,7 @@ int lfs_emubd_prog(const struct lfs_config *cfg, lfs_block_t block,
|
|||||||
if (err) {
|
if (err) {
|
||||||
err = -errno;
|
err = -errno;
|
||||||
LFS_TRACE("lfs_emubd_prog -> %d", err);
|
LFS_TRACE("lfs_emubd_prog -> %d", err);
|
||||||
|
fclose(f);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -252,6 +259,7 @@ int lfs_emubd_prog(const struct lfs_config *cfg, lfs_block_t block,
|
|||||||
if (res < 1) {
|
if (res < 1) {
|
||||||
err = -errno;
|
err = -errno;
|
||||||
LFS_TRACE("lfs_emubd_prog -> %d", err);
|
LFS_TRACE("lfs_emubd_prog -> %d", err);
|
||||||
|
fclose(f);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -340,6 +348,7 @@ int lfs_emubd_sync(const struct lfs_config *cfg) {
|
|||||||
if (res < 1) {
|
if (res < 1) {
|
||||||
int err = -errno;
|
int err = -errno;
|
||||||
LFS_TRACE("lfs_emubd_sync -> %d", err);
|
LFS_TRACE("lfs_emubd_sync -> %d", err);
|
||||||
|
fclose(f);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -364,6 +373,7 @@ int lfs_emubd_sync(const struct lfs_config *cfg) {
|
|||||||
if (res < 1) {
|
if (res < 1) {
|
||||||
err = -errno;
|
err = -errno;
|
||||||
LFS_TRACE("lfs_emubd_sync -> %d", err);
|
LFS_TRACE("lfs_emubd_sync -> %d", err);
|
||||||
|
fclose(f);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -388,6 +398,7 @@ int lfs_emubd_sync(const struct lfs_config *cfg) {
|
|||||||
if (res < 1) {
|
if (res < 1) {
|
||||||
err = -errno;
|
err = -errno;
|
||||||
LFS_TRACE("lfs_emubd_sync -> %d", err);
|
LFS_TRACE("lfs_emubd_sync -> %d", err);
|
||||||
|
fclose(f);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
52
lfs.c
52
lfs.c
@@ -352,7 +352,7 @@ static inline bool lfs_gstate_hasmovehere(const struct lfs_gstate *a,
|
|||||||
|
|
||||||
static inline void lfs_gstate_xororphans(struct lfs_gstate *a,
|
static inline void lfs_gstate_xororphans(struct lfs_gstate *a,
|
||||||
const struct lfs_gstate *b, bool orphans) {
|
const struct lfs_gstate *b, bool orphans) {
|
||||||
a->tag ^= LFS_MKTAG(0x800, 0, 0) & (b->tag ^ (orphans << 31));
|
a->tag ^= LFS_MKTAG(0x800, 0, 0) & (b->tag ^ ((uint32_t)orphans << 31));
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void lfs_gstate_xormove(struct lfs_gstate *a,
|
static inline void lfs_gstate_xormove(struct lfs_gstate *a,
|
||||||
@@ -846,7 +846,7 @@ static lfs_stag_t lfs_dir_fetchmatch(lfs_t *lfs,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// reset the next bit if we need to
|
// reset the next bit if we need to
|
||||||
ptag ^= (lfs_tag_chunk(tag) & 1U) << 31;
|
ptag ^= (lfs_tag_t)(lfs_tag_chunk(tag) & 1U) << 31;
|
||||||
|
|
||||||
// toss our crc into the filesystem seed for
|
// toss our crc into the filesystem seed for
|
||||||
// pseudorandom numbers
|
// pseudorandom numbers
|
||||||
@@ -977,7 +977,7 @@ static int lfs_dir_fetch(lfs_t *lfs,
|
|||||||
lfs_mdir_t *dir, const lfs_block_t pair[2]) {
|
lfs_mdir_t *dir, const lfs_block_t pair[2]) {
|
||||||
// note, mask=-1, tag=0 can never match a tag since this
|
// note, mask=-1, tag=0 can never match a tag since this
|
||||||
// pattern has the invalid bit set
|
// pattern has the invalid bit set
|
||||||
return lfs_dir_fetchmatch(lfs, dir, pair, -1, 0, NULL, NULL, NULL);
|
return (int)lfs_dir_fetchmatch(lfs, dir, pair, -1, 0, NULL, NULL, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lfs_dir_getgstate(lfs_t *lfs, const lfs_mdir_t *dir,
|
static int lfs_dir_getgstate(lfs_t *lfs, const lfs_mdir_t *dir,
|
||||||
@@ -1010,7 +1010,7 @@ static int lfs_dir_getinfo(lfs_t *lfs, lfs_mdir_t *dir,
|
|||||||
lfs_stag_t tag = lfs_dir_get(lfs, dir, LFS_MKTAG(0x780, 0x3ff, 0),
|
lfs_stag_t tag = lfs_dir_get(lfs, dir, LFS_MKTAG(0x780, 0x3ff, 0),
|
||||||
LFS_MKTAG(LFS_TYPE_NAME, id, lfs->name_max+1), info->name);
|
LFS_MKTAG(LFS_TYPE_NAME, id, lfs->name_max+1), info->name);
|
||||||
if (tag < 0) {
|
if (tag < 0) {
|
||||||
return tag;
|
return (int)tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
info->type = lfs_tag_type3(tag);
|
info->type = lfs_tag_type3(tag);
|
||||||
@@ -1019,7 +1019,7 @@ static int lfs_dir_getinfo(lfs_t *lfs, lfs_mdir_t *dir,
|
|||||||
tag = lfs_dir_get(lfs, dir, LFS_MKTAG(0x700, 0x3ff, 0),
|
tag = lfs_dir_get(lfs, dir, LFS_MKTAG(0x700, 0x3ff, 0),
|
||||||
LFS_MKTAG(LFS_TYPE_STRUCT, id, sizeof(ctz)), &ctz);
|
LFS_MKTAG(LFS_TYPE_STRUCT, id, sizeof(ctz)), &ctz);
|
||||||
if (tag < 0) {
|
if (tag < 0) {
|
||||||
return tag;
|
return (int)tag;
|
||||||
}
|
}
|
||||||
lfs_ctz_fromle32(&ctz);
|
lfs_ctz_fromle32(&ctz);
|
||||||
|
|
||||||
@@ -1062,7 +1062,7 @@ static int lfs_dir_find_match(void *data,
|
|||||||
return LFS_CMP_EQ;
|
return LFS_CMP_EQ;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lfs_dir_find(lfs_t *lfs, lfs_mdir_t *dir,
|
static lfs_stag_t lfs_dir_find(lfs_t *lfs, lfs_mdir_t *dir,
|
||||||
const char **path, uint16_t *id) {
|
const char **path, uint16_t *id) {
|
||||||
// we reduce path to a single name if we can find it
|
// we reduce path to a single name if we can find it
|
||||||
const char *name = *path;
|
const char *name = *path;
|
||||||
@@ -1275,7 +1275,7 @@ static int lfs_dir_commitcrc(lfs_t *lfs, struct lfs_commit *commit) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
commit->off += sizeof(tag)+lfs_tag_size(tag);
|
commit->off += sizeof(tag)+lfs_tag_size(tag);
|
||||||
commit->ptag = tag ^ (reset << 31);
|
commit->ptag = tag ^ ((lfs_tag_t)reset << 31);
|
||||||
commit->crc = LFS_BLOCK_NULL; // reset crc for next "commit"
|
commit->crc = LFS_BLOCK_NULL; // reset crc for next "commit"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3006,10 +3006,10 @@ int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) {
|
|||||||
} else if (size > oldsize) {
|
} else if (size > oldsize) {
|
||||||
// flush+seek if not already at end
|
// flush+seek if not already at end
|
||||||
if (file->pos != oldsize) {
|
if (file->pos != oldsize) {
|
||||||
int err = lfs_file_seek(lfs, file, 0, LFS_SEEK_END);
|
lfs_soff_t res = lfs_file_seek(lfs, file, 0, LFS_SEEK_END);
|
||||||
if (err < 0) {
|
if (res < 0) {
|
||||||
LFS_TRACE("lfs_file_truncate -> %d", err);
|
LFS_TRACE("lfs_file_truncate -> %d", res);
|
||||||
return err;
|
return (int)res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3018,16 +3018,16 @@ int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) {
|
|||||||
lfs_ssize_t res = lfs_file_write(lfs, file, &(uint8_t){0}, 1);
|
lfs_ssize_t res = lfs_file_write(lfs, file, &(uint8_t){0}, 1);
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
LFS_TRACE("lfs_file_truncate -> %d", res);
|
LFS_TRACE("lfs_file_truncate -> %d", res);
|
||||||
return res;
|
return (int)res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// restore pos
|
// restore pos
|
||||||
int err = lfs_file_seek(lfs, file, pos, LFS_SEEK_SET);
|
lfs_soff_t res = lfs_file_seek(lfs, file, pos, LFS_SEEK_SET);
|
||||||
if (err < 0) {
|
if (res < 0) {
|
||||||
LFS_TRACE("lfs_file_truncate -> %d", err);
|
LFS_TRACE("lfs_file_truncate -> %d", res);
|
||||||
return err;
|
return (int)res;
|
||||||
}
|
}
|
||||||
|
|
||||||
LFS_TRACE("lfs_file_truncate -> %d", 0);
|
LFS_TRACE("lfs_file_truncate -> %d", 0);
|
||||||
@@ -3047,7 +3047,7 @@ int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file) {
|
|||||||
lfs_soff_t res = lfs_file_seek(lfs, file, 0, LFS_SEEK_SET);
|
lfs_soff_t res = lfs_file_seek(lfs, file, 0, LFS_SEEK_SET);
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
LFS_TRACE("lfs_file_rewind -> %d", res);
|
LFS_TRACE("lfs_file_rewind -> %d", res);
|
||||||
return res;
|
return (int)res;
|
||||||
}
|
}
|
||||||
|
|
||||||
LFS_TRACE("lfs_file_rewind -> %d", 0);
|
LFS_TRACE("lfs_file_rewind -> %d", 0);
|
||||||
@@ -3076,7 +3076,7 @@ int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) {
|
|||||||
lfs_stag_t tag = lfs_dir_find(lfs, &cwd, &path, NULL);
|
lfs_stag_t tag = lfs_dir_find(lfs, &cwd, &path, NULL);
|
||||||
if (tag < 0) {
|
if (tag < 0) {
|
||||||
LFS_TRACE("lfs_stat -> %d", tag);
|
LFS_TRACE("lfs_stat -> %d", tag);
|
||||||
return tag;
|
return (int)tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
int err = lfs_dir_getinfo(lfs, &cwd, lfs_tag_id(tag), info);
|
int err = lfs_dir_getinfo(lfs, &cwd, lfs_tag_id(tag), info);
|
||||||
@@ -3097,7 +3097,7 @@ int lfs_remove(lfs_t *lfs, const char *path) {
|
|||||||
lfs_stag_t tag = lfs_dir_find(lfs, &cwd, &path, NULL);
|
lfs_stag_t tag = lfs_dir_find(lfs, &cwd, &path, NULL);
|
||||||
if (tag < 0 || lfs_tag_id(tag) == 0x3ff) {
|
if (tag < 0 || lfs_tag_id(tag) == 0x3ff) {
|
||||||
LFS_TRACE("lfs_remove -> %d", (tag < 0) ? tag : LFS_ERR_INVAL);
|
LFS_TRACE("lfs_remove -> %d", (tag < 0) ? tag : LFS_ERR_INVAL);
|
||||||
return (tag < 0) ? tag : LFS_ERR_INVAL;
|
return (tag < 0) ? (int)tag : LFS_ERR_INVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
lfs_mdir_t dir;
|
lfs_mdir_t dir;
|
||||||
@@ -3108,7 +3108,7 @@ int lfs_remove(lfs_t *lfs, const char *path) {
|
|||||||
LFS_MKTAG(LFS_TYPE_STRUCT, lfs_tag_id(tag), 8), pair);
|
LFS_MKTAG(LFS_TYPE_STRUCT, lfs_tag_id(tag), 8), pair);
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
LFS_TRACE("lfs_remove -> %d", res);
|
LFS_TRACE("lfs_remove -> %d", res);
|
||||||
return res;
|
return (int)res;
|
||||||
}
|
}
|
||||||
lfs_pair_fromle32(pair);
|
lfs_pair_fromle32(pair);
|
||||||
|
|
||||||
@@ -3171,7 +3171,7 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
|
|||||||
lfs_stag_t oldtag = lfs_dir_find(lfs, &oldcwd, &oldpath, NULL);
|
lfs_stag_t oldtag = lfs_dir_find(lfs, &oldcwd, &oldpath, NULL);
|
||||||
if (oldtag < 0 || lfs_tag_id(oldtag) == 0x3ff) {
|
if (oldtag < 0 || lfs_tag_id(oldtag) == 0x3ff) {
|
||||||
LFS_TRACE("lfs_rename -> %d", (oldtag < 0) ? oldtag : LFS_ERR_INVAL);
|
LFS_TRACE("lfs_rename -> %d", (oldtag < 0) ? oldtag : LFS_ERR_INVAL);
|
||||||
return (oldtag < 0) ? oldtag : LFS_ERR_INVAL;
|
return (oldtag < 0) ? (int)oldtag : LFS_ERR_INVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// find new entry
|
// find new entry
|
||||||
@@ -3181,7 +3181,7 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
|
|||||||
if ((prevtag < 0 || lfs_tag_id(prevtag) == 0x3ff) &&
|
if ((prevtag < 0 || lfs_tag_id(prevtag) == 0x3ff) &&
|
||||||
!(prevtag == LFS_ERR_NOENT && newid != 0x3ff)) {
|
!(prevtag == LFS_ERR_NOENT && newid != 0x3ff)) {
|
||||||
LFS_TRACE("lfs_rename -> %d", (prevtag < 0) ? prevtag : LFS_ERR_INVAL);
|
LFS_TRACE("lfs_rename -> %d", (prevtag < 0) ? prevtag : LFS_ERR_INVAL);
|
||||||
return (prevtag < 0) ? prevtag : LFS_ERR_INVAL;
|
return (prevtag < 0) ? (int)prevtag : LFS_ERR_INVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
lfs_mdir_t prevdir;
|
lfs_mdir_t prevdir;
|
||||||
@@ -3202,7 +3202,7 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
|
|||||||
LFS_MKTAG(LFS_TYPE_STRUCT, newid, 8), prevpair);
|
LFS_MKTAG(LFS_TYPE_STRUCT, newid, 8), prevpair);
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
LFS_TRACE("lfs_rename -> %d", res);
|
LFS_TRACE("lfs_rename -> %d", res);
|
||||||
return res;
|
return (int)res;
|
||||||
}
|
}
|
||||||
lfs_pair_fromle32(prevpair);
|
lfs_pair_fromle32(prevpair);
|
||||||
|
|
||||||
@@ -3369,6 +3369,12 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
|
|||||||
lfs->cfg = cfg;
|
lfs->cfg = cfg;
|
||||||
int err = 0;
|
int err = 0;
|
||||||
|
|
||||||
|
// validate that the lfs-cfg sizes were initiated properly before
|
||||||
|
// performing any arithmetic logics with them
|
||||||
|
LFS_ASSERT(lfs->cfg->read_size != 0);
|
||||||
|
LFS_ASSERT(lfs->cfg->prog_size != 0);
|
||||||
|
LFS_ASSERT(lfs->cfg->cache_size != 0);
|
||||||
|
|
||||||
// check that block size is a multiple of cache size is a multiple
|
// check that block size is a multiple of cache size is a multiple
|
||||||
// of prog and read sizes
|
// of prog and read sizes
|
||||||
LFS_ASSERT(lfs->cfg->cache_size % lfs->cfg->read_size == 0);
|
LFS_ASSERT(lfs->cfg->cache_size % lfs->cfg->read_size == 0);
|
||||||
|
|||||||
@@ -179,7 +179,7 @@ echo "--- Really big path test ---"
|
|||||||
scripts/test.py << TEST
|
scripts/test.py << TEST
|
||||||
lfs_mount(&lfs, &cfg) => 0;
|
lfs_mount(&lfs, &cfg) => 0;
|
||||||
memset(path, 'w', LFS_NAME_MAX);
|
memset(path, 'w', LFS_NAME_MAX);
|
||||||
path[LFS_NAME_MAX+1] = '\0';
|
path[LFS_NAME_MAX] = '\0';
|
||||||
lfs_mkdir(&lfs, path) => 0;
|
lfs_mkdir(&lfs, path) => 0;
|
||||||
lfs_remove(&lfs, path) => 0;
|
lfs_remove(&lfs, path) => 0;
|
||||||
lfs_file_open(&lfs, &file, path,
|
lfs_file_open(&lfs, &file, path,
|
||||||
@@ -189,7 +189,7 @@ scripts/test.py << TEST
|
|||||||
|
|
||||||
memcpy(path, "coffee/", strlen("coffee/"));
|
memcpy(path, "coffee/", strlen("coffee/"));
|
||||||
memset(path+strlen("coffee/"), 'w', LFS_NAME_MAX);
|
memset(path+strlen("coffee/"), 'w', LFS_NAME_MAX);
|
||||||
path[strlen("coffee/")+LFS_NAME_MAX+1] = '\0';
|
path[strlen("coffee/")+LFS_NAME_MAX] = '\0';
|
||||||
lfs_mkdir(&lfs, path) => 0;
|
lfs_mkdir(&lfs, path) => 0;
|
||||||
lfs_remove(&lfs, path) => 0;
|
lfs_remove(&lfs, path) => 0;
|
||||||
lfs_file_open(&lfs, &file, path,
|
lfs_file_open(&lfs, &file, path,
|
||||||
|
|||||||
Reference in New Issue
Block a user