Skip to content

Commit 403e1b8

Browse files
authored
Merge pull request #61 from jason50123/external-journal
Implement mounting/unmounting for external journal device
2 parents e76e0cc + d0dbe63 commit 403e1b8

File tree

4 files changed

+397
-4
lines changed

4 files changed

+397
-4
lines changed

Diff for: Makefile

+11-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ all: $(MKFS)
1010

1111
IMAGE ?= test.img
1212
IMAGESIZE ?= 200
13+
JOURNAL ?= journal.img
14+
JOURNALSIZE ?= 8
15+
1316
# To test max files(40920) in directory, the image size should be at least 159.85 MiB
1417
# 40920 * 4096(block size) ~= 159.85 MiB
1518

@@ -20,12 +23,18 @@ $(IMAGE): $(MKFS)
2023
dd if=/dev/zero of=${IMAGE} bs=1M count=${IMAGESIZE}
2124
./$< $(IMAGE)
2225

26+
journal: $(JOURNAL)
27+
28+
$(JOURNAL):
29+
dd if=/dev/zero of=$(JOURNAL) bs=1M count=$(JOURNALSIZE)
30+
mke2fs -b 4096 -O journal_dev $(JOURNAL)
31+
2332
check: all
2433
script/test.sh $(IMAGE) $(IMAGESIZE) $(MKFS)
2534

2635
clean:
2736
make -C $(KDIR) M=$(PWD) clean
2837
rm -f *~ $(PWD)/*.ur-safe
29-
rm -f $(MKFS) $(IMAGE)
38+
rm -f $(MKFS) $(IMAGE) $(JOURNAL)
3039

31-
.PHONY: all clean
40+
.PHONY: all clean journal

Diff for: README.md

+78-2
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,85 @@ struct simplefs_extent
172172
+---------+
173173
```
174174

175-
## TODO
175+
### journalling support
176176

177-
- journalling support
177+
Simplefs now includes support for an external journal device, leveraging the journaling block device (jbd2) subsystem in the Linux kernel. This enhancement improves the file system's resilience by maintaining a log of changes, which helps prevent corruption and facilitates recovery in the event of a crash or power failure.
178+
179+
180+
The journaling support in simplefs is implemented using the jbd2 subsystem, which is a widely-used journaling layer in Linux. Currently, simplefs primarily stores the journal-related information in an external journal device.
181+
182+
For a detailed introduction to journaling, please refer to these two websites:
183+
[Journal(jbd2) document](https://www.kernel.org/doc/html/latest/filesystems/ext4/journal.html)
184+
[Journal(jbd2) api](https://docs.kernel.org/filesystems/journalling.html)
185+
186+
External journal device disk layout:
187+
188+
+--------------------+------------------+---------------------------+--------------+
189+
| Journal Superblock | Descriptor Block | Metadata/Data ( modified ) | Commit Block |
190+
+--------------------+------------------+---------------------------+--------------+
191+
192+
Hint:
193+
Each transaction starts with a descriptor block, followed by several metadata blocks or data blocks, and ends with a commit block. Every modified metadata (such as inode, bitmap, etc.) occupies its own block. Currently, simplefs primarily records "extent" metadata.
194+
195+
196+
How to Enable Journaling in simplefs:
197+
198+
Step 1: Create the Journal Disk Image
199+
To create an 8MB disk image for the journal, use the following make command:
200+
201+
Note:
202+
Assuming an 8 MB size for the external journal device, which is an arbitrary choice for now, I will set the journal block length to a fixed 2048, calculated by dividing the device size by the block size (4096 bytes).
203+
204+
```shell
205+
$ make journal
206+
```
207+
208+
Step 2: Make sure you've loaded the SimpleFS Kernel Module
209+
210+
```shell
211+
$ insmod simplefs/simplefs.ko
212+
```
213+
214+
Step 3: Setup the Loop Device for the Journal
215+
Find an available loop device and associate it with the journal image:
216+
217+
``` shell
218+
$ loop_device=$(losetup -f)
219+
$ losetup $loop_device /simplefs/journal.img
220+
```
221+
222+
You shall get the following kernel messages:
223+
```
224+
loop0: detected capacity change from 0 to 16384
225+
```
226+
227+
Step 4: Mount the SimpleFS File System with the External Journal
228+
Mount the SimpleFS file system along with the external journal device using the following command:
229+
230+
```shell
231+
mount -o loop,rw,owner,group,users,journal_path="$loop_device" -t simplefs /simplefs/test.img /test
232+
```
233+
234+
Corresponding kernel message:
235+
```
236+
loop1: detected capacity change from 0 to 409600
237+
simplefs: simplefs_parse_options: parsing options 'owner,group,journal_path=/dev/loop0'
238+
simplefs: '/dev/loop1' mount success
239+
```
240+
241+
Current Limitations and Known Issues
242+
243+
1. External Journal Device Size:
244+
245+
- The exact size of the external journal device cannot be determined. As a temporary solution, the size is set by dividing the device size by the block size, with the external journal device size fixed at 8 MB.
246+
247+
2. Metadata Recording:
248+
249+
- At present, only "extent" metadata is recorded. In the future, additional metadata such as "super block" and inode metadata can be included.
250+
251+
3. Implementation of External Journal Device:
252+
253+
- Only the external journal device is implemented. Future improvements can include the use of an internal journal (inode journal). However, this will require the addition of a bmap function and appropriate adjustments to the disk partition during mkfs.
178254

179255
## License
180256

Diff for: simplefs.h

+7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
* | blocks | rest of the blocks
4040
* +---------------+
4141
*/
42+
#ifdef __KERNEL__
43+
#include <linux/jbd2.h>
44+
#endif
4245

4346
struct simplefs_inode {
4447
uint32_t i_mode; /* File mode */
@@ -71,6 +74,10 @@ struct simplefs_sb_info {
7174
uint32_t nr_free_blocks; /* Number of free blocks */
7275

7376
#ifdef __KERNEL__
77+
journal_t *journal;
78+
struct block_device *s_journal_bdev; /* v5.10+ external journal device */
79+
struct bdev_handle
80+
*s_journal_bdev_handle; /* v6.7+ external journal device */
7481
unsigned long *ifree_bitmap; /* In-memory free inodes bitmap */
7582
unsigned long *bfree_bitmap; /* In-memory free blocks bitmap */
7683
#endif

0 commit comments

Comments
 (0)