mirror of
https://github.com/eledio-devices/thirdparty-littlefs.git
synced 2025-11-01 16:14:13 +01:00
Compare commits
2 Commits
sanity-tes
...
big-endian
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ebc0d24211 | ||
|
|
dbce53672b |
9
.gitignore
vendored
9
.gitignore
vendored
@@ -1,9 +0,0 @@
|
||||
# Compilation output
|
||||
*.o
|
||||
*.d
|
||||
*.a
|
||||
|
||||
# Testing things
|
||||
blocks/
|
||||
lfs
|
||||
test.c
|
||||
239
.travis.yml
239
.travis.yml
@@ -1,224 +1,47 @@
|
||||
# Environment variables
|
||||
env:
|
||||
global:
|
||||
- CFLAGS=-Werror
|
||||
|
||||
# Common test script
|
||||
script:
|
||||
# make sure example can at least compile
|
||||
- sed -n '/``` c/,/```/{/```/d; p;}' README.md > test.c &&
|
||||
make all CFLAGS+="
|
||||
# make sure example can at least compile
|
||||
- sed -n '/``` c/,/```/{/```/d; p;}' README.md > test.c &&
|
||||
CFLAGS='
|
||||
-Duser_provided_block_device_read=NULL
|
||||
-Duser_provided_block_device_prog=NULL
|
||||
-Duser_provided_block_device_erase=NULL
|
||||
-Duser_provided_block_device_sync=NULL
|
||||
-include stdio.h"
|
||||
-include stdio.h -Werror' make all size
|
||||
|
||||
# run tests
|
||||
- make test QUIET=1
|
||||
# run tests
|
||||
- make test QUIET=1
|
||||
|
||||
# run tests with a few different configurations
|
||||
- make test QUIET=1 CFLAGS+="-DLFS_READ_SIZE=1 -DLFS_PROG_SIZE=1"
|
||||
- make test QUIET=1 CFLAGS+="-DLFS_READ_SIZE=512 -DLFS_PROG_SIZE=512"
|
||||
- make test QUIET=1 CFLAGS+="-DLFS_BLOCK_COUNT=1023 -DLFS_LOOKAHEAD=2048"
|
||||
|
||||
- make clean test QUIET=1 CFLAGS+="-DLFS_NO_INTRINSICS"
|
||||
|
||||
# compile and find the code size with the smallest configuration
|
||||
- make clean size
|
||||
OBJ="$(ls lfs*.o | tr '\n' ' ')"
|
||||
CFLAGS+="-DLFS_NO_ASSERT -DLFS_NO_DEBUG -DLFS_NO_WARN -DLFS_NO_ERROR"
|
||||
| tee sizes
|
||||
|
||||
# update status if we succeeded, compare with master if possible
|
||||
- |
|
||||
if [ "$TRAVIS_TEST_RESULT" -eq 0 ]
|
||||
then
|
||||
CURR=$(tail -n1 sizes | awk '{print $1}')
|
||||
PREV=$(curl -u $GEKY_BOT_STATUSES https://api.github.com/repos/$TRAVIS_REPO_SLUG/status/master \
|
||||
| jq -re "select(.sha != \"$TRAVIS_COMMIT\")
|
||||
| .statuses[] | select(.context == \"$STAGE/$NAME\").description
|
||||
| capture(\"code size is (?<size>[0-9]+)\").size" \
|
||||
|| echo 0)
|
||||
|
||||
STATUS="Passed, code size is ${CURR}B"
|
||||
if [ "$PREV" -ne 0 ]
|
||||
then
|
||||
STATUS="$STATUS ($(python -c "print '%+.2f' % (100*($CURR-$PREV)/$PREV.0)")%)"
|
||||
fi
|
||||
fi
|
||||
|
||||
# CI matrix
|
||||
jobs:
|
||||
include:
|
||||
# native testing
|
||||
- stage: test
|
||||
env:
|
||||
- STAGE=test
|
||||
- NAME=littlefs-x86
|
||||
|
||||
# cross-compile with ARM (thumb mode)
|
||||
- stage: test
|
||||
env:
|
||||
- STAGE=test
|
||||
- NAME=littlefs-arm
|
||||
- CC="arm-linux-gnueabi-gcc --static -mthumb"
|
||||
- EXEC="qemu-arm"
|
||||
install:
|
||||
- sudo apt-get install gcc-arm-linux-gnueabi qemu-user
|
||||
- arm-linux-gnueabi-gcc --version
|
||||
- qemu-arm -version
|
||||
|
||||
# cross-compile with PowerPC
|
||||
- stage: test
|
||||
env:
|
||||
- STAGE=test
|
||||
- NAME=littlefs-powerpc
|
||||
- CC="powerpc-linux-gnu-gcc --static"
|
||||
- EXEC="qemu-ppc"
|
||||
install:
|
||||
- sudo apt-get install gcc-powerpc-linux-gnu qemu-user
|
||||
- powerpc-linux-gnu-gcc --version
|
||||
- qemu-ppc -version
|
||||
|
||||
# cross-compile with MIPS
|
||||
- stage: test
|
||||
env:
|
||||
- STAGE=test
|
||||
- NAME=littlefs-mips
|
||||
- CC="mips-linux-gnu-gcc --static"
|
||||
- EXEC="qemu-mips"
|
||||
install:
|
||||
- sudo add-apt-repository -y "deb http://archive.ubuntu.com/ubuntu/ xenial main universe"
|
||||
- sudo apt-get -qq update
|
||||
- sudo apt-get install gcc-mips-linux-gnu qemu-user
|
||||
- mips-linux-gnu-gcc --version
|
||||
- qemu-mips -version
|
||||
# run tests with a few different configurations
|
||||
- CFLAGS="-DLFS_READ_SIZE=1 -DLFS_PROG_SIZE=1" make test QUIET=1
|
||||
- CFLAGS="-DLFS_READ_SIZE=512 -DLFS_PROG_SIZE=512" make test QUIET=1
|
||||
- CFLAGS="-DLFS_BLOCK_COUNT=1023 -DLFS_LOOKAHEAD=2048" make test QUIET=1
|
||||
|
||||
# self-host with littlefs-fuse for fuzz test
|
||||
- stage: test
|
||||
env:
|
||||
- STAGE=test
|
||||
- NAME=littlefs-fuse
|
||||
install:
|
||||
- sudo apt-get install libfuse-dev
|
||||
- git clone --depth 1 https://github.com/geky/littlefs-fuse
|
||||
- fusermount -V
|
||||
- gcc --version
|
||||
before_script:
|
||||
# setup disk for littlefs-fuse
|
||||
- rm -rf littlefs-fuse/littlefs/*
|
||||
- cp -r $(git ls-tree --name-only HEAD) littlefs-fuse/littlefs
|
||||
- make -C littlefs-fuse
|
||||
|
||||
- mkdir mount
|
||||
- sudo chmod a+rw /dev/loop0
|
||||
- dd if=/dev/zero bs=512 count=2048 of=disk
|
||||
- losetup /dev/loop0 disk
|
||||
script:
|
||||
# self-host test
|
||||
- make -C littlefs-fuse
|
||||
- littlefs-fuse/lfs --format /dev/loop0
|
||||
- littlefs-fuse/lfs /dev/loop0 mount
|
||||
|
||||
- littlefs-fuse/lfs --format /dev/loop0
|
||||
- littlefs-fuse/lfs /dev/loop0 mount
|
||||
- ls mount
|
||||
- mkdir mount/littlefs
|
||||
- cp -r $(git ls-tree --name-only HEAD) mount/littlefs
|
||||
- cd mount/littlefs
|
||||
- ls
|
||||
- make -B test_dirs test_files QUIET=1
|
||||
|
||||
- ls mount
|
||||
- mkdir mount/littlefs
|
||||
- cp -r $(git ls-tree --name-only HEAD) mount/littlefs
|
||||
- cd mount/littlefs
|
||||
- ls
|
||||
- make -B test_dirs test_files QUIET=1
|
||||
|
||||
# Automatically update releases
|
||||
- stage: deploy
|
||||
env:
|
||||
- STAGE=deploy
|
||||
- NAME=deploy
|
||||
script:
|
||||
# Update tag for version defined in lfs.h
|
||||
- LFS_VERSION=$(grep -ox '#define LFS_VERSION .*' lfs.h | cut -d ' ' -f3)
|
||||
- LFS_VERSION_MAJOR=$((0xffff & ($LFS_VERSION >> 16)))
|
||||
- LFS_VERSION_MINOR=$((0xffff & ($LFS_VERSION >> 0)))
|
||||
- LFS_VERSION="v$LFS_VERSION_MAJOR.$LFS_VERSION_MINOR"
|
||||
- echo "littlefs version $LFS_VERSION"
|
||||
- |
|
||||
curl -u $GEKY_BOT_RELEASES -X POST \
|
||||
https://api.github.com/repos/$TRAVIS_REPO_SLUG/git/refs \
|
||||
-d "{
|
||||
\"ref\": \"refs/tags/$LFS_VERSION\",
|
||||
\"sha\": \"$TRAVIS_COMMIT\"
|
||||
}"
|
||||
- |
|
||||
curl -f -u $GEKY_BOT_RELEASES -X PATCH \
|
||||
https://api.github.com/repos/$TRAVIS_REPO_SLUG/git/refs/tags/$LFS_VERSION \
|
||||
-d "{
|
||||
\"sha\": \"$TRAVIS_COMMIT\"
|
||||
}"
|
||||
# Create release notes from commits
|
||||
- LFS_PREV_VERSION="v$LFS_VERSION_MAJOR.$(($LFS_VERSION_MINOR-1))"
|
||||
- |
|
||||
if [ $(git tag -l "$LFS_PREV_VERSION") ]
|
||||
then
|
||||
curl -u $GEKY_BOT_RELEASES -X POST \
|
||||
https://api.github.com/repos/$TRAVIS_REPO_SLUG/releases \
|
||||
-d "{
|
||||
\"tag_name\": \"$LFS_VERSION\",
|
||||
\"name\": \"$LFS_VERSION\"
|
||||
}"
|
||||
RELEASE=$(
|
||||
curl -f -u $GEKY_BOT_RELEASES \
|
||||
https://api.github.com/repos/$TRAVIS_REPO_SLUG/releases/tags/$LFS_VERSION
|
||||
)
|
||||
CHANGES=$(
|
||||
git log --oneline $LFS_PREV_VERSION.. --grep='^Merge' --invert-grep
|
||||
)
|
||||
curl -f -u $GEKY_BOT_RELEASES -X PATCH \
|
||||
https://api.github.com/repos/$TRAVIS_REPO_SLUG/releases/$(
|
||||
jq -r '.id' <<< "$RELEASE"
|
||||
) \
|
||||
-d "$(
|
||||
jq -s '{
|
||||
"body": ((.[0] // "" | sub("(?<=\n)#+ Changes.*"; ""; "mi"))
|
||||
+ "### Changes\n\n" + .[1])
|
||||
}' <(jq '.body' <<< "$RELEASE") <(jq -sR '.' <<< "$CHANGES")
|
||||
)"
|
||||
fi
|
||||
|
||||
# Manage statuses
|
||||
before_install:
|
||||
- |
|
||||
curl -u $GEKY_BOT_STATUSES -X POST \
|
||||
https://api.github.com/repos/$TRAVIS_REPO_SLUG/statuses/${TRAVIS_PULL_REQUEST_SHA:-$TRAVIS_COMMIT} \
|
||||
-d "{
|
||||
\"context\": \"$STAGE/$NAME\",
|
||||
\"state\": \"pending\",
|
||||
\"description\": \"${STATUS:-In progress}\",
|
||||
\"target_url\": \"https://travis-ci.org/$TRAVIS_REPO_SLUG/jobs/$TRAVIS_JOB_ID\"
|
||||
}"
|
||||
- fusermount -V
|
||||
- gcc --version
|
||||
|
||||
after_failure:
|
||||
- |
|
||||
curl -u $GEKY_BOT_STATUSES -X POST \
|
||||
https://api.github.com/repos/$TRAVIS_REPO_SLUG/statuses/${TRAVIS_PULL_REQUEST_SHA:-$TRAVIS_COMMIT} \
|
||||
-d "{
|
||||
\"context\": \"$STAGE/$NAME\",
|
||||
\"state\": \"failure\",
|
||||
\"description\": \"${STATUS:-Failed}\",
|
||||
\"target_url\": \"https://travis-ci.org/$TRAVIS_REPO_SLUG/jobs/$TRAVIS_JOB_ID\"
|
||||
}"
|
||||
install:
|
||||
- sudo apt-get install libfuse-dev
|
||||
- git clone --depth 1 https://github.com/geky/littlefs-fuse
|
||||
|
||||
after_success:
|
||||
- |
|
||||
curl -u $GEKY_BOT_STATUSES -X POST \
|
||||
https://api.github.com/repos/$TRAVIS_REPO_SLUG/statuses/${TRAVIS_PULL_REQUEST_SHA:-$TRAVIS_COMMIT} \
|
||||
-d "{
|
||||
\"context\": \"$STAGE/$NAME\",
|
||||
\"state\": \"success\",
|
||||
\"description\": \"${STATUS:-Passed}\",
|
||||
\"target_url\": \"https://travis-ci.org/$TRAVIS_REPO_SLUG/jobs/$TRAVIS_JOB_ID\"
|
||||
}"
|
||||
before_script:
|
||||
- rm -rf littlefs-fuse/littlefs/*
|
||||
- cp -r $(git ls-tree --name-only HEAD) littlefs-fuse/littlefs
|
||||
|
||||
# Job control
|
||||
stages:
|
||||
- name: test
|
||||
- name: deploy
|
||||
if: branch = master AND type = push
|
||||
- mkdir mount
|
||||
- sudo chmod a+rw /dev/loop0
|
||||
- dd if=/dev/zero bs=512 count=2048 of=disk
|
||||
- losetup /dev/loop0 disk
|
||||
|
||||
88
DESIGN.md
88
DESIGN.md
@@ -27,17 +27,16 @@ cheap, and can be very granular. For NOR flash specifically, byte-level
|
||||
programs are quite common. Erasing, however, requires an expensive operation
|
||||
that forces the state of large blocks of memory to reset in a destructive
|
||||
reaction that gives flash its name. The [Wikipedia entry](https://en.wikipedia.org/wiki/Flash_memory)
|
||||
has more information if you are interested in how this works.
|
||||
has more information if you are interesting in how this works.
|
||||
|
||||
This leaves us with an interesting set of limitations that can be simplified
|
||||
to three strong requirements:
|
||||
|
||||
1. **Power-loss resilient** - This is the main goal of the littlefs and the
|
||||
focus of this project.
|
||||
|
||||
Embedded systems are usually designed without a shutdown routine and a
|
||||
notable lack of user interface for recovery, so filesystems targeting
|
||||
embedded systems must be prepared to lose power at any given time.
|
||||
focus of this project. Embedded systems are usually designed without a
|
||||
shutdown routine and a notable lack of user interface for recovery, so
|
||||
filesystems targeting embedded systems must be prepared to lose power an
|
||||
any given time.
|
||||
|
||||
Despite this state of things, there are very few embedded filesystems that
|
||||
handle power loss in a reasonable manner, and most can become corrupted if
|
||||
@@ -53,8 +52,7 @@ to three strong requirements:
|
||||
which stores a file allocation table (FAT) at a specific offset from the
|
||||
beginning of disk. Every block allocation will update this table, and after
|
||||
100,000 updates, the block will likely go bad, rendering the filesystem
|
||||
unusable even if there are many more erase cycles available on the storage
|
||||
as a whole.
|
||||
unusable even if there are many more erase cycles available on the storage.
|
||||
|
||||
3. **Bounded RAM/ROM** - Even with the design difficulties presented by the
|
||||
previous two limitations, we have already seen several flash filesystems
|
||||
@@ -74,7 +72,7 @@ to three strong requirements:
|
||||
|
||||
## Existing designs?
|
||||
|
||||
There are of course, many different existing filesystem. Here is a very rough
|
||||
There are of course, many different existing filesystem. Heres a very rough
|
||||
summary of the general ideas behind some of them.
|
||||
|
||||
Most of the existing filesystems fall into the one big category of filesystem
|
||||
@@ -82,21 +80,21 @@ designed in the early days of spinny magnet disks. While there is a vast amount
|
||||
of interesting technology and ideas in this area, the nature of spinny magnet
|
||||
disks encourage properties, such as grouping writes near each other, that don't
|
||||
make as much sense on recent storage types. For instance, on flash, write
|
||||
locality is not important and can actually increase wear.
|
||||
locality is not important and can actually increase wear destructively.
|
||||
|
||||
One of the most popular designs for flash filesystems is called the
|
||||
[logging filesystem](https://en.wikipedia.org/wiki/Log-structured_file_system).
|
||||
The flash filesystems [jffs](https://en.wikipedia.org/wiki/JFFS)
|
||||
and [yaffs](https://en.wikipedia.org/wiki/YAFFS) are good examples. In a
|
||||
logging filesystem, data is not stored in a data structure on disk, but instead
|
||||
and [yaffs](https://en.wikipedia.org/wiki/YAFFS) are good examples. In
|
||||
logging filesystem, data is not store in a data structure on disk, but instead
|
||||
the changes to the files are stored on disk. This has several neat advantages,
|
||||
such as the fact that the data is written in a cyclic log format and naturally
|
||||
such as the fact that the data is written in a cyclic log format naturally
|
||||
wear levels as a side effect. And, with a bit of error detection, the entire
|
||||
filesystem can easily be designed to be resilient to power loss. The
|
||||
journaling component of most modern day filesystems is actually a reduced
|
||||
journalling component of most modern day filesystems is actually a reduced
|
||||
form of a logging filesystem. However, logging filesystems have a difficulty
|
||||
scaling as the size of storage increases. And most filesystems compensate by
|
||||
caching large parts of the filesystem in RAM, a strategy that is inappropriate
|
||||
caching large parts of the filesystem in RAM, a strategy that is unavailable
|
||||
for embedded systems.
|
||||
|
||||
Another interesting filesystem design technique is that of [copy-on-write (COW)](https://en.wikipedia.org/wiki/Copy-on-write).
|
||||
@@ -109,14 +107,14 @@ where the COW data structures are synchronized.
|
||||
## Metadata pairs
|
||||
|
||||
The core piece of technology that provides the backbone for the littlefs is
|
||||
the concept of metadata pairs. The key idea here is that any metadata that
|
||||
the concept of metadata pairs. The key idea here, is that any metadata that
|
||||
needs to be updated atomically is stored on a pair of blocks tagged with
|
||||
a revision count and checksum. Every update alternates between these two
|
||||
pairs, so that at any time there is always a backup containing the previous
|
||||
state of the metadata.
|
||||
|
||||
Consider a small example where each metadata pair has a revision count,
|
||||
a number as data, and the XOR of the block as a quick checksum. If
|
||||
a number as data, and the xor of the block as a quick checksum. If
|
||||
we update the data to a value of 9, and then to a value of 5, here is
|
||||
what the pair of blocks may look like after each update:
|
||||
```
|
||||
@@ -132,7 +130,7 @@ what the pair of blocks may look like after each update:
|
||||
After each update, we can find the most up to date value of data by looking
|
||||
at the revision count.
|
||||
|
||||
Now consider what the blocks may look like if we suddenly lose power while
|
||||
Now consider what the blocks may look like if we suddenly loss power while
|
||||
changing the value of data to 5:
|
||||
```
|
||||
block 1 block 2 block 1 block 2 block 1 block 2
|
||||
@@ -151,7 +149,7 @@ check our checksum we notice that block 1 was corrupted. So we fall back to
|
||||
block 2 and use the value 9.
|
||||
|
||||
Using this concept, the littlefs is able to update metadata blocks atomically.
|
||||
There are a few other tweaks, such as using a 32 bit CRC and using sequence
|
||||
There are a few other tweaks, such as using a 32 bit crc and using sequence
|
||||
arithmetic to handle revision count overflow, but the basic concept
|
||||
is the same. These metadata pairs define the backbone of the littlefs, and the
|
||||
rest of the filesystem is built on top of these atomic updates.
|
||||
@@ -163,7 +161,7 @@ requires two blocks for each block of data. I'm sure users would be very
|
||||
unhappy if their storage was suddenly cut in half! Instead of storing
|
||||
everything in these metadata blocks, the littlefs uses a COW data structure
|
||||
for files which is in turn pointed to by a metadata block. When
|
||||
we update a file, we create copies of any blocks that are modified until
|
||||
we update a file, we create a copies of any blocks that are modified until
|
||||
the metadata blocks are updated with the new copy. Once the metadata block
|
||||
points to the new copy, we deallocate the old blocks that are no longer in use.
|
||||
|
||||
@@ -186,7 +184,7 @@ Here is what updating a one-block file may look like:
|
||||
update data in file update metadata pair
|
||||
```
|
||||
|
||||
It doesn't matter if we lose power while writing new data to block 5,
|
||||
It doesn't matter if we lose power while writing block 5 with the new data,
|
||||
since the old data remains unmodified in block 4. This example also
|
||||
highlights how the atomic updates of the metadata blocks provide a
|
||||
synchronization barrier for the rest of the littlefs.
|
||||
@@ -208,7 +206,7 @@ files in filesystems. Of these, the littlefs uses a rather unique [COW](https://
|
||||
data structure that allows the filesystem to reuse unmodified parts of the
|
||||
file without additional metadata pairs.
|
||||
|
||||
First lets consider storing files in a simple linked-list. What happens when we
|
||||
First lets consider storing files in a simple linked-list. What happens when
|
||||
append a block? We have to change the last block in the linked-list to point
|
||||
to this new block, which means we have to copy out the last block, and change
|
||||
the second-to-last block, and then the third-to-last, and so on until we've
|
||||
@@ -242,8 +240,8 @@ Exhibit B: A backwards linked-list
|
||||
```
|
||||
|
||||
However, a backwards linked-list does come with a rather glaring problem.
|
||||
Iterating over a file _in order_ has a runtime cost of O(n^2). Gah! A quadratic
|
||||
runtime to just _read_ a file? That's awful. Keep in mind reading files is
|
||||
Iterating over a file _in order_ has a runtime of O(n^2). Gah! A quadratic
|
||||
runtime to just _read_ a file? That's awful. Keep in mind reading files are
|
||||
usually the most common filesystem operation.
|
||||
|
||||
To avoid this problem, the littlefs uses a multilayered linked-list. For
|
||||
@@ -268,7 +266,7 @@ Exhibit C: A backwards CTZ skip-list
|
||||
```
|
||||
|
||||
The additional pointers allow us to navigate the data-structure on disk
|
||||
much more efficiently than in a singly linked-list.
|
||||
much more efficiently than in a single linked-list.
|
||||
|
||||
Taking exhibit C for example, here is the path from data block 5 to data
|
||||
block 1. You can see how data block 3 was completely skipped:
|
||||
@@ -291,15 +289,15 @@ The path to data block 0 is even more quick, requiring only two jumps:
|
||||
|
||||
We can find the runtime complexity by looking at the path to any block from
|
||||
the block containing the most pointers. Every step along the path divides
|
||||
the search space for the block in half. This gives us a runtime of O(log n).
|
||||
the search space for the block in half. This gives us a runtime of O(logn).
|
||||
To get to the block with the most pointers, we can perform the same steps
|
||||
backwards, which puts the runtime at O(2 log n) = O(log n). The interesting
|
||||
backwards, which puts the runtime at O(2logn) = O(logn). The interesting
|
||||
part about this data structure is that this optimal path occurs naturally
|
||||
if we greedily choose the pointer that covers the most distance without passing
|
||||
our target block.
|
||||
|
||||
So now we have a representation of files that can be appended trivially with
|
||||
a runtime of O(1), and can be read with a worst case runtime of O(n log n).
|
||||
a runtime of O(1), and can be read with a worst case runtime of O(nlogn).
|
||||
Given that the the runtime is also divided by the amount of data we can store
|
||||
in a block, this is pretty reasonable.
|
||||
|
||||
@@ -364,7 +362,7 @@ N = file size in bytes
|
||||
|
||||
And this works quite well, but is not trivial to calculate. This equation
|
||||
requires O(n) to compute, which brings the entire runtime of reading a file
|
||||
to O(n^2 log n). Fortunately, the additional O(n) does not need to touch disk,
|
||||
to O(n^2logn). Fortunately, the additional O(n) does not need to touch disk,
|
||||
so it is not completely unreasonable. But if we could solve this equation into
|
||||
a form that is easily computable, we can avoid a big slowdown.
|
||||
|
||||
@@ -381,11 +379,11 @@ unintuitive property:
|
||||

|
||||
|
||||
where:
|
||||
ctz(x) = the number of trailing bits that are 0 in x
|
||||
popcount(x) = the number of bits that are 1 in x
|
||||
ctz(i) = the number of trailing bits that are 0 in i
|
||||
popcount(i) = the number of bits that are 1 in i
|
||||
|
||||
It's a bit bewildering that these two seemingly unrelated bitwise instructions
|
||||
are related by this property. But if we start to dissect this equation we can
|
||||
are related by this property. But if we start to disect this equation we can
|
||||
see that it does hold. As n approaches infinity, we do end up with an average
|
||||
overhead of 2 pointers as we find earlier. And popcount seems to handle the
|
||||
error from this average as it accumulates in the CTZ skip-list.
|
||||
@@ -412,7 +410,8 @@ a bit to avoid integer overflow:
|
||||

|
||||
|
||||
The solution involves quite a bit of math, but computers are very good at math.
|
||||
Now we can solve for both the block index and offset from the file size in O(1).
|
||||
We can now solve for the block index + offset while only needed to store the
|
||||
file size in O(1).
|
||||
|
||||
Here is what it might look like to update a file stored with a CTZ skip-list:
|
||||
```
|
||||
@@ -501,17 +500,16 @@ scanned to find the most recent free list, but once the list was found the
|
||||
state of all free blocks becomes known.
|
||||
|
||||
However, this approach had several issues:
|
||||
|
||||
- There was a lot of nuanced logic for adding blocks to the free list without
|
||||
modifying the blocks, since the blocks remain active until the metadata is
|
||||
updated.
|
||||
- The free list had to support both additions and removals in FIFO order while
|
||||
- The free list had to support both additions and removals in fifo order while
|
||||
minimizing block erases.
|
||||
- The free list had to handle the case where the file system completely ran
|
||||
out of blocks and may no longer be able to add blocks to the free list.
|
||||
- If we used a revision count to track the most recently updated free list,
|
||||
metadata blocks that were left unmodified were ticking time bombs that would
|
||||
cause the system to go haywire if the revision count overflowed.
|
||||
cause the system to go haywire if the revision count overflowed
|
||||
- Every single metadata block wasted space to store these free list references.
|
||||
|
||||
Actually, to simplify, this approach had one massive glaring issue: complexity.
|
||||
@@ -541,7 +539,7 @@ would have an abhorrent runtime.
|
||||
So the littlefs compromises. It doesn't store a bitmap the size of the storage,
|
||||
but it does store a little bit-vector that contains a fixed set lookahead
|
||||
for block allocations. During a block allocation, the lookahead vector is
|
||||
checked for any free blocks. If there are none, the lookahead region jumps
|
||||
checked for any free blocks, if there are none, the lookahead region jumps
|
||||
forward and the entire filesystem is scanned for free blocks.
|
||||
|
||||
Here's what it might look like to allocate 4 blocks on a decently busy
|
||||
@@ -624,7 +622,7 @@ So, as a solution, the littlefs adopted a sort of threaded tree. Each
|
||||
directory not only contains pointers to all of its children, but also a
|
||||
pointer to the next directory. These pointers create a linked-list that
|
||||
is threaded through all of the directories in the filesystem. Since we
|
||||
only use this linked list to check for existence, the order doesn't actually
|
||||
only use this linked list to check for existance, the order doesn't actually
|
||||
matter. As an added plus, we can repurpose the pointer for the individual
|
||||
directory linked-lists and avoid using any additional space.
|
||||
|
||||
@@ -775,7 +773,7 @@ deorphan step that simply iterates through every directory in the linked-list
|
||||
and checks it against every directory entry in the filesystem to see if it
|
||||
has a parent. The deorphan step occurs on the first block allocation after
|
||||
boot, so orphans should never cause the littlefs to run out of storage
|
||||
prematurely. Note that the deorphan step never needs to run in a read-only
|
||||
prematurely. Note that the deorphan step never needs to run in a readonly
|
||||
filesystem.
|
||||
|
||||
## The move problem
|
||||
@@ -885,7 +883,7 @@ a power loss will occur during filesystem activity. We still need to handle
|
||||
the condition, but runtime during a power loss takes a back seat to the runtime
|
||||
during normal operations.
|
||||
|
||||
So what littlefs does is inelegantly simple. When littlefs moves a file, it
|
||||
So what littlefs does is unelegantly simple. When littlefs moves a file, it
|
||||
marks the file as "moving". This is stored as a single bit in the directory
|
||||
entry and doesn't take up much space. Then littlefs moves the directory,
|
||||
finishing with the complete remove of the "moving" directory entry.
|
||||
@@ -981,7 +979,7 @@ if it exists elsewhere in the filesystem.
|
||||
So now that we have all of the pieces of a filesystem, we can look at a more
|
||||
subtle attribute of embedded storage: The wear down of flash blocks.
|
||||
|
||||
The first concern for the littlefs, is that perfectly valid blocks can suddenly
|
||||
The first concern for the littlefs, is that prefectly valid blocks can suddenly
|
||||
become unusable. As a nice side-effect of using a COW data-structure for files,
|
||||
we can simply move on to a different block when a file write fails. All
|
||||
modifications to files are performed in copies, so we will only replace the
|
||||
@@ -1153,7 +1151,7 @@ develops errors and needs to be moved.
|
||||
|
||||
## Wear leveling
|
||||
|
||||
The second concern for the littlefs is that blocks in the filesystem may wear
|
||||
The second concern for the littlefs, is that blocks in the filesystem may wear
|
||||
unevenly. In this situation, a filesystem may meet an early demise where
|
||||
there are no more non-corrupted blocks that aren't in use. It's common to
|
||||
have files that were written once and left unmodified, wasting the potential
|
||||
@@ -1173,7 +1171,7 @@ of wear leveling:
|
||||
|
||||
In littlefs's case, it's possible to use the revision count on metadata pairs
|
||||
to approximate the wear of a metadata block. And combined with the COW nature
|
||||
of files, littlefs could provide your usual implementation of dynamic wear
|
||||
of files, littlefs could provide your usually implementation of dynamic wear
|
||||
leveling.
|
||||
|
||||
However, the littlefs does not. This is for a few reasons. Most notably, even
|
||||
@@ -1212,9 +1210,9 @@ So, to summarize:
|
||||
metadata block is active
|
||||
4. Directory blocks contain either references to other directories or files
|
||||
5. Files are represented by copy-on-write CTZ skip-lists which support O(1)
|
||||
append and O(n log n) reading
|
||||
append and O(nlogn) reading
|
||||
6. Blocks are allocated by scanning the filesystem for used blocks in a
|
||||
fixed-size lookahead region that is stored in a bit-vector
|
||||
fixed-size lookahead region is that stored in a bit-vector
|
||||
7. To facilitate scanning the filesystem, all directories are part of a
|
||||
linked-list that is threaded through the entire filesystem
|
||||
8. If a block develops an error, the littlefs allocates a new block, and
|
||||
|
||||
183
LICENSE.md
183
LICENSE.md
@@ -1,36 +1,165 @@
|
||||
Copyright (c) 2017, Arm Limited. All rights reserved.
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
- Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
- Redistributions in binary form must reproduce the above copyright notice, this
|
||||
list of conditions and the following disclaimer in the documentation and/or
|
||||
other materials provided with the distribution.
|
||||
- Neither the name of ARM nor the names of its contributors may be used to
|
||||
endorse or promote products derived from this software without specific prior
|
||||
written permission.
|
||||
1. Definitions.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
"License" shall mean the terms and conditions for use, reproduction, and
|
||||
distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
---
|
||||
"Licensor" shall mean the copyright owner or entity authorized by the copyright
|
||||
owner that is granting the License.
|
||||
|
||||
*Note*:
|
||||
Individual files contain the following tag instead of the full license text.
|
||||
"Legal Entity" shall mean the union of the acting entity and all other entities
|
||||
that control, are controlled by, or are under common control with that entity.
|
||||
For the purposes of this definition, "control" means (i) the power, direct or
|
||||
indirect, to cause the direction or management of such entity, whether by
|
||||
contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
||||
permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications, including
|
||||
but not limited to software source code, documentation source, and configuration
|
||||
files.
|
||||
|
||||
SPDX-License-Identifier: BSD-3-Clause
|
||||
"Object" form shall mean any form resulting from mechanical transformation or
|
||||
translation of a Source form, including but not limited to compiled object code,
|
||||
generated documentation, and conversions to other media types.
|
||||
|
||||
This enables machine processing of license information based on the SPDX
|
||||
License Identifiers that are here available: http://spdx.org/licenses/
|
||||
"Work" shall mean the work of authorship, whether in Source or Object form, made
|
||||
available under the License, as indicated by a copyright notice that is included
|
||||
in or attached to the work (an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object form, that
|
||||
is based on (or derived from) the Work and for which the editorial revisions,
|
||||
annotations, elaborations, or other modifications represent, as a whole, an
|
||||
original work of authorship. For the purposes of this License, Derivative Works
|
||||
shall not include works that remain separable from, or merely link (or bind by
|
||||
name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including the original version
|
||||
of the Work and any modifications or additions to that Work or Derivative Works
|
||||
thereof, that is intentionally submitted to Licensor for inclusion in the Work
|
||||
by the copyright owner or by an individual or Legal Entity authorized to submit
|
||||
on behalf of the copyright owner. For the purposes of this definition,
|
||||
"submitted" means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems, and
|
||||
issue tracking systems that are managed by, or on behalf of, the Licensor for
|
||||
the purpose of discussing and improving the Work, but excluding communication
|
||||
that is conspicuously marked or otherwise designated in writing by the copyright
|
||||
owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
|
||||
of whom a Contribution has been received by Licensor and subsequently
|
||||
incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License.
|
||||
|
||||
Subject to the terms and conditions of this License, each Contributor hereby
|
||||
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
||||
irrevocable copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the Work and such
|
||||
Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License.
|
||||
|
||||
Subject to the terms and conditions of this License, each Contributor hereby
|
||||
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
||||
irrevocable (except as stated in this section) patent license to make, have
|
||||
made, use, offer to sell, sell, import, and otherwise transfer the Work, where
|
||||
such license applies only to those patent claims licensable by such Contributor
|
||||
that are necessarily infringed by their Contribution(s) alone or by combination
|
||||
of their Contribution(s) with the Work to which such Contribution(s) was
|
||||
submitted. If You institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work or a
|
||||
Contribution incorporated within the Work constitutes direct or contributory
|
||||
patent infringement, then any patent licenses granted to You under this License
|
||||
for that Work shall terminate as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution.
|
||||
|
||||
You may reproduce and distribute copies of the Work or Derivative Works thereof
|
||||
in any medium, with or without modifications, and in Source or Object form,
|
||||
provided that You meet the following conditions:
|
||||
|
||||
You must give any other recipients of the Work or Derivative Works a copy of
|
||||
this License; and
|
||||
You must cause any modified files to carry prominent notices stating that You
|
||||
changed the files; and
|
||||
You must retain, in the Source form of any Derivative Works that You distribute,
|
||||
all copyright, patent, trademark, and attribution notices from the Source form
|
||||
of the Work, excluding those notices that do not pertain to any part of the
|
||||
Derivative Works; and
|
||||
If the Work includes a "NOTICE" text file as part of its distribution, then any
|
||||
Derivative Works that You distribute must include a readable copy of the
|
||||
attribution notices contained within such NOTICE file, excluding those notices
|
||||
that do not pertain to any part of the Derivative Works, in at least one of the
|
||||
following places: within a NOTICE text file distributed as part of the
|
||||
Derivative Works; within the Source form or documentation, if provided along
|
||||
with the Derivative Works; or, within a display generated by the Derivative
|
||||
Works, if and wherever such third-party notices normally appear. The contents of
|
||||
the NOTICE file are for informational purposes only and do not modify the
|
||||
License. You may add Your own attribution notices within Derivative Works that
|
||||
You distribute, alongside or as an addendum to the NOTICE text from the Work,
|
||||
provided that such additional attribution notices cannot be construed as
|
||||
modifying the License.
|
||||
You may add Your own copyright statement to Your modifications and may provide
|
||||
additional or different license terms and conditions for use, reproduction, or
|
||||
distribution of Your modifications, or for any such Derivative Works as a whole,
|
||||
provided Your use, reproduction, and distribution of the Work otherwise complies
|
||||
with the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions.
|
||||
|
||||
Unless You explicitly state otherwise, any Contribution intentionally submitted
|
||||
for inclusion in the Work by You to the Licensor shall be under the terms and
|
||||
conditions of this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify the terms of
|
||||
any separate license agreement you may have executed with Licensor regarding
|
||||
such Contributions.
|
||||
|
||||
6. Trademarks.
|
||||
|
||||
This License does not grant permission to use the trade names, trademarks,
|
||||
service marks, or product names of the Licensor, except as required for
|
||||
reasonable and customary use in describing the origin of the Work and
|
||||
reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty.
|
||||
|
||||
Unless required by applicable law or agreed to in writing, Licensor provides the
|
||||
Work (and each Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
|
||||
including, without limitation, any warranties or conditions of TITLE,
|
||||
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
|
||||
solely responsible for determining the appropriateness of using or
|
||||
redistributing the Work and assume any risks associated with Your exercise of
|
||||
permissions under this License.
|
||||
|
||||
8. Limitation of Liability.
|
||||
|
||||
In no event and under no legal theory, whether in tort (including negligence),
|
||||
contract, or otherwise, unless required by applicable law (such as deliberate
|
||||
and grossly negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special, incidental,
|
||||
or consequential damages of any character arising as a result of this License or
|
||||
out of the use or inability to use the Work (including but not limited to
|
||||
damages for loss of goodwill, work stoppage, computer failure or malfunction, or
|
||||
any and all other commercial damages or losses), even if such Contributor has
|
||||
been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability.
|
||||
|
||||
While redistributing the Work or Derivative Works thereof, You may choose to
|
||||
offer, and charge a fee for, acceptance of support, warranty, indemnity, or
|
||||
other liability obligations and/or rights consistent with this License. However,
|
||||
in accepting such obligations, You may act only on Your own behalf and on Your
|
||||
sole responsibility, not on behalf of any other Contributor, and only if You
|
||||
agree to indemnify, defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason of your
|
||||
accepting any such warranty or additional liability.
|
||||
|
||||
31
Makefile
31
Makefile
@@ -1,11 +1,8 @@
|
||||
TARGET = lfs.a
|
||||
ifneq ($(wildcard test.c main.c),)
|
||||
override TARGET = lfs
|
||||
endif
|
||||
TARGET = lfs
|
||||
|
||||
CC ?= gcc
|
||||
AR ?= ar
|
||||
SIZE ?= size
|
||||
CC = gcc
|
||||
AR = ar
|
||||
SIZE = size
|
||||
|
||||
SRC += $(wildcard *.c emubd/*.c)
|
||||
OBJ := $(SRC:.c=.o)
|
||||
@@ -17,15 +14,15 @@ TEST := $(patsubst tests/%.sh,%,$(wildcard tests/test_*))
|
||||
SHELL = /bin/bash -o pipefail
|
||||
|
||||
ifdef DEBUG
|
||||
override CFLAGS += -O0 -g3
|
||||
CFLAGS += -O0 -g3
|
||||
else
|
||||
override CFLAGS += -Os
|
||||
CFLAGS += -Os
|
||||
endif
|
||||
ifdef WORD
|
||||
override CFLAGS += -m$(WORD)
|
||||
CFLAGS += -m$(WORD)
|
||||
endif
|
||||
override CFLAGS += -I.
|
||||
override CFLAGS += -std=c99 -Wall -pedantic -Wshadow -Wunused-parameter
|
||||
CFLAGS += -I.
|
||||
CFLAGS += -std=c99 -Wall -pedantic
|
||||
|
||||
|
||||
all: $(TARGET)
|
||||
@@ -36,20 +33,18 @@ size: $(OBJ)
|
||||
$(SIZE) -t $^
|
||||
|
||||
.SUFFIXES:
|
||||
test: test_format test_dirs test_files test_seek test_truncate \
|
||||
test_interspersed test_alloc test_paths test_orphan test_move test_corrupt
|
||||
@rm test.c
|
||||
test: test_format test_dirs test_files test_seek test_truncate test_parallel \
|
||||
test_alloc test_paths test_orphan test_move test_corrupt
|
||||
test_%: tests/test_%.sh
|
||||
|
||||
ifdef QUIET
|
||||
@./$< | sed -n '/^[-=]/p'
|
||||
./$< | sed -n '/^[-=]/p'
|
||||
else
|
||||
./$<
|
||||
endif
|
||||
|
||||
-include $(DEP)
|
||||
|
||||
lfs: $(OBJ)
|
||||
$(TARGET): $(OBJ)
|
||||
$(CC) $(CFLAGS) $^ $(LFLAGS) -o $@
|
||||
|
||||
%.a: $(OBJ)
|
||||
|
||||
31
README.md
31
README.md
@@ -16,7 +16,7 @@ of memory. Recursion is avoided and dynamic memory is limited to configurable
|
||||
buffers that can be provided statically.
|
||||
|
||||
**Power-loss resilient** - The littlefs is designed for systems that may have
|
||||
random power failures. The littlefs has strong copy-on-write guarantees and
|
||||
random power failures. The littlefs has strong copy-on-write guaruntees and
|
||||
storage on disk is always kept in a valid state.
|
||||
|
||||
**Wear leveling** - Since the most common form of embedded storage is erodible
|
||||
@@ -88,7 +88,7 @@ int main(void) {
|
||||
## Usage
|
||||
|
||||
Detailed documentation (or at least as much detail as is currently available)
|
||||
can be found in the comments in [lfs.h](lfs.h).
|
||||
can be cound in the comments in [lfs.h](lfs.h).
|
||||
|
||||
As you may have noticed, littlefs takes in a configuration structure that
|
||||
defines how the filesystem operates. The configuration struct provides the
|
||||
@@ -101,12 +101,12 @@ to the user to allocate, allowing multiple filesystems to be in use
|
||||
simultaneously. With the `lfs_t` and configuration struct, a user can
|
||||
format a block device or mount the filesystem.
|
||||
|
||||
Once mounted, the littlefs provides a full set of POSIX-like file and
|
||||
Once mounted, the littlefs provides a full set of posix-like file and
|
||||
directory functions, with the deviation that the allocation of filesystem
|
||||
structures must be provided by the user.
|
||||
|
||||
All POSIX operations, such as remove and rename, are atomic, even in event
|
||||
of power-loss. Additionally, no file updates are actually committed to the
|
||||
All posix operations, such as remove and rename, are atomic, even in event
|
||||
of power-loss. Additionally, no file updates are actually commited to the
|
||||
filesystem until sync or close is called on the file.
|
||||
|
||||
## Other notes
|
||||
@@ -115,17 +115,10 @@ All littlefs have the potential to return a negative error code. The errors
|
||||
can be either one of those found in the `enum lfs_error` in [lfs.h](lfs.h),
|
||||
or an error returned by the user's block device operations.
|
||||
|
||||
In the configuration struct, the `prog` and `erase` function provided by the
|
||||
user may return a `LFS_ERR_CORRUPT` error if the implementation already can
|
||||
detect corrupt blocks. However, the wear leveling does not depend on the return
|
||||
code of these functions, instead all data is read back and checked for
|
||||
integrity.
|
||||
|
||||
If your storage caches writes, make sure that the provided `sync` function
|
||||
flushes all the data to memory and ensures that the next read fetches the data
|
||||
from memory, otherwise data integrity can not be guaranteed. If the `write`
|
||||
function does not perform caching, and therefore each `read` or `write` call
|
||||
hits the memory, the `sync` function can simply return 0.
|
||||
It should also be noted that the current implementation of littlefs doesn't
|
||||
really do anything to insure that the data written to disk is machine portable.
|
||||
This is fine as long as all of the involved machines share endianness
|
||||
(little-endian) and don't have strange padding requirements.
|
||||
|
||||
## Reference material
|
||||
|
||||
@@ -138,9 +131,9 @@ with all the nitty-gritty details. Can be useful for developing tooling.
|
||||
|
||||
## Testing
|
||||
|
||||
The littlefs comes with a test suite designed to run on a PC using the
|
||||
The littlefs comes with a test suite designed to run on a pc using the
|
||||
[emulated block device](emubd/lfs_emubd.h) found in the emubd directory.
|
||||
The tests assume a Linux environment and can be started with make:
|
||||
The tests assume a linux environment and can be started with make:
|
||||
|
||||
``` bash
|
||||
make test
|
||||
@@ -155,7 +148,7 @@ littlefs is available in Mbed OS as the [LittleFileSystem](https://os.mbed.com/d
|
||||
class.
|
||||
|
||||
[littlefs-fuse](https://github.com/geky/littlefs-fuse) - A [FUSE](https://github.com/libfuse/libfuse)
|
||||
wrapper for littlefs. The project allows you to mount littlefs directly on a
|
||||
wrapper for littlefs. The project allows you to mount littlefs directly in a
|
||||
Linux machine. Can be useful for debugging littlefs if you have an SD card
|
||||
handy.
|
||||
|
||||
|
||||
12
SPEC.md
12
SPEC.md
@@ -46,7 +46,7 @@ Here's the layout of metadata blocks on disk:
|
||||
| 0x04 | 32 bits | dir size |
|
||||
| 0x08 | 64 bits | tail pointer |
|
||||
| 0x10 | size-16 bytes | dir entries |
|
||||
| 0x00+s | 32 bits | CRC |
|
||||
| 0x00+s | 32 bits | crc |
|
||||
|
||||
**Revision count** - Incremented every update, only the uncorrupted
|
||||
metadata-block with the most recent revision count contains the valid metadata.
|
||||
@@ -75,7 +75,7 @@ Here's an example of a simple directory stored on disk:
|
||||
(32 bits) revision count = 10 (0x0000000a)
|
||||
(32 bits) dir size = 154 bytes, end of dir (0x0000009a)
|
||||
(64 bits) tail pointer = 37, 36 (0x00000025, 0x00000024)
|
||||
(32 bits) CRC = 0xc86e3106
|
||||
(32 bits) crc = 0xc86e3106
|
||||
|
||||
00000000: 0a 00 00 00 9a 00 00 00 25 00 00 00 24 00 00 00 ........%...$...
|
||||
00000010: 22 08 00 03 05 00 00 00 04 00 00 00 74 65 61 22 "...........tea"
|
||||
@@ -138,12 +138,12 @@ not include the entry type size, attributes, or name. The full size in bytes
|
||||
of the entry is 4 + entry length + attribute length + name length.
|
||||
|
||||
**Attribute length** - Length of system-specific attributes in bytes. Since
|
||||
attributes are system specific, there is not much guarantee on the values in
|
||||
attributes are system specific, there is not much garuntee on the values in
|
||||
this section, and systems are expected to work even when it is empty. See the
|
||||
[attributes](#entry-attributes) section for more details.
|
||||
|
||||
**Name length** - Length of the entry name. Entry names are stored as UTF8,
|
||||
although most systems will probably only support ASCII. Entry names can not
|
||||
**Name length** - Length of the entry name. Entry names are stored as utf8,
|
||||
although most systems will probably only support ascii. Entry names can not
|
||||
contain '/' and can not be '.' or '..' as these are a part of the syntax of
|
||||
filesystem paths.
|
||||
|
||||
@@ -222,7 +222,7 @@ Here's an example of a complete superblock:
|
||||
(32 bits) block count = 1024 blocks (0x00000400)
|
||||
(32 bits) version = 1.1 (0x00010001)
|
||||
(8 bytes) magic string = littlefs
|
||||
(32 bits) CRC = 0xc50b74fa
|
||||
(32 bits) crc = 0xc50b74fa
|
||||
|
||||
00000000: 03 00 00 00 34 00 00 00 03 00 00 00 02 00 00 00 ....4...........
|
||||
00000010: 2e 14 00 08 03 00 00 00 02 00 00 00 00 02 00 00 ................
|
||||
|
||||
@@ -1,8 +1,19 @@
|
||||
/*
|
||||
* Block device emulated on standard files
|
||||
*
|
||||
* Copyright (c) 2017, Arm Limited. All rights reserved.
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2017 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "emubd/lfs_emubd.h"
|
||||
|
||||
@@ -179,13 +190,13 @@ int lfs_emubd_erase(const struct lfs_config *cfg, lfs_block_t block) {
|
||||
}
|
||||
|
||||
if (!err && S_ISREG(st.st_mode) && (S_IWUSR & st.st_mode)) {
|
||||
err = unlink(emu->path);
|
||||
int err = unlink(emu->path);
|
||||
if (err) {
|
||||
return -errno;
|
||||
}
|
||||
}
|
||||
|
||||
if (err || (S_ISREG(st.st_mode) && (S_IWUSR & st.st_mode))) {
|
||||
if (errno == ENOENT || (S_ISREG(st.st_mode) && (S_IWUSR & st.st_mode))) {
|
||||
FILE *f = fopen(emu->path, "w");
|
||||
if (!f) {
|
||||
return -errno;
|
||||
|
||||
@@ -1,8 +1,19 @@
|
||||
/*
|
||||
* Block device emulated on standard files
|
||||
*
|
||||
* Copyright (c) 2017, Arm Limited. All rights reserved.
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2017 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef LFS_EMUBD_H
|
||||
#define LFS_EMUBD_H
|
||||
|
||||
403
lfs.c
403
lfs.c
@@ -1,19 +1,34 @@
|
||||
/*
|
||||
* The little filesystem
|
||||
*
|
||||
* Copyright (c) 2017, Arm Limited. All rights reserved.
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2017 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "lfs.h"
|
||||
#include "lfs_util.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
|
||||
/// Caching block device operations ///
|
||||
static int lfs_cache_read(lfs_t *lfs, lfs_cache_t *rcache,
|
||||
const lfs_cache_t *pcache, lfs_block_t block,
|
||||
lfs_off_t off, void *buffer, lfs_size_t size) {
|
||||
uint8_t *data = buffer;
|
||||
LFS_ASSERT(block < lfs->cfg->block_count);
|
||||
assert(block < lfs->cfg->block_count);
|
||||
|
||||
while (size > 0) {
|
||||
if (pcache && block == pcache->block && off >= pcache->off &&
|
||||
@@ -107,19 +122,6 @@ static int lfs_cache_crc(lfs_t *lfs, lfs_cache_t *rcache,
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline void lfs_cache_drop(lfs_t *lfs, lfs_cache_t *rcache) {
|
||||
// do not zero, cheaper if cache is readonly or only going to be
|
||||
// written with identical data (during relocates)
|
||||
(void)lfs;
|
||||
rcache->block = 0xffffffff;
|
||||
}
|
||||
|
||||
static inline void lfs_cache_zero(lfs_t *lfs, lfs_cache_t *pcache) {
|
||||
// zero to avoid information leak
|
||||
memset(pcache->buffer, 0xff, lfs->cfg->prog_size);
|
||||
pcache->block = 0xffffffff;
|
||||
}
|
||||
|
||||
static int lfs_cache_flush(lfs_t *lfs,
|
||||
lfs_cache_t *pcache, lfs_cache_t *rcache) {
|
||||
if (pcache->block != 0xffffffff) {
|
||||
@@ -141,7 +143,7 @@ static int lfs_cache_flush(lfs_t *lfs,
|
||||
}
|
||||
}
|
||||
|
||||
lfs_cache_zero(lfs, pcache);
|
||||
pcache->block = 0xffffffff;
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -151,7 +153,7 @@ static int lfs_cache_prog(lfs_t *lfs, lfs_cache_t *pcache,
|
||||
lfs_cache_t *rcache, lfs_block_t block,
|
||||
lfs_off_t off, const void *buffer, lfs_size_t size) {
|
||||
const uint8_t *data = buffer;
|
||||
LFS_ASSERT(block < lfs->cfg->block_count);
|
||||
assert(block < lfs->cfg->block_count);
|
||||
|
||||
while (size > 0) {
|
||||
if (block == pcache->block && off >= pcache->off &&
|
||||
@@ -178,7 +180,7 @@ static int lfs_cache_prog(lfs_t *lfs, lfs_cache_t *pcache,
|
||||
|
||||
// pcache must have been flushed, either by programming and
|
||||
// entire block or manually flushing the pcache
|
||||
LFS_ASSERT(pcache->block == 0xffffffff);
|
||||
assert(pcache->block == 0xffffffff);
|
||||
|
||||
if (off % lfs->cfg->prog_size == 0 &&
|
||||
size >= lfs->cfg->prog_size) {
|
||||
@@ -246,7 +248,7 @@ static int lfs_bd_erase(lfs_t *lfs, lfs_block_t block) {
|
||||
}
|
||||
|
||||
static int lfs_bd_sync(lfs_t *lfs) {
|
||||
lfs_cache_drop(lfs, &lfs->rcache);
|
||||
lfs->rcache.block = 0xffffffff;
|
||||
|
||||
int err = lfs_cache_flush(lfs, &lfs->pcache, NULL);
|
||||
if (err) {
|
||||
@@ -272,10 +274,11 @@ int lfs_deorphan(lfs_t *lfs);
|
||||
static int lfs_alloc_lookahead(void *p, lfs_block_t block) {
|
||||
lfs_t *lfs = p;
|
||||
|
||||
lfs_block_t off = ((block - lfs->free.off)
|
||||
lfs_block_t off = (((lfs_soff_t)(block - lfs->free.begin)
|
||||
% (lfs_soff_t)(lfs->cfg->block_count))
|
||||
+ lfs->cfg->block_count) % lfs->cfg->block_count;
|
||||
|
||||
if (off < lfs->free.size) {
|
||||
if (off < lfs->cfg->lookahead) {
|
||||
lfs->free.buffer[off / 32] |= 1U << (off % 32);
|
||||
}
|
||||
|
||||
@@ -284,38 +287,30 @@ static int lfs_alloc_lookahead(void *p, lfs_block_t block) {
|
||||
|
||||
static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) {
|
||||
while (true) {
|
||||
while (lfs->free.i != lfs->free.size) {
|
||||
lfs_block_t off = lfs->free.i;
|
||||
lfs->free.i += 1;
|
||||
lfs->free.ack -= 1;
|
||||
while (true) {
|
||||
// check if we have looked at all blocks since last ack
|
||||
if (lfs->free.begin + lfs->free.off == lfs->free.end) {
|
||||
LFS_WARN("No more free space %d", lfs->free.end);
|
||||
return LFS_ERR_NOSPC;
|
||||
}
|
||||
|
||||
if (lfs->free.off >= lfs_min(
|
||||
lfs->cfg->lookahead, lfs->cfg->block_count)) {
|
||||
break;
|
||||
}
|
||||
|
||||
lfs_block_t off = lfs->free.off;
|
||||
lfs->free.off += 1;
|
||||
|
||||
if (!(lfs->free.buffer[off / 32] & (1U << (off % 32)))) {
|
||||
// found a free block
|
||||
*block = (lfs->free.off + off) % lfs->cfg->block_count;
|
||||
|
||||
// eagerly find next off so an alloc ack can
|
||||
// discredit old lookahead blocks
|
||||
while (lfs->free.i != lfs->free.size &&
|
||||
(lfs->free.buffer[lfs->free.i / 32]
|
||||
& (1U << (lfs->free.i % 32)))) {
|
||||
lfs->free.i += 1;
|
||||
lfs->free.ack -= 1;
|
||||
}
|
||||
|
||||
*block = (lfs->free.begin + off) % lfs->cfg->block_count;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// check if we have looked at all blocks since last ack
|
||||
if (lfs->free.ack == 0) {
|
||||
LFS_WARN("No more free space %d", lfs->free.i + lfs->free.off);
|
||||
return LFS_ERR_NOSPC;
|
||||
}
|
||||
|
||||
lfs->free.off = (lfs->free.off + lfs->free.size)
|
||||
% lfs->cfg->block_count;
|
||||
lfs->free.size = lfs_min(lfs->cfg->lookahead, lfs->free.ack);
|
||||
lfs->free.i = 0;
|
||||
lfs->free.begin += lfs_min(lfs->cfg->lookahead, lfs->cfg->block_count);
|
||||
lfs->free.off = 0;
|
||||
|
||||
// find mask of free blocks from tree
|
||||
memset(lfs->free.buffer, 0, lfs->cfg->lookahead/8);
|
||||
@@ -327,7 +322,7 @@ static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) {
|
||||
}
|
||||
|
||||
static void lfs_alloc_ack(lfs_t *lfs) {
|
||||
lfs->free.ack = lfs->cfg->block_count;
|
||||
lfs->free.end = lfs->free.begin + lfs->free.off + lfs->cfg->block_count;
|
||||
}
|
||||
|
||||
|
||||
@@ -534,7 +529,7 @@ static int lfs_dir_commit(lfs_t *lfs, lfs_dir_t *dir,
|
||||
while (newoff < (0x7fffffff & dir->d.size)-4) {
|
||||
if (i < count && regions[i].oldoff == oldoff) {
|
||||
lfs_crc(&crc, regions[i].newdata, regions[i].newlen);
|
||||
err = lfs_bd_prog(lfs, dir->pair[0],
|
||||
int err = lfs_bd_prog(lfs, dir->pair[0],
|
||||
newoff, regions[i].newdata, regions[i].newlen);
|
||||
if (err) {
|
||||
if (err == LFS_ERR_CORRUPT) {
|
||||
@@ -548,7 +543,7 @@ static int lfs_dir_commit(lfs_t *lfs, lfs_dir_t *dir,
|
||||
i += 1;
|
||||
} else {
|
||||
uint8_t data;
|
||||
err = lfs_bd_read(lfs, oldpair[1], oldoff, &data, 1);
|
||||
int err = lfs_bd_read(lfs, oldpair[1], oldoff, &data, 1);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
@@ -605,7 +600,7 @@ relocate:
|
||||
|
||||
// drop caches and prepare to relocate block
|
||||
relocated = true;
|
||||
lfs_cache_drop(lfs, &lfs->pcache);
|
||||
lfs->pcache.block = 0xffffffff;
|
||||
|
||||
// can't relocate superblock, filesystem is now frozen
|
||||
if (lfs_paircmp(oldpair, (const lfs_block_t[2]){0, 1}) == 0) {
|
||||
@@ -670,17 +665,17 @@ static int lfs_dir_append(lfs_t *lfs, lfs_dir_t *dir,
|
||||
|
||||
// we need to allocate a new dir block
|
||||
if (!(0x80000000 & dir->d.size)) {
|
||||
lfs_dir_t olddir = *dir;
|
||||
int err = lfs_dir_alloc(lfs, dir);
|
||||
lfs_dir_t newdir;
|
||||
int err = lfs_dir_alloc(lfs, &newdir);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
dir->d.tail[0] = olddir.d.tail[0];
|
||||
dir->d.tail[1] = olddir.d.tail[1];
|
||||
entry->off = dir->d.size - 4;
|
||||
newdir.d.tail[0] = dir->d.tail[0];
|
||||
newdir.d.tail[1] = dir->d.tail[1];
|
||||
entry->off = newdir.d.size - 4;
|
||||
lfs_entry_tole32(&entry->d);
|
||||
err = lfs_dir_commit(lfs, dir, (struct lfs_region[]){
|
||||
err = lfs_dir_commit(lfs, &newdir, (struct lfs_region[]){
|
||||
{entry->off, 0, &entry->d, sizeof(entry->d)},
|
||||
{entry->off, 0, data, entry->d.nlen}
|
||||
}, 2);
|
||||
@@ -689,10 +684,10 @@ static int lfs_dir_append(lfs_t *lfs, lfs_dir_t *dir,
|
||||
return err;
|
||||
}
|
||||
|
||||
olddir.d.size |= 0x80000000;
|
||||
olddir.d.tail[0] = dir->pair[0];
|
||||
olddir.d.tail[1] = dir->pair[1];
|
||||
return lfs_dir_commit(lfs, &olddir, NULL, 0);
|
||||
dir->d.size |= 0x80000000;
|
||||
dir->d.tail[0] = newdir.pair[0];
|
||||
dir->d.tail[1] = newdir.pair[1];
|
||||
return lfs_dir_commit(lfs, dir, NULL, 0);
|
||||
}
|
||||
|
||||
int err = lfs_dir_fetch(lfs, dir, dir->d.tail);
|
||||
@@ -785,15 +780,9 @@ static int lfs_dir_find(lfs_t *lfs, lfs_dir_t *dir,
|
||||
lfs_entry_t *entry, const char **path) {
|
||||
const char *pathname = *path;
|
||||
size_t pathlen;
|
||||
entry->d.type = LFS_TYPE_DIR;
|
||||
entry->d.elen = sizeof(entry->d) - 4;
|
||||
entry->d.alen = 0;
|
||||
entry->d.nlen = 0;
|
||||
entry->d.u.dir[0] = lfs->root[0];
|
||||
entry->d.u.dir[1] = lfs->root[1];
|
||||
|
||||
while (true) {
|
||||
nextname:
|
||||
nextname:
|
||||
// skip slashes
|
||||
pathname += strspn(pathname, "/");
|
||||
pathlen = strcspn(pathname, "/");
|
||||
@@ -829,27 +818,12 @@ nextname:
|
||||
suffix += sufflen;
|
||||
}
|
||||
|
||||
// found path
|
||||
if (pathname[0] == '\0') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// update what we've found
|
||||
*path = pathname;
|
||||
|
||||
// continue on if we hit a directory
|
||||
if (entry->d.type != LFS_TYPE_DIR) {
|
||||
return LFS_ERR_NOTDIR;
|
||||
}
|
||||
|
||||
int err = lfs_dir_fetch(lfs, dir, entry->d.u.dir);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
// find entry matching name
|
||||
// find path
|
||||
while (true) {
|
||||
err = lfs_dir_next(lfs, dir, entry);
|
||||
int err = lfs_dir_next(lfs, dir, entry);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
@@ -883,8 +857,21 @@ nextname:
|
||||
entry->d.type &= ~0x80;
|
||||
}
|
||||
|
||||
// to next name
|
||||
pathname += pathlen;
|
||||
pathname += strspn(pathname, "/");
|
||||
if (pathname[0] == '\0') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// continue on if we hit a directory
|
||||
if (entry->d.type != LFS_TYPE_DIR) {
|
||||
return LFS_ERR_NOTDIR;
|
||||
}
|
||||
|
||||
int err = lfs_dir_fetch(lfs, dir, entry->d.u.dir);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -901,8 +888,13 @@ int lfs_mkdir(lfs_t *lfs, const char *path) {
|
||||
|
||||
// fetch parent directory
|
||||
lfs_dir_t cwd;
|
||||
int err = lfs_dir_fetch(lfs, &cwd, lfs->root);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
lfs_entry_t entry;
|
||||
int err = lfs_dir_find(lfs, &cwd, &entry, &path);
|
||||
err = lfs_dir_find(lfs, &cwd, &entry, &path);
|
||||
if (err != LFS_ERR_NOENT || strchr(path, '/') != NULL) {
|
||||
return err ? err : LFS_ERR_EXIST;
|
||||
}
|
||||
@@ -946,8 +938,22 @@ int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) {
|
||||
dir->pair[0] = lfs->root[0];
|
||||
dir->pair[1] = lfs->root[1];
|
||||
|
||||
int err = lfs_dir_fetch(lfs, dir, dir->pair);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
// check for root, can only be something like '/././../.'
|
||||
if (strspn(path, "/.") == strlen(path)) {
|
||||
dir->head[0] = dir->pair[0];
|
||||
dir->head[1] = dir->pair[1];
|
||||
dir->pos = sizeof(dir->d) - 2;
|
||||
dir->off = sizeof(dir->d);
|
||||
return 0;
|
||||
}
|
||||
|
||||
lfs_entry_t entry;
|
||||
int err = lfs_dir_find(lfs, dir, &entry, &path);
|
||||
err = lfs_dir_find(lfs, dir, &entry, &path);
|
||||
if (err) {
|
||||
return err;
|
||||
} else if (entry.d.type != LFS_TYPE_DIR) {
|
||||
@@ -1059,7 +1065,7 @@ int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) {
|
||||
return LFS_ERR_INVAL;
|
||||
}
|
||||
|
||||
err = lfs_dir_fetch(lfs, dir, dir->d.tail);
|
||||
int err = lfs_dir_fetch(lfs, dir, dir->d.tail);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
@@ -1070,7 +1076,6 @@ int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) {
|
||||
}
|
||||
|
||||
lfs_soff_t lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir) {
|
||||
(void)lfs;
|
||||
return dir->pos;
|
||||
}
|
||||
|
||||
@@ -1127,7 +1132,7 @@ static int lfs_ctz_find(lfs_t *lfs,
|
||||
return err;
|
||||
}
|
||||
|
||||
LFS_ASSERT(head >= 2 && head <= lfs->cfg->block_count);
|
||||
assert(head >= 2 && head <= lfs->cfg->block_count);
|
||||
current -= 1 << skip;
|
||||
}
|
||||
|
||||
@@ -1147,7 +1152,7 @@ static int lfs_ctz_extend(lfs_t *lfs,
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
LFS_ASSERT(nblock >= 2 && nblock <= lfs->cfg->block_count);
|
||||
assert(nblock >= 2 && nblock <= lfs->cfg->block_count);
|
||||
|
||||
if (true) {
|
||||
err = lfs_bd_erase(lfs, nblock);
|
||||
@@ -1172,7 +1177,7 @@ static int lfs_ctz_extend(lfs_t *lfs,
|
||||
if (size != lfs->cfg->block_size) {
|
||||
for (lfs_off_t i = 0; i < size; i++) {
|
||||
uint8_t data;
|
||||
err = lfs_cache_read(lfs, rcache, NULL,
|
||||
int err = lfs_cache_read(lfs, rcache, NULL,
|
||||
head, i, &data, 1);
|
||||
if (err) {
|
||||
return err;
|
||||
@@ -1199,7 +1204,7 @@ static int lfs_ctz_extend(lfs_t *lfs,
|
||||
|
||||
for (lfs_off_t i = 0; i < skips; i++) {
|
||||
head = lfs_tole32(head);
|
||||
err = lfs_cache_prog(lfs, pcache, rcache,
|
||||
int err = lfs_cache_prog(lfs, pcache, rcache,
|
||||
nblock, 4*i, &head, 4);
|
||||
head = lfs_fromle32(head);
|
||||
if (err) {
|
||||
@@ -1218,7 +1223,7 @@ static int lfs_ctz_extend(lfs_t *lfs,
|
||||
}
|
||||
}
|
||||
|
||||
LFS_ASSERT(head >= 2 && head <= lfs->cfg->block_count);
|
||||
assert(head >= 2 && head <= lfs->cfg->block_count);
|
||||
}
|
||||
|
||||
*block = nblock;
|
||||
@@ -1230,7 +1235,7 @@ relocate:
|
||||
LFS_DEBUG("Bad block at %d", nblock);
|
||||
|
||||
// just clear cache and try a new block
|
||||
lfs_cache_drop(lfs, &lfs->pcache);
|
||||
pcache->block = 0xffffffff;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1289,8 +1294,13 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
|
||||
|
||||
// allocate entry for file if it doesn't exist
|
||||
lfs_dir_t cwd;
|
||||
int err = lfs_dir_fetch(lfs, &cwd, lfs->root);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
lfs_entry_t entry;
|
||||
int err = lfs_dir_find(lfs, &cwd, &entry, &path);
|
||||
err = lfs_dir_find(lfs, &cwd, &entry, &path);
|
||||
if (err && (err != LFS_ERR_NOENT || strchr(path, '/') != NULL)) {
|
||||
return err;
|
||||
}
|
||||
@@ -1335,27 +1345,21 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
|
||||
}
|
||||
|
||||
// allocate buffer if needed
|
||||
file->cache.block = 0xffffffff;
|
||||
if (lfs->cfg->file_buffer) {
|
||||
if (lfs->files) {
|
||||
// already in use
|
||||
return LFS_ERR_NOMEM;
|
||||
}
|
||||
file->cache.buffer = lfs->cfg->file_buffer;
|
||||
} else if ((file->flags & 3) == LFS_O_RDONLY) {
|
||||
file->cache.buffer = lfs_malloc(lfs->cfg->read_size);
|
||||
file->cache.buffer = malloc(lfs->cfg->read_size);
|
||||
if (!file->cache.buffer) {
|
||||
return LFS_ERR_NOMEM;
|
||||
}
|
||||
} else {
|
||||
file->cache.buffer = lfs_malloc(lfs->cfg->prog_size);
|
||||
file->cache.buffer = malloc(lfs->cfg->prog_size);
|
||||
if (!file->cache.buffer) {
|
||||
return LFS_ERR_NOMEM;
|
||||
}
|
||||
}
|
||||
|
||||
// zero to avoid information leak
|
||||
lfs_cache_zero(lfs, &file->cache);
|
||||
|
||||
// add to list of files
|
||||
file->next = lfs->files;
|
||||
lfs->files = file;
|
||||
@@ -1376,7 +1380,7 @@ int lfs_file_close(lfs_t *lfs, lfs_file_t *file) {
|
||||
|
||||
// clean up memory
|
||||
if (!lfs->cfg->file_buffer) {
|
||||
lfs_free(file->cache.buffer);
|
||||
free(file->cache.buffer);
|
||||
}
|
||||
|
||||
return err;
|
||||
@@ -1424,7 +1428,7 @@ relocate:
|
||||
memcpy(file->cache.buffer, lfs->pcache.buffer, lfs->cfg->prog_size);
|
||||
file->cache.block = lfs->pcache.block;
|
||||
file->cache.off = lfs->pcache.off;
|
||||
lfs_cache_zero(lfs, &lfs->pcache);
|
||||
lfs->pcache.block = 0xffffffff;
|
||||
|
||||
file->block = nblock;
|
||||
return 0;
|
||||
@@ -1433,7 +1437,7 @@ relocate:
|
||||
static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) {
|
||||
if (file->flags & LFS_F_READING) {
|
||||
// just drop read cache
|
||||
lfs_cache_drop(lfs, &file->cache);
|
||||
file->cache.block = 0xffffffff;
|
||||
file->flags &= ~LFS_F_READING;
|
||||
}
|
||||
|
||||
@@ -1448,7 +1452,7 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) {
|
||||
.pos = file->pos,
|
||||
.cache = lfs->rcache,
|
||||
};
|
||||
lfs_cache_drop(lfs, &lfs->rcache);
|
||||
lfs->rcache.block = 0xffffffff;
|
||||
|
||||
while (file->pos < file->size) {
|
||||
// copy over a byte at a time, leave it up to caching
|
||||
@@ -1466,8 +1470,8 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) {
|
||||
|
||||
// keep our reference to the rcache in sync
|
||||
if (lfs->rcache.block != 0xffffffff) {
|
||||
lfs_cache_drop(lfs, &orig.cache);
|
||||
lfs_cache_drop(lfs, &lfs->rcache);
|
||||
orig.cache.block = 0xffffffff;
|
||||
lfs->rcache.block = 0xffffffff;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1512,7 +1516,7 @@ int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) {
|
||||
!lfs_pairisnull(file->pair)) {
|
||||
// update dir entry
|
||||
lfs_dir_t cwd;
|
||||
err = lfs_dir_fetch(lfs, &cwd, file->pair);
|
||||
int err = lfs_dir_fetch(lfs, &cwd, file->pair);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
@@ -1525,7 +1529,11 @@ int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) {
|
||||
return err;
|
||||
}
|
||||
|
||||
LFS_ASSERT(entry.d.type == LFS_TYPE_REG);
|
||||
if (entry.d.type != LFS_TYPE_REG) {
|
||||
// sanity check valid entry
|
||||
return LFS_ERR_INVAL;
|
||||
}
|
||||
|
||||
entry.d.u.file.head = file->head;
|
||||
entry.d.u.file.size = file->size;
|
||||
|
||||
@@ -1546,7 +1554,7 @@ lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file,
|
||||
lfs_size_t nsize = size;
|
||||
|
||||
if ((file->flags & 3) == LFS_O_WRONLY) {
|
||||
return LFS_ERR_BADF;
|
||||
return LFS_ERR_INVAL;
|
||||
}
|
||||
|
||||
if (file->flags & LFS_F_WRITING) {
|
||||
@@ -1602,7 +1610,7 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
|
||||
lfs_size_t nsize = size;
|
||||
|
||||
if ((file->flags & 3) == LFS_O_RDONLY) {
|
||||
return LFS_ERR_BADF;
|
||||
return LFS_ERR_INVAL;
|
||||
}
|
||||
|
||||
if (file->flags & LFS_F_READING) {
|
||||
@@ -1645,7 +1653,7 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
|
||||
}
|
||||
|
||||
// mark cache as dirty since we may have read data into it
|
||||
lfs_cache_zero(lfs, &file->cache);
|
||||
file->cache.block = 0xffffffff;
|
||||
}
|
||||
|
||||
// extend file with new blocks
|
||||
@@ -1725,11 +1733,10 @@ lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file,
|
||||
|
||||
int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) {
|
||||
if ((file->flags & 3) == LFS_O_RDONLY) {
|
||||
return LFS_ERR_BADF;
|
||||
return LFS_ERR_INVAL;
|
||||
}
|
||||
|
||||
lfs_off_t oldsize = lfs_file_size(lfs, file);
|
||||
if (size < oldsize) {
|
||||
if (size < lfs_file_size(lfs, file)) {
|
||||
// need to flush since directly changing metadata
|
||||
int err = lfs_file_flush(lfs, file);
|
||||
if (err) {
|
||||
@@ -1746,13 +1753,13 @@ int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) {
|
||||
|
||||
file->size = size;
|
||||
file->flags |= LFS_F_DIRTY;
|
||||
} else if (size > oldsize) {
|
||||
} else if (size > lfs_file_size(lfs, file)) {
|
||||
lfs_off_t pos = file->pos;
|
||||
|
||||
// flush+seek if not already at end
|
||||
if (file->pos != oldsize) {
|
||||
int err = lfs_file_seek(lfs, file, 0, LFS_SEEK_END);
|
||||
if (err < 0) {
|
||||
if (file->pos != lfs_file_size(lfs, file)) {
|
||||
int err = lfs_file_seek(lfs, file, 0, SEEK_END);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
@@ -1776,7 +1783,6 @@ int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) {
|
||||
}
|
||||
|
||||
lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file) {
|
||||
(void)lfs;
|
||||
return file->pos;
|
||||
}
|
||||
|
||||
@@ -1790,7 +1796,6 @@ int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file) {
|
||||
}
|
||||
|
||||
lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) {
|
||||
(void)lfs;
|
||||
if (file->flags & LFS_F_WRITING) {
|
||||
return lfs_max(file->pos, file->size);
|
||||
} else {
|
||||
@@ -1799,11 +1804,24 @@ lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) {
|
||||
}
|
||||
|
||||
|
||||
/// General fs operations ///
|
||||
/// General fs oprations ///
|
||||
int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) {
|
||||
// check for root, can only be something like '/././../.'
|
||||
if (strspn(path, "/.") == strlen(path)) {
|
||||
memset(info, 0, sizeof(*info));
|
||||
info->type = LFS_TYPE_DIR;
|
||||
strcpy(info->name, "/");
|
||||
return 0;
|
||||
}
|
||||
|
||||
lfs_dir_t cwd;
|
||||
int err = lfs_dir_fetch(lfs, &cwd, lfs->root);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
lfs_entry_t entry;
|
||||
int err = lfs_dir_find(lfs, &cwd, &entry, &path);
|
||||
err = lfs_dir_find(lfs, &cwd, &entry, &path);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
@@ -1814,15 +1832,11 @@ int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) {
|
||||
info->size = entry.d.u.file.size;
|
||||
}
|
||||
|
||||
if (lfs_paircmp(entry.d.u.dir, lfs->root) == 0) {
|
||||
strcpy(info->name, "/");
|
||||
} else {
|
||||
err = lfs_bd_read(lfs, cwd.pair[0],
|
||||
entry.off + 4+entry.d.elen+entry.d.alen,
|
||||
info->name, entry.d.nlen);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
err = lfs_bd_read(lfs, cwd.pair[0],
|
||||
entry.off + 4+entry.d.elen+entry.d.alen,
|
||||
info->name, entry.d.nlen);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -1838,8 +1852,13 @@ int lfs_remove(lfs_t *lfs, const char *path) {
|
||||
}
|
||||
|
||||
lfs_dir_t cwd;
|
||||
int err = lfs_dir_fetch(lfs, &cwd, lfs->root);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
lfs_entry_t entry;
|
||||
int err = lfs_dir_find(lfs, &cwd, &entry, &path);
|
||||
err = lfs_dir_find(lfs, &cwd, &entry, &path);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
@@ -1849,7 +1868,7 @@ int lfs_remove(lfs_t *lfs, const char *path) {
|
||||
// must be empty before removal, checking size
|
||||
// without masking top bit checks for any case where
|
||||
// dir is not empty
|
||||
err = lfs_dir_fetch(lfs, &dir, entry.d.u.dir);
|
||||
int err = lfs_dir_fetch(lfs, &dir, entry.d.u.dir);
|
||||
if (err) {
|
||||
return err;
|
||||
} else if (dir.d.size != sizeof(dir.d)+4) {
|
||||
@@ -1870,11 +1889,11 @@ int lfs_remove(lfs_t *lfs, const char *path) {
|
||||
return res;
|
||||
}
|
||||
|
||||
LFS_ASSERT(res); // must have pred
|
||||
assert(res); // must have pred
|
||||
cwd.d.tail[0] = dir.d.tail[0];
|
||||
cwd.d.tail[1] = dir.d.tail[1];
|
||||
|
||||
err = lfs_dir_commit(lfs, &cwd, NULL, 0);
|
||||
int err = lfs_dir_commit(lfs, &cwd, NULL, 0);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
@@ -1894,14 +1913,24 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
|
||||
|
||||
// find old entry
|
||||
lfs_dir_t oldcwd;
|
||||
int err = lfs_dir_fetch(lfs, &oldcwd, lfs->root);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
lfs_entry_t oldentry;
|
||||
int err = lfs_dir_find(lfs, &oldcwd, &oldentry, &oldpath);
|
||||
err = lfs_dir_find(lfs, &oldcwd, &oldentry, &oldpath);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
// allocate new entry
|
||||
lfs_dir_t newcwd;
|
||||
err = lfs_dir_fetch(lfs, &newcwd, lfs->root);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
lfs_entry_t preventry;
|
||||
err = lfs_dir_find(lfs, &newcwd, &preventry, &newpath);
|
||||
if (err && (err != LFS_ERR_NOENT || strchr(newpath, '/') != NULL)) {
|
||||
@@ -1913,7 +1942,7 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
|
||||
|
||||
// must have same type
|
||||
if (prevexists && preventry.d.type != oldentry.d.type) {
|
||||
return LFS_ERR_ISDIR;
|
||||
return LFS_ERR_INVAL;
|
||||
}
|
||||
|
||||
lfs_dir_t dir;
|
||||
@@ -1921,11 +1950,11 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
|
||||
// must be empty before removal, checking size
|
||||
// without masking top bit checks for any case where
|
||||
// dir is not empty
|
||||
err = lfs_dir_fetch(lfs, &dir, preventry.d.u.dir);
|
||||
int err = lfs_dir_fetch(lfs, &dir, preventry.d.u.dir);
|
||||
if (err) {
|
||||
return err;
|
||||
} else if (dir.d.size != sizeof(dir.d)+4) {
|
||||
return LFS_ERR_NOTEMPTY;
|
||||
return LFS_ERR_INVAL;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1948,12 +1977,12 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
|
||||
newentry.d.nlen = strlen(newpath);
|
||||
|
||||
if (prevexists) {
|
||||
err = lfs_dir_update(lfs, &newcwd, &newentry, newpath);
|
||||
int err = lfs_dir_update(lfs, &newcwd, &newentry, newpath);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
} else {
|
||||
err = lfs_dir_append(lfs, &newcwd, &newentry, newpath);
|
||||
int err = lfs_dir_append(lfs, &newcwd, &newentry, newpath);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
@@ -1977,11 +2006,11 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
|
||||
return res;
|
||||
}
|
||||
|
||||
LFS_ASSERT(res); // must have pred
|
||||
assert(res); // must have pred
|
||||
newcwd.d.tail[0] = dir.d.tail[0];
|
||||
newcwd.d.tail[1] = dir.d.tail[1];
|
||||
|
||||
err = lfs_dir_commit(lfs, &newcwd, NULL, 0);
|
||||
int err = lfs_dir_commit(lfs, &newcwd, NULL, 0);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
@@ -1996,47 +2025,45 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
|
||||
lfs->cfg = cfg;
|
||||
|
||||
// setup read cache
|
||||
lfs->rcache.block = 0xffffffff;
|
||||
if (lfs->cfg->read_buffer) {
|
||||
lfs->rcache.buffer = lfs->cfg->read_buffer;
|
||||
} else {
|
||||
lfs->rcache.buffer = lfs_malloc(lfs->cfg->read_size);
|
||||
lfs->rcache.buffer = malloc(lfs->cfg->read_size);
|
||||
if (!lfs->rcache.buffer) {
|
||||
return LFS_ERR_NOMEM;
|
||||
}
|
||||
}
|
||||
|
||||
// setup program cache
|
||||
lfs->pcache.block = 0xffffffff;
|
||||
if (lfs->cfg->prog_buffer) {
|
||||
lfs->pcache.buffer = lfs->cfg->prog_buffer;
|
||||
} else {
|
||||
lfs->pcache.buffer = lfs_malloc(lfs->cfg->prog_size);
|
||||
lfs->pcache.buffer = malloc(lfs->cfg->prog_size);
|
||||
if (!lfs->pcache.buffer) {
|
||||
return LFS_ERR_NOMEM;
|
||||
}
|
||||
}
|
||||
|
||||
// zero to avoid information leaks
|
||||
lfs_cache_zero(lfs, &lfs->rcache);
|
||||
lfs_cache_zero(lfs, &lfs->pcache);
|
||||
|
||||
// setup lookahead, round down to nearest 32-bits
|
||||
LFS_ASSERT(lfs->cfg->lookahead % 32 == 0);
|
||||
LFS_ASSERT(lfs->cfg->lookahead > 0);
|
||||
assert(lfs->cfg->lookahead % 32 == 0);
|
||||
assert(lfs->cfg->lookahead > 0);
|
||||
if (lfs->cfg->lookahead_buffer) {
|
||||
lfs->free.buffer = lfs->cfg->lookahead_buffer;
|
||||
} else {
|
||||
lfs->free.buffer = lfs_malloc(lfs->cfg->lookahead/8);
|
||||
lfs->free.buffer = malloc(lfs->cfg->lookahead/8);
|
||||
if (!lfs->free.buffer) {
|
||||
return LFS_ERR_NOMEM;
|
||||
}
|
||||
}
|
||||
|
||||
// check that program and read sizes are multiples of the block size
|
||||
LFS_ASSERT(lfs->cfg->prog_size % lfs->cfg->read_size == 0);
|
||||
LFS_ASSERT(lfs->cfg->block_size % lfs->cfg->prog_size == 0);
|
||||
assert(lfs->cfg->prog_size % lfs->cfg->read_size == 0);
|
||||
assert(lfs->cfg->block_size % lfs->cfg->prog_size == 0);
|
||||
|
||||
// check that the block size is large enough to fit ctz pointers
|
||||
LFS_ASSERT(4*lfs_npw2(0xffffffff / (lfs->cfg->block_size-2*4))
|
||||
assert(4*lfs_npw2(0xffffffff / (lfs->cfg->block_size-2*4))
|
||||
<= lfs->cfg->block_size);
|
||||
|
||||
// setup default state
|
||||
@@ -2052,15 +2079,15 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
|
||||
static int lfs_deinit(lfs_t *lfs) {
|
||||
// free allocated memory
|
||||
if (!lfs->cfg->read_buffer) {
|
||||
lfs_free(lfs->rcache.buffer);
|
||||
free(lfs->rcache.buffer);
|
||||
}
|
||||
|
||||
if (!lfs->cfg->prog_buffer) {
|
||||
lfs_free(lfs->pcache.buffer);
|
||||
free(lfs->pcache.buffer);
|
||||
}
|
||||
|
||||
if (!lfs->cfg->lookahead_buffer) {
|
||||
lfs_free(lfs->free.buffer);
|
||||
free(lfs->free.buffer);
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -2074,12 +2101,12 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) {
|
||||
|
||||
// create free lookahead
|
||||
memset(lfs->free.buffer, 0, lfs->cfg->lookahead/8);
|
||||
lfs->free.begin = 0;
|
||||
lfs->free.off = 0;
|
||||
lfs->free.size = lfs_min(lfs->cfg->lookahead, lfs->cfg->block_count);
|
||||
lfs->free.i = 0;
|
||||
lfs_alloc_ack(lfs);
|
||||
lfs->free.end = lfs->free.begin + lfs->free.off + lfs->cfg->block_count;
|
||||
|
||||
// create superblock dir
|
||||
lfs_alloc_ack(lfs);
|
||||
lfs_dir_t superdir;
|
||||
err = lfs_dir_alloc(lfs, &superdir);
|
||||
if (err) {
|
||||
@@ -2107,7 +2134,7 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) {
|
||||
.d.type = LFS_TYPE_SUPERBLOCK,
|
||||
.d.elen = sizeof(superblock.d) - sizeof(superblock.d.magic) - 4,
|
||||
.d.nlen = sizeof(superblock.d.magic),
|
||||
.d.version = LFS_DISK_VERSION,
|
||||
.d.version = 0x00010001,
|
||||
.d.magic = {"littlefs"},
|
||||
.d.block_size = lfs->cfg->block_size,
|
||||
.d.block_count = lfs->cfg->block_count,
|
||||
@@ -2121,7 +2148,7 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) {
|
||||
lfs_superblock_tole32(&superblock.d);
|
||||
bool valid = false;
|
||||
for (int i = 0; i < 2; i++) {
|
||||
err = lfs_dir_commit(lfs, &superdir, (struct lfs_region[]){
|
||||
int err = lfs_dir_commit(lfs, &superdir, (struct lfs_region[]){
|
||||
{sizeof(superdir.d), sizeof(superblock.d),
|
||||
&superblock.d, sizeof(superblock.d)}
|
||||
}, 1);
|
||||
@@ -2153,10 +2180,9 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
|
||||
}
|
||||
|
||||
// setup free lookahead
|
||||
lfs->free.off = 0;
|
||||
lfs->free.size = 0;
|
||||
lfs->free.i = 0;
|
||||
lfs_alloc_ack(lfs);
|
||||
lfs->free.begin = -lfs_min(lfs->cfg->lookahead, lfs->cfg->block_count);
|
||||
lfs->free.off = -lfs->free.begin;
|
||||
lfs->free.end = lfs->free.begin + lfs->free.off + lfs->cfg->block_count;
|
||||
|
||||
// load superblock
|
||||
lfs_dir_t dir;
|
||||
@@ -2167,7 +2193,7 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
|
||||
}
|
||||
|
||||
if (!err) {
|
||||
err = lfs_bd_read(lfs, dir.pair[0], sizeof(dir.d),
|
||||
int err = lfs_bd_read(lfs, dir.pair[0], sizeof(dir.d),
|
||||
&superblock.d, sizeof(superblock.d));
|
||||
lfs_superblock_fromle32(&superblock.d);
|
||||
if (err) {
|
||||
@@ -2179,15 +2205,14 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
|
||||
}
|
||||
|
||||
if (err || memcmp(superblock.d.magic, "littlefs", 8) != 0) {
|
||||
LFS_ERROR("Invalid superblock at %d %d", 0, 1);
|
||||
LFS_ERROR("Invalid superblock at %d %d", dir.pair[0], dir.pair[1]);
|
||||
return LFS_ERR_CORRUPT;
|
||||
}
|
||||
|
||||
uint16_t major_version = (0xffff & (superblock.d.version >> 16));
|
||||
uint16_t minor_version = (0xffff & (superblock.d.version >> 0));
|
||||
if ((major_version != LFS_DISK_VERSION_MAJOR ||
|
||||
minor_version > LFS_DISK_VERSION_MINOR)) {
|
||||
LFS_ERROR("Invalid version %d.%d", major_version, minor_version);
|
||||
if (superblock.d.version > (0x00010001 | 0x0000ffff)) {
|
||||
LFS_ERROR("Invalid version %d.%d",
|
||||
0xffff & (superblock.d.version >> 16),
|
||||
0xffff & (superblock.d.version >> 0));
|
||||
return LFS_ERR_INVAL;
|
||||
}
|
||||
|
||||
@@ -2225,7 +2250,7 @@ int lfs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data) {
|
||||
|
||||
// iterate over contents
|
||||
while (dir.off + sizeof(entry.d) <= (0x7fffffff & dir.d.size)-4) {
|
||||
err = lfs_bd_read(lfs, dir.pair[0], dir.off,
|
||||
int err = lfs_bd_read(lfs, dir.pair[0], dir.off,
|
||||
&entry.d, sizeof(entry.d));
|
||||
lfs_entry_fromle32(&entry.d);
|
||||
if (err) {
|
||||
@@ -2234,7 +2259,7 @@ int lfs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data) {
|
||||
|
||||
dir.off += lfs_entry_size(&entry);
|
||||
if ((0x70 & entry.d.type) == (0x70 & LFS_TYPE_REG)) {
|
||||
err = lfs_ctz_traverse(lfs, &lfs->rcache, NULL,
|
||||
int err = lfs_ctz_traverse(lfs, &lfs->rcache, NULL,
|
||||
entry.d.u.file.head, entry.d.u.file.size, cb, data);
|
||||
if (err) {
|
||||
return err;
|
||||
@@ -2288,7 +2313,7 @@ static int lfs_pred(lfs_t *lfs, const lfs_block_t dir[2], lfs_dir_t *pdir) {
|
||||
return true;
|
||||
}
|
||||
|
||||
err = lfs_dir_fetch(lfs, pdir, pdir->d.tail);
|
||||
int err = lfs_dir_fetch(lfs, pdir, pdir->d.tail);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
@@ -2314,7 +2339,7 @@ static int lfs_parent(lfs_t *lfs, const lfs_block_t dir[2],
|
||||
}
|
||||
|
||||
while (true) {
|
||||
err = lfs_dir_next(lfs, parent, entry);
|
||||
int err = lfs_dir_next(lfs, parent, entry);
|
||||
if (err && err != LFS_ERR_NOENT) {
|
||||
return err;
|
||||
}
|
||||
@@ -2348,13 +2373,13 @@ static int lfs_moved(lfs_t *lfs, const void *e) {
|
||||
// iterate over all directory directory entries
|
||||
lfs_entry_t entry;
|
||||
while (!lfs_pairisnull(cwd.d.tail)) {
|
||||
err = lfs_dir_fetch(lfs, &cwd, cwd.d.tail);
|
||||
int err = lfs_dir_fetch(lfs, &cwd, cwd.d.tail);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
while (true) {
|
||||
err = lfs_dir_next(lfs, &cwd, &entry);
|
||||
int err = lfs_dir_next(lfs, &cwd, &entry);
|
||||
if (err && err != LFS_ERR_NOENT) {
|
||||
return err;
|
||||
}
|
||||
@@ -2485,7 +2510,7 @@ int lfs_deorphan(lfs_t *lfs) {
|
||||
// check entries for moves
|
||||
lfs_entry_t entry;
|
||||
while (true) {
|
||||
err = lfs_dir_next(lfs, &cwd, &entry);
|
||||
int err = lfs_dir_next(lfs, &cwd, &entry);
|
||||
if (err && err != LFS_ERR_NOENT) {
|
||||
return err;
|
||||
}
|
||||
@@ -2504,7 +2529,7 @@ int lfs_deorphan(lfs_t *lfs) {
|
||||
if (moved) {
|
||||
LFS_DEBUG("Found move %d %d",
|
||||
entry.d.u.dir[0], entry.d.u.dir[1]);
|
||||
err = lfs_dir_remove(lfs, &cwd, &entry);
|
||||
int err = lfs_dir_remove(lfs, &cwd, &entry);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
@@ -2512,7 +2537,7 @@ int lfs_deorphan(lfs_t *lfs) {
|
||||
LFS_DEBUG("Found partial move %d %d",
|
||||
entry.d.u.dir[0], entry.d.u.dir[1]);
|
||||
entry.d.type &= ~0x80;
|
||||
err = lfs_dir_update(lfs, &cwd, &entry, NULL);
|
||||
int err = lfs_dir_update(lfs, &cwd, &entry, NULL);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
42
lfs.h
42
lfs.h
@@ -1,8 +1,19 @@
|
||||
/*
|
||||
* The little filesystem
|
||||
*
|
||||
* Copyright (c) 2017, Arm Limited. All rights reserved.
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2017 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef LFS_H
|
||||
#define LFS_H
|
||||
@@ -11,23 +22,6 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
/// Version info ///
|
||||
|
||||
// Software library version
|
||||
// Major (top-nibble), incremented on backwards incompatible changes
|
||||
// Minor (bottom-nibble), incremented on feature additions
|
||||
#define LFS_VERSION 0x00010004
|
||||
#define LFS_VERSION_MAJOR (0xffff & (LFS_VERSION >> 16))
|
||||
#define LFS_VERSION_MINOR (0xffff & (LFS_VERSION >> 0))
|
||||
|
||||
// Version of On-disk data structures
|
||||
// Major (top-nibble), incremented on backwards incompatible changes
|
||||
// Minor (bottom-nibble), incremented on feature additions
|
||||
#define LFS_DISK_VERSION 0x00010001
|
||||
#define LFS_DISK_VERSION_MAJOR (0xffff & (LFS_DISK_VERSION >> 16))
|
||||
#define LFS_DISK_VERSION_MINOR (0xffff & (LFS_DISK_VERSION >> 0))
|
||||
|
||||
|
||||
/// Definitions ///
|
||||
|
||||
// Type definitions
|
||||
@@ -55,7 +49,6 @@ enum lfs_error {
|
||||
LFS_ERR_NOTDIR = -20, // Entry is not a dir
|
||||
LFS_ERR_ISDIR = -21, // Entry is a dir
|
||||
LFS_ERR_NOTEMPTY = -39, // Dir is not empty
|
||||
LFS_ERR_BADF = -9, // Bad file number
|
||||
LFS_ERR_INVAL = -22, // Invalid parameter
|
||||
LFS_ERR_NOSPC = -28, // No space left on device
|
||||
LFS_ERR_NOMEM = -12, // No more memory available
|
||||
@@ -248,10 +241,9 @@ typedef struct lfs_superblock {
|
||||
} lfs_superblock_t;
|
||||
|
||||
typedef struct lfs_free {
|
||||
lfs_block_t begin;
|
||||
lfs_block_t end;
|
||||
lfs_block_t off;
|
||||
lfs_block_t size;
|
||||
lfs_block_t i;
|
||||
lfs_block_t ack;
|
||||
uint32_t *buffer;
|
||||
} lfs_free_t;
|
||||
|
||||
@@ -309,6 +301,10 @@ int lfs_remove(lfs_t *lfs, const char *path);
|
||||
// If the destination exists, it must match the source in type.
|
||||
// If the destination is a directory, the directory must be empty.
|
||||
//
|
||||
// Note: If power loss occurs, it is possible that the file or directory
|
||||
// will exist in both the oldpath and newpath simultaneously after the
|
||||
// next mount.
|
||||
//
|
||||
// Returns a negative error code on failure.
|
||||
int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath);
|
||||
|
||||
|
||||
21
lfs_util.c
21
lfs_util.c
@@ -1,16 +1,23 @@
|
||||
/*
|
||||
* lfs util functions
|
||||
*
|
||||
* Copyright (c) 2017, Arm Limited. All rights reserved.
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2017 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "lfs_util.h"
|
||||
|
||||
// Only compile if user does not provide custom config
|
||||
#ifndef LFS_CONFIG
|
||||
|
||||
|
||||
// Software CRC implementation with small lookup table
|
||||
void lfs_crc(uint32_t *restrict crc, const void *buffer, size_t size) {
|
||||
static const uint32_t rtable[16] = {
|
||||
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
|
||||
@@ -27,5 +34,3 @@ void lfs_crc(uint32_t *restrict crc, const void *buffer, size_t size) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
153
lfs_util.h
153
lfs_util.h
@@ -1,79 +1,30 @@
|
||||
/*
|
||||
* lfs utility functions
|
||||
*
|
||||
* Copyright (c) 2017, Arm Limited. All rights reserved.
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2017 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef LFS_UTIL_H
|
||||
#define LFS_UTIL_H
|
||||
|
||||
// Users can override lfs_util.h with their own configuration by defining
|
||||
// LFS_CONFIG as a header file to include (-DLFS_CONFIG=lfs_config.h).
|
||||
//
|
||||
// If LFS_CONFIG is used, none of the default utils will be emitted and must be
|
||||
// provided by the config file. To start I would suggest copying lfs_util.h and
|
||||
// modifying as needed.
|
||||
#ifdef LFS_CONFIG
|
||||
#define LFS_STRINGIZE(x) LFS_STRINGIZE2(x)
|
||||
#define LFS_STRINGIZE2(x) #x
|
||||
#include LFS_STRINGIZE(LFS_CONFIG)
|
||||
#else
|
||||
|
||||
// System includes
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef LFS_NO_MALLOC
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
#ifndef LFS_NO_ASSERT
|
||||
#include <assert.h>
|
||||
#endif
|
||||
#if !defined(LFS_NO_DEBUG) || !defined(LFS_NO_WARN) || !defined(LFS_NO_ERROR)
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
|
||||
// Macros, may be replaced by system specific wrappers. Arguments to these
|
||||
// macros must not have side-effects as the macros can be removed for a smaller
|
||||
// code footprint
|
||||
|
||||
// Logging functions
|
||||
#ifndef LFS_NO_DEBUG
|
||||
#define LFS_DEBUG(fmt, ...) \
|
||||
printf("lfs debug:%d: " fmt "\n", __LINE__, __VA_ARGS__)
|
||||
#else
|
||||
#define LFS_DEBUG(fmt, ...)
|
||||
#endif
|
||||
|
||||
#ifndef LFS_NO_WARN
|
||||
#define LFS_WARN(fmt, ...) \
|
||||
printf("lfs warn:%d: " fmt "\n", __LINE__, __VA_ARGS__)
|
||||
#else
|
||||
#define LFS_WARN(fmt, ...)
|
||||
#endif
|
||||
|
||||
#ifndef LFS_NO_ERROR
|
||||
#define LFS_ERROR(fmt, ...) \
|
||||
printf("lfs error:%d: " fmt "\n", __LINE__, __VA_ARGS__)
|
||||
#else
|
||||
#define LFS_ERROR(fmt, ...)
|
||||
#endif
|
||||
|
||||
// Runtime assertions
|
||||
#ifndef LFS_NO_ASSERT
|
||||
#define LFS_ASSERT(test) assert(test)
|
||||
#else
|
||||
#define LFS_ASSERT(test)
|
||||
#endif
|
||||
|
||||
|
||||
// Builtin functions, these may be replaced by more efficient
|
||||
// toolchain-specific implementations. LFS_NO_INTRINSICS falls back to a more
|
||||
// expensive basic C implementation for debugging purposes
|
||||
|
||||
// Min/max functions for unsigned 32-bit numbers
|
||||
// Builtin functions, these may be replaced by more
|
||||
// efficient implementations in the system
|
||||
static inline uint32_t lfs_max(uint32_t a, uint32_t b) {
|
||||
return (a > b) ? a : b;
|
||||
}
|
||||
@@ -82,60 +33,26 @@ static inline uint32_t lfs_min(uint32_t a, uint32_t b) {
|
||||
return (a < b) ? a : b;
|
||||
}
|
||||
|
||||
// Find the next smallest power of 2 less than or equal to a
|
||||
static inline uint32_t lfs_npw2(uint32_t a) {
|
||||
#if !defined(LFS_NO_INTRINSICS) && (defined(__GNUC__) || defined(__CC_ARM))
|
||||
return 32 - __builtin_clz(a-1);
|
||||
#else
|
||||
uint32_t r = 0;
|
||||
uint32_t s;
|
||||
a -= 1;
|
||||
s = (a > 0xffff) << 4; a >>= s; r |= s;
|
||||
s = (a > 0xff ) << 3; a >>= s; r |= s;
|
||||
s = (a > 0xf ) << 2; a >>= s; r |= s;
|
||||
s = (a > 0x3 ) << 1; a >>= s; r |= s;
|
||||
return (r | (a >> 1)) + 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Count the number of trailing binary zeros in a
|
||||
// lfs_ctz(0) may be undefined
|
||||
static inline uint32_t lfs_ctz(uint32_t a) {
|
||||
#if !defined(LFS_NO_INTRINSICS) && defined(__GNUC__)
|
||||
return __builtin_ctz(a);
|
||||
#else
|
||||
return lfs_npw2((a & -a) + 1) - 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Count the number of binary ones in a
|
||||
static inline uint32_t lfs_npw2(uint32_t a) {
|
||||
return 32 - __builtin_clz(a-1);
|
||||
}
|
||||
|
||||
static inline uint32_t lfs_popc(uint32_t a) {
|
||||
#if !defined(LFS_NO_INTRINSICS) && (defined(__GNUC__) || defined(__CC_ARM))
|
||||
return __builtin_popcount(a);
|
||||
#else
|
||||
a = a - ((a >> 1) & 0x55555555);
|
||||
a = (a & 0x33333333) + ((a >> 2) & 0x33333333);
|
||||
return (((a + (a >> 4)) & 0xf0f0f0f) * 0x1010101) >> 24;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Find the sequence comparison of a and b, this is the distance
|
||||
// between a and b ignoring overflow
|
||||
static inline int lfs_scmp(uint32_t a, uint32_t b) {
|
||||
return (int)(unsigned)(a - b);
|
||||
}
|
||||
|
||||
// Convert from 32-bit little-endian to native order
|
||||
static inline uint32_t lfs_fromle32(uint32_t a) {
|
||||
#if !defined(LFS_NO_INTRINSICS) && ( \
|
||||
(defined( BYTE_ORDER ) && BYTE_ORDER == ORDER_LITTLE_ENDIAN ) || \
|
||||
(defined(__BYTE_ORDER ) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN ) || \
|
||||
(defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
|
||||
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||
return a;
|
||||
#elif !defined(LFS_NO_INTRINSICS) && ( \
|
||||
(defined( BYTE_ORDER ) && BYTE_ORDER == ORDER_BIG_ENDIAN ) || \
|
||||
(defined(__BYTE_ORDER ) && __BYTE_ORDER == __ORDER_BIG_ENDIAN ) || \
|
||||
(defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
|
||||
#elif defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
||||
return __builtin_bswap32(a);
|
||||
#else
|
||||
return (((uint8_t*)&a)[0] << 0) |
|
||||
@@ -145,33 +62,19 @@ static inline uint32_t lfs_fromle32(uint32_t a) {
|
||||
#endif
|
||||
}
|
||||
|
||||
// Convert to 32-bit little-endian from native order
|
||||
static inline uint32_t lfs_tole32(uint32_t a) {
|
||||
return lfs_fromle32(a);
|
||||
}
|
||||
|
||||
// Calculate CRC-32 with polynomial = 0x04c11db7
|
||||
// CRC-32 with polynomial = 0x04c11db7
|
||||
void lfs_crc(uint32_t *crc, const void *buffer, size_t size);
|
||||
|
||||
// Allocate memory, only used if buffers are not provided to littlefs
|
||||
static inline void *lfs_malloc(size_t size) {
|
||||
#ifndef LFS_NO_MALLOC
|
||||
return malloc(size);
|
||||
#else
|
||||
(void)size;
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Deallocate memory, only used if buffers are not provided to littlefs
|
||||
static inline void lfs_free(void *p) {
|
||||
#ifndef LFS_NO_MALLOC
|
||||
free(p);
|
||||
#else
|
||||
(void)p;
|
||||
#endif
|
||||
}
|
||||
// Logging functions, these may be replaced by system-specific
|
||||
// logging functions
|
||||
#define LFS_DEBUG(fmt, ...) printf("lfs debug: " fmt "\n", __VA_ARGS__)
|
||||
#define LFS_WARN(fmt, ...) printf("lfs warn: " fmt "\n", __VA_ARGS__)
|
||||
#define LFS_ERROR(fmt, ...) printf("lfs error: " fmt "\n", __VA_ARGS__)
|
||||
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -7,11 +7,11 @@
|
||||
|
||||
|
||||
// test stuff
|
||||
static void test_log(const char *s, uintmax_t v) {{
|
||||
void test_log(const char *s, uintmax_t v) {{
|
||||
printf("%s: %jd\n", s, v);
|
||||
}}
|
||||
|
||||
static void test_assert(const char *file, unsigned line,
|
||||
void test_assert(const char *file, unsigned line,
|
||||
const char *s, uintmax_t v, uintmax_t e) {{
|
||||
static const char *last[6] = {{0, 0}};
|
||||
if (v != e || !(last[0] == s || last[1] == s ||
|
||||
@@ -37,8 +37,7 @@ static void test_assert(const char *file, unsigned line,
|
||||
|
||||
|
||||
// utility functions for traversals
|
||||
static int __attribute__((used)) test_count(void *p, lfs_block_t b) {{
|
||||
(void)b;
|
||||
int test_count(void *p, lfs_block_t b) {{
|
||||
unsigned *u = (unsigned*)p;
|
||||
*u += 1;
|
||||
return 0;
|
||||
@@ -59,7 +58,7 @@ lfs_size_t size;
|
||||
lfs_size_t wsize;
|
||||
lfs_size_t rsize;
|
||||
|
||||
uintmax_t test;
|
||||
uintmax_t res;
|
||||
|
||||
#ifndef LFS_READ_SIZE
|
||||
#define LFS_READ_SIZE 16
|
||||
@@ -97,7 +96,7 @@ const struct lfs_config cfg = {{
|
||||
|
||||
|
||||
// Entry point
|
||||
int main(void) {{
|
||||
int main() {{
|
||||
lfs_emubd_create(&cfg, "blocks");
|
||||
|
||||
{tests}
|
||||
|
||||
@@ -14,34 +14,22 @@ def generate(test):
|
||||
match = re.match('(?: *\n)*( *)(.*)=>(.*);', line, re.DOTALL | re.MULTILINE)
|
||||
if match:
|
||||
tab, test, expect = match.groups()
|
||||
lines.append(tab+'test = {test};'.format(test=test.strip()))
|
||||
lines.append(tab+'test_assert("{name}", test, {expect});'.format(
|
||||
lines.append(tab+'res = {test};'.format(test=test.strip()))
|
||||
lines.append(tab+'test_assert("{name}", res, {expect});'.format(
|
||||
name = re.match('\w*', test.strip()).group(),
|
||||
expect = expect.strip()))
|
||||
else:
|
||||
lines.append(line)
|
||||
|
||||
# Create test file
|
||||
with open('test.c', 'w') as file:
|
||||
file.write(template.format(tests='\n'.join(lines)))
|
||||
|
||||
# Remove build artifacts to force rebuild
|
||||
try:
|
||||
os.remove('test.o')
|
||||
os.remove('lfs')
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
def compile():
|
||||
subprocess.check_call([
|
||||
os.environ.get('MAKE', 'make'),
|
||||
'--no-print-directory', '-s'])
|
||||
os.environ['CFLAGS'] = os.environ.get('CFLAGS', '') + ' -Werror'
|
||||
subprocess.check_call(['make', '--no-print-directory', '-s'], env=os.environ)
|
||||
|
||||
def execute():
|
||||
if 'EXEC' in os.environ:
|
||||
subprocess.check_call([os.environ['EXEC'], "./lfs"])
|
||||
else:
|
||||
subprocess.check_call(["./lfs"])
|
||||
subprocess.check_call(["./lfs"])
|
||||
|
||||
def main(test=None):
|
||||
if test and not test.startswith('-'):
|
||||
|
||||
@@ -266,163 +266,6 @@ tests/test.py << TEST
|
||||
lfs_mkdir(&lfs, "exhaustiondir2") => LFS_ERR_NOSPC;
|
||||
TEST
|
||||
|
||||
echo "--- Split dir test ---"
|
||||
rm -rf blocks
|
||||
tests/test.py << TEST
|
||||
lfs_format(&lfs, &cfg) => 0;
|
||||
TEST
|
||||
tests/test.py << TEST
|
||||
lfs_mount(&lfs, &cfg) => 0;
|
||||
|
||||
// create one block whole for half a directory
|
||||
lfs_file_open(&lfs, &file[0], "bump", LFS_O_WRONLY | LFS_O_CREAT) => 0;
|
||||
lfs_file_write(&lfs, &file[0], (void*)"hi", 2) => 2;
|
||||
lfs_file_close(&lfs, &file[0]) => 0;
|
||||
|
||||
lfs_file_open(&lfs, &file[0], "exhaustion", LFS_O_WRONLY | LFS_O_CREAT);
|
||||
size = strlen("blahblahblahblah");
|
||||
memcpy(buffer, "blahblahblahblah", size);
|
||||
for (lfs_size_t i = 0;
|
||||
i < (cfg.block_count-6)*(cfg.block_size-8);
|
||||
i += size) {
|
||||
lfs_file_write(&lfs, &file[0], buffer, size) => size;
|
||||
}
|
||||
lfs_file_close(&lfs, &file[0]) => 0;
|
||||
|
||||
// open hole
|
||||
lfs_remove(&lfs, "bump") => 0;
|
||||
|
||||
lfs_mkdir(&lfs, "splitdir") => 0;
|
||||
lfs_file_open(&lfs, &file[0], "splitdir/bump",
|
||||
LFS_O_WRONLY | LFS_O_CREAT) => 0;
|
||||
lfs_file_write(&lfs, &file[0], buffer, size) => LFS_ERR_NOSPC;
|
||||
lfs_file_close(&lfs, &file[0]) => 0;
|
||||
|
||||
lfs_unmount(&lfs) => 0;
|
||||
TEST
|
||||
|
||||
echo "--- Outdated lookahead test ---"
|
||||
rm -rf blocks
|
||||
tests/test.py << TEST
|
||||
lfs_format(&lfs, &cfg) => 0;
|
||||
|
||||
lfs_mount(&lfs, &cfg) => 0;
|
||||
|
||||
// fill completely with two files
|
||||
lfs_file_open(&lfs, &file[0], "exhaustion1",
|
||||
LFS_O_WRONLY | LFS_O_CREAT) => 0;
|
||||
size = strlen("blahblahblahblah");
|
||||
memcpy(buffer, "blahblahblahblah", size);
|
||||
for (lfs_size_t i = 0;
|
||||
i < ((cfg.block_count-4)/2)*(cfg.block_size-8);
|
||||
i += size) {
|
||||
lfs_file_write(&lfs, &file[0], buffer, size) => size;
|
||||
}
|
||||
lfs_file_close(&lfs, &file[0]) => 0;
|
||||
|
||||
lfs_file_open(&lfs, &file[0], "exhaustion2",
|
||||
LFS_O_WRONLY | LFS_O_CREAT) => 0;
|
||||
size = strlen("blahblahblahblah");
|
||||
memcpy(buffer, "blahblahblahblah", size);
|
||||
for (lfs_size_t i = 0;
|
||||
i < ((cfg.block_count-4+1)/2)*(cfg.block_size-8);
|
||||
i += size) {
|
||||
lfs_file_write(&lfs, &file[0], buffer, size) => size;
|
||||
}
|
||||
lfs_file_close(&lfs, &file[0]) => 0;
|
||||
|
||||
// remount to force reset of lookahead
|
||||
lfs_unmount(&lfs) => 0;
|
||||
|
||||
lfs_mount(&lfs, &cfg) => 0;
|
||||
|
||||
// rewrite one file
|
||||
lfs_file_open(&lfs, &file[0], "exhaustion1",
|
||||
LFS_O_WRONLY | LFS_O_TRUNC) => 0;
|
||||
lfs_file_sync(&lfs, &file[0]) => 0;
|
||||
size = strlen("blahblahblahblah");
|
||||
memcpy(buffer, "blahblahblahblah", size);
|
||||
for (lfs_size_t i = 0;
|
||||
i < ((cfg.block_count-4)/2)*(cfg.block_size-8);
|
||||
i += size) {
|
||||
lfs_file_write(&lfs, &file[0], buffer, size) => size;
|
||||
}
|
||||
lfs_file_close(&lfs, &file[0]) => 0;
|
||||
|
||||
// rewrite second file, this requires lookahead does not
|
||||
// use old population
|
||||
lfs_file_open(&lfs, &file[0], "exhaustion2",
|
||||
LFS_O_WRONLY | LFS_O_TRUNC) => 0;
|
||||
lfs_file_sync(&lfs, &file[0]) => 0;
|
||||
size = strlen("blahblahblahblah");
|
||||
memcpy(buffer, "blahblahblahblah", size);
|
||||
for (lfs_size_t i = 0;
|
||||
i < ((cfg.block_count-4+1)/2)*(cfg.block_size-8);
|
||||
i += size) {
|
||||
lfs_file_write(&lfs, &file[0], buffer, size) => size;
|
||||
}
|
||||
lfs_file_close(&lfs, &file[0]) => 0;
|
||||
TEST
|
||||
|
||||
echo "--- Outdated lookahead and split dir test ---"
|
||||
rm -rf blocks
|
||||
tests/test.py << TEST
|
||||
lfs_format(&lfs, &cfg) => 0;
|
||||
|
||||
lfs_mount(&lfs, &cfg) => 0;
|
||||
|
||||
// fill completely with two files
|
||||
lfs_file_open(&lfs, &file[0], "exhaustion1",
|
||||
LFS_O_WRONLY | LFS_O_CREAT) => 0;
|
||||
size = strlen("blahblahblahblah");
|
||||
memcpy(buffer, "blahblahblahblah", size);
|
||||
for (lfs_size_t i = 0;
|
||||
i < ((cfg.block_count-4)/2)*(cfg.block_size-8);
|
||||
i += size) {
|
||||
lfs_file_write(&lfs, &file[0], buffer, size) => size;
|
||||
}
|
||||
lfs_file_close(&lfs, &file[0]) => 0;
|
||||
|
||||
lfs_file_open(&lfs, &file[0], "exhaustion2",
|
||||
LFS_O_WRONLY | LFS_O_CREAT) => 0;
|
||||
size = strlen("blahblahblahblah");
|
||||
memcpy(buffer, "blahblahblahblah", size);
|
||||
for (lfs_size_t i = 0;
|
||||
i < ((cfg.block_count-4+1)/2)*(cfg.block_size-8);
|
||||
i += size) {
|
||||
lfs_file_write(&lfs, &file[0], buffer, size) => size;
|
||||
}
|
||||
lfs_file_close(&lfs, &file[0]) => 0;
|
||||
|
||||
// remount to force reset of lookahead
|
||||
lfs_unmount(&lfs) => 0;
|
||||
|
||||
lfs_mount(&lfs, &cfg) => 0;
|
||||
|
||||
// rewrite one file with a hole of one block
|
||||
lfs_file_open(&lfs, &file[0], "exhaustion1",
|
||||
LFS_O_WRONLY | LFS_O_TRUNC) => 0;
|
||||
lfs_file_sync(&lfs, &file[0]) => 0;
|
||||
size = strlen("blahblahblahblah");
|
||||
memcpy(buffer, "blahblahblahblah", size);
|
||||
for (lfs_size_t i = 0;
|
||||
i < ((cfg.block_count-4)/2 - 1)*(cfg.block_size-8);
|
||||
i += size) {
|
||||
lfs_file_write(&lfs, &file[0], buffer, size) => size;
|
||||
}
|
||||
lfs_file_close(&lfs, &file[0]) => 0;
|
||||
|
||||
// try to allocate a directory, should fail!
|
||||
lfs_mkdir(&lfs, "split") => LFS_ERR_NOSPC;
|
||||
|
||||
// file should not fail
|
||||
lfs_file_open(&lfs, &file[0], "notasplit",
|
||||
LFS_O_WRONLY | LFS_O_CREAT) => 0;
|
||||
lfs_file_write(&lfs, &file[0], "hi", 2) => 2;
|
||||
lfs_file_close(&lfs, &file[0]) => 0;
|
||||
|
||||
lfs_unmount(&lfs) => 0;
|
||||
TEST
|
||||
|
||||
echo "--- Results ---"
|
||||
tests/stats.py
|
||||
|
||||
@@ -118,7 +118,6 @@ tests/test.py << TEST
|
||||
sprintf((char*)buffer, "test%d", i);
|
||||
lfs_dir_read(&lfs, &dir[0], &info) => 1;
|
||||
strcmp(info.name, (char*)buffer) => 0;
|
||||
info.type => LFS_TYPE_DIR;
|
||||
}
|
||||
lfs_dir_read(&lfs, &dir[0], &info) => 0;
|
||||
lfs_unmount(&lfs) => 0;
|
||||
@@ -221,7 +220,7 @@ tests/test.py << TEST
|
||||
lfs_mount(&lfs, &cfg) => 0;
|
||||
lfs_mkdir(&lfs, "warmpotato") => 0;
|
||||
lfs_mkdir(&lfs, "warmpotato/mushy") => 0;
|
||||
lfs_rename(&lfs, "hotpotato", "warmpotato") => LFS_ERR_NOTEMPTY;
|
||||
lfs_rename(&lfs, "hotpotato", "warmpotato") => LFS_ERR_INVAL;
|
||||
|
||||
lfs_remove(&lfs, "warmpotato/mushy") => 0;
|
||||
lfs_rename(&lfs, "hotpotato", "warmpotato") => 0;
|
||||
@@ -356,70 +355,5 @@ tests/test.py << TEST
|
||||
lfs_unmount(&lfs) => 0;
|
||||
TEST
|
||||
|
||||
echo "--- Multi-block directory with files ---"
|
||||
tests/test.py << TEST
|
||||
lfs_mount(&lfs, &cfg) => 0;
|
||||
lfs_mkdir(&lfs, "prickly-pear") => 0;
|
||||
for (int i = 0; i < $LARGESIZE; i++) {
|
||||
sprintf((char*)buffer, "prickly-pear/test%d", i);
|
||||
lfs_file_open(&lfs, &file[0], (char*)buffer,
|
||||
LFS_O_WRONLY | LFS_O_CREAT) => 0;
|
||||
size = 6;
|
||||
memcpy(wbuffer, "Hello", size);
|
||||
lfs_file_write(&lfs, &file[0], wbuffer, size) => size;
|
||||
lfs_file_close(&lfs, &file[0]) => 0;
|
||||
}
|
||||
lfs_unmount(&lfs) => 0;
|
||||
TEST
|
||||
tests/test.py << TEST
|
||||
lfs_mount(&lfs, &cfg) => 0;
|
||||
lfs_dir_open(&lfs, &dir[0], "prickly-pear") => 0;
|
||||
lfs_dir_read(&lfs, &dir[0], &info) => 1;
|
||||
strcmp(info.name, ".") => 0;
|
||||
info.type => LFS_TYPE_DIR;
|
||||
lfs_dir_read(&lfs, &dir[0], &info) => 1;
|
||||
strcmp(info.name, "..") => 0;
|
||||
info.type => LFS_TYPE_DIR;
|
||||
for (int i = 0; i < $LARGESIZE; i++) {
|
||||
sprintf((char*)buffer, "test%d", i);
|
||||
lfs_dir_read(&lfs, &dir[0], &info) => 1;
|
||||
strcmp(info.name, (char*)buffer) => 0;
|
||||
info.type => LFS_TYPE_REG;
|
||||
info.size => 6;
|
||||
}
|
||||
lfs_dir_read(&lfs, &dir[0], &info) => 0;
|
||||
lfs_unmount(&lfs) => 0;
|
||||
TEST
|
||||
|
||||
echo "--- Multi-block remove with files ---"
|
||||
tests/test.py << TEST
|
||||
lfs_mount(&lfs, &cfg) => 0;
|
||||
lfs_remove(&lfs, "prickly-pear") => LFS_ERR_NOTEMPTY;
|
||||
|
||||
for (int i = 0; i < $LARGESIZE; i++) {
|
||||
sprintf((char*)buffer, "prickly-pear/test%d", i);
|
||||
lfs_remove(&lfs, (char*)buffer) => 0;
|
||||
}
|
||||
|
||||
lfs_remove(&lfs, "prickly-pear") => 0;
|
||||
lfs_unmount(&lfs) => 0;
|
||||
TEST
|
||||
tests/test.py << TEST
|
||||
lfs_mount(&lfs, &cfg) => 0;
|
||||
lfs_dir_open(&lfs, &dir[0], "/") => 0;
|
||||
lfs_dir_read(&lfs, &dir[0], &info) => 1;
|
||||
strcmp(info.name, ".") => 0;
|
||||
info.type => LFS_TYPE_DIR;
|
||||
lfs_dir_read(&lfs, &dir[0], &info) => 1;
|
||||
strcmp(info.name, "..") => 0;
|
||||
info.type => LFS_TYPE_DIR;
|
||||
lfs_dir_read(&lfs, &dir[0], &info) => 1;
|
||||
strcmp(info.name, "burito") => 0;
|
||||
info.type => LFS_TYPE_REG;
|
||||
lfs_dir_read(&lfs, &dir[0], &info) => 0;
|
||||
lfs_dir_close(&lfs, &dir[0]) => 0;
|
||||
lfs_unmount(&lfs) => 0;
|
||||
TEST
|
||||
|
||||
echo "--- Results ---"
|
||||
tests/stats.py
|
||||
|
||||
@@ -30,7 +30,7 @@ TEST
|
||||
|
||||
w_test() {
|
||||
tests/test.py << TEST
|
||||
size = $1;
|
||||
lfs_size_t size = $1;
|
||||
lfs_size_t chunk = 31;
|
||||
srand(0);
|
||||
lfs_mount(&lfs, &cfg) => 0;
|
||||
@@ -50,7 +50,7 @@ TEST
|
||||
|
||||
r_test() {
|
||||
tests/test.py << TEST
|
||||
size = $1;
|
||||
lfs_size_t size = $1;
|
||||
lfs_size_t chunk = 29;
|
||||
srand(0);
|
||||
lfs_mount(&lfs, &cfg) => 0;
|
||||
@@ -135,24 +135,5 @@ tests/test.py << TEST
|
||||
lfs_unmount(&lfs) => 0;
|
||||
TEST
|
||||
|
||||
echo "--- Many file test ---"
|
||||
tests/test.py << TEST
|
||||
lfs_format(&lfs, &cfg) => 0;
|
||||
TEST
|
||||
tests/test.py << TEST
|
||||
// Create 300 files of 6 bytes
|
||||
lfs_mount(&lfs, &cfg) => 0;
|
||||
lfs_mkdir(&lfs, "directory") => 0;
|
||||
for (unsigned i = 0; i < 300; i++) {
|
||||
snprintf((char*)buffer, sizeof(buffer), "file_%03d", i);
|
||||
lfs_file_open(&lfs, &file[0], (char*)buffer, LFS_O_WRONLY | LFS_O_CREAT) => 0;
|
||||
size = 6;
|
||||
memcpy(wbuffer, "Hello", size);
|
||||
lfs_file_write(&lfs, &file[0], wbuffer, size) => size;
|
||||
lfs_file_close(&lfs, &file[0]) => 0;
|
||||
}
|
||||
lfs_unmount(&lfs) => 0;
|
||||
TEST
|
||||
|
||||
echo "--- Results ---"
|
||||
tests/stats.py
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
#!/bin/bash
|
||||
set -eu
|
||||
|
||||
echo "=== Interspersed tests ==="
|
||||
echo "=== Parallel tests ==="
|
||||
rm -rf blocks
|
||||
tests/test.py << TEST
|
||||
lfs_format(&lfs, &cfg) => 0;
|
||||
TEST
|
||||
|
||||
echo "--- Interspersed file test ---"
|
||||
echo "--- Parallel file test ---"
|
||||
tests/test.py << TEST
|
||||
lfs_mount(&lfs, &cfg) => 0;
|
||||
lfs_file_open(&lfs, &file[0], "a", LFS_O_WRONLY | LFS_O_CREAT) => 0;
|
||||
@@ -77,7 +77,7 @@ tests/test.py << TEST
|
||||
lfs_unmount(&lfs) => 0;
|
||||
TEST
|
||||
|
||||
echo "--- Interspersed remove file test ---"
|
||||
echo "--- Parallel remove file test ---"
|
||||
tests/test.py << TEST
|
||||
lfs_mount(&lfs, &cfg) => 0;
|
||||
lfs_file_open(&lfs, &file[0], "e", LFS_O_WRONLY | LFS_O_CREAT) => 0;
|
||||
@@ -90,22 +90,6 @@ tests/test.py << TEST
|
||||
lfs_unmount(&lfs) => 0;
|
||||
TEST
|
||||
|
||||
echo "--- Trailing dot path tests ---"
|
||||
tests/test.py << TEST
|
||||
lfs_mount(&lfs, &cfg) => 0;
|
||||
lfs_stat(&lfs, "tea/hottea/", &info) => 0;
|
||||
strcmp(info.name, "hottea") => 0;
|
||||
lfs_stat(&lfs, "tea/hottea/.", &info) => 0;
|
||||
strcmp(info.name, "hottea") => 0;
|
||||
lfs_stat(&lfs, "tea/hottea/./.", &info) => 0;
|
||||
strcmp(info.name, "hottea") => 0;
|
||||
lfs_stat(&lfs, "tea/hottea/..", &info) => 0;
|
||||
strcmp(info.name, "tea") => 0;
|
||||
lfs_stat(&lfs, "tea/hottea/../.", &info) => 0;
|
||||
strcmp(info.name, "tea") => 0;
|
||||
lfs_unmount(&lfs) => 0;
|
||||
TEST
|
||||
|
||||
echo "--- Root dot dot path tests ---"
|
||||
tests/test.py << TEST
|
||||
lfs_mount(&lfs, &cfg) => 0;
|
||||
@@ -124,10 +108,6 @@ tests/test.py << TEST
|
||||
lfs_stat(&lfs, "/", &info) => 0;
|
||||
info.type => LFS_TYPE_DIR;
|
||||
strcmp(info.name, "/") => 0;
|
||||
|
||||
lfs_mkdir(&lfs, "/") => LFS_ERR_EXIST;
|
||||
lfs_file_open(&lfs, &file[0], "/", LFS_O_WRONLY | LFS_O_CREAT)
|
||||
=> LFS_ERR_ISDIR;
|
||||
lfs_unmount(&lfs) => 0;
|
||||
TEST
|
||||
|
||||
|
||||
@@ -153,7 +153,7 @@ tests/test.py << TEST
|
||||
lfs_file_read(&lfs, &file[0], buffer, size) => size;
|
||||
memcmp(buffer, "kittycatcat", size) => 0;
|
||||
|
||||
size = lfs_file_size(&lfs, &file[0]);
|
||||
lfs_size_t size = lfs_file_size(&lfs, &file[0]);
|
||||
lfs_file_seek(&lfs, &file[0], 0, LFS_SEEK_CUR) => size;
|
||||
|
||||
lfs_file_close(&lfs, &file[0]) => 0;
|
||||
@@ -202,7 +202,7 @@ tests/test.py << TEST
|
||||
lfs_file_read(&lfs, &file[0], buffer, size) => size;
|
||||
memcmp(buffer, "kittycatcat", size) => 0;
|
||||
|
||||
size = lfs_file_size(&lfs, &file[0]);
|
||||
lfs_size_t size = lfs_file_size(&lfs, &file[0]);
|
||||
lfs_file_seek(&lfs, &file[0], 0, LFS_SEEK_CUR) => size;
|
||||
|
||||
lfs_file_close(&lfs, &file[0]) => 0;
|
||||
@@ -243,7 +243,7 @@ tests/test.py << TEST
|
||||
lfs_file_read(&lfs, &file[0], buffer, size) => size;
|
||||
memcmp(buffer, "kittycatcat", size) => 0;
|
||||
|
||||
size = lfs_file_size(&lfs, &file[0]);
|
||||
lfs_size_t size = lfs_file_size(&lfs, &file[0]);
|
||||
lfs_file_seek(&lfs, &file[0], 0, LFS_SEEK_CUR) => size;
|
||||
|
||||
lfs_file_close(&lfs, &file[0]) => 0;
|
||||
@@ -286,7 +286,7 @@ tests/test.py << TEST
|
||||
lfs_file_read(&lfs, &file[0], buffer, size) => size;
|
||||
memcmp(buffer, "kittycatcat", size) => 0;
|
||||
|
||||
size = lfs_file_size(&lfs, &file[0]);
|
||||
lfs_size_t size = lfs_file_size(&lfs, &file[0]);
|
||||
lfs_file_seek(&lfs, &file[0], 0, LFS_SEEK_CUR) => size;
|
||||
|
||||
lfs_file_close(&lfs, &file[0]) => 0;
|
||||
|
||||
@@ -13,12 +13,10 @@ TEST
|
||||
|
||||
truncate_test() {
|
||||
STARTSIZES="$1"
|
||||
STARTSEEKS="$2"
|
||||
HOTSIZES="$3"
|
||||
COLDSIZES="$4"
|
||||
HOTSIZES="$2"
|
||||
COLDSIZES="$3"
|
||||
tests/test.py << TEST
|
||||
static const lfs_off_t startsizes[] = {$STARTSIZES};
|
||||
static const lfs_off_t startseeks[] = {$STARTSEEKS};
|
||||
static const lfs_off_t hotsizes[] = {$HOTSIZES};
|
||||
|
||||
lfs_mount(&lfs, &cfg) => 0;
|
||||
@@ -35,11 +33,6 @@ tests/test.py << TEST
|
||||
}
|
||||
lfs_file_size(&lfs, &file[0]) => startsizes[i];
|
||||
|
||||
if (startseeks[i] != startsizes[i]) {
|
||||
lfs_file_seek(&lfs, &file[0],
|
||||
startseeks[i], LFS_SEEK_SET) => startseeks[i];
|
||||
}
|
||||
|
||||
lfs_file_truncate(&lfs, &file[0], hotsizes[i]) => 0;
|
||||
lfs_file_size(&lfs, &file[0]) => hotsizes[i];
|
||||
|
||||
@@ -114,21 +107,18 @@ TEST
|
||||
|
||||
echo "--- Cold shrinking truncate ---"
|
||||
truncate_test \
|
||||
"2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE" \
|
||||
"2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE" \
|
||||
"2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE" \
|
||||
" 0, $SMALLSIZE, $MEDIUMSIZE, $LARGESIZE, 2*$LARGESIZE"
|
||||
|
||||
echo "--- Cold expanding truncate ---"
|
||||
truncate_test \
|
||||
" 0, $SMALLSIZE, $MEDIUMSIZE, $LARGESIZE, 2*$LARGESIZE" \
|
||||
" 0, $SMALLSIZE, $MEDIUMSIZE, $LARGESIZE, 2*$LARGESIZE" \
|
||||
" 0, $SMALLSIZE, $MEDIUMSIZE, $LARGESIZE, 2*$LARGESIZE" \
|
||||
"2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE"
|
||||
|
||||
echo "--- Warm shrinking truncate ---"
|
||||
truncate_test \
|
||||
"2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE" \
|
||||
"2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE" \
|
||||
" 0, $SMALLSIZE, $MEDIUMSIZE, $LARGESIZE, 2*$LARGESIZE" \
|
||||
" 0, 0, 0, 0, 0"
|
||||
@@ -136,21 +126,6 @@ truncate_test \
|
||||
echo "--- Warm expanding truncate ---"
|
||||
truncate_test \
|
||||
" 0, $SMALLSIZE, $MEDIUMSIZE, $LARGESIZE, 2*$LARGESIZE" \
|
||||
" 0, $SMALLSIZE, $MEDIUMSIZE, $LARGESIZE, 2*$LARGESIZE" \
|
||||
"2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE" \
|
||||
"2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE"
|
||||
|
||||
echo "--- Mid-file shrinking truncate ---"
|
||||
truncate_test \
|
||||
"2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE" \
|
||||
" $LARGESIZE, $LARGESIZE, $LARGESIZE, $LARGESIZE, $LARGESIZE" \
|
||||
" 0, $SMALLSIZE, $MEDIUMSIZE, $LARGESIZE, 2*$LARGESIZE" \
|
||||
" 0, 0, 0, 0, 0"
|
||||
|
||||
echo "--- Mid-file expanding truncate ---"
|
||||
truncate_test \
|
||||
" 0, $SMALLSIZE, $MEDIUMSIZE, $LARGESIZE, 2*$LARGESIZE" \
|
||||
" 0, 0, $SMALLSIZE, $MEDIUMSIZE, $LARGESIZE" \
|
||||
"2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE" \
|
||||
"2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user