Azat Khuzhin bb70d76497 Add BUG_ON() after each sb_bread() to avoid NULL dereferencing
If mkfs-simple will have bugs, than it could obtain illegal block number, and
sb_bread() will return NULL, even without it could return NULL, so since this
fs is just for testing, simply add bug_on() to avoid going further.

[ 1247.861163] The magic number obtained in disk is: [268640275]
[ 1247.861165] simplefs filesystem of version [1] formatted with a block size of [4096] detected in the device.
[ 1247.861194] simplefs is succesfully mounted on [/dev/loop0]
[ 1247.886800] BUG: unable to handle kernel NULL pointer dereference at           (null)
[ 1247.886804] IP: [<ffffffffa00ca695>] simplefs_lookup+0xb5/0x180 [simplefs]
[ 1247.886808] PGD 5eda6067 PUD 5eda5067 PMD 0
[ 1247.886810] Oops: 0000 [#1] SMP
[ 1247.886812] Modules linked in: simplefs(POE) snd_intel8x0 snd_ac97_codec ac97_bus snd_pcm snd_seq_midi snd_seq_midi_event snd_rawmidi snd_seq snd_seq_device intel_rapl snd_timer joydev snd rfcomm bnep bluetooth 6lowpan_iphc serio_raw i2c_piix4 mac_hid soundcore parport_pc ppdev lp parport hid_generic usbhid hid psmouse ahci libahci e1000 pata_acpi [last unloaded: simplefs]
[ 1247.886826] CPU: 0 PID: 9467 Comm: cat Tainted: P           OE 3.15.0-6-generic #11-Ubuntu
[ 1247.886827] Hardware name: innotek GmbH VirtualBox/VirtualBox, BIOS VirtualBox 12/01/2006
[ 1247.886828] task: ffff88005e72cb90 ti: ffff88005ede8000 task.ti: ffff88005ede8000
[ 1247.886829] RIP: 0010:[<ffffffffa00ca695>]  [<ffffffffa00ca695>] simplefs_lookup+0xb5/0x180 [simplefs]
[ 1247.886832] RSP: 0018:ffff88005edebc88  EFLAGS: 00010292
[ 1247.886833] RAX: 0000000000000002 RBX: 0000000000000000 RCX: ffff88005f5370a0
[ 1247.886834] RDX: ffff880024c9a108 RSI: ffff880024c9bb60 RDI: ffff880024c9a000
[ 1247.886834] RBP: ffff88005edebcc0 R08: 0000000000000000 R09: 0000000000000001
[ 1247.886835] R10: ffff88005edebab8 R11: 0000000000000000 R12: ffff88005f537000
[ 1247.886836] R13: ffff88005ed68000 R14: 0000000000000000 R15: ffff880024c9a000
[ 1247.886837] FS:  00007ffb35497740(0000) GS:ffff880065c00000(0000) knlGS:0000000000000000
[ 1247.886838] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 1247.886839] CR2: 0000000000000000 CR3: 000000005edfa000 CR4: 00000000000006f0
[ 1247.886843] Stack:
[ 1247.886844]  ffff880024c9bb60 ffff88001d19e180 ffff88001d19e180 0000000000008000
[ 1247.886845]  ffff88005edebde8 ffff88001d159e40 ffff88005edebe40 ffff88005edebce0
[ 1247.886847]  ffffffff811da5ed ffff88001d159e40 ffff88005edebf2c ffff88005edebd88
[ 1247.886848] Call Trace:
[ 1247.886853]  [<ffffffff811da5ed>] lookup_real+0x1d/0x70
[ 1247.886856]  [<ffffffff811df537>] do_last+0x797/0x1270
[ 1247.886860]  [<ffffffff8132c81b>] ? apparmor_file_alloc_security+0x5b/0x180
[ 1247.886862]  [<ffffffff812ed116>] ? security_file_alloc+0x16/0x20
[ 1247.886864]  [<ffffffff811e119b>] path_openat+0xbb/0x6a0
[ 1247.886866]  [<ffffffff811e285a>] do_filp_open+0x3a/0xa0
[ 1247.886868]  [<ffffffff811ef597>] ? __alloc_fd+0xa7/0x130
[ 1247.886871]  [<ffffffff811cff8a>] do_sys_open+0x12a/0x280
[ 1247.886874]  [<ffffffff81020925>] ? syscall_trace_enter+0x165/0x2a0
[ 1247.886876]  [<ffffffff811d00fe>] SyS_open+0x1e/0x20
[ 1247.886879]  [<ffffffff8176e33f>] tracesys+0xe1/0xe6
[ 1247.886880] Code: 01 00 00 4c 89 e7 e8 ab fe ff ff 4c 89 e7 48 89 c3 e8 f0 38 12 e1 49 89 c7 49 8b 85 00 01 00 00 48 8b 34 24 4c 89 ff 49 89 47 40 <0f> b7 13 e8 83 0f 12 e1 41 0f b7 07 4d 89 67 28 49 c7 47 20 40
[ 1247.886894] RIP  [<ffffffffa00ca695>] simplefs_lookup+0xb5/0x180 [simplefs]
[ 1247.886896]  RSP <ffff88005edebc88>
[ 1247.886896] CR2: 0000000000000000
[ 1247.886898] ---[ end trace 89702ff0fda32679 ]---
2014-07-12 22:21:49 +04:00
2013-07-28 22:37:15 +05:30
2013-08-15 14:51:20 +05:30

