# root is not show "." and ".\." source code repo - [simplefs](https://github.com/sysprog21/simplefs) ## root directory do not show "." and ".\." As we know the "." is a symbol link to currnet directory, and ".\." point to it's parent When the file system is root for kernel system, the 'ls' do not show the =='.' / ".\."==, but if the file system is mounted to other file system, this file system's root should has '.' and ".\." ## test simplefs test environmet: 1. ubuntu 5.15 run on ubuntu 22.04 with multipass emulator 2. UML 6.2 kernel using bz box ## test result * ubuntu 5.15 ``` ubuntu@primary:~/simplefs/test15$ ls -la total 0 ``` * UML 6.2 kernel ``` root@(none):/tmp/simplefs# ls -a . .. (others hidden...) ``` ## Root cause By testing, it seems the glibc and bz box have different way to proecc the readdir, so let's read the source code: * glibc readdir [src here](https://elixir.bootlin.com/glibc/glibc-2.35.9000/source/sysdeps/unix/sysv/linux/readdir64.c#L31), glibc readdir seems skip the ino 0 * bz box readdir [src here](https://github.com/mirror/busybox/blob/2d4a3d9e6c1493a9520b907e07a41aca90cdfd94/coreutils/ls.c#L924), bz box will read '.' and ".." for show so by different lib, this has different way to show, but [ext2/ ext3/ ext4 file system don't use 0 for root inode](https://stackoverflow.com/questions/2099121/why-do-inode-numbers-start-from-1-and-not-0), and linux kernel doc. also suggest that the inode should not be 0 - ["There is no inode 0"](https://www.kernel.org/doc/html/latest/filesystems/ext4/inodes.html?highlight=inode) vfs also avoid the inode number 0 in [RFC](https://patchwork.kernel.org/project/linux-fsdevel/patch/1435245958-4507-1-git-send-email-cmaiolino@redhat.com/) so solution is very easy ```diff } /* Create root inode */ - root_inode = simplefs_iget(sb, 0); + root_inode = simplefs_iget(sb, 1); if (IS_ERR(root_inode)) { ret = PTR_ERR(root_inode); goto free_bfree; ``` ## commit log: following is commit log Fix fs not showing current and parent directories ### Description ----------- In previous linux kernel version (e.g 5.8), ls -la will not show . and .. in root directory ### Root cause ---------- In Linux system, there are different way to implement "ls" command. Like Ubuntu system, using glibc. Embedded system using busybox, [glibc](https://elixir.bootlin.com/glibc/glibc-2.35.9000/source/sysdeps/unix/sysv/linux/readdir64.c#L31)/ [busybox](https://github.com/mirror/busybox/blob/2d4a3d9e6c1493a9520b907e07a41aca90cdfd94/coreutils/ls.c#L924) And you can see that the ext2/ ext3/ ext4/ system all [skip inode number 0](https://stackoverflow.com/questions/2099121/why-do-inode-numbers-start-from-1-and-not-0) In kernel document [ext4](https://www.kernel.org/doc/html/latest/filesystems/ext4/inodes.html?highlight=inode) also say that "There is no inode 0" And [patchwork - vfs: avoid creation of inode number 0 in get_next_ino](https://patchwork.kernel.org/project/linux-fsdevel/patch/1435245958-4507-1-git-send-email-cmaiolino@redhat.com/) add code to avoid to provide the 0 inode so in [6.3 kernel](https://elixir.bootlin.com/linux/v6.3.13/source/fs/inode.c#L983) src code, we can see these patch code ### Fix solution ---------- By following ext4 rule, skip inode number 0. When creating image layout in inode section, we skip first block and use next inode ( inode number 1 ) with the root inode. And we also modify the d_make_root, we use inode number 1 for the root inode. ### How has this been tested? ---------- In kernel 5.15 here is original source code ``` ubuntu@primary:/home/roy/src-code/github/roy/simplefs/test15$ uname -a Linux primary 5.15.0-73-generic #80-Ubuntu SMP Mon May 15 15:18:26 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux ubuntu@primary:/home/roy/src-code/github/roy/simplefs/test15$ ls -la total 0 ``` In kernel 5.15 here is patch code ``` ubuntu@primary:/home/roy/src-code/github/roy/simplefs/test15$ uname -a Linux primary 5.15.0-73-generic #80-Ubuntu SMP Mon May 15 15:18:26 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux ubuntu@primary:/home/roy/src-code/github/roy/simplefs/test15$ ls -la total 5 drwxrwxr-x 2 root root 4096 Jan 1 1970 . drwxrwxr-x 1 ubuntu ubuntu 4096 Jul 22 22:10 .. ``` In kernel 5.4 here is original source code ``` ubuntu@vcluster101-vxm:/home/roy/src-code/github/roy/simplefs$ uname -a Linux vcluster101-vxm 5.4.0-153-generic #170-Ubuntu SMP Fri Jun 16 13:43:31 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux ubuntu@vcluster101-vxm:/home/roy/src-code/github/roy/simplefs/test4$ ls -la total 0 ``` In kernel 5.4 here is patch code ``` buntu@vcluster101-vxm:/home/roy/src-code/github/roy/simplefs/test4$ ls -la total 5 drwxrwxr-x 2 root root 4096 Jan 1 1970 . drwxrwxr-x 1 ubuntu ubuntu 4096 Jul 22 22:42 .. ubuntu@vcluster101-vxm:/home/roy/src-code/github/roy/simplefs/test4$ uname -a Linux vcluster101-vxm 5.4.0-153-generic #170-Ubuntu SMP Fri Jun 16 13:43:31 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux ``` ### references --------- [glibc](https://sourceware.org/bugzilla/show_bug.cgi?id=19970) may have a patch to fix readdir?