Skip to content

Commit e05fa22

Browse files
authored
Merge pull request #60 from HotMercury/master
Fix missing data synchronization
2 parents 79a2831 + a84c775 commit e05fa22

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

bitmap.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ static inline uint32_t get_free_blocks(struct super_block *sb, uint32_t len)
4747
{
4848
struct simplefs_sb_info *sbi = SIMPLEFS_SB(sb);
4949
uint32_t ret = get_first_free_bits(sbi->bfree_bitmap, sbi->nr_blocks, len);
50+
uint32_t i;
5051
if (!ret) /* No enough free blocks */
5152
return 0;
5253

5354
sbi->nr_free_blocks -= len;
54-
for (uint32_t i = 0; i < len; i++) {
55+
for (i = 0; i < len; i++) {
5556
struct buffer_head *bh = sb_bread(sb, ret + i);
5657
if (!bh) {
5758
pr_err("get_free_blocks: sb_bread failed for block %d\n", ret + i);
@@ -60,6 +61,7 @@ static inline uint32_t get_free_blocks(struct super_block *sb, uint32_t len)
6061
}
6162
memset(bh->b_data, 0, SIMPLEFS_BLOCK_SIZE);
6263
mark_buffer_dirty(bh);
64+
sync_dirty_buffer(bh); /* write the buffer to disk */
6365
brelse(bh);
6466
}
6567
return ret;

script/test.sh

+38-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ F_MOD="-rw-r--r--"
1010
S_MOD="lrwxrwxrwx"
1111
MAXFILESIZE=11173888 # SIMPLEFS_MAX_EXTENTS * SIMPLEFS_MAX_BLOCKS_PER_EXTENT * SIMPLEFS_BLOCK_SIZE
1212
MAXFILES=40920 # max files per dir
13+
MOUNT_TEST=100
1314

1415
test_op() {
1516
local op=$1
@@ -61,6 +62,42 @@ done
6162
filecnts=$(ls | wc -w)
6263
test $filecnts -eq $MAXFILES || echo "Failed, it should be $MAXFILES files"
6364
find . -name '[0-9]*.txt' | xargs -n 2000 sudo rm
65+
sync
66+
67+
# create 100 files with filenames inside
68+
for ((i=1; i<=$MOUNT_TEST; i++))
69+
do
70+
echo file_$i | sudo tee file_$i.txt >/dev/null && echo "file_$i.txt created."
71+
done
72+
sync
73+
74+
# unmount and remount the filesystem
75+
echo "Unmounting filesystem..."
76+
popd >/dev/null || { echo "popd failed"; exit 1; }
77+
sudo umount test || { echo "umount failed"; exit 1; }
78+
sleep 1
79+
echo "Remounting filesystem..."
80+
sudo mount -t simplefs -o loop $IMAGE test || { echo "mount failed"; exit 1; }
81+
echo "Remount succeeds."
82+
pushd test >/dev/null || { echo "pushd failed"; exit 1; }
83+
84+
# check if files exist and content is correct after remounting
85+
for ((i=1; i<=$MOUNT_TEST; i++))
86+
do
87+
if [[ -f "file_$i.txt" ]]; then
88+
content=$(cat "file_$i.txt" | tr -d '\000')
89+
if [[ "$content" == "file_$i" ]]; then
90+
echo "Success: file_$i.txt content is correct."
91+
else
92+
echo "Failed: file_$i.txt content is incorrect."
93+
exit 1
94+
fi
95+
else
96+
echo "Failed: file_$i.txt does not exist."
97+
exit 1
98+
fi
99+
done
100+
find . -name 'file_[0-9]*.txt' | xargs sudo rm || { echo "Failed to delete files"; exit 1; }
64101

65102
# hard link
66103
test_op 'ln file hdlink'
@@ -96,4 +133,4 @@ check_exist $S_MOD 1 symlink
96133
sleep 1
97134
popd >/dev/null
98135
sudo umount test
99-
sudo rmmod simplefs
136+
sudo rmmod simplefs

0 commit comments

Comments
 (0)