A simple filesystem to understand things.

This is a Work In Progress. Do not use this yet.

If you are planning to learn filesystems, start from the scratch. You can look from the first commit in this repository and move the way up.

The source files are licensed under Creative Commons Zero License.
More information at:	http://creativecommons.org/publicdomain/zero/1.0/
Full license text at:	http://creativecommons.org/publicdomain/zero/1.0/legalcode

simplefs 1.0 Architecture + Notes
---------------------------------

Block Zero = Super block
Block One = Inode store
Block Two = Occupied by the initial file that is created as part of the mkfs.

Only a limited number of filesystem objects are supported.
Files and Directories can be created. Support for .create and .mkdir is implemented. Nested directories can be created.
A file cannot grow beyond one block. ENOSPC will be returned as an error on attempting to do.
Directories store the children inode number and name in their data blocks.
Read support is implemented.
Basic write support is implemented. Writes may not succeed if done in an offset. Works when you overwrite the entire block.
Locks are not well thought-out. The current locking scheme works but needs more analysis + code reviews.
Memory leaks may (will ?) exist.


Credits
--------
The initial source code is written by psankar and patches are contributed by other github members. azat being the first contributor.

An O_LARGETHANKS to the guidance of VijaiBabu M and Santosh Venugopal. Their excellent talks on filesystems motivated me to implement a file system from the scratch. Without their inspirational speeches, I would not have focussed on filesystems.

A big thanks should go to the kernelnewbies mailing list for helping me with clarifying the nitty-gritties of the linux kernel, especially people like Mulyadi Santosa, Valdis Kletnieks, Manish Katiyar, Rajat Sharma etc.

Special thanks to Ankit Jain who provides interesting conversations to help my intellectual curiosities.


Patch Submission
-----------------
Please send a merge request only if you are ready to publish your changes in the same creative commons zero license.

We would like to keep the filesystem simple and minimal so that it can be used as a good teaching material.

We are not planning to use this filesystem in a production machine. So some design choices are driven by learning/teaching simplicity over performance/scalability. This is intentional. So do not try to fix those things :)

TODO
-----
- After the 1.0 release, start with support for extents, which on completion will be 2.0
- After the 2.0 release, start with journalling support, which on completion will be 3.0


To compile:
------------
install linux kernel sources and run make from the checkedout directory.


To test:
---------

psankar@linux-9dni:~/src/simplefs> make

psankar@linux-9dni:~/src/simplefs> dd bs=4096 count=100 if=/dev/zero of=image
100+0 records in
100+0 records out
409600 bytes (410 kB) copied, 0.00106827 s, 383 MB/s
psankar@linux-9dni:~/src/simplefs> ./mkfs-simplefs image
Super block written succesfully
root directory inode written succesfully
welcomefile inode written succesfully
inode store padding bytes (after the two inodes) written sucessfully
root directory datablocks (name+inode_no pair for welcomefile) written succesfully
padding after the rootdirectory children written succesfully
welcomefilebody has been written succesfully

psankar@linux-9dni:~/src/simplefs>

