From df3b0ae17f8f4004248e7a79190b0ac9d8c998d7 Mon Sep 17 00:00:00 2001 From: RoyHuang <roywfhuang@gmail.com> Date: Fri, 28 Mar 2025 21:39:43 +0800 Subject: [PATCH 1/2] Fix user-space data write with correct offset The simplefs_write will copy data from user space. If the input length is larger than BLOCK_SIZE, the data will not be copied from user space buffer. --- file.c | 2 +- script/test.sh | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/file.c b/file.c index b6cfc6d..aaf1f25 100644 --- a/file.c +++ b/file.c @@ -413,7 +413,7 @@ static ssize_t simplefs_write(struct file *file, size_t bytes_to_write = min_t(size_t, len, SIMPLEFS_BLOCK_SIZE - pos % SIMPLEFS_BLOCK_SIZE); - if (copy_from_user(bh_data->b_data + pos % SIMPLEFS_BLOCK_SIZE, buf, + if (copy_from_user(bh_data->b_data + pos % SIMPLEFS_BLOCK_SIZE, buf + bytes_write, bytes_to_write)) { brelse(bh_data); bytes_write = -EFAULT; diff --git a/script/test.sh b/script/test.sh index b1df6fc..6ba5ad0 100755 --- a/script/test.sh +++ b/script/test.sh @@ -123,6 +123,18 @@ test_op 'dd if=/dev/zero of=file bs=1M count=12 status=none' filesize=$(sudo ls -lR | grep -e "$F_MOD 2".*file | awk '{print $5}') test $filesize -le $MAXFILESIZE || echo "Failed, file size over the limit" +# Write the file size larger than BLOCK_SIZE +# test serial to write +test_op 'bash -c "printf \"%.0s123456789\" {1..1600} > file.txt"' +count=$(awk '{count += gsub(/123456789/, "")} END {print count}' "file.txt") +echo "test $count" +test "$count" -eq 1600 || echo "Failed, file size not matching" +# test block to write +test_op 'cat file.txt > checkfile.txt' +count=$(awk '{count += gsub(/123456789/, "")} END {print count}' "checkfile.txt") +echo "test $count" +test "$count" -eq 1600 || echo "Failed, file size not matching" + # test remove symbolic link test_op 'ln -s file symlink_fake' test_op 'rm -f symlink_fake' From a36011e20d183c732f760a1eb8ab986c1a39ed65 Mon Sep 17 00:00:00 2001 From: RoyHuang <roywfhuang@gmail.com> Date: Sat, 29 Mar 2025 10:51:12 +0800 Subject: [PATCH 2/2] Fix workflow lib dep. --- .github/workflows/main.yaml | 2 +- file.c | 4 ++-- mkfs.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index e8067b4..8edb577 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -22,7 +22,7 @@ jobs: uses: actions/checkout@v4 - name: coding convention run: | - sudo apt-get install -q -y clang-format-12 + sudo apt-get install -q -y clang-format .ci/check-newline.sh .ci/check-format.sh shell: bash diff --git a/file.c b/file.c index aaf1f25..f6868c0 100644 --- a/file.c +++ b/file.c @@ -413,8 +413,8 @@ static ssize_t simplefs_write(struct file *file, size_t bytes_to_write = min_t(size_t, len, SIMPLEFS_BLOCK_SIZE - pos % SIMPLEFS_BLOCK_SIZE); - if (copy_from_user(bh_data->b_data + pos % SIMPLEFS_BLOCK_SIZE, buf + bytes_write, - bytes_to_write)) { + if (copy_from_user(bh_data->b_data + pos % SIMPLEFS_BLOCK_SIZE, + buf + bytes_write, bytes_to_write)) { brelse(bh_data); bytes_write = -EFAULT; break; diff --git a/mkfs.c b/mkfs.c index 01a6e0b..59dd967 100644 --- a/mkfs.c +++ b/mkfs.c @@ -26,7 +26,7 @@ _Static_assert(sizeof(struct superblock) == SIMPLEFS_BLOCK_SIZE); * * Return the result of n / d, rounded up to the nearest integer. */ -#define DIV_ROUND_UP(n, d) (((n) + (d) -1) / (d)) +#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) static struct superblock *write_superblock(int fd, struct stat *fstats) {