Now as root:
linux-9dni:/home/psankar/src/simplefs # insmod simplefs.ko ; mount -o loop -t simplefs image /home/psankar/src/simplefs/mount/ ; dmesg
[41932.281196] Sucessfully registered simplefs
[41932.284811] The magic number obtained in disk is: [268640275]
[41932.284813] simplefs filesystem of version [1] formatted with a block size of [4096] detected in the device.
[41932.284823] simplefs is succesfully mounted on [/dev/loop16]
linux-9dni:/home/psankar/src/simplefs # cd mount/
linux-9dni:/home/psankar/src/simplefs/mount # ls
vanakkam
linux-9dni:/home/psankar/src/simplefs/mount # cat vanakkam
Love is God. God is Love. Anbe Murugan.
linux-9dni:/home/psankar/src/simplefs/mount # cp vanakkam hello
linux-9dni:/home/psankar/src/simplefs/mount # cat hello
Love is God. God is Love. Anbe Murugan.
linux-9dni:/home/psankar/src/simplefs/mount # echo "Hello World" > hello
linux-9dni:/home/psankar/src/simplefs/mount # cat hello
Hello World
linux-9dni:/home/psankar/src/simplefs/mount # mkdir dir1
linux-9dni:/home/psankar/src/simplefs/mount # cd dir1
linux-9dni:/home/psankar/src/simplefs/mount/dir1 # cp ../hello .
linux-9dni:/home/psankar/src/simplefs/mount/dir1 # cat hello
Hello World
linux-9dni:/home/psankar/src/simplefs/mount/dir1 # echo "First level directory" > hello
linux-9dni:/home/psankar/src/simplefs/mount/dir1 # cat hello
First level directory
linux-9dni:/home/psankar/src/simplefs/mount/dir1 # mkdir dir2
linux-9dni:/home/psankar/src/simplefs/mount/dir1 # cd dir2
linux-9dni:/home/psankar/src/simplefs/mount/dir1/dir2 # touch hello
linux-9dni:/home/psankar/src/simplefs/mount/dir1/dir2 # cat hello
linux-9dni:/home/psankar/src/simplefs/mount/dir1/dir2 # echo "Second level directory" > hello
linux-9dni:/home/psankar/src/simplefs/mount/dir1/dir2 # cat hello
Second level directory
linux-9dni:/home/psankar/src/simplefs/mount/dir1/dir2 # ...
linux-9dni:/home/psankar/src/simplefs/mount # ..
linux-9dni:/home/psankar/src/simplefs # umount mount ; rmmod simplefs.ko ;
linux-9dni:/home/psankar/src/simplefs # insmod simplefs.ko ; mount -o loop -t simplefs image /home/psankar/src/simplefs/mount/
linux-9dni:/home/psankar/src/simplefs # cd mount
linux-9dni:/home/psankar/src/simplefs/mount # ls -lR
.:
total 0
drwxr-xr-x 1 root root 0 Aug  5 22:43 dir1
---------- 1 root root 0 Aug  5 22:43 hello
---------- 1 root root 0 Aug  5 22:43 vanakkam

./dir1:
total 0
drwxr-xr-x 1 root root 0 Aug  5 22:43 dir2
---------- 1 root root 0 Aug  5 22:43 hello

./dir1/dir2:
total 0
-rw-r--r-- 1 root root 0 Aug  5 22:43 hello
linux-9dni:/home/psankar/src/simplefs/mount # cat hello
Hello World
linux-9dni:/home/psankar/src/simplefs/mount # cd dir1
linux-9dni:/home/psankar/src/simplefs/mount/dir1 # cat hello
First level directory
linux-9dni:/home/psankar/src/simplefs/mount/dir1 # cd dir2/
linux-9dni:/home/psankar/src/simplefs/mount/dir1/dir2 # cat hello
Second level directory
linux-9dni:/home/psankar/src/simplefs/mount/dir1/dir2 # ...
linux-9dni:/home/psankar/src/simplefs/mount # ..
linux-9dni:/home/psankar/src/simplefs # umount mount ; rmmod simplefs.ko ;
linux-9dni:/home/psankar/src/simplefs # dmesg
[41932.281196] Sucessfully registered simplefs
[41932.284811] The magic number obtained in disk is: [268640275]
[41932.284813] simplefs filesystem of version [1] formatted with a block size of [4096] detected in the device.
[41932.284823] simplefs is succesfully mounted on [/dev/loop16]
[41951.102689] No inode found for the filename [hello]
[41951.102706] No inode found for the filename [hello]
[41951.102724] No inode found for the filename [hello]
[41951.102731] New file creation request
[41951.102986] The new filesize that is written is: [41] and len was: [41]
[41961.195423] The new filesize that is written is: [12] and len was: [12]
[41968.055946] No inode found for the filename [dir1]
[41968.055959] New directory creation request
[41980.265026] No inode found for the filename [hello]
[41980.265073] No inode found for the filename [hello]
[41980.265082] New file creation request
[41980.265168] The new filesize that is written is: [12] and len was: [12]
[41991.857978] The new filesize that is written is: [22] and len was: [22]
[41999.993579] No inode found for the filename [dir2]
[41999.993592] New directory creation request
[42005.461745] No inode found for the filename [hello]
[42005.461756] New file creation request
[42016.694882] The new filesize that is written is: [23] and len was: [23]
[42024.480843] simplefs superblock is destroyed. Unmount succesful.
[42024.485339] Sucessfully unregistered simplefs
[42031.157983] Sucessfully registered simplefs
[42031.162331] The magic number obtained in disk is: [268640275]
[42031.162334] simplefs filesystem of version [1] formatted with a block size of [4096] detected in the device.
[42031.162360] simplefs is succesfully mounted on [/dev/loop17]
[42104.389807] simplefs superblock is destroyed. Unmount succesful.
[42104.395575] Sucessfully unregistered simplefs
linux-9dni:/home/psankar/src/simplefs #
Description
A simple, kernel-space, on-disk filesystem from the scratch
Readme 158 KiB
Languages
C 92.7%
Shell 6.5%
Makefile 0.